
! script name: v3dcpaths.d/drivecurves
! var_topic: cuts surface using lines between two drive curves

!A
!A NAME:
!A
!A 	Gouge: Drive Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script cuts a group of surfaces with cuts generated by connecting 
!A 	two contours with line segments. The cutter center is locked in the
!A 	vertical plane containing each line and then moved in that plane in
!A 	contact with the part.  The cutter path is generated using gouge
!A 	avoidance.
!A
!A~ image location - images/drivecurves.gif
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER TYPE Ball, Endmill:
!B
!B 		B - Cutter is assumed to be a ball endmill.  Offset is
!B 		    measured from the center of the ball radius.  The bottom
!B 		    of the ball is the programmed point.
!B 		E - Cutter is assumed to be a standard, flat-bottom endmill,
!B 		    with possibly rounded corners.  The programmed point is the
!B 		    center of the bottom of the cutter.  It is this point which
!B 		    is kept away the part boundary.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	PICK DRIVE CONTOUR #1: Pick the first generating contour.
!B
!B 	PICK DRIVE CONTOUR #2: Pick the second generating contour.
!B 		Cuts are generated by lines connecting the two contours.
!B 		The cutter is locked in the vertical plane containing
!B 		the line and is moved to remain in contact with the part.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C  NOTES:
!C
!C 	An additional feature of drive curve gouge is auto edge extension.  If
!C 	the auto edge extend switch is set on an edge, the edge will extend a
!C 	distance equal to the extension dist (a named variable), along the
!C 	tangent to the surface on the edge.  Then the normal vector controlling
!C 	the cutter will roll through 90 deg of arc.  This feature helps insure
!C 	that the gouge check will work in tight corners, where the cutter paths
!C 	extend beyond the edge of the part.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: drive curve gouge ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Contour1$[256]
DIM Contour2$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Name$[256]

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

Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,1.0)
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)
Units=CurrentUnits

ActivateUndo
Start:!

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("CUTTER PATH TOOL",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
LowerSelectBox
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques

Past_dia:!
Cutter_dia=GetUserValue(Tool,"Diameter:")
Widthr=NamedVarVal("width of cut",WsSystemVars,Width_cut)
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Cut_width=Cutter_dia*GetVariableVal(Width_val)
ELSE
	Cut_width= Cutter_dia
END IF
IF Widthr>Cut_width THEN Widthr= Cut_width

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Widthr)
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Width_cut=ABS(Width_cut)
Widthr=Width_cut
No_width_var= LastVariableInput(Width_var$)
Vwidth=CreateVariable("width of cut",WsSystemVars,Widthr)

Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow
No_finish_var= LastVariableInput(Finish_var$)

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


Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)

Pick_loop:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF
IF NOT ValidDependency(Part_group) THEN Pick_loop
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)

Cut_part:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Contour1=PickGroupEnt("PICK DRIVE CONTOUR #1",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Contour1)) THEN Cut_part
Stat= EntityNameCheck(Contour1,Contour1$)
IF NOT LEN(Contour1$) THEN Cut_part
Stat=Highlight(Contour1,WsPickPen,WsDirArrowOff)

Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Contour2=PickGroupEnt("PICK DRIVE CONTOUR #2",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Contour2)) THEN Cut_part
Stat= EntityNameCheck(Contour2,Contour2$)
IF NOT LEN(Contour2$) THEN Cut_part
Stat=Highlight(Contour2,WsPickPen,WsDirArrowOff)

Stat=SetInputFormat("a1=",",L,C,")
Stat=SetEqualsValue(0,"L")
Stat=GetValue("CUT METHOD Lines or Curves ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="C" THEN
	Cut_method=2
ELSE
	Cut_method=1
END IF


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Drive Curve Gouge Check")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Stat= NullReference(Contour1$)
Stat= NullReference(Contour2$)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_var THEN
        V2= FreeParameter("Width cut",Width_cut)
ELSE
        V2= FreeParameter("Width cut",Width_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Extension dist",Ext_dist)
V7= FreeParameter("Zigzag cuts",Zigzag_cuts)
V8= FreeParameter("Cut method",Cut_method)

Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1,0.0,V5)
Stat=Setup3dCutter(Mdef1)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=DriveCurveGouge(0,Part_group$,Contour1$,Contour2$,V2,V4,V7,V6,V8)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntity(Mdef1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("drive curve gouge checl")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_loop



Leave:!
LowerSelectBox
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Nline)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF


Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

RETURN

END
