
! script name: pksoliddraft.d/offsetsectionview
! solids: creates an offset section view of a solid body

!A
!A NAME:
!A
!A 	Offset Section View
!A
!A DESCRIPTION:
!A
!A	This script generates a section view where the cutting plane consists
!A	of a series of straight line segments. The view is created by 
!A	projecting perpindicular to the first line segment of the 
!A	cutting plane curve.
!A
!A~ image location - images/pkoffsetsectview.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	A comparison of the results of this script with a drafting textbook 
!C	will show that this script will yield the proper views for what is
!C	commonly called an offset section view. In an offset section 
!C	the views of the areas of interest are all parallel. In an aligned
!C	section where part of the cutting plane follows an angular feature
!C	but the rest of the cutting plane is straight, proper drafting
!C	convention requires a view that can only be generated by the judgement
!C	of the draftsman. In this case the results are not to convention and
!C	manual drawing will be required to produce the proper view.
!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: offset section view ")

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

ActivateUndo
Units= CurrentUnits
Forward_color=3
Reverse_color=4
IF Units= WsMetric THEN
	Hwidth= 6
	Length1= 25
ELSE
	Hwidth= 0.25
	Length1= 1
END IF
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:!
Color_rem= CurrentColor
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


Line_chain:!
Contour= CreateGroup("LINE CHAIN",WsContourGroup)
Pick_first:!
Stat=SetInputFormat("n4b"," ")
Ps=XyzInput("STARTING",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(Ps)
Pstart= ParameterPt(Ps,0.0)

Line_loop:!

PlotOff
Pe= PtIncrXyz(Ps,Length1,0.0,0.0)
L1= LineTwoPoints(Ps,Pe)
Stat= MakeInvisible(Ps)
Stat= MakeInvisible(Pe)
Stat= MakeInvisible(L1)
PlotOn

Vnum= LastPickView
Stat=DynamicEntity(Vnum,L1,Ps,Pe,WsDynamicEnd,"PICK END POINT")
IF (ExitScript(Destination$)) THEN 
	Stat=DeleteEntity(L1,WsTrue)
	PlotOff
	Stat=DeleteEntity(Pe)
	Stat=DeleteEntity(Ps)
	PlotOn
        IF Destination$= "BACKUP" THEN 
		N1=GroupCount(Contour)
		IF N1=0  THEN Pick_first
		L1= GroupElement(Contour,N1)
		PlotOff
		Ps= ParameterPt(L1,0.0)
		Stat= MakeInvisible(Ps)
		PlotOn
		Stat= RemoveFromGrp(Contour,N1)
		Stat= DeleteEntity(L1)
		Stat= PlotEntity(Contour)
		GOTO Line_loop
	END IF
	IF NOT GroupCount(Contour) THEN
		Stat= DeleteEntity(Contour)
		GOTO Leave
	END IF
	GOTO Make_section
END IF
D1= LineLength(L1)
IF D1< 0.001 THEN
	Stat= DeleteEntity(L1)
	IF NOT GroupCount(Contour) THEN
                Stat= DeleteEntity(Contour)
                GOTO Leave
        END IF
        GOTO Make_section
END IF
Stat= MakeVisible(L1)
Stat= PlotEntity(L1)
Stat= AddToGroup(Contour,L1)

PlotOff
Stat=DeleteEntity(Pe)
Pe= ParameterPt(L1,1.0)
Stat=SetLastPoint(Pe)
Stat=DeleteEntity(Ps)
Ps=Pe
PlotOn
GOTO Line_loop

Make_section:!
Color_rem= CurrentColor
Stat= SetLineType(Contour,Cut_plane_ltype)
Stat= PlotEntity(Contour)
Stat=DeleteEntity(Pstart)
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)

Element= GroupElement(Contour,1)
GOSUB Which_side
Side= -Norm_sign

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

Color_rem= CurrentColor
IF Side= 1 THEN 
	Which_side=2 
ELSE 
	Which_side=1
END IF
PlotOff
L1= GroupElement(Contour,1)
P1= ParameterPt(L1,0.0)
P2= ParameterPt(L1,Side*1.0)
P3= PtIncrXyz(P1,0.0,0.0,1.0)
P4= ParameterPt(L1,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,(10+Label_location),Cut_plane_ltype,Side,Text$,Text_scale)

	N= GroupCount(Contour)
	L2= GroupElement(Contour,N)
	P5= ParameterPt(L2,0.0)
	P6= ParameterPt(L2,1.0)

        Dim2= CuttingPlaneLine(WsCuttingPlaneLine,P5,P6,Height,(20+Label_location),Cut_plane_ltype,Side,Text$,Text_scale)

	Stat= DeleteEntity(P5)
	Stat= DeleteEntity(P6)
END IF


WPKClearErrorFlag
Pkbody1= WPKCopyBody(Pkbody)
Body_group= WPKContourSection(Pkbody1,Contour,Which_side)
IF Make_section THEN
	Pkbody2= WPKCopyBody(Pkbody)
	Sheet_group= WPKContourSection(Pkbody2,Contour,3)
END IF
IF NOT WPKErrorFlag THEN
   Workr= CurrentWorkplane
   Trans1=ThreePtTrans(P1,P2,P3)
   Stat=SetWorkplane(Trans1)
   Group= WPKExplodeBodyView(Body_group,Trans1,Hidden_mode,Smooth_edges,Pt,0.0,0.0,View_angle)

   IF Make_section THEN
        Sections1= WPKHatchSheet(Sheet_group,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)
IF Make_section THEN
	Stat= WPKDeleteBody(Sheet_group)
	Stat= DeleteEntity(Sheet_group)
END IF
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)
IF Cut_plane_line_on THEN Stat= PlotEntity(Dim2)

ActivateUndo
Stat= AdvanceUndoNumber("offset section view")
Stat= AddGroupToUndo(WsUBCreateMode,Contour,0,WsTrue)
IF Cut_plane_line_on THEN Stat= AddDimToUndo(WsUBCreateMode,Dim1)
IF Cut_plane_line_on THEN Stat= AddDimToUndo(WsUBCreateMode,Dim2)
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddGroupToUndo(WsUBCreateMode,Group,0,WsTrue)
IF Make_section THEN
	Stat= AddGroupToUndo(WsUBCreateMode,Sections,0,WsTrue)
	Stat= DeleteEntity(Sections)
END IF
Stat= Unlink(Contour)
Stat= DeleteEntity(Contour)
Stat= DeleteEntity(Group)
WPKErrorCheck


Leave:!
Stat= SetCurrentColor(Color_rem)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)
Stat= DeleteEntity(P4)
PlotOn
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 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
