
! script name: 3dcpaths.d/calcsolidslices
! 3DTOPIC: recalculate cpaths for solid cuts

!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 Type$[256]
DIM Island$[256]
DIM Body_name$[256]
DIM Surface_group$[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

Stat=SetWorkplane(Path)
Path_color=GetColor(Path)
Path_layer=GetLayer(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)

Lsw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)
Lclear=NamedVarVal("3D Level Clear",Ws3DSystemVars,1.0)

Zstart=GetUserValue(Cpath3d,"Z START:")
Zend=GetUserValue(Cpath3d,"Z END:")
Zstep=GetUserValue(Cpath3d,"Z STEP:")
ZStepMethod=GetUserValue(Cpath3d,"Z STEP METHOD:")
Make_cpath=GetUserValue(Cpath3d,"MAKE CPATH:")
Mill_dir=GetUserValue(Cpath3d,"MILL DIRECTION:")
Circle_sw=GetUserValue(Cpath3d,"CIRCLES:")

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

SELECT Type$
CASE "Z PART GOUGE SOLID"
	GOSUB Slice_part
END SELECT

IF ValidGeometry(CpthGrp) THEN
	IF Make_cpath THEN
		Stat= DeleteEntity(Path)
		Stat= AssocUserData(CpthGrp,Cpath3d,Ws3dCpath)
		IF ValidGeometry(Tool) THEN Stat= AssociateTool(CpthGrp,Tool)
	ELSE
		Stat= DeleteGroup(Path_group)
		Stat= AssocUserData(CpthGrp,Cpath3d,Ws3dCpath)
		IF ValidGeometry(Tool) THEN Stat= AssociateTool(CpthGrp,Tool)
		FOR I=1 TO GroupCount(CpthGrp)
		   Cpth= GroupElement(CpthGrp,I)
			Stat= AssocUserData(Cpth,Cpath3d,Ws3dCpath)
			IF ValidGeometry(Tool) THEN
				Stat= AssociateTool(Cpth,Tool)
			END IF
		NEXT I
	END IF
END IF
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
GOTO Leave

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


X=GetUserValue(Cpath3d,"START POINT X:")
Y=GetUserValue(Cpath3d,"START POINT Y:")
Z=GetUserValue(Cpath3d,"START POINT Z:")
Sp= PointXYZ(X,Y,Z)

SELECT Type$
CASE "Z PART GOUGE SOLID"
	CpthGrp= GougeSlicePart(Part,Zgrp,Tol,Mill_dir,Make_cpath,Circle_sw,Sp)
END SELECT
Stat= DeleteEntity(Sp)
Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)

RETURN


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
