
! script name: 3dintersect.d/autoxpart
! 3dtopic: slices a part parallel to the X axis

!A
!A NAME:
!A
!A 	X SLICE PART
!A
!A DESCRIPTION:
!A
!A	This script generates slices through a part parallel to the X axis.
!A
!B INSTRUCTIONS:
!B
!B 	PART OFFSET: Offset to part.
!B
!B 	Y START: Enter the Y coordinate of the first slice.
!B
!B 	Y END: Enter the Y coordinate of the last slice.
!B
!B 	Y STEP: Enter the Y distance between slices.
!B
!B 	PICK SURFACE GROUP: Pick the surface group 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 	NUMBER OF SLICES IN ROW: Enter the number of slices in a rows.
!B
!B 	SPACING BETWEEN SLICES: Enter the distance that the slices should
!B 		be spaced in X.
!B
!B 	SPACING BETWEEN ROWS OF SLICES: Enter the distance that the slices
!B 		should be spaced in Y.
!B
!C NOTE:
!C
!C 	Slice curves are created as pointchains.
!C
!# see also - points.d/xyzinput.hlp

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

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)
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)

Y_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=SetEqualsValue(Ystart)
Ystart=GetValue("Y START",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Yend)
Yend=GetValue("Y END",Answer$)
IF ExitScript(Destination$) THEN Leave

IF Yend=Ystart THEN
	Ystep=0
ELSE
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Ystep)
Ystep=GetValue("Y STEP",Answer$)
IF ExitScript(Destination$) THEN Leave
END IF

Y_slice:StdWorkplane

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

Stat=SetPickMask(WsPickGeometry)

IF Ystart=Yend THEN
        Number_steps=0
ELSE
        Number_steps=ABS(Yend- Ystart)/ABS(Ystep)+ 0.50
END IF
Number_steps=INT(Number_steps)

Y_Group=CreateGroup("Y Depths")
FOR I=0 TO Number_steps
        IF (Number_steps> 0) THEN
                T=I/Number_steps
        ELSE
                T=0.0
        END IF
        Y=Ystart+ T*(Yend- Ystart)
        Ypt=PointXYZ(0.0,Y,0.0)
        Stat=AddToGroup(Y_group,Ypt)
NEXT I

Pt=PointXYZ(0.0,0.0,0.0)
T1=AngleTransform(Pt,90,0,0)
Direction=1
Path_group=SlicePart(Part,Y_group,Tol,Direction,Make_cpaths,Layer_sw,0,0,T1)

Stat=DeleteEntity(Pt)

IF Slice_type=1 THEN
	Stat=DeleteGroup(Y_group)
	GOTO Y_level
END IF

! 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

Grp_count=GroupCount(Path_group)

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

IF Grp_count>1 THEN
	Stat=SetInputFormat("n0","")
	Num_slices=GetValue("NUMBER OF SLICES IN ROW",Answer$)
	IF ExitScript(Destination$) THEN Leave

	Angle=0

	IF Num_slices=1 THEN
		X_space=0
	ELSE
		Stat=SetInputFormat("n4","")
		X_space=GetValue("SPACING BETWEEN SLICES",Answer$)
		IF ExitScript(Destination$) THEN Leave
	END IF

	IF Grp_count>Num_slices THEN
		Stat=SetInputFormat("n4","")
		Y_space=GetValue("SPACING BETWEEN ROWS OF SLICES",Answer$)
		IF ExitScript(Destination$) THEN Leave
	ELSE
		Y_space=0
	END IF

ELSE

	Num_slices=1

END IF

FOR Group_num=1 TO Grp_count
	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
	E1=GroupElement(Temp_group,1)
	Etype=EntityType(E1)
	SELECT Etype
	    CASE WsGroupEnt
		E2=GroupElement(E1,1)
		Parpt=ParameterPt(E2,0)
		Ystpt=YPointValue(Parpt)
	    CASE ELSE
		Parpt=ParameterPt(E1,0)
		Ystpt=YPointValue(Parpt)
	END SELECT

	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

	IF Group_num<=Num_slices THEN
		Xpos=Xpos+X_space*(Group_num-1)
	ELSE
		No_step=(Group_num/Num_slices=INT(Group_num/Num_slices))
		Row_no=INT(Group_num/Num_slices)-No_step
		Xpos=Xpos+X_space*(Group_num-(Num_slices)*Row_no-1)
		Ypos=Ypos-Y_space*(INT(Group_num/Num_Slices)-No_step)
	END IF

	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(Parpt)
	Stat=DeleteEntity(Pz)
	Stat=DeleteEntity(T2)
	Stat=DeleteEntity(T3)
	Stat=DeleteEntity(T4)
	Stat=DeleteEntity(Inv_work)
	Stat=DeleteEntity(Tshift1)
	Stat=DeleteEntity(Tshift2)

NEXT Group_num

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)
Stat=DeleteGroup(Y_group)

ResizeAll

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

! end workplane

GOTO Y_level

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 Text

	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

Text: !
	
	Ystpt=PROUND(Ystpt,-4)
	Text_pt=PointXYZ(X4,Y4,0)
	Text_label=TextArrow(WsTextLine,Text_pt,Text_pt,"SLICE: Y = "&VAL$(Ystpt))
	Stat=DeleteEntity(Text_pt)

	Text_dim=DimensionGroup(Text_label)
	Stat=CombineGroups(G3,Text_dim)
	Stat=UnLink(Text_label)
	Stat=DeleteEntity(Text_dim)

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
