 
! script name: v3dwrap.d/polarcutcont
! var_topic: finish mill contour for polar face output

!A
!A NAME:
!A
!A 	Polar cut contour		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	Machines a contour on the face of a rotary part.
!A
!A INSTRUCTIONS:
!A
!A	This routine does the following.
!A
!A		1) The contour is located in a workplane parallel
!A		      to the YZ plane.
!A		2) The current workplane should be the plane of the contour.
!A		3) The contour is offset for the given tool.
!A		4) The resulting cutterpath is then interpolated to generate
!A		     the C axis moves required to machine the contour. This
!A		     creates a bivectorchain.
!A	 	
!A
!A
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is
!B 		    clockwise for an interior cut and counterclockwise
!B 		    for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for
!B 		    an exterior cut and counterclockwise for an interior cut.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick an element on the
!B 		contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path should
!B 		be generated.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter and
!C 	the finish allowance.  (offset=.5*cutter_diameter + finish_allowance)
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: polar cut contour ")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Boundary_name$[256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Depth_var$[256]
DIM Name$[256]
REAL Leadintype
REAL Leadouttype

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

ActivateUndo
Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)
Units= CurrentUnits
IF Units= WsMetric THEN
        Dist= 12.5
        Radius= 6.25
ELSE
        Dist= 0.5
        Radius= 0.25
END IF

LeadinVarsType= 20011
Leadintype=NamedVarVal("leadin type",LeadinVarsType,1.0)
Leadouttype=NamedVarVal("leadout type",LeadinVarsType,1.0)
Leadindist=NamedVarVal("leadin dist",LeadinVarsType,Dist)
Leadoutdist=NamedVarVal("leadout dist",LeadinVarsType,leadindist)
Rin=NamedVarVal("leadin radius",LeadinVarsType,Radius)
Rout=NamedVarVal("leadout radius",LeadinVarsType,Radius)


Current_pen=CurrentColor

Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="C") THEN
	Mill_dir=WsConventional
	Pen_color=WsConvCutPen
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
END IF
Stat=SetCurrentColor(Pen_color)

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
		LowerSelectBox
		GOTO Leave
END IF
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Stat=SetInputFormat("n4=","")
Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)
No_finish_var= LastVariableInput(Finish_var$)


Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)



Pick_loop:!
E1=PickEntity("PICK AN ELEMENT OF CONTOUR TO BE OFFSET")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(E1,0)
IF NOT ValidDependency(Contour) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)


Pt=InputFreePoint("PICK WHICH SIDE")
IF (ExitScript(Destination$)) THEN Leave

Side=OffsetLeftRight(E1,Pt)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Direction_sign=MillDirection(E1,Pt)
Dist=-Side*Offset

Leadinside= Side
Direction=WsForward
IF (Direction_sign=1)AND(Mill_dir=WsConventional) THEN
	Direction=WsReverse
	Leadinside= -Side
END IF
IF (Direction_sign=-1)AND(Mill_dir=WsClimb) THEN
	Direction=WsReverse
	Leadinside= -Side
END IF
Stat=DeleteEntity(Pt)

Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)

Stat=SetCurrentColor(Pen_color)
Rough_stock= 0.0
Width_cut= 0.0
Side1= -Side

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("PATH: Finish mill with leadin ")
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
IF No_finish_var THEN
	V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
	V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V2= FreeParameter("Rough stock",Rough_stock)
V3= FreeParameter("Width cut",Width_cut)
V4= FreeParameter("Side",Side1)
IF No_depth_var THEN
	V5= FreeParameter("Finish depth",Finish_depth)
ELSE
	V5= FreeParameter("Finish depth",Depth_var$)
END IF
V6= FreeParameter("Depth cut",Depth_cut)
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
        Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
        Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V5,V6,V1)
Paths=MillContour(Contour$,Mdef1,WsFullLook,WsCircleMoves)
Stat= UnAssocMachDef(Paths,Mdef1)
Stat=DeleteEntity(Mdef1)
V1= FreeParameter("Leadin radius",Rin)
V2= FreeParameter("Leadin distance",Leadindist)
V3= FreeParameter("Leadin type",Leadintype)
V4= FreeParameter("Leadout radius",Rout)
V5= FreeParameter("Leadout distance",Leadoutdist)
V6= FreeParameter("Leadout type",Leadouttype)
V7= FreeParameter("Leadin side",Leadinside)
Stat= PathGroupLeadIn(Paths,V1,V4,V2,V5,V3,V6,V7)
V8= FreeParameter("Tolerance",Tol)
Work= CurrentWorkplane
Stat= SetWorkplane
Center=PointXYZ(0,0,0)
Axis=PointXYZ(1.0,0,0)
Orient=PointXYZ(0,0,1)
Path_group=PolarWrapContGrp(Paths,Center,Axis,Orient,V8)
Stat=AssociateTool(Path_group,Tool)
Stat= SetNoPathFlag(Path_group,WsTrue)
Stat= AutoNameGroup(Path_group,Name$)
Stat= DeleteGroup(Paths,WsTrue,WsTrue)
Stat= DeleteEntityList(Center,Axis,Orient)
Stat= SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Path_group) THEN 
	DelLastVarRecord
	Curr_record= 0
ELSE
        Stat= AnimateTool(Tool,Path_group)
	ActivateUndo
	Stat= AdvanceUndoNumber("polar cut curve")
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_loop


Leave:!
LowerSelectBox
Vleadintype=CreateVariable("leadin type",LeadinVarsType,leadintype)
Vleadouttype=CreateVariable("leadout type",LeadinVarsType,leadouttype)
Vleadin=CreateVariable("leadin dist",LeadinVarsType,leadindist)
Vleadout=CreateVariable("leadout dist",LeadinVarsType,leadoutdist)
VRout=CreateVariable("leadin radius",LeadinVarsType,Rin)
VRin=CreateVariable("leadout radius",LeadinVarsType,Rout)
Stat=DeleteEntity(Pt)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

