
! script name: vpksection.d/vpksectionbody
! var_solids: section a body with a sheet

!A
!A NAME:
!A
!A 	Section Body with Sheet   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script sections a body with a sheet body. This will trim the
!A 	first body to the sheet body. The first body may be a solid or
!A 	sheet body.
!A
!A~ image location - images/pksectionbody.gif
!A
!B INSTRUCTIONS:
!B
!B 	PICK BODY TO BE SECTIONED: Pick body to be sectioned.
!B
!B 	PICK SHEET BODY: Pick sheet body to use as a section.
!B
!B 	SAVE WHICH SIDE:  Front, Back, All   Which side
!B 		of the sheet do you want to save. All means
!B 		that there will be two bodies.
!B
!B NOTES:
!B
!B 	The sheet body must totally cover the body to be sectioned.
!B 	If the sheet only partially intersects the body an error will
!B 	occur.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: Section Body with Sheet")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Body1$[256]
DIM Body2$[256]

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)

Stat= GLPlotOn
Body_loop:!

E1=WPKPickEntity("PICK BODY TO BE SECTIONED")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Body_loop
Body1= WPKParentBody(E1)
Stat= EntityNameCheck(Body1,Body1$)

Pick_sheet:!
E2=WPKPickEntity("PICK SHEET BODY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E2) THEN Body_loop
Body2= WPKParentBody(E2)
Body_type= WPKBodyType(Body2)
IF Body_type<> WsPKSheetBody THEN Pick_sheet
Stat= EntityNameCheck(Body2,Body2$)

Surface= E2
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_sheet
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
Stat= DeleteEntity(UVpoint)
GOSUB Orient_normal
IF Norm_sign=-1 THEN
        Which_side=2
ELSE
        Which_side=1
END IF


WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("SOLID: Section with sheet")
V1= FreeParameter("Which side",Which_side)
G1= WPKSectionBody(Body1$,Body2$,V1)
Stat= WPKNameBodyGroup(G1,Body1$)
Stat= WPKSetBodyParent(G1,1)
Stat= DeleteEntity(G1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("section with sheet")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck


GOTO Body_loop

Orient_normal:!
	Forward_color=3
	Reverse_color=4
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK SIDE TO BE SAVED",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        Norm_sign=0
        Pt=ParameterPt(L1,1.0)
        D1=DistTwoPts(Pt,Pforward)
        D2=DistTwoPts(Pt,Preverse)
        Norm_sign=1
        IF D2<D1 THEN Norm_sign=-1
        IF NOT Norm_sign THEN Pick_normal
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN

ShowNormalDir:!
        if(IsDirReversed(Surface)=WsFalse)THEN
                Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
                Pforward=ParameterPt(Forward_normal,1.0)
                Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
                Preverse=ParameterPt(Reverse_normal,1.0)
        ELSE
                Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
                Pforward=ParameterPt(Forward_normal,1.0)
                Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
                Preverse=ParameterPt(Reverse_normal,1.0)
        END IF

        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

Leave:!
Stat= DeleteEntity(UVpoint)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
