
! script name: pkvmach.d/pkpocketsectionholes
! var_topic: follow pocket the interiors at z level on a solid body

!A
!A NAME:
!A
!A 	Pocket Section Interiors	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script pocket mills a section interiors through a solid body
!A
!B INSTRUCTIONS:
!B
!B 	WIDTH OF CUT: Enter the actual width of cut (the distance
!B 		between successive offsets).
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!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	PICK BODY: Select a solid body.
!B
!B	Z VALUE: Give the z level in the current workplane.The cut
!B		 will be located on that level.
!B
!C NOTES:
!C
!C	This routine assumes that the solid body is the part to be 
!C	machined.
!C
!C	The body should not present any undercut areas. Undercut
!C	areas will create incorrect machining paths.
!C
!C	This is a 2D cutting technique applied to a 3D solid body. Cutters
!C	are treated like Standard Endmills with zero corner radius and with
!C	no taper.
!C
!C	It is assumed that when the body is sectioned, the exterior
!C	contour is ignored. The remaining contours represent cavities
!C	or holes in the solid body. These contours may have interior
!C	contours which represent bosses or protrusions into the cavity.
!C	This routine will pocket the area between these contours.
!C	If a boss standing inside a cavity has a hole or cavity in it,
!C	the inside of that cavity will be ignored.
!C
!C 	The cutter offset path is calculated from the cutter diameter
!C 	and the finish allowance.
!C
!C 	( offset=.5*cutter_diameter + finish_allowance)
!C
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: pocket section interiors")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Stock_name$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Width_var$[256]
DIM Name$[256]
DIM Z_var$[256]
DIM Point$[256]


Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

ActivateUndo
Current_pen=CurrentColor
Min_pickups=NamedVarVal("Minimum pickups",WsSystemVars,0.0)
V1=CreateVariable("Minimum pickups",WsSystemVars,Min_pickups)

IF ARGC>1 THEN
	Answer$= ARGV(1)
	GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:pkvmach.d/pkpocketsectionholes"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
	LowerSelectBox
	GOTO Leave
END IF
IF Answer$="New_tool" THEN
	LowerSelectBox
  	LOAD "vtools.d/readmilltool:pkvmach.d/pkpocketsectionholes"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

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

Mill_direction:!
Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="C") THEN
	Mill_dir=WsConventional
	Pen_color=WsConvCutPen
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
	Dcomp_dir= WsLeft
END IF
Stat=SetCurrentColor(Pen_color)

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)

Width_cut:!
Width_cut= 0
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Width_factor=GetVariableVal(Width_val)
	Width_cut=Cutter_dia*Width_factor
END IF
IF NOT Width_cut THEN Width_cut=NamedVarVal("width of cut",WsSystemVars,0.0)

Stat=SetEqualsValue(Width_cut)
Stat=SetInputFormat("n4=","")
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Width_cut=ABS(Width_cut)
Vwidth=CreateVariable("width of cut",WsSystemVars,Width_cut)
No_width_var= LastVariableInput(Width_var$)

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


Pick_loop:!
Stock_body=WPKPickEntity("PICK BODY",WsPKPickAll)
IF (ExitScript(Destination$)) THEN Leave
Stat= EntityNameCheck(Stock_body,Stock_name$)


Ask_value:!
Pick_return= CurrPickReturn
Stat=SetPickReturn(WsGeometryZ)
Stat=SetInputFormat("n4a1=",",P,N,")
Stat=SetEqualsValue(0.0)
Zvalue=GetValue("Z VALUE  Pick New ",Answer$)
Stat=SetPickReturn(Pick_return)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Start
IF Answer$= "P" THEN
Pick_point:Stat=SetInputFormat("n4=","")
        Pc=XyzInput("PICK",WsXyzInput)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pc) THEN Pick_point
        E1= LastPickEnt
        Pick_para= LastPickPara
        Pick_return= CurrPickReturn
        IF NOT ValidDependency(E1) THEN
                 E1= CopyEntity(Pc)
                 Stat= EntityNameCheck(E1,Point$)
                 Pc_created= WsTrue
        ELSE
                 Stat= EntityNameCheck(E1,Point$)
                 Pc_created= WsFalse
        END IF
        Stat=SetLastPoint(Pc)
        Zvalue= ZPointValue(Pc)
        Stat= DeleteEntity(Pc)
        Picked_point=1
ELSE
        No_z_var= LastVariableInput(Z_var$)
        Picked_point=0
        Pc_created= WsFalse
END IF


Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POCKET: section interiors")
Stat= SetRecDelLastExec
IF Picked_point THEN
        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
        V11= FreeParameter("Z Value",ZPointValue(Pc))
        Stat= DeleteEntity(Pc)
ELSE
        IF No_z_var THEN
                V11= FreeParameter("Z Value",Zvalue)
        ELSE
                V11= FreeParameter("Z Value",Z_var$)
        END IF
END IF
IF No_finish_var THEN
	V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
	V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_var THEN
	V2= FreeParameter("Width cut",Width_cut)
ELSE
	V2= FreeParameter("Width cut",Width_var$)
END IF
V5= FreeParameter("Minimum pickups",Min_pickups)
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
        Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
        Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
PlotOff
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
V0= FreeParameter("Bottom allowance",0)
V3= FreeParameter("Stock above section",0.0)
V4= FreeParameter("Depth cut",0.0)
Stat= MillDefZInfo(Mdef1,1,V3,V4,V0)
G1= WPKSectionContours(Stock_name$,V11)
RecordFunctionOff
IF ValidGeometry(G1) THEN
RecordFunctionOn

   Pm=PointXYZ(0.0,0.0,VariableValue(V3))
   T1=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
   Stat=TransGroupTrans(G1,T1,1,1)

   G2= CreateBoundarys(G1,WsTrue)
   Paths=PocketMillBoundary(G2,Mdef1,WsCircleMoves,1,0,V5)
   Stat= AutoNameGroup(Paths,Name$)
   Stat= UnAssocMachDef(Paths,Mdef1)
   Stat= DeleteGroup(G1,WsTrue)
   Stat= DeleteGroup(G2,WsTrue)
   V8= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
   Stat= SetDCompDirection(Paths,V8)
RecordFunctionOff
END IF
RecordFunctionOn
Stat= DeleteEntityList(Pm,Mdef1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= PlotEntity(Paths)
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("pocket section interiors")
	IF Pc_created THEN Stat= DeleteEntity(Point$)
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Ask_value

Leave:!
PlotOn
LowerSelectBox
Stat=SetPickMask(WsPickGeometry)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

