
! script name: pksurfaces.d/pkentitytrim
! solids: trim a solid using an entity

!A
!A NAME:
!A
!A 	Entity Section
!A
!A DESCRIPTION:
!A
!A	This script uses an entity to trim a solid body. The entity is
!A	projected perpendicular to the current workplane.
!A
!A
!A~ image location - images/pkentitytrim.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK BODY: Pick the body to be trimmed. It may be a solid
!B 		or a sheet body.
!B
!B	PICK ENTITY: Pick the entity to be used.
!B
!B      PICK SIDE TO BE SAVED: Pick the side of the entity to be saved.
!B
!C NOTES:
!C
!C	The entity must either be closed or it must begin and end outside
!C	the body. The entity cannot cross itself. Note the entity could
!C	be a spline or nurb curve.
!C
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: entity section")

DIM Destination$[256]
DIM Answer$[256]

ActivateUndo
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)
Stat= GLPlotOn

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


Pick_loop: !
Stat=SetInputFormat("a1",",P,")
Stat=SetPickMask(WsPickNoSurf)
Element=PickEntity("PICK ENTITY ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Element) THEN Pick_loop

GOSUB Which_side
Side= -Norm_sign

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

WPKClearErrorFlag
Body_group= WPKContourSection(Pkbody,Element,Which_side)
Stat= DeleteEntity(Body_group)

ActivateUndo
Stat= AdvanceUndoNumber("entity trim body")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck
GOTO Pick_loop

Leave:!
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 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
