
! script name: 3dtrim.d/trimtoedges
! 3dtopic: trim surface to surface edges

!A
!A NAME:
!A
!A 	Trim Surface To Several Surface Edges
!A
!A DESCRIPTION:
!A
!A 	This script allows a surface to be trimmed to
!A 	the edges of other surfaces. 
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE TO BE TRIMMED: Pick the surface to be trimmed.
!B 		The pick location determines what part will be saved.
!B
!B 	PICK EDGE OF BOUNDING SURFACE #: Pick the edges of the surfaces
!B 		to be used as the boundary for the trim.
!B
!C NOTES:
!C
!C 	It is important that the edge of each bounding surface lay on the
!C 	surface to within the trimming or intersect tolerance.
!C 	If the edge of each bounding surface extends off the surface or
!C 	deviates from the surface, the routine may or may not be successful
!C 	in trimming.  If it is successful, an excessive amount of time may
!C 	be taken for calculations.
!C
!C 	The "3D Trimming Tol" should be left at its default value unless
!C 	problems occur.  If a problem is encountered, then go to a
!C 	smaller tolerance.  If small parts are the normal, then consider
!C 	changing the default tolerance.  The tolerance may be edited
!C 	temporarily by selecting the Variables option in the Settings menu.
!C 	The default tolerance may be changed permanently by selecting
!C 	the Constants option in the Settings menu.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: trim to surface edges ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]

ActivateUndo
Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
Start:!
Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Highlight_color= 4
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Intersect2:StdWorkplane
Pick_surf1:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface1=PickEntity("PICK SURFACE TO BE TRIMMED")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface1) THEN Pick_surf1
UVpoint1=LastUVSurfPick(Surface1)
IF NOT ValidGeometry(UVpoint1) THEN Pick_surf1

Curve_group=CreateGroup("TEMPGROUP")

Pick_surf2:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("a1",",P,")
Count1=GroupCount(Curve_group)+1
Surface2=PickEntity("PICK EDGE OF BOUNDING SURFACE #"&VAL$(Count1)&" , Proceed ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN Trim_surf
IF NOT ValidGeometry(Surface2) THEN Pick_surf2
Surface=Surface2
GOSUB Which_edge
IF NOT Surf_edge THEN Pick_surf2
Which1=Surf_edge
Path=EdgeCurve(surface2,Which1,tol,Edge_dir)
Stat= SetSelectedFlag(Path,WsTrue)
Stat= PlotEntity(Path)
Stat=AddToGroup(Curve_group,Path)
GOTO Pick_surf2

Trim_surf:Stat=SetPickMask(WsPickGeometry)

ActivateUndo
Stat= AdvanceUndoNumber("trim to curves")
Stat= AddItemToUndo(WsUBModifyMode,Surface1)

Stat=TrimToCurveGrp(Curve_group,Surface1,Tol,UVpoint1)
Stat=DeleteEntity(UVpoint1)
Stat=DeleteGroup(Curve_group,1)

GOTO Intersect2

Which_edge: Surf_edge=0
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Surf_edge=ClosestSurfEdge(Surface,U,V)

SELECT Surf_edge
    CASE 1,3
	Edge_dir=WsForward
	IF U>0.5 THEN Edge_dir=WsReverse
    CASE 2,4
	Edge_dir=WsForward
	IF V>0.5 THEN Edge_dir=WsReverse
END SELECT
RETURN

Leave:!
IF ValidGeometry(Curve_group) THEN 
	Stat=DeleteGroup(Curve_group,1)
END IF
Stat=DeleteEntity(UVpoint1)
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
