
! script name: vpksection.d/vpkentitytrim
! var_solids: trim a solid using an entity

!A
!A NAME:
!A
!A 	Entity Section   VARIATIONAL
!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~ 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]
DIM Element$[256]
DIM Body$[256]

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave
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
Stat= EntityNameCheck(Pkbody,Body$)


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


GOSUB Which_side
Side= -Norm_sign

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

WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("SOLID: Entity Section")
V1= FreeParameter("Which side",Which_side)
G1= WPKContourSection(Body$,Element$,V1)
Stat= WPKNameBodyGroup(G1,Body$)
Stat= WPKSetBodyParent(G1,1)
Stat= DeleteEntity(G1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("section with entity")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
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
