
! script name: pkmachining.d/pkdrivepocket
! solids: slices a solid using lines between two drive curves

!A
!A NAME:
!A
!A 	MCheck: Drive Curve Pocket Gouge a Solid
!A
!A DESCRIPTION:
!A
!A 	This script cuts a solid body. Each slice is generated as  
!A 	follows: The cutter center is locked on a line joining corresponding
!A	points on the drive curves in a constant Z plane. The cutter is
!A	then moved along the line until it comes in contact with the part.
!A	As the lines sweep around the two drive curves the contact points 
!A	generate the slice.
!A	This function will remove the material between the first drive
!A	curve and the slice generated above.
!A
!B INSTRUCTIONS:
!B
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!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
!B 		variable 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
!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 	PICK DRIVE CONTOUR #1: Pick the first generating contour.
!B
!B 	PICK DRIVE CONTOUR #2: Pick the second generating contour.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C  NOTES:
!C
!C 	Drive Slice Rules:
!C
!C 	It is assumed that if the cutter is placed on the first drive contour
!C 	it is clear of interference with the part.  Drive slice does not work
!C 	well with standing bosses and islands.  It leaves uncut areas.  This
!C 	routine will not cut areas that are in the shadow of the cutter as
!C 	determined by the lines joining the drive curves.
!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 	Gouge Level:
!C
!C 		WsContactCheck -   0: Fastest- Checks cutter contact point only.
!C 		WsCutterCylinder - 1: Slower- Checks cutter "cylinder".
!C 			The cylinder generated by the cutter diameter.
!C 		WsFullGougeCheck - 2: Slowest- Treats the cutter as a surface
!C 			of revolution and calculates the intersection
!C 			between the part and the cutter surface.
!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

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

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

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Layer_sw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)
Level_clear=NamedVarVal("3D Level Clear",Ws3DSystemVars,1.0)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,1.0)
Units=CurrentUnits

Gouge_level= WsCutterCylinder
Make_cpaths= WsTrue

IF ARGC<2 THEN
	Gouge_level= WsCutterCylinder
ELSE
        Gouge_level= VAL(ARGV(1))
END IF


Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0
Shank_radius=0
Flute_length=1
Total_length= 1

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$)
Flute_length=GetUserValue(Tool,"Flute length:")
Total_length=GetUserValue(Tool,"Tip to holder:")
SELECT Tool_type$
CASE "ENDMILL","BULLMILL"
	Cutter_type=WsEndMill
	Cutter_dia=GetUserValue(Tool,"Diameter:")
	Corner_rad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Shank_radius= 0.5*Shank_dia
CASE "TAPERMILL"
	Cutter_type=WsEndMill
	Cutter_dia=GetUserValue(Tool,"Diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
	Corner_rad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Shank_radius= 0.5*Shank_dia
CASE "TAPERBALL"
        Cutter_type=WsTaperedBall
        Cutter_dia=GetUserValue(Tool,"Diameter:")
        Taper=GetUserValue(Tool,"Taper angle:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Shank_radius= 0.5*Shank_dia
CASE "BALLMILL"
	Cutter_type=WsBallCutter
	Cutter_dia=GetUserValue(Tool,"Diameter:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Shank_radius= 0.5*Shank_dia
CASE ELSE
	GOTO Tool_ques
END SELECT
LowerSelectBox

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

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

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,Flute_length,0.0,Shank_radius,Total_length)



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

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(WsPickNoSurf)

Stat=SetInputFormat("n4","")
Contour1=PickGroupEnt("PICK DRIVE CONTOUR #1",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(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 ValidGeometry(Contour2) THEN Cut_part

Stat=Highlight(Contour2,WsPickPen,WsDirArrowOff)

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

Z_Group=ZStepsPtGrp(ZStepMethod,Zstart,Zend,Zstep)

PlotOff
Body_group= WPKConvertBody(Body_ent,WsPKConvertFaces)
Stat= HideEntity(Body_group)
Stat= SetEdgeExt(Body_group)
Stat=SetFullGouge(Body_group,WsTrue)
IF Extra_surfs THEN
	Part= CopyEntity(Part_group)
	Stat= CombineGroups(Part,Body_group)
ELSE
	Part= CopyEntity(Body_group)
END IF
PlotOn

Work= CurrentWorkplane
Path_group=DrivePocketGouge(Work,Part,Contour1,Contour2,Z_group,Width_cut,Tol,Circle_output,Gouge_level,Make_cpaths,Zigzag_cuts,Layer_sw,Level_clear)

Stat= DeleteGroup(Body_group,WsTrue)
Stat=DeleteEntity(Part)
Stat= DeleteGroup(Z_group,WsTrue)

IF ValidGeometry(Path_group) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=GroupName(Path_group,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  Z DRIVE POCKET 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
	Stat=GroupName(Contour1,Answer$)
	Datastr$=Datastr$&"CONTOUR 1: "&Answer$&CHR$(10)
	Stat=GroupName(Contour2,Answer$)
	Datastr$=Datastr$&"CONTOUR 2: "&Answer$&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&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$&"CIRCLES: "&VAL$(Circle_output)&CHR$(10)
	Datastr$=Datastr$&"GOUGE LEVEL: "&VAL$(Gouge_level)&CHR$(10)
	Datastr$=Datastr$&"MAKE CPATHS: "&VAL$(Make_cpaths)&CHR$(10)
	Datastr$=Datastr$&"ZIGZAGS: "&VAL$(Zigzag_cuts)&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(Path_group,Cpath3d,Ws3dCpath)
        Stat=AssociateTool(Path_group,Tool)
        Number=GroupCount(Path_group)
        FOR I=1 TO Number
               GrpTool=GroupElement(Path_group,I)
               Stat=AssociateTool(GrpTool,Tool)
        NEXT I

	ActivateUndo
        Stat= AdvanceUndoNumber("drive curve pocket")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	PlotAll
END IF

GOTO Start

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
