
! script name: v3dtrim.d/trimtoedge
! var_topic: trim surface to surface edge

!A
!A NAME:
!A
!A 	Trim Surface To Surface Edge	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a surface to be trimmed to the
!A 	edge of another surface.
!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 edge of the surface
!B 		to be used as the boundary for the trim.
!B
!C NOTES:
!C
!C 	It is important that the edge of the bounding surface lay on the
!C 	surface to within the trimming or intersect tolerance.
!C 	If the edge of the 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 edge ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]
DIM Surface1$[256]
DIM Surface2$[256]
DIM Name$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
Start:!

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

Pick_surf2:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface2=PickEntity("PICK EDGE OF BOUNDING SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface2) THEN Pick_surf2
Stat= EntityNameCheck(Surface2,Surface2$)
Surface=Surface2
GOSUB Which_edge
IF NOT Surf_edge THEN Pick_surf2
Which1=Surf_edge

Stat=SetPickMask(WsPickGeometry)
Uvalue=XPointValue(UVpoint1)
Vvalue=YPointValue(UVpoint1)
Stat=DeleteEntity(UVpoint1)

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

RecordFunctionOn
Stat= RecordDescr("TRIM: Surface to surface edge")
UVpoint= PointXYZ(Uvalue,Vvalue,0)
V1= FreeParameter("Tolerance",Tol)
V2= FreeParameter("Which edge",Which1)
Stat=TrimSurfToEdge(Surface1$,UVpoint,Surface2$,V2,V1)
Stat=DeleteEntity(UVpoint)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat=DeleteEntity(UVpoint1)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
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:!
Stat=DeleteEntity(UVpoint1)
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
