
! script name: v3dtrim.d/trimtocurves
! var_topic: trim surface to curve group

!A
!A NAME:
!A
!A 	To XYZ Curves	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a group of XYZ curves to be used as a trim boundary.
!A	The Curves are automatically linked into a connected curves internally.
!A 	Then either the part of the surface inside the resultant curves, 
!A	or the part of the surface outside the curve may be saved.  
!A	This feature applies to 3D surfaces only.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE: Pick the surface to be trimmed to the XYZ curve.
!B
!B 	PICK CONTOUR #: Pick the contours to be used as the XYZ trim curve
!B 		for the surface.  The XYZ curve must lie on the surface.
!B		If the curve deviates from the surface by more than the
!B		tolerance, the calculation time is drastically increased.
!B
!C NOTES:
!C
!C 	It is important that the XYZ curve lay on the surface to within
!C 	the trimming or intersect tolerance.
!C 	If the XYZ curve extends off the surface or deviates from the
!C 	surface, the routine may or may not be successful in trimming.
!C 	If it is successful, an excessive amount of time may be taken 
!C 	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: curve on surface ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface$[256]
DIM Contour$[256]
DIM Curve$[256]
DIM Name$[256]


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

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

ActivateUndo
Start:!
Intersect2:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface) THEN Intersect2
Stat= EntityNameCheck(Surface,Surface$)
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Intersect2


Curve_group=CreateGroup("TEMPGROUP")

Pick_loop:Stat=SetInputFormat("a1",",P,")
Stat=SetPickMask(WsPickNoSurf)
Count1=GroupCount(Curve_group)+1
Curve=PickGroupEnt("PICK CONTOUR #"&VAL$(Count1)&" , Proceed ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN Trim_surf
IF NOT ValidDependency(Curve) THEN Pick_loop
Stat=AddToGroup(Curve_group,Curve)
Stat=Highlight(Curve,WsPickPen,WsDirArrowOff)
GOTO Pick_loop

Trim_surf:Stat=SetPickMask(WsPickGeometry)
Uvalue=XPointValue(UVpoint)
Vvalue=YPointValue(UVpoint)
Stat=DeleteEntity(UVpoint)
Ncurves= GroupCount(Curve_group)
IF Ncurves< 1 THEN Leave


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

RecordFunctionOn
Stat= RecordDescr("TRIM: Surface to curves")
G1=CreateGroup("Trim curves")
RecordFunctionOff
FOR I= 1 TO Ncurves
        Curve= GroupElement(Curve_group,I)
        Stat= EntityNameCheck(Curve,Curve$)
        RecordFunctionOn
        Stat= AddToGroup(G1,Curve$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
UVpoint= PointXYZ(Uvalue,Vvalue,0)
V1= FreeParameter("Tolerance",Tol)
Stat=TrimToCurveGrp(G1,Surface$,V1,UVpoint)
Stat=DeleteEntityList(UVpoint,G1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat=DeleteEntity(Curve_group)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Intersect2

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