
! script name: pkvmach.d/vpkhelicalslice
! var_topic: runs a helical cutter path around a solid.

!A
!A NAME:
!A
!A 	Gouge Check: Helical Slice	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script is a version of the drive curve slice where the two
!A	curves are concentric helices.  To calculate the cutter path, 
!A	imaginary lines are drawn between the two curves. We then imagine
!A	the cutter moving along that line until it contacts the part. The
!A	location point of the cutter center then becomes the point on the
!A	cutter path.
!A
!A~ image location - images/helicalslice.gif
!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
!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: Z location of the top of the helical path.
!B
!B 	Z END: Z location of the bottom of the helical path.
!B
!B 	Z STEP: Z depth of each revolution.
!B
!B      PICK CENTER POINT: Pick the center of the helical curves.
!B
!B      START RADIUS:  Radius of the first helical curve. For outside cutting
!B			this should be a radius which is greater than the cutter
!B			radius beyond the exterior of the part. For inside
!B			cutting it should be clear of the inner walls by
!B			the cutter radius.
!B
!B      END RADIUS:  Radius of the second helical curve. For outside cutting
!B			this radius should be within the part. For inside
!B			cutting it should be clear of the exterior of the part.
!B
!B      STARTING ANGLE:  Starting angle of the helical curves.
!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 to extend parts of the body for machining.
!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 	An additional feature is auto edge extension.  If the auto edge extend
!C 	switch is set on an edge, the edge will extend a distance equal to the
!C 	extension dist (a named variable), along the tangent to the surface on
!C 	the edge.  Then the normal vector controlling the cutter will roll
!C 	through 90 deg of arc.  This feature helps insure that the gouge check
!C 	will work in tight corners, where the cutter paths extend beyond the
!C 	edge of the part.
!C
!C 	Gouge Level:
!C
!C 		WsContactCheck -   0: Fastest- Checks cutter contact point only.
!C 		WsCutterCylinder - 1: Slower- Checks cutter "cylinder".  The
!C 				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 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: solid drive curve slice gouge ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Body_name$[256]
DIM Contour1$[256]
DIM Contour2$[256]
DIM Finish_var$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Astart_var$[256]
DIM Rstart_var$[256]
DIM Rend_var$[256]
DIM Point$[256]
DIM Name$[256]

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


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)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Units=CurrentUnits

ActivateUndo
Current_pen=CurrentColor
Gouge_level= WsCutterCylinder
Make_cpaths= WsTrue
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/vpkhelicalslice"
  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/vpkhelicalslice"
END IF
Have_tool:!

Tool=FindUserData(Tool_name$,WsToolData)

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

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



Past_dia:!
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$)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)

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

Stat=SetPickMask(WsPickGeometry)
Start_point: Stat=SetInputFormat("n4","")
E1=PickEntity("PICK CENTER POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Start_point
Stat= EntityNameCheck(E1,Point$)
Pick_para= LastPickPara
Pick_return= CurrPickReturn

Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Startr)
Startr=InputRadius("START RADIUS")
IF ExitScript(Destination$) THEN Leave
No_rstart_var= LastVariableInput(Rstart_var$)

Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Endr)
Endr=InputRadius("END RADIUS")
IF ExitScript(Destination$) THEN Leave
No_rend_var= LastVariableInput(Rend_var$)

Stat=SetInputFormat("n4=","")
Stat=SetAngleEquals(Astart)
Astart=InputAngle("START ANGLE")
IF ExitScript(Destination$) THEN Leave
No_astart_var= LastVariableInput(Astart_var$)

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=SetCurrentColor(Pen_color)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: helical slice")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
END SELECT
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V3= FreeParameter("Tolerance",Tol)
IF No_rstart_var THEN
        V5= FreeParameter("Start radius",Startr)
ELSE
        V5= FreeParameter("Start radius",Rstart_var$)
END IF
IF No_rend_var THEN
        V6= FreeParameter("End radius",Endr)
ELSE
        V6= FreeParameter("End radius",Rend_var$)
END IF
IF No_zstart_var THEN
        V7= FreeParameter("Z start",Zstart)
ELSE
        V7= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
        V8= FreeParameter("Z end",Zend)
ELSE
        V8= FreeParameter("Z end",Zend_var$)
END IF
IF No_zstep_var THEN
        V9= FreeParameter("Z step",Zstep)
ELSE
        V9= FreeParameter("Z step",Zstep_var$)
END IF
IF No_astart_var THEN
        V10= FreeParameter("Start angle",Astart)
ELSE
        V10= FreeParameter("Start angle",Astart_var$)
END IF
V11= FreeParameter("Gouge check level",Gouge_level)
IF Mill_dir= WsConventional THEN
        V12= FreeParameter("Mill direction",WsConventional)
ELSE
        V12= FreeParameter("Mill direction",WsClimb)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0,V1)
Stat=Setup3dCutter(Mdef1)
PlotOff
Stat= PrintToVarRecord("PlotOff")
Body_group= WPKConvertBody(Body_name$,WsPKConvertFaces)
Stat= SetEdgeExt(Body_group)
Stat= SetFullGouge(Body_group,WsTrue)
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
Work= CurrentWorkplane
Cutter_path=HelicalSliceGouge(Work,Part,Pc,V5,V6,V7,V8,V9,V10,V12,V3,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,Ps,Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("helical slice")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Z_level

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