
! script name: v3dcpaths.d/zbdycrosspocket
! var_topic: pockets a part by slicing cross section cuts.

!A
!A NAME:
!A
!A 	Gouge: Z Boundary Cross Pocket
!A
!A DESCRIPTION:
!A	
!A	This script will generate a cutterpath by pocketing a part. Unlike
!A	other slicing routines which slice part geometry, this script
!A	creates a cross grid of cuts using parallel gouge check, it then
!A	creates cutterpaths by slicing these cuts to produce a planar
!A	cutterpath. It then pockets the slice created. An advantage of
!A	using this method is that it can pocket parts which have 
!A	overhanging sections by avoiding the overhang.
!A
!A
!A
!B INSTRUCTIONS:
!B	
!B	This script is designed for 3D roughing. It is designed to be
!B	used with a finish allowance which will keep the cutter away
!B	from the part. 
!B
!B	The surface normals should be pointed in the correct direction.
!B	Usually, the normal directions determined by auto grouping
!B	will be sufficient. If a surface is partially visible to the cutter,
!B	the normal direction must be properly set. Surface which are invisible
!B	to the cutter do not matter.
!B		
!B	The part should form a closed volume, at least relative to the
!B	current workplane. 
!B	
!B	The part should not have any "holes". Any interior holes should
!B	be covered.
!B
!B	The part should have auto edge extension on. 
!B
!B	Any cutter with vertical sides may be used. The best paths are 
!B	generated by cutters which have a corner radius.
!B
!B	The roughing cuts produced may be messy in areas where the
!B	cutter just barely fits. These areas should be avoided by 
!B	choice of cutter, finish allowance, and if necessary, covering
!B	the area in question.
!B
!B	The cuts may be messy on Z levels which coincide with nearly flat areas
!B	on the part. Use a Z step which does not coincide with these flat
!B	areas.
!B
!B	This method can be very slow. It will however, produce a cutterpath
!B	for parts which cannot be sliced by other means.
!B
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B	CROSS GRID WIDTH: Cross grid resolution. This is the resolution for
!B			  cross section grid used to slice the part.
!B
!B 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: Enter the Z depth increment.
!B
!B 		The Z depth calculation is controlled by the user named variable
!B 		Z Step Method.  There are two methods available:
!B 			WsEqualZSteps - 0: Equalized steps in Z.
!B 			WsFullZDepth - 1: Each Z pass is at full depth
!B 			        increment, only the last Z pass is smaller.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!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

Stat=ChangeLabelStr("ScriptNameStatus","Function: z cross pocket ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Contour$[256]
DIM Island$[256]
DIM Width_var$[256]
DIM Finish_var$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Grid_var$[256]
DIM Point$[256]
DIM Name$[256]

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

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)
Units=CurrentUnits


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:v3dcpaths.d/zbdycrosspocket"
  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:v3dcpaths.d/zbdycrosspocket"
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:!
Stat=SetInputFormat("a1=",",1,2,")
Stat=SetEqualsValue(0,"2")
Stat=GetValue("MILLING DIRECTION ( 1 )CW or ( 2 )CCW ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "1"
        Mill_dir= WsCW
CASE ELSE
        Mill_dir= WsCCW
END SELECT

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_cut_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)

Cutter_rad= ToolDiameter(Tool,0.5)
Widthr= Cutter_rad/3
IF Cutter_rad> Tol THEN
        D= SQR(ABS(2*Cutter_rad*Tol- Tol*Tol))
        Widthr= (D< Widthr)*D + (D> Widthr)*Widthr
END IF

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Widthr)
Grid_width=GetValue("CROSS GRID WIDTH",Answer$)
IF ExitScript(Destination$) THEN Leave
Grid_width=ABS(Grid_width)
Widthr=Grid_width
No_width_var= LastVariableInput(Grid_var$)


Z_level: !
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zstart_var= LastVariableInput(Zstart_var$)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zend)
Zend=GetValue("Z END",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zend_var= LastVariableInput(Zend_var$)

IF Zend=Zstart THEN
        Zstep=0
        No_zstep_var=1
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Zstep)
        Zstep=GetValue("Z STEP",Answer$)
        IF ExitScript(Destination$) THEN Leave
        No_zstep_var= LastVariableInput(Zstep_var$)
END IF

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)

Boundary_loop:!
Stat=SetPickMask(WsPickNoSurf)
Contour=PickGroupEnt("PICK EXTERIOR CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Boundary_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN Boundary_loop
Stat=SetPickMask(WsPickGeometry)

Closed=IsContClosed(Contour)
IF NOT Closed THEN Boundary_loop

Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(0.0)
Nisles=GetValue("NUMBER OF ISLANDS",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4","")
Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
        Stat=SetPickMask(WsPickNoSurf)
Pick_isle:Isle=PickGroupEnt("PICK ISLAND #"&VAL$(I))
        IF ExitScript(Destination$) THEN Leave
        Stat=SetPickMask(WsPickGeometry)
        IF (NOT ValidDependency(Isle)) THEN Pick_isle
        Stat= EntityNameCheck(Isle,Island$)
        IF NOT LEN(Island$) THEN Pick_isle
        Stat=AppendToGroup(Islands,Isle)
NEXT I

Cut_part:!
Stat=SetPickMask(WsPickGeometry)
IF Circle_output THEN
        Make_circles= WsCircleMoves
ELSE
        Make_circles= WsLinearMoves
END IF
Zbottom= (Zstart >= Zend )*Zend+ (Zstart< Zend)*Zstart
Angle= 43

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Z Boundary Cross Pocket")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Stat= NullReference(Contour$)
Isles=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Nisles
        Isle= GroupElement(Islands,I)
        Stat= EntityNameCheck(Isle,Island$)
        RecordFunctionOn
        Stat= AppendToGroup(Isles,Island$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_cut_var THEN
        V21= FreeParameter("Width of cut",Width_cut)
ELSE
        V21= FreeParameter("Width of cut",Width_var$)
END IF
IF No_width_var THEN
        V2= FreeParameter("Grid width",Grid_width)
ELSE
        V2= FreeParameter("Grid width",Grid_var$)
END IF
V3= FreeParameter("Grid Angle",Angle)
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Z step method",ZStepMethod)
IF No_zstart_var THEN
        V6= FreeParameter("Z start",Zstart)
ELSE
        V6= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
        V7= FreeParameter("Z end",Zend)
ELSE
        V7= FreeParameter("Z end",Zend_var$)
END IF
IF No_zstep_var THEN
        V8= FreeParameter("Z step",Zstep)
ELSE
        V8= FreeParameter("Z step",Zstep_var$)
END IF
V9= FreeParameter("Z bottom",Zbottom)
V14= FreeParameter("Make circles",Make_circles)
V16= FreeParameter("X center",0.0)
V17= FreeParameter("Y center",0.0)
V18= FreeParameter("Z center",0.0)
V19= FreeParameter("Z rough method",ZRoughMethod)
V20= FreeParameter("Mill direction",Mill_dir)
V22= FreeParameter("Layer switch",1)
V23= FreeParameter("Minimum pickups",0)
Pc= PointXYZ(V16,V17,V18)
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,V20)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat=Setup3dCutter(Mdef1)
PlotOff
Stat= PrintToVarRecord("PlotOff")
Cgroup=BoundaryCrossGouge(Trans4,Part_group$,Contour$,Isles,V9,V2,V4)
Z_Group=ZStepsPtGrp(V5,V6,V7,V8)
PlotOn
Stat= PrintToVarRecord("PlotOn")
Path_group= ZGrpPocketParaCuts(Cgroup,Z_group,V21,V2,V4,V20,V22,V23,V14)
Stat= PrintToVarRecord("PlotOff")
Stat= AutoNameGroup(Path_group,Name$)
Stat=AssociateTool(Path_group,Tool)
PlotOff
Stat= DeleteEntity(Isles)
Stat= DeleteGroup(Cgroup,WsTrue)
Stat= DeleteGroup(Z_group,WsTrue)
PlotOn
Stat= DeleteEntityList(Mdef1,Pc,Pz,Trans1,Trans2,Trans3,Trans4)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Path_group) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("z bdy cross pocket")
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= DeleteEntity(Islands)

GOTO Start

Leave:!
LowerSelectBox
Stat= DeleteEntity(Islands)
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
