
! script name: pksurfaces.d/trimbody
! solid: trim a body to another sheet or solid

!A
!A NAME:
!A
!A 	Trim Body
!A
!A DESCRIPTION:
!A
!A 	This script trims a body to a sheet or solid body. 
!A	The trimming body is left intact.
!A
!B INSTRUCTIONS:
!B
!B 	PICK BODY TO BE TRIMMED: Pick the first sheet body.
!B
!B 	PICK TRIMMING BODY: Pick the second body.
!B
!B	If the TRIMMING BODY is solid then 
!B
!B	SAVE:  Inside, Outside:  Save inside or outside of trimming body
!B
!B	If the TRIMMING BODY is a sheet body
!B
!B 	PICK SIDE TO BE SAVED: Pick the side of the trimming sheet
!B 		body to be saved.
!B
!B NOTES:
!B
!B 	If the trimming body is a sheet it must completely 
!B 	intersect the body.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: trim body ")

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

ActivateUndo

Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Gap_dist= Tol

Stat= GLPlotOn
Start:!

Pick_body:!
E1=WPKPickEntity("PICK BODY TO BE TRIMMED")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Start

Pick_sheet:!
E2=WPKPickEntity("PICK TRIMMING BODY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E2) THEN Start

Body= WPKParentBody(E2)
Body_type= WPKBodyType(Body)

SELECT Body_type
CASE WsPKSheetBody

	Surface= E2
	UVpoint2=LastUVSurfPick(Surface)
	IF NOT ValidGeometry(UVpoint2) THEN 
Pick_sheet: E2=WPKPickEntity("PICK FACE ON TRIMMING BODY",WsPKPickFace)
		IF (ExitScript(Destination$)) THEN Leave
		IF NOT WPKIsEntity(E2) THEN Pick_sheet
		Body1= WPKParentBody(E2)
		Same_ent= WPKAreSameEntity(Body,Body1)
		IF Same_ent= WsFalse THEN Pick_sheet
		Surface= E2
        	UVpoint2=LastUVSurfPick(Surface)
		IF NOT ValidGeometry(UVpoint2) THEN Pick_sheet
	END IF
	U=XPointValue(UVpoint2)
	V=YPointValue(UVpoint2)
	Stat= DeleteEntity(UVpoint2)
	GOSUB Orient_normal
	IF Norm_sign=-1 THEN
       		 Which_side2=2
	ELSE
       		 Which_side2=1
	END IF

	WPKClearErrorFlag
	G1= WPKTrimBody(E1,E2,Which_side2)
	Stat= DeleteEntity(G1)

	ActivateUndo
	Stat= AdvanceUndoNumber("trim body")
	Stat= AddItemToUndo(WsUBPKSessionMark)
	WPKErrorCheck
CASE WsPKSolidBody
	Stat=SetInputFormat("a1=",",I,O,")
	Stat=SetEqualsValue(0,"O")
	Stat=GetValue("SAVE:  Inside, Outside ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	SELECT Answer$
	CASE "O"
		which_side2= WsOutside
	CASE "I"
		which_side2= WsInside
	END SELECT
	WPKClearErrorFlag
        G1= WPKTrimBody(E1,E2,which_side2)
	Stat= DeleteEntity(G1)

        ActivateUndo
        Stat= AdvanceUndoNumber("trim body")
        Stat= AddItemToUndo(WsUBPKSessionMark)
        WPKErrorCheck
END SELECT

GOTO Start



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