
! script name: pkmachining.d/zslicecut
! solids: cuts bodies using Z slices with gouge check

!A
!A NAME:
!A
!A 	MCheck: Z Slice Cut
!A
!A DESCRIPTION:
!A
!A 	This script generates the cutter path of a Z slice through a body.  The
!A 	cutter is locked in the horizontal Z plane and allowed to move in the
!A 	path which is generated by placing the cutter in contact with the part.
!A 	Gouge checking is performed on the cutter paths. 
!A
!B INSTRUCTIONS:
!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 	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 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: 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 the full depth
!B 			        increment, only the last Z pass is smaller.
!B
!B 		The Z rough method is controlled by the user named variable
!B 		Z Rough Method.  There are three methods available:
!B 			WsIncludeZAbove - 0: Includes the Z cuts above Z limit
!B 			        plane.
!B 			WsZBetweenCuts - 1: Includes Z cuts between passes.
!B 			WsZOnPlaneOnly - 2: Includes Z cuts only on limit plane.
!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
!B                                a body. This group will be treated as if it
!B                                were a free standing sheet body.
!B
!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
!B 	APPROXIMATE START POINT: Input or pick a point to determine the path
!B 		start points. Each closed path will start on the point closest
!B 		to this point. If the path is open the start point is ignored.
!B
!C NOTES:
!C
!C	To insure best results when using this routine:
!C
!C 	1.)  The part should be properly trimmed. Instances where surfaces
!C 	     are not trimmed may be handled correctly if the surfaces have
!C 	     definite non-tangent intersects. Overlapping surfaces and
!C 	     gaps between trimmed surfaces can cause problems. 
!C
!C 	2.)  This routine does not round sharp external corners. A sharp 
!C 	     external corner will create a gap in the cutter path.
!C 	     Sharp internal corners are handled well. 
!C 	     Badly trimmed tangent intersections may behave like sharp
!C 	     corners for large offsets.
!C
!C 	3.)  Choose the Z step so as to avoid horizontal surfaces.
!C 	     It may be very difficult to obtain a good cut at a z
!C 	     level close to a flat horizontal level. In general
!C 	     choose irregular numbers for the z levels.
!C
!C 	4.)  Use a cutter offset that is either smaller or larger
!C 	     than any internal radii.
!C
!C 	5.)  Auto edge extension should be turned on with this routine.
!C
!C 	6.)  Avoid backdraft on any surface.
!C
!C 	7.)  Irregularities in the cutterpath should be within tolerance.
!C 	     The user should carefully examine all cutter paths for problems
!C 	     before processing the NC code. Problem areas may be checked
!C 	     by placing a Cutter Revolution at a point and intersecting it
!C 	     with the part.
!C
!C 	8.)  Reasonable tolerances and finish allowances should be used.
!C 	     The tolerance represents a RANDOM deviation from the
!C 	     nominal value. If irregularities are present in the part
!C 	     such as gaps between trimmed edges or sharp external corners
!C 	     then the tolerance calculation may not be correct.
!C 	     It may be impossible to hold tolerance on a z level
!C 	     near a horizontal surface.
!C
!C 	9.) This routine will process cutter paths for Ball and Standard
!C 	    endmills with straight sides. The routine will not
!C 	    gouge check for tapered cutters. 
!C
!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
!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 gouge part")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Body_name$[256]

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Start:!
Make_cpath=1
Cutter_type=WsBallCutter
Offset=0
Taper=0
Current_pen=CurrentColor

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:!
Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Stat=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

Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

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

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)

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

Z_level: !

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

Z_slice:!
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

Additional:!
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 Additional
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Extra_surfs=1


Cut_part:!
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

Z_Group=ZStepsPtGrp(ZStepMethod,Zstart,Zend,Zstep)
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
PlotOn


Stat=SetCurrentColor(Pen_color)
CutterPath=GougeSlicePart(Part,Z_group,Tol,Mill_dir,Make_cpath,Circle_output,Start_pt)

Stat= DeleteGroup(Body_group,WsTrue)
Stat=DeleteEntity(Part)
IF ValidGeometry(CutterPath) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=CPathFileName(Cutterpath,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE: Z PART GOUGE 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
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&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$&"MAKE CPATH: "&VAL$(Make_cpath)&CHR$(10)
	X=XPointValue(Start_pt)
	Y=YPointValue(Start_pt)
	Z=ZPointValue(Start_pt)
	Datastr$=Datastr$&"START POINT X: "&VAL$(X)&CHR$(10)
	Datastr$=Datastr$&"START POINT Y: "&VAL$(Y)&CHR$(10)
	Datastr$=Datastr$&"START POINT Z: "&VAL$(Z)&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)
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

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

	ActivateUndo
        Stat= AdvanceUndoNumber("a part gouge check")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutterpath,1,WsTrue)
	PlotAll
END IF

Stat=DeleteGroup(Z_group,1)
Stat=DeleteEntity(Start_pt)

GOTO Z_level

Leave:!
Stat=SetCurrentColor(Current_pen)
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
