
! script name: vpksections.d/vpkpolygontrim
! var_solids: trim a solid using a line chain

!A
!A NAME:
!A
!A 	Polygon Section  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script trims a solid by projecting a line chain perpendicular
!A	to the workplane. The line chain is formed just like a standard
!A	geometry line chain.
!A
!A~ image location - images/pkpolygontrim.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK BODY: Pick the body to be trimmed.
!B
!B	STARTING:  Pick the points on the line chain. ESC will end the chain.
!B
!B	PICK SIDE TO BE SAVED: Pick the side of the chain to be saved.
!B
!C NOTE:
!C
!C	The line chain must either be closed or it must begin and end
!C	outside the solid body. The chain cannot cross itself. 
!C
!C
!# see also - points.d/dyninput.hlp

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

DIM Answer$[256]
DIM Destination$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Body$[256]
DIM Name$[256]
DEG

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Units= CurrentUnits
Forward_color=3
Reverse_color=4
IF Units= WsMetric THEN
	Length1= 25
ELSE
	Length1= 1
END IF

Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)

Start:!
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= EntityNameCheck(Pkbody,Body$)


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= PlotEntity(Contour)
Stat=DeleteEntity(Pstart)
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)

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

Color_rem= CurrentColor
IF Side= 1 THEN 
	Which_side=2 
ELSE 
	Which_side=1
END IF

ActivateUndo
Stat= GenerateVarName(WsGroupEnt,Name$)
WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("SOLID: Polygon Section")
C1= CreateGroup("")
RecordFunctionOff
FOR I= 1 TO GroupCount(Contour)
        E1= GroupElement(Contour,I)
	IF I=1 THEN
		P1= ParameterPt(E1,0.0)
        	Stat= EntityNameCheck(P1,Point1$)
		Stat= AdvanceUndoNumber("polygon section")
		Stat= AddItemToUndo(WsUBCreateMode,P1)
	END IF
	P2= ParameterPt(E1,1.0)
        Stat= EntityNameCheck(P2,Point2$)
	Stat= AddItemToUndo(WsUBCreateMode,P2)
        RecordFunctionOn
	L1= LineTwoPoints(Point1$,Point2$)
        Stat= AppendToGroup(C1,L1)
        RecordFunctionOff
	Point1$= Point2$
NEXT I
RecordFunctionOn
V1= FreeParameter("Which side",Which_side)
G1= WPKContourSection(Body$,C1,V1)
Stat= WPKNameBodyGroup(G1,Body$)
Stat= WPKSetBodyParent(G1,1)
Stat= DeleteEntity(G1)
Stat= AutoNameGroup(C1,Name$)
RecordFunctionOff
Stat= AddGroupToUndo(WsUBCreateMode,C1,0,WsTrue)
RecordFunctionOn
Stat= DeleteEntity(C1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

PlotOff
Stat= DeleteGroup(Contour,WsTrue)
PlotOn

Stat= WPKSolidRecordOn(Curr_record)
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck

GOTO Start


Leave:!
Stat= DeleteEntity(Contour)
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 SIDE TO BE SAVED",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
