
! script name: 3dcpaths.d/calcslices
! 3DTOPIC: recalculate cpaths

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

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","Z PART SLICE"
	GOSUB Slice_part
CASE "Z SURFACE GOUGE","Z SURFACE SLICE"
	GOSUB Slice_surface
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,"SURFACE GROUP:",Contour$)
Part=FindNamedGrp(Contour$)
IF NOT ValidGeometry(Part) THEN RETURN

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"
	CpthGrp= GougeSlicePart(Part,Zgrp,Tol,Mill_dir,Make_cpath,Circle_sw,Sp)
CASE "Z PART SLICE"
   Work= CurrentWorkplane
   CpthGrp= SlicePart(Part,Zgrp,Tol,Mill_dir,Make_cpath,0,Circle_sw,Sp,Work)
END SELECT
Stat= DeleteEntity(Sp)

RETURN

Slice_surface:!
Stat=GetUserString(Cpath3d,"SURFACE:",Contour$)
Surface=FindNamedSurface(Contour$)
IF NOT ValidGeometry(Surface) THEN RETURN

SELECT Type$
CASE "Z SURFACE GOUGE"
	CpthGrp= GougeSliceSurf(Surface,Zgrp,Tol,Mill_dir,Make_cpath,Circle_sw)
CASE "Z SURFACE SLICE"
  Work= CurrentWorkplane
  CpthGrp= SliceSurface(Surface,Zgrp,Tol,Mill_dir,Make_cpath,0,Circle_sw,0,Work)
END SELECT

RETURN

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