
! script name: pksurfaces.d/trim2sheets
! solid: trim two sheet bodies

!A
!A NAME:
!A
!A 	Trim two sheet bodues.
!A
!A DESCRIPTION:
!A
!A 	This script trims two sheet bodies. And sews them together.
!A	
!A
!B INSTRUCTIONS:
!B
!B	PICK FIRST SHEET BODY: Pick the first sheet body.
!B
!B	PICK SIDE TO BE SAVED: Pick the side of the first sheet body to be
!B				saved.
!B
!B	PICK SECOND SHEET BODY: Pick the second sheet body.
!B
!B	PICK SIDE TO BE SAVED: Pick the side of the first sheet body to be
!B				saved.
!B
!B NOTES:
!B	
!B	The each sheet body must totally cover the other sheet.
!B	If one sheet only partially intersects the other sheet an error will
!B	occur.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: trim two sheets 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_sheet1:!
E1=WPKPickEntity("PICK FIRST SHEET BODY",WsPKPickFace)
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Start

Body= WPKParentBody(E1)
Body_type= WPKBodyType(Body)
IF Body_type<> WsPKSheetBody THEN Pick_sheet1

Surface= E1
UVpoint1=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint1) THEN Pick_sheet1
U=XPointValue(UVpoint1)
V=YPointValue(UVpoint1)
Stat= DeleteEntity(UVpoint1)
GOSUB Orient_normal
IF Norm_sign=-1 THEN
        Which_side1=2
ELSE
        Which_side1=1
END IF

Pick_sheet2:!
E2=WPKPickEntity("PICK SECOND SHEET BODY",WsPKPickFace)
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E2) THEN Start

Body= WPKParentBody(E2)
Body_type= WPKBodyType(Body)
IF Body_type<> WsPKSheetBody THEN Pick_sheet2

Surface= E2
UVpoint2=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_sheet2
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
PlotOff
C1= WPKCopyBody(E1)
C2= WPKCopyBody(E2)
PlotOn
G1= WPKSectionBody(E1,C2,Which_side2)
G2= WPKSectionBody(E2,C1,Which_side1)
IF NOT WPKErrorFlag THEN
	E5= WPKSewBodies(E1,E2,Gap_dist)
END IF
Stat= DeleteEntity(G1)
Stat= DeleteEntity(G2)

ActivateUndo
Stat= AdvanceUndoNumber("trim two sheets")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck

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

        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
