 
! script name: v3dwrap.d/odcutwrapcont
! var_topic: finish mill contour for od rotary output.

!A
!A NAME:
!A
!A 	Od rotary cut contour		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	Wrap a cutterpath on the OD of a rotary part.
!A			
!A INSTRUCTIONS:
!A
!A	This routine does the following:
!A		1) The contour must be in the XY plane at Z=0.0.
!A		2) A 2d offset cutterpath is driven around the contour.
!A		3) The Y axis of the cutterpath is wrapped around the X axis.
!A		      creating a bivectorchain for driving the rotary C axis
!A		      of a mill-turn machine tool.
!A		4) Note that the walls of the resulting shape will not be
!A		      radial. To get radial walls requires that the cutter
!A		      be able to move simultaneously in XYZ and C.	 	
!A
!A
!B
!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: od cut wrapped 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 Radius_var$[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)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

Ask_radius:Stat=SetInputFormat("n4=","")
Radius=InputRadius("RADIUS OF CYLINDER")
IF (ExitScript(Destination$)) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_radius
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_var$)
END IF


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$)
Work= CurrentWorkplane
Stat= SetWorkplane
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)
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("Astart",90)
V10= FreeParameter("Tolerance",Tol)
V11= FreeParameter("Normal direction",1)
P1= PointXYZ(0,0,0)
P2= PointXYZ(0,1,0)
P3= PointXYZ(-1,0,0)
Trans1= ThreePtTrans(P1,P2,P3)
Stat= SetWorkplane(Trans1)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)
Stat= MillDefCutInfo(Mdef1,0.0,0.0,0.0,0.0,1,V10)
Stat= MillDefZInfo(Mdef1,1,0.0)
IF No_radius_var THEN
        V12= FreeParameter("Radius",Radius)
        Path_group=CutWrapCurveGrp(Mdef1,Paths,V12,V8,0.0,V10,V11)
ELSE
        Path_group=CutWrapCurveGrp(Mdef1,Paths,Radius_var$,V8,0.0,V10,V11)
END IF
Stat= AutoNameGroup(Path_group,Name$)
Stat= DeleteEntity(Mdef1)
Stat= SetNoPathFlag(Path_group,WsTrue)
Stat=AssociateTool(Path_group,Tool)
Stat= DeleteGroup(Paths,WsTrue,WsTrue)
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("cut wrap 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

