
! script name: pksoliddraft.d/sectionview
! solids: explode the view of a section of a body 

!A
!A NAME:
!A
!A 	 Section View
!A
!A DESCRIPTION:
!A
!A	This script creates a view from a section of a solid model.
!A
!A~ image location - images/pksectionview.gif
!A
!B INSTRUCTIONS:
!B
!B	INPUT LABEL: Input the section label eg. A B C
!B
!B 	PICK BODY: Pick body.
!B
!B	STARTING:  Start of the cutting plane line.
!B
!B	ENDING:  End of the cutting plane line.
!B
!B	PICK LEFT/RIGHT OF LINE: Pick to the right or left of the line.
!B
!B	PLACEMENT POSITION: Placement position of the view.
!B
!C NOTES:
!C
!C	The following vatiables control the different options:
!C
!C
!C	Hidden line mode: 0    all lines are shown as solid.
!C			  1    hidden lines are not shown. 
!C			  2    hidden lines are drawn in the linetype and
!C			       color determined by the opengl settings.
!C
!C	Smooth edges mode: 0 Smooth on the solid model are included in the view.
!C			   1 Smooth edges are not included.
!C
!C
!C	Section hatch on: 0    section through solid is not hatched.
!C			  1    section through solid is hatched.
!C
!C	Label location: 1	Cutting plane labels are above the arrows.
!C			2	Labels are below the cutting plane line.
!C			3       Labels are to the right and left of the ends
!C				of the cutting plane line.
!C
!C	Section hatch pattern: Pattern # of section hatching.
!C
!C	Section hatch width: Parallel spacing of hatch lines.
!C
!C	Section hatch angle: Angle of hatch lines.
!C	
!C	Section hatch color: Color of hatch pattern.
!C
!C	Exterior line type: Line type assigned to section contours.
!C
!C	Cut plane line on: 0  Cutting plane line is not drawn
!C			   1  Cutting plane line with arrows is drawn.
!C
!C	Cut plane text scale: Text scale for cutting plane line.
!C
!C	Cut plane line type: Line type assigned to cutting plane line.
!C
!C	View orientation: 1  Section view oriented with cut plane line.
!C			  2  Section view is always placed horizontal.
!C			  3  Section view is always placed vertical.
!C
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: section view ")

DIM Destination$[256]
DIM Answer$[256]
DIM Text$[256]
DIM Text1$[256]

ActivateUndo

Stat= GLPlotOn
IF Units= WsMetric THEN
        Hwidth= 6.25
	Length1= 25
ELSE
        Hwidth= 0.25
	Length1= 1
END IF
Forward_color=3
Reverse_color=4
New_line$= "\n"
End_line$= CHR$(10)
Text_height= GetDefault(WsTextHeight)
Text_width= GetDefault(WsTextWidth)
Arrow_height= GetDefault(WsArrowHeight)
Arrow_width= GetDefault(WsArrowWidth)
Hidden_mode= 2
Smooth_edges=WsTrue
Make_section= WsTrue
Hatch_pattern= 1.0
HAngle= 60
Hatch_pen= 19.0
Text_scale= 3.0
Label_location=1
Cut_plane_line_on=1
Ext_ltype=0
View_angle= 0.0
View_orientation=1
Cut_plane_ltype=8

Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)
Hidden_mode=NamedVarVal("Hidden line mode",WsPKSectionVars,Hidden_mode)
Smooth_edges=NamedVarVal("Smooth edges mode",WsPKSectionVars,Smooth_edges)
Label_location=NamedVarVal("Label location",WsPKSectionVars,Label_location)
Make_section=NamedVarVal("Section hatch on",WsPKSectionVars,Make_section)
Hatch_pattern=NamedVarVal("Section hatch pattern",WsPKSectionVars,Hatch_pattern)
Hwidth=NamedVarVal("Section hatch width",WsPKSectionVars,Hwidth)
Angle=NamedVarVal("Section hatch angle",WsPKSectionVars,Angle)
Hatch_pen=NamedVarVal("Section hatch color",WsPKSectionVars,Hatch_pen)
Ext_ltype=NamedVarVal("Exterior line type",WsPKSectionVars,Ext_ltype)
Text_scale=NamedVarVal("Cut plane text scale",WsPKSectionVars,Text_scale)
Cut_plane_line_on=NamedVarVal("Cut plane line on",WsPKSectionVars,Cut_plane_line_on)
Cut_plane_ltype=NamedVarVal("Cut plane line type",WsPKSectionVars,Cut_plane_ltype)
View_orientation=NamedVarVal("View orientation",WsPKSectionVars,View_orientation)


Start:!
stat= SetInputFormat("a99","")
stat= GetValue("INPUT LABEL  A B C D E F G H , None ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "N", "n", "None","NONE"
        Text$=""
CASE ELSE
        Stat= BackSlashChars(Answer$,Text1$)
        Stat= ReplaceString(Text1$,New_line$,End_line$,Text$)
END SELECT

Body_loop:!
Pkbody= WPKCurrentBody
IF NOT WPKIsEntity(Pkbody) THEN
        Stat=SetInputFormat("n4","")
        E1=WPKPickEntity("PICK BODY")
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT WPKIsEntity(E1) THEN Body_loop
        Pkbody= WPKParentBody(E1)
END IF

stat= SetInputFormat("n4b","")
Line_loop:Ps=XyzInput("STARTING",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ps) THEN Line_loop

Stat=SetLastPoint(Ps)

IF NOT Graphical_mode THEN
	stat= SetInputFormat("n4b","")
	Pe=XyzInput("ENDING",WsXyzInput)
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidGeometry(Pe) THEN Line_loop

	Stat=SetLastPoint(Ps)
	Section_line= LineTwoPoints(Ps,Pe)
	Stat=DeleteEntity(Ps)
	Stat=DeleteEntity(Pe)
ELSE
	PlotOff
	Pe= PtIncrXyz(Ps,Length1,0.0,0.0)
	Section_line= LineTwoPoints(Ps,Pe)
	Stat= MakeInvisible(Section_line)
	Stat= MakeInvisible(Pe)
	PlotOn

        Vnum= LastPickView
	stat= SetInputFormat("n4b","")
	Stat=DynamicEntity(Vnum,Section_line,Ps,Pe,WsDynamicEnd,"PICK LINE ENDPOINT")
	IF (ExitScript(Destination$)) THEN
       		 Stat=DeleteEntity(Section_line,WsTrue)
       		 GOTO Leave
	END IF
	Stat=DeleteEntity(Ps)
	PlotOff
	Stat=DeleteEntity(Pe)
	Pe= ParameterPt(Section_line,1.0)
	Stat=SetLastPoint(Pe)
	Stat=DeleteEntity(Pe)
	PlotOn
	Stat= MakeVisible(Section_line)
	Stat= PlotEntity(Section_line)
END IF
IF NOT ValidGeometry(Section_line) THEN Leave
Stat=SetLineType(Section_line,6)

Element= Section_line
GOSUB Which_side
Side= -Norm_sign

Ploop:Pt=XyzInput("PLACEMENT POSITION",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(Pt,WsXyzInput)

PlotOff
P1= ParameterPt(Section_line,0.0)
P2= ParameterPt(Section_line,Side*1.0)
P3= PtIncrXyz(P1,0.0,0.0,1.0)
P4= ParameterPt(Section_line,1.0)

SELECT View_orientation
CASE 0,1	! view orients with cut plane angle
	L2= LineTwoPoints(P1,P2)
	View_angle= LineAngle(L2)
	Stat= DeleteEntity(L2)
CASE 2	! view horizontal
	View_angle= 0
	X1= XPointValue(P1)
	X2= XPointValue(P2)
	IF X1> X2 THEN View_angle= 180
CASE 3	! view vertical
	View_angle= 90
        Y1= YPointValue(P1)
        Y2= YPointValue(P2)
        IF Y1> Y2 THEN View_angle= -90
CASE ELSE  ! view orients with cut plane angle
        L2= LineTwoPoints(P1,P2)
        View_angle= LineAngle(L2)
        Stat= DeleteEntity(L2)
END SELECT



IF Cut_plane_line_on THEN
	Height= Text_scale*Text_height
	Dim1= CuttingPlaneLine(WsCuttingPlaneLine,P1,P4,Height,Label_location,Cut_plane_ltype,Side,Text$,Text_scale)
END IF
Which_side=2

Color_rem= CurrentColor
Pkbody1= WPKCopyBody(Pkbody)
Workr= CurrentWorkplane
Trans1=ThreePtTrans(P1,P2,P3)
Stat=SetWorkplane(Trans1)
WPKClearErrorFlag
Body_group= WPKPlnSectionBody(Pkbody1,P1,Which_side)
IF NOT WPKErrorFlag THEN
   Group= WPKExplodeBodyView(Body_group,Trans1,Hidden_mode,Smooth_edges,Pt,0.0,0.0,View_angle)
   IF Make_section THEN
	Sections1= WPKHatchSection(Pkbody,Hatch_pattern,0.0,Hwidth,Hangle,Hatch_pen,WsTrue,Ext_ltype)
   	IF ValidGeometry(Sections1) THEN
      		Stat=SetWorkplane(Workr)
      		Trans2= InverseTrans(Trans1)
      		Trans3=AngleTransform(Pt,0.0,0.0,View_angle,WsWorkplaneVals)
      		Trans4= MultTransforms(Trans3,Trans2)
		Sections= TransformGroup(Sections1,Trans4,"HATCHING")
		Stat= DeleteGroup(Sections1,WsTrue)
		Stat= DeleteEntity(Trans2)
		Stat= DeleteEntity(Trans3)
   	END IF
   END IF
END IF
Stat= WPKDeleteBody(Body_group)
Stat= DeleteEntity(Body_group)
Stat= DeleteEntity(Section_line)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)
Stat= DeleteEntity(P4)
Stat=SetWorkplane(Workr)
PlotOn
Stat= SetCurrentColor(Color_rem)
Stat= PlotEntity(Group)
Stat= PlotEntity(Sections)
IF Cut_plane_line_on THEN Stat= PlotEntity(Dim1)

ActivateUndo
Stat= AdvanceUndoNumber("section body")
IF Cut_plane_line_on THEN Stat= AddDimToUndo(WsUBCreateMode,Dim1)
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddGroupToUndo(WsUBCreateMode,Group,0,WsTrue)
Stat= AddGroupToUndo(WsUBCreateMode,Sections,0,WsTrue)
Stat= DeleteEntity(Group)
Stat= DeleteEntity(Sections)
WPKErrorCheck
GOTO Start


Leave:!
Stat= DeleteEntity(Section_line)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)
Stat= DeleteEntity(P4)
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF


ShowNormalDir:!
        Forward_normal= ParameterLine(Element,Para_value,Normal_len,WsNormal)
        Pforward=ParameterPt(Forward_normal,1.0)

        Reverse_normal= ParameterLine(Element,Para_value,-Normal_len,WsNormal)
        Preverse=ParameterPt(Reverse_normal,1.0)

        Stat= SetForceVisible(Forward_normal,WsTrue)
        Stat= SetForceVisible(Pforward,WsTrue)
        Stat= SetForceVisible(Reverse_normal,WsTrue)
        Stat= SetForceVisible(Preverse,WsTrue)

        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN

Which_side:!
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        T1= LastPickPara
        Norm_sign=0
        Pc=ParameterPt(Forward_normal,0.0)
        Pt=ParameterPt(L1,T1)
        D1=DistTwoPts(Pt,Pforward)
        D2=DistTwoPts(Pt,Preverse)
        IF D1<= D2 THEN
                Norm_sign=-1
                IF IsDirReversed(Element) THEN Norm_sign= 1
        ELSE
                Norm_sign=1
                IF IsDirReversed(Element) THEN Norm_sign= -1
        END IF
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN

END
