
! script name: 3dintersect.d/zslicepart
! 3dtopic: slices a part parallel to the xy plane.

!A
!A NAME:
!A
!A 	Z Slice Part
!A
!A DESCRIPTION:
!A
!A 	This script generates slices through a part parallel
!A 	to the XY plane.
!A
!B INSTRUCTIONS:
!B	
!B 	PART OFFSET: Enter the offset to the part.
!B
!B 	Z START: Enter the Z coordinate of the first slice.
!B
!B 	Z END: Enter the Z coordinate of the last slice.
!B
!B 	Z STEP: Enter the distance between slices.
!B
!B 	PICK SURFACE GROUP: Pick the surface group to be sliced.
!B
!C NOTE:
!C
!C 	Slice curves are created as pointchains.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: z slice part")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]


ActivateUndo
Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
Layer_sw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)

Start:!
Make_cpaths=0
Ask_offset=1
StdWorkplane
Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Z_level: !

IF ask_offset THEN
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset)
Offset=GetValue("PART OFFSET",Answer$)
IF ExitScript(Destination$) THEN Leave
Stat=Setup3dCutter(WsNullTool,0.0,0.0,Offset,0.0,0.0)
END IF

Stat=SetInputFormat("n4=","")
Stat=SetPickReturn(WsGeometryZ)
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave
Stat=SetPickReturn(WsGeometryEnd)

Stat=SetInputFormat("n4=","")
Stat=SetPickReturn(WsGeometryZ)
Stat=SetEqualsValue(Zend)
Zend=GetValue("Z END",Answer$)
IF ExitScript(Destination$) THEN Leave
Stat=SetPickReturn(WsGeometryEnd)

IF Zend=Zstart THEN
        Zstep=0
ELSE
	Stat=SetInputFormat("n4=","")
	Stat=SetEqualsValue(Zstep)
	Zstep=GetValue("Z STEP",Answer$)
	IF ExitScript(Destination$) THEN Leave
END IF

Z_slice:StdWorkplane

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Part=PickGroupEnt("PICK SURFACE GROUP",WsSurfaceGroup)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Part) THEN Z_slice

Stat=SetPickMask(WsPickGeometry)

IF Zstart=Zend THEN
	Number_steps=0
ELSE
	Number_steps=ABS(Zend-Zstart)/ABS(Zstep)+0.50
END IF
Number_steps=INT(Number_steps)

Z_Group=CreateGroup("Z Depths")
FOR I=0 TO Number_steps
	IF Number_steps>0 THEN
		T=I/Number_steps
	ELSE
		T=0.0
	END IF
	Z=Zstart+T*(Zend-Zstart)
	Zpt=PointXYZ(0.0,0.0,Z)
        Stat=AddToGroup(Z_group,Zpt)
NEXT I

direction=1
path_group=SlicePart(Part,Z_group,Tol,direction,Make_cpaths,Layer_sw)

ActivateUndo
Stat= AdvanceUndoNumber("slice curve")
Stat= AddGroupToUndo(WsUBCreateMode,Path_group,0,WsTrue)

Stat=DeleteGroup(Z_group,1)
Stat=DeleteEntity(path_group)
GOTO Z_level

Leave:!
Stat=SetInputFormat("n4","")
Stat=SetPickReturn(WsGeometryEnd)
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
