
! script name: v3dextend.d/ctrimnormext
! var_topic: extend trim curve with normal bicubic surface

!A
!A NAME:
!A
!A 	Normal Extend Trim Curve: Bicubic	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will create a new bicubic surface by extending the trim
!A 	curve of the surface the specified distance.  The new surface, which
!A 	extends the trim curve of the original surface, will be normal to the
!A 	original surface.
!A
!B INSTRUCTIONS:
!B
!B 	EXTENSION DISTANCE: Enter the amount of extension.
!B
!B 	PATCH WIDTH: Enter the width of the patch to be used along
!B 		the trim curve.
!B
!B 	PICK TRIM CURVE ON SURFACE: Pick the trim curve on surface.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of the surface.

Stat=ChangeLabelStr("ScriptNameStatus","Function: cubic trim curve normal extension")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]
DIM Surface$[80]
DIM Ext_var$[80]
DIM Patch_var$[80]
DIM Name$[80]

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

IF ARGC<2 THEN
        Ext_type= WsNormal
ELSE
        Ext_type= WsPlaneNormalExt
END IF

ActivateUndo
Patch_width=NamedVarVal("3D Default Patch",Ws3DSystemVars,0.25)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4

Start:!

stat= SetInputFormat("n4=","")
stat= SetEqualsValue(Extend_dist)
Extend_dist= GetValue("EXTENSION DISTANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_dist_var= LastVariableInput(Ext_var$)
IF Extend_dist=0 THEN Start



stat= SetInputFormat("n4=","")
stat= SetEqualsValue(Patch_width)
V_width= GetValue("PATCH WIDTH ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_patch_var= LastVariableInput(Patch_var$)
U_width= V_width
Patch_width= V_width

Intersect2:!
Pick_loop:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n4","")
Surface=PickEntity("PICK TRIM CURVE ON SURFACE ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_loop
Stat= EntityNameCheck(Surface,Surface$)

Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Which1=LastSurfTrimPick(Surface)
GOSUB Orient_normal

Stat=SetPickMask(WsPickGeometry)

Dist=Extend_dist*Norm_sign
Dir1=WsForward

Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("BICUBIC: Normal trim extension")
Stat= SetRecDelLastExec
Stat= Set3dInterOffset(0.0)
V1= FreeParameter("Which curve",Which1)
V2= FreeParameter("Direction",Dir1)
V3= FreeParameter("Tolerance",Tol)
IF No_dist_var THEN
        V4= FreeParameter("Distance",Extend_dist)
ELSE
        V4= FreeParameter("Distance",Ext_var$)
END IF
IF No_patch_var THEN
        V5= FreeParameter("Patch width",Patch_width)
ELSE
        V5= FreeParameter("Patch width",Patch_var$)
END IF

RecordFunctionOff
IF Ext_type= WsPlaneNormalExt THEN
RecordFunctionOn
S1=TrimExtendSurf(WsPlaneNormalExt,WsCubicSurfEnt,Surface$,V1,V2,Norm_sign*GetVariableVal(v4),V5,V5,V3)
RecordFunctionOff
ELSE
RecordFunctionOn
S1=TrimExtendSurf(WsNormal,WsCubicSurfEnt,Surface$,V1,V2,Norm_sign*GetVariableVal(v4),V5,V5,V3)
RecordFunctionOff
END IF
RecordFunctionOn
Stat= NameEntity(S1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(S1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("cubic ext surf")
	Stat= AddItemToUndo(WsUBCreateMode,S1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Intersect2

Orient_normal:!
	GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF SURFACE",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:!
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
