
! script name: 3dintersect.d/autoplane
! 3dtopic: slices a surface by a plane

!A
!A NAME:
!A
!A 	PLANE SLICE A SURFACE
!A
!A DESCRIPTION:
!A
!A 	This script generates a slice through a surface by a plane.
!A
!B INSTRUCTIONS:
!B	
!B 	SURFACE OFFSET: Offset to surface.
!B
!B 	PICK SLICING PLANE: Pick the plane to be used to slice the surface.
!B
!B 	PICK SURFACE: Pick the surface to be sliced.
!B
!B 	POSITION OF SLICE ABS X,Y,Z: Enter the X,Y,Z coordinates of the start
!B 		position of the first slice.  NOTE: See XYZ INPUT OPTIONS.
!B
!B 	The following two questions will not be asked if the "rectangle
!B 		switch" is set to "0".  The switch may be set by selecting
!B 		the "Settings" menu, and then selecting the "Variables"
!B 		option.
!B
!B 	        LENGTH OF RECTANGLE: Enter the length of the rectangle to be
!B 		constructed around the slice.
!B
!B 	        WIDTH OF RECTANGLE: Enter the width of the rectangle to be
!B 		constructed around the slice.
!B
!C NOTE:
!C
!C 	Slice curves are created as pointchains.
!C
!# see also - points.d/xyzinput.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: plane slice surface")

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

DeActivateUndo
Active_workplane=0
Current_work=CurrentWorkplane
IF ValidGeometry(Current_work) THEN Active_workplane=1
Current_color=CurrentColor
Current_layer=CurrentLayer

Slice_type=NamedVarVal("slice type",WsSystemVars,0.0)
IF Slice_type=0 THEN
	Vslice_type=CreateVariable("slice type",WsSystemVars,3.0)
	Slice_type=3
END IF
Ref_point=NamedVarVal("reference point",WsSystemVars,-1.0)
IF Ref_point<0 THEN
	Vref_pt=CreateVariable("reference point",WsSystemVars,1.0)
	Ref_point=1
END IF
Rectangle_sw=NamedVarVal("rectangle switch",WsSystemVars,-1.0)
IF Rectangle_sw<0 THEN
	Vrectangle_sw=CreateVariable("rectangle switch",WsSystemVars,1.0)
	Rectangle_sw=1
END IF
Slice_layer=NamedVarVal("slice layer",WsSystemVars,-1.0)
IF Slice_layer<0 THEN
	Vslice_layer=CreateVariable("slice layer",WsSystemVars,20000.0)
	Slice_layer=20000
END IF
Slice_color=NamedVarVal("slice color",WsSystemVars,-1.0)
IF Slice_color<0 THEN
	Vslice_color=CreateVariable("slice color",WsSystemVars,7.0)
	Slice_color=7
END IF

! Default variables for drawing rectangle around slices
Border=NamedVarVal("border size",WsSystemVars,-1.0)
IF Border<0 THEN
	Vborder=CreateVariable("border size",WsSystemVars,0.25)
	Border=.25
END IF
X_space_def=NamedVarVal("rectangle length",WsSystemVars,-1.0)
IF X_space_def<0 THEN
	Vx_space_def=CreateVariable("rectangle length",WsSystemVars,10.0)
	X_space_def=10
END IF
Y_space_def=NamedVarVal("rectangle width",WsSystemVars,-1.0)
IF Y_space_def<0 THEN
	Vy_space_def=CreateVariable("rectangle width",WsSystemVars,5.0)
	Y_space_def=5
END IF

Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)
Ystep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)

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

Part_offset: !

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

Pick_plane: Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Slice_plane=PickEntity("PICK SLICE PLANE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Slice_plane) THEN Pick_plane

Pick_surface:StdWorkplane
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surface

Stat=SetPickMask(WsPickGeometry)

Group=CreateGroup("temp")
Pt=SurfParameterPt(Slice_plane,0.0,0.0)
Stat=AddToGroup(Group,Pt)

T1=GetTransform(Slice_plane)
direction=1

IF ValidGeometry(Slice_plane) THEN 
	path_group=SliceSurface(Surface,Group,Tol,direction,Make_cpaths,WsFalse,0,0,T1)
ELSE
	path_group=SliceSurface(Surface,Group,Tol,direction,Make_cpaths)
END IF

Stat=DeleteGroup(Group,1)
Stat=DeleteEntity(Pt)

! start workplane

Slice_no=NamedVarVal("group slice number",WsSystemVars,0.0)
IF Slice_no=0 THEN
	Vslice_no=CreateVariable("group slice number",WsSystemVars,Slice_no)
END IF

Ask_pos: !
	Stat=SetInputFormat("n4","")
	P1=XyzInput("POSITION OF SLICE",WsXyInput)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(P1) THEN Ask_pos

	IF Rectangle_sw THEN
		Stat=SetInputFormat("n4=","")
		X_space=GetValue("LENGTH OF RECTANGLE",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Y_space=GetValue("WIDTH OF RECTANGLE",Answer$)
		IF ExitScript(Destination$) THEN Leave
	END IF

	Stat=SetCurrentColor(Slice_color)
	Stat=SetCurrentLayer(Slice_layer)
	Slice_no=Slice_no+1
	Temp_group=GroupElement(Path_group,1)
	IF NOT ValidGeometry(Temp_group) THEN Y_level

	StdWorkplane

	Origin_plane=GetTransform(Temp_group)
	Pz=PointXYZ(0,0,0)
	IF ValidGeometry(Origin_plane) THEN
		Delete_trans1=0
	ELSE
		Origin_plane=AngleTransform(Pz,0,0,0)
		Delete_trans1=1
	END IF

	Xpos=XPointValue(P1)
	Ypos=YPointValue(P1)
	Zpos=0

	Pm=PointXYZ(Xpos,Ypos,Zpos)
	GOSUB Referencept

	Dest_plane=AngleTransform(Pz,0,0,0)
	Delete_trans2=1

	Stat=SetWorkplane(Origin_plane)

	StdWorkplane
	Tshift1=AngleTransform(Pz,0,0,0)

	Inv_work=InverseTrans(Origin_plane)

	StdWorkplane
	Tshift2=AngleTransform(Pz,0,0,0)

	T2=MultTransforms(Tshift1,Inv_work)
	T3=MultTransforms(Tshift2,T2)
	T4=MultTransforms(Dest_plane,T3)
	G1=TransformGroup(Temp_group,T4,"TEMP1")
	IF Group_num=1 THEN
		Pcen=Centroid(G1)
	END IF
	M1=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
	PlotOff
	G2=CopyGroup(G1)
	PlotOn
	Stat=TransGroupTrans(G2,M1,0,1)
	G3=ProjectEntity(G2,0)
	GOSUB Draw_rectangle

	Stat=Highlight(G3,2,0)
	Stat=ColorContour(G3,Slice_color)
	Stat=AddToGroup(G3,Pr)
	Stat=LayerContour(G3,Slice_layer)
	Stat=ChangeGrpName(G3,"3d slice # "&VAL$(Slice_no))

	Stat=RemoveFromGrp(Path_group,1)
	IF Slice_type=3 THEN
		Stat=UnLink(Temp_group)
		Stat=DeleteGroup(Temp_group)
	END IF
	Stat=UnLink(G1)
	Stat=DeleteGroup(G1,0)
	Stat=DeleteGroup(G2,0)
	Stat=DeleteEntity(Pm)
	Stat=DeleteEntity(Pz)
	Stat=DeleteEntity(T2)
	Stat=DeleteEntity(T3)
	Stat=DeleteEntity(T4)
	Stat=DeleteEntity(Inv_work)
	Stat=DeleteEntity(Tshift1)
	Stat=DeleteEntity(Tshift2)

Vslice_no=FindNamedVar("group slice number",WsSystemVars)
IF ValidGeometry(Vslice_no) THEN
	Stat=PutVariableVal(Vslice_no,Slice_no)
END IF

Stat=DeleteEntity(P1)
Stat=Unlink(Path_group)
Stat=DeleteGroup(Path_group)

ResizeAll

IF Active_workplane THEN
	Stat=SetWorkplane(Current_work)
ELSE
	StdWorkplane
END IF

! end workplane

GOTO Part_offset

Referencept: IF Ref_point=0 THEN RETURN

Curr_color=CurrentColor

Stat=SetCurrentColor(3)

Pr=PointXYZ(Xpos,Ypos,Zpos)

Stat=SetCurrentColor(Curr_color)

RETURN

Draw_rectangle: !
	Stat=SetWorkplane(Dest_plane)
	Xlen=(X_space-Border/2)*(X_space<>0)+X_space_def*(X_space=0)
	Ylen=(Y_space-Border/2)*(Y_space<>0)+Y_space_def*(Y_space=0)
	IF Group_num=1 THEN
		Xc=XPointValue(Pcen)
		Yc=YPointValue(Pcen)
	END IF
	Xcen=Xc+Xpos
	Ycen=Yc+Ypos
	X1=Xcen-Xlen/2
	Y1=Ycen+Ylen/2
	X2=Xcen+Xlen/2
	Y2=Y1
	X3=X2
	Y3=Ycen-Ylen/2
	X4=X1
	Y4=Y3
	IF Rectangle_sw=0 THEN RETURN

	Tp1=Type1(1,X1,Y1,X2,Y2)
	IF NOT ValidGeometry(Tp1) THEN Leave
	Tp2=Type1(1,X2,Y2,X3,Y3)
	IF NOT ValidGeometry(Tp2) THEN Leave
	Stat=JoinTypes(Tp1,Tp2)
	Tp3=Type1(1,X3,Y3,X4,Y4)
	IF NOT ValidGeometry(Tp3) THEN Leave
	Stat=JoinTypes(Tp2,Tp3)
	Tp4=Type1(1,X4,Y4,X1,Y1)
	IF NOT ValidGeometry(Tp4) THEN Leave
	Stat=JoinTypes(Tp3,Tp4)
	Stat=CloseType(Tp4)
	Contour=TypesContour(Tp4,1)
	IF Group_num=1 THEN
		Stat=DeleteEntity(Pcen)
	END IF
	IF ValidGeometry(Contour) THEN
		Stat=UnLink(Contour)
		Stat=CombineGroups(G3,Contour)
		Stat=DeleteEntity(Contour)
	END IF
	StdWorkplane

RETURN

Leave:!
IF Active_workplane THEN
	Stat=SetWorkplane(Current_work)
ELSE
	StdWorkplane
END IF

Stat=SetCurrentColor(Current_color)
Stat=SetCurrentLayer(Current_layer)

Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF Delete_trans1 THEN
	Stat=DeleteEntity(Origin_plane)
END IF
IF Delete_trans2 THEN
	Stat=DeleteEntity(Dest_plane)
END IF
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
