! script name: pkmachining.d/pkzbdycrossslice
! solids: creates slice cutterpaths by intersecting parallel cross cuts

!A
!A NAME:
!A
!A 	MCheck: Boundary Cross Slice
!A
!A DESCRIPTION:
!A	
!A	This script will generate a cutterpath by slicing a solid. 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.
!A
!A~ image location - images/zbdycrossslice.gif
!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 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 	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 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 
!B 		areas of the body or to extend parts of the body for machining.
!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
!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: boundary cross gouge ")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Name$[256]
DIM Name1$[256]
DIM Body_name$[256]

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)
ZRoughMethod=NamedVarVal("3D Z Rough Method",Ws3DSystemVars,1.0)
Units=CurrentUnits


Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0

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

Tool=FindUserData(Tool_name$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Tool_type$)

SELECT Tool_type$
CASE "ENDMILL","BULLMILL"
      Cutter_type=WsEndMill
      Cutter_dia=GetUserValue(Tool,"Diameter:")
      Corner_rad=GetUserValue(Tool,"Corner radius:")
CASE "TAPERMILL"
      Cutter_type=WsTaperedEndmill
      Cutter_dia=GetUserValue(Tool,"Diameter:")
      Taper=GetUserValue(Tool,"Taper angle:")
      Corner_rad=GetUserValue(Tool,"Corner radius:")
      Length=GetUserValue(Tool,"Flute length:")
CASE "TAPERBALL"
      Cutter_type=WsTaperedBall
      Cutter_dia=GetUserValue(Tool,"Diameter:")
      Taper=GetUserValue(Tool,"Taper angle:")
      Length=GetUserValue(Tool,"Flute length:")
CASE "BALLMILL"
      Cutter_type=WsBallCutter
      Cutter_dia=GetUserValue(Tool,"Diameter:")
CASE ELSE
      GOTO Tool_ques
END SELECT
LowerSelectBox

Past_dia:!
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

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

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

VCutter_type=CreateVariable("cutter type",WsSystemVars,Cutter_type)
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VCorner=CreateVariable("corner radius",WsSystemVars,Corner_rad)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)

Stat=Setup3dCutter(Cutter_type,Cutter_rad,Corner_rad,Offset,Finish_allow,Taper,Length)

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

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

Pick_loop:!
Stat=SetInputFormat("a1",",F,")
E1=WPKPickEntity("PICK BODY TO BE MACHINED  Face group ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="F" THEN
        Stat=SetInputFormat("n4","")
        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$)
	Body_ent= G1
ELSE
        IF NOT WPKIsEntity(E1) THEN Pick_loop
        Body_ent= WPKParentBody(E1)
        Stat= EntityNameCheck(Body_ent,Body_name$)
END IF


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 ValidGeometry(Part_group) THEN Pick_loop
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Extra_surfs=1


Cut_part:!
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave

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

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

Boundary_loop:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Contour=PickEntity("PICK EXTERIOR CONTOUR",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(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)
        Isle=PickEntity("PICK ISLAND #"&VAL$(I))
        IF ExitScript(Destination$) THEN Leave
	Stat=SetPickMask(WsPickGeometry)

        Stat=AddToGroup(Islands,Isle)

NEXT I

Stat=SetPickMask(WsPickGeometry)
Start_point: Stat=SetInputFormat("n4","")
Start_pt=XyzInput("APPROXIMATE START POINT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Start_pt) THEN Start_point


Pc= PointXyz(0,0,0)
X=XPointValue(Pc)
Y=YPointValue(Pc)
Z=ZPointValue(Pc)

Pz=PointXyz(0,0,0)
Trans3=AngleTransform(Pz,0,0,-Angle,WsWorkplaneVals)
Stat=DeleteEntity(Pz)

Angle= 43.5
Pz=PointXyz(-X,-Y,-Z)
Trans1=AngleTransform(Pz,0,0,0,WsWorkplaneVals)
Stat=DeleteEntity(Pz)
Pz=PointXyz(0,0,0)
Trans2=AngleTransform(Pz,0,0,-Angle,WsWorkplaneVals)
Trans3=MultTransforms(Trans2,Trans1)
Stat=DeleteEntity(Pz)
Stat=DeleteEntity(Pc)

Stat=SetPickMask(WsPickGeometry)
IF Circle_output THEN
        Make_circles= WsCircleMoves
ELSE
        Make_circles= WsLinearMoves
END IF
Zbottom= (Zstart >= Zend )*Zend+ (Zstart< Zend)*Zstart
PlotOff
Body_group= WPKConvertBody(Body_ent,WsPKConvertFaces)
Stat= HideEntity(Body_group)
Stat= SetEdgeExt(Body_group)
IF Extra_surfs THEN
	Part= CopyEntity(Part_group)
	Stat= CombineGroups(Part,Body_group)
ELSE
	Part= CopyEntity(Body_group)
END IF
Cgroup=BoundaryCrossGouge(Trans3,Part,Contour,Islands,Zbottom,Grid_width,Tol)
ZGroup= ZStepsPtGrp(ZStepMethod,Zstart,Zend,Zstep)
PlotOn
Cutter_path= ZGrpSliceParaCuts(Cgroup,Zgroup,Start_pt,Grid_width,Tol,Mill_dir,Make_circles)
PlotOff

Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteGroup(Cgroup,WsTrue)
Stat= DeleteGroup(Zgroup,WsTrue)
Stat=DeleteEntity(Part)
Stat=DeleteEntity(Trans1)
Stat=DeleteEntity(Trans2)
Stat=DeleteEntity(Trans3)
PlotOn
IF ValidGeometry(Cutter_path) THEN

	X=XPointValue(Pc)
        Y=YPointValue(Pc)
        Z=ZPointValue(Pc)
        Xs=XPointValue(Start_pt)
        Ys=YPointValue(Start_pt)
        Zs=ZPointValue(Start_pt)

	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=CPathFileName(Cutter_path,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  Z BOUNDARY CROSS SLICE SOLID"&CHR$(10)
	Datastr$=Datastr$&"BODY NAME: "&Body_name$&CHR$(10)
	IF Extra_surfs THEN
		Stat=GroupName(Part_group,Answer$)
		Datastr$=Datastr$&"SURFACE GROUP: "&Answer$&CHR$(10)
	END IF
	Group=ParentGroup(Contour,0)
	Stat=GroupName(group,Name1$)
	Datastr$=Datastr$&"EXTERIOR: "&Name1$&CHR$(10)
	Nisles=GroupCount(Islands)
	Datastr$=Datastr$&"NUMBER ISLANDS: "&VAL$(Nisles)&CHR$(10)
	FOR I=1 TO Nisles
		isle=GroupElement(Islands,I)
		group=ParentGroup(Isle,0)
		Stat=GroupName(group,Name1$)
		Datastr$=Datastr$&"ISLAND"&VAL$(I)&": "&Name1$&CHR$(10)
	NEXT I
	Datastr$=Datastr$&"CENTER PT X: "&VAL$(X)&CHR$(10)
        Datastr$=Datastr$&"CENTER PT Y: "&VAL$(Y)&CHR$(10)
        Datastr$=Datastr$&"CENTER PT Z: "&VAL$(Z)&CHR$(10)
        Datastr$=Datastr$&"START POINT X: "&VAL$(Xs)&CHR$(10)
        Datastr$=Datastr$&"START POINT Y: "&VAL$(Ys)&CHR$(10)
        Datastr$=Datastr$&"START POINT Z: "&VAL$(Zs)&CHR$(10)
	Datastr$=Datastr$&"CUT ANGLE: "&VAL$(Angle)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
	Datastr$=Datastr$&"CROSS GRID WIDTH: "&VAL$(Grid_width)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)
	Datastr$=Datastr$&"MILL DIRECTION: "&VAL$(Mill_dir)&CHR$(10)
	Datastr$=Datastr$&"CIRCLES: "&VAL$(Circle_output)&CHR$(10)
        Datastr$=Datastr$&"Z START: "&VAL$(Zstart)&CHR$(10)
        Datastr$=Datastr$&"Z END: "&VAL$(Zend)&CHR$(10)
        Datastr$=Datastr$&"Z STEP: "&VAL$(Zstep)&CHR$(10)
        Datastr$=Datastr$&"Z STEP METHOD: "&VAL$(Zstepmethod)&CHR$(10)

	Cpath3d=FindUserData(Name$,Ws3dCpath)
	IF ValidGeometry(Cpath3d) THEN
		 Stat=DeleteEntity(Cpath3d)
	END IF
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

	Stat=AssocUserData(Cutter_path,Cpath3d,Ws3dCpath)
	Stat=AssociateTool(Cutter_path,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("boundary gouge check")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutter_path,1,WsTrue)

	PlotAll

END IF
Stat=DeleteEntity(Islands)
Stat=DeleteEntity(Start_pt)

GOTO Start

Leave:!
PlotOn
Stat=DeleteEntity(Start_pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pz)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Islands)
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
