
! script name: pkvmach.d/vpkparagouge
! solids: cuts a body using parallel gouge

!A
!A NAME:
!A
!A      Gouge Check: Parallel	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script cuts a solid body using parallel cuts.  The cutter
!A 	center is locked in the vertical plane containing the cut, and allowed
!A 	to move in that plane, in contact with the part.  The script is setup
!A 	to output circles for cuts parallel to the X or Y axes.  The cutter
!A 	path is generated using gouge avoidance.  The cuts are bounded by the
!A 	XY plane and by a rectangular box specified in the questions.
!A
!B~ image location - images/parallelgouge.gif
!B
!B INSTRUCTIONS:
!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 BODY TO BE MACHINED: Pick the body. It may be a sheet or solid.
!B 		A group of faces may be selected instead of a body.  This
!B 		group will be treated as if it were a free standing sheet body.
!B
!B 	PICK ADDITIONAL SURFACE GROUP, Name, Proceed:  Additional synergy
!B 		surfaces may be added here to the body to be machined.
!B 		There surfaces may be used to keep the cutter out of areas
!B 		of the body or to to extend parts of the body for machining.
!B
!B 	ANGLE OF CUTS: Enter the angle of cuts. An angle of cut of zero
!B 		produces cuts parallel to the X axis going from -Y to +Y.
!B 		An angle of 90 produces cuts parallel to the Y axis going
!B 		from +X to -X.
!B
!B 	START LENGTH: Enter the start length.  If the angle of cuts is zero,
!B 		this is the Y coordinate of the first cut; otherwise the
!B 		configuration rotates with the angle of cut.
!B
!B 	END LENGTH: Enter the end length.  If the angle of cuts is zero, this
!B 		is the Y coordinate of the last cut.
!B
!B 	START WIDTH: Enter the start width.  If the angle of cuts is zero, this
!B 		is the X coordinate of the start of each cut.
!B
!B 	END WIDTH: Enter the end width.  If the angle of cuts is zero, this is
!B 		the X coordinate of the end of each cut.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C NOTES:
!C
!C 	An additional feature of parallel gouge is auto edge extension.  If the
!C 	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 	the the gouge check will work in tight corners where the cutter paths
!C 	extend beyond the edge of the part.
!C
!C OPTIONS:
!C
!C 	Circle output is controlled by the named variable "3D Circle Output".
!C
!C 	There are two methods available:
!C
!C 		WsLinearMoves - 0: Outputs straight line moves.
!C 		WsCircleMoves - 1: Outputs circular arc moves.
!C
!C NOTE:
!C
!C	If this function is applied to a group of faces, the group of
!C	faces is treated like a stand alone sheet body. It does not check
!C	for cutter interference in the body beyond the faces in the group.
!C	Special care should be taken with the behavior at the edge of the
!C	group where the edge rolling may cause interference. It is 
!C	recommended that face groups only be used when a boundary contour
!C	is present to contain the cuts.
!C


Stat=ChangeLabelStr("ScriptNameStatus","Function: parallel gouge ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Body_name$[256]
DIM Part_group$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Angle_var$[256]
DIM Name$[256]

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

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

IF Units=WsMetric THEN 
	Startx=-1000
	Endx=1000
	Starty=-1000
	Endy=1000
ELSE
	Startx=-100
	Endx=100
	Starty=-100
	Endy=100
END IF

IF ARGC>1 THEN
	Tool_name$= ARGV(1)
	GOTO Have_tool
END IF
Start:!

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:pkvmach.d/vpkparagouge"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL, New_tool ",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
IF Tool_name$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:pkvmach.d/vpkparagouge"
END IF
Have_tool:!

Tool=FindUserData(Tool_name$,WsToolData)

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



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

Stat=SetInputFormat("n4=","")
Stat=SetAngleEquals(Angle)
Angle=InputAngle("ANGLE OF CUTS")
IF ExitScript(Destination$) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)


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


Pick_loop:!
Stat=SetInputFormat("a1",",F,A,")
E1=WPKPickEntity("PICK BODY  Face group, Assembly ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "A"
	Pkassembly= WPKCurrentAssembly
	IF NOT WPKIsEntity(Pkassembly) THEN
       		 Stat=SetInputFormat("n4","")
       		 E1=WPKPickEntity("PICK ASSEMBLY")
       		 IF (ExitScript(Destination$)) THEN Leave
       		 IF NOT WPKIsEntity(E1) THEN pick_loop

       		 Pkbody= WPKParentBody(E1)
       		 Pkassembly= WPKParentAssembly(Pkbody)
	END IF
	IF NOT WPKIsEntity(Pkassembly) THEN Pick_loop
        Stat= EntityNameCheck(Pkassembly,Body_name$)
CASE "F"
	Stat=SetInputFormat("n4","")
	E1=WPKPickEntity("PICK GROUP TO BE MACHINED")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT WPKIsEntity(E1) THEN Pick_loop
	G1= WPKContainingGroup(E1,1,WsTrue)
	IF NOT WPKIsEntity(G1) THEN Pick_loop
	Stat= EntityNameCheck(G1,Body_name$)
CASE ELSE
	IF NOT WPKIsEntity(E1) THEN Pick_loop
	Body_ent= WPKParentBody(E1)
	Stat= EntityNameCheck(Body_ent,Body_name$)
END SELECT

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,P,")
Part_group=PickGroupEnt("PICK ADDITIONAL SURFACE GROUP, Name, Proceed ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        GOSUB Get_named_group
CASE "P"
        Extra_surfs=0
        GOTO Cut_part
END SELECT
IF NOT ValidDependency(Part_group) THEN Pick_loop
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Extra_surfs=1

Cut_part:!
Stat=SetPickMask(WsPickGeometry)
Make_circles=0
IF Circle_output THEN
	Ang=(Angle+720) MOD 360
	Ang=PROUND(Ang,-3)
	IF Ang=0 OR Ang=90 OR Ang=180 OR Ang=270 OR Ang=360 THEN
		Make_circles=1
	END IF
END IF


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Parallel Gouge Check")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
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
IF No_angle_var THEN
        V3= FreeParameter("Cut Angle",Angle)
ELSE
        V3= FreeParameter("Cut Angle",Angle_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Zigzag cuts",Zigzag_cuts)
V6= FreeParameter("Start length",Startx)
V7= FreeParameter("End length",Endx)
V8= FreeParameter("Start width",Starty)
V9= FreeParameter("End width",Endy)
V10= FreeParameter("Make circles",Make_circles)
V11= FreeParameter("Extension dist",Ext_dist)
V12= FreeParameter("X center",0.0)
V13= FreeParameter("Y center",0.0)
V14= FreeParameter("Z center",0.0)
Pc= PointXYZ(V12,V13,V14)
Pz=PointXyz(-XPointValue(Pc),-YPointValue(Pc),-ZPointValue(Pc))
Trans1=AngleTransform(Pz,0,0,0,WsWorkplaneVals)
Stat=DeleteEntity(Pz)
Pz=PointXyz(0,0,0)
Trans2=AngleTransform(Pz,0,0,V3,WsWorkplaneVals)
Trans3= InverseTrans(Trans2)
Trans4=MultTransforms(Trans3,Trans1)

Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat=Setup3dCutter(Mdef1)
PlotOff
Stat= PrintToVarRecord("PlotOff")
Body_group= WPKConvertBody(Body_name$,WsPKConvertFaces)
Stat= SetEdgeExt(Body_group)
Stat= HideEntity(Body_group)
RecordFunctionOff
IF Extra_surfs THEN
RecordFunctionOn
        Part= CopyEntity(Part_group$)
        Stat= CombineGroups(Part,Body_group)
RecordFunctionOff
ELSE
RecordFunctionOn
        Part= CopyEntity(Body_group)
RecordFunctionOff
END IF
RecordFunctionOn
Stat= PrintToVarRecord("PlotOn")
PlotOn
Cutter_path=ParallelGouge(Trans4,Part,V6,V7,V8,V9,V2,V4,V5,V10,V11)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)
Stat= DeleteEntitylist(Mdef1,Pc,Pz,Trans1,Trans2,Trans3,Trans4)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("parallel gouge check")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Start

Leave:!
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
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
