
! script name: 3dcpaths.d/calcsolidpocket
! 3DTOPIC: recalculate cpaths for solid cutting

!A
!A NAME:
!A
!A 	RECALCULATE GOUGE
!A
!A DESCRIPTION:
!A
!A 	This script recalculates the cutter path after
!A 	the cutter path data has been edited.
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: recalculate cpaths")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Cpath_name$[256]
DIM Holes$[256]
DIM Contour$[256]
DIM Body_name$[256]
DIM Surface_group$[256]
DIM Type$[256]
DIM Island$[256]

DeActivateUndo
Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1

Current_pen=CurrentColor
Current_layer= CurrentLayer
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)

Circle_sw=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)

Pick_loop:!
Vname= FindNamedVar("cutter path name",WsSystemVars)

IF NOT ValidGeometry(Vname) THEN Leave

Stat= GetVariableStr(Vname,Cpath_name$)
Path_group= FindNamedGrp(Cpath_name$)

IF ValidGeometry(Path_group) THEN

	Stat=GroupName(Path_group,Cpath_name$)
	Stat=Highlight(Path_group,WsPickPen,WsDirArrowOff)
	Path= GroupElement(Path_group,1)
	IF NOT ValidGeometry(Path) THEN Leave
	Cpath3d=GetAssocEnt(Path_group,Ws3dCpath)
	IF NOT ValidGeometry(Cpath3d) THEN
		Cpath3d=GetAssocEnt(Path,Ws3dCpath)
		IF NOT ValidGeometry(Cpath3d) THEN Leave
	END IF
	Tool=GetAssocTool(Path_group)
	IF NOT ValidGeometry(Tool) THEN Tool=GetAssocTool(Path)
	Stat= MakeInvisible(Path_group)
	PlotAll

ELSE
	Path= FindNamedCPath(Cpath_name$)
	IF NOT ValidGeometry(Path) THEN Leave
	Cpath3d=GetAssocEnt(Path,Ws3dCpath)
	IF NOT ValidGeometry(Cpath3d) THEN Leave
	Tool=GetAssocTool(Path)
	Stat= MakeInvisible(Path)
	PlotAll
END IF

Path_color=GetColor(Path)
Path_layer=GetLayer(Path)
Stat= SetWorkplane(Path)

Stat= SetCurrentLayer(Path_layer)
Stat= SetCurrentColor(Path_color)

Setup_cutter:!
Stat=GetUserString(Cpath3d,"TYPE:",Type$)
Cutter_dia=GetUserValue(Cpath3d,"CUTTER DIA:")
Finish_allow=GetUserValue(Cpath3d,"FINISH ALLOWANCE:")
Taper=GetUserValue(Cpath3d,"TAPER:")
Offset=GetUserValue(Cpath3d,"OFFSET:")
Corner_rad=GetUserValue(Cpath3d,"CORNER RAD:")
Cutter_type=GetUserValue(Cpath3d,"CUTTER TYPE:")
Tol=GetUserValue(Cpath3d,"TOLERANCE:")
IF ValidGeometry(Tool) THEN
	Length=GetUserValue(Tool,"Flute length:")
END IF

Stat= Setup3dCutter(Cutter_type,Cutter_dia/2,Corner_rad,Offset,Finish_allow,Taper,Length)

Stat=GetUserString(Cpath3d,"BODY NAME:",Body_name$)
Body_ent= WPKFindbyName(Body_name$)
Extra_surfs=0
Stat=GetUserString(Cpath3d,"SURFACE GROUP:",Surface_group$)
Part_group=FindNamedGrp(Surface_group$)
IF ValidGeometry(Part_group) THEN Extra_surfs=1

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


Mill_dir=GetUserValue(Cpath3d,"MILL DIRECTION:")
Width_cut=GetUserValue(Cpath3d,"WIDTH CUT:")
Lsw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)
Lclear=NamedVarVal("3D Level Clear",Ws3DSystemVars,1.0)
Level_fill=NamedVarVal("3D Level Fill",Ws3DSystemVars,1.0)
Depth_taper=NamedVarVal("3D Depth Taper",Ws3DSystemVars,(TAN(1.0)))

Zstart=GetUserValue(Cpath3d,"Z START:")
Zend=GetUserValue(Cpath3d,"Z END:")
Zstep=GetUserValue(Cpath3d,"Z STEP:")
ZStepMethod=GetUserValue(Cpath3d,"Z STEP METHOD:")
Circle_sw=GetUserValue(Cpath3d,"CIRCLES:")
Min_pickups=GetUserValue(Cpath3d,"MINIMUM PICKUPS:")

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

SELECT Type$
CASE "POCKET SOLID"
	Cpath_group= PocketRoughPart(Part,Zgrp,Width_cut,Depth_taper,Tol,Mill_dir,Circle_sw,Lsw,Lclear,Level_fill,Min_pickups)

CASE "ZIGZAG SOLID"
	Angle=GetUserValue(Cpath3d,"CUT ANGLE:")
	Semifinish= 1
	Cpath_group= ZigzagRoughPart(Part,Zgrp,Width_cut,Angle,Depth_taper,Tol,Mill_dir,Semifinish,Circle_sw,Lsw,Lclear,Level_fill)

CASE "Z ROUGH SOLID"
	Stock=GetUserValue(Cpath3d,"STOCK:")
	Inout=GetUserValue(Cpath3d,"CUT SIDE:")
	X=GetUserValue(Cpath3d,"START POINT X:")
	Y=GetUserValue(Cpath3d,"START POINT Y:")
	Z=GetUserValue(Cpath3d,"START POINT Z:")
	
	Start_pt= PointXYZ(X,Y,Z)
	Cpath_group=Rough3DPart(Part,Zgrp,Width_cut,Stock,Depth_taper,Tol,Inout,Mill_dir,Circle_sw,Lsw,Lclear,Level_fill,Start_pt)

	stat= DeleteEntity(Start_pt)

END SELECT
	IF ValidGeometry(Cpath_group) THEN
		Stat= DeleteGroup(Path_group)
		Stat= AssocUserData(Cpath_group,Cpath3d,Ws3dCpath)
		IF ValidGeometry(Tool) THEN
			 Stat= AssociateTool(Cpath_group,Tool)
		END IF
		FOR I=1 TO GroupCount(Cpath_group)
			Cutterpath= GroupElement(Cpath_group,I)
			Stat= AssocUserData(Cutterpath,Cpath3d,Ws3dCpath)
			IF ValidGeometry(Tool) THEN
				 Stat= AssociateTool(Cutterpath,Tool)
			END IF
		NEXT I
	END IF

Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)
Stat= DeleteGroup(Zgrp,1)
IF ValidGeometry(Path_group) THEN
        Stat= MakeVisible(Path_group)
END IF
IF ValidGeometry(Path) THEN
        Stat= MakeVisible(Path)
END IF

PlotOn

Leave:!
Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)
PlotAll
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetCurrentColor(Current_pen)
Stat=SetCurrentLayer(Current_layer)
IF (NOT LEN(Destination$)) THEN
	  LOAD "3dcpaths.d/editcpaths"
ELSE
	  LOAD Destination$
END IF
END
