
! script name: 3dextend.d/nedgenormal
! 3dtopic: extend edge curve with normal NURBS surface

!A
!A NAME:
!A
!A 	Nurb Edge Curve: Normal Extension
!A
!A DESCRIPTION:
!A
!A 	This script will create a new NURBS surface normal, or perpendicular,
!A 	to the specified surface at the edge for the specified distance.
!A
!B INSTRUCTIONS:
!B
!B 	EXTENSION DISTANCE: Enter the amount of extension.
!B
!B 	PATCH WIDTH ALONG EDGES: Enter the width of the patch to be used along
!B 		the edge.
!B
!B 	PICK EDGE CURVE ON SURFACE : Pick the edge curve on surface.
!B
!C NOTE:
!C
!C 	Ext_type controls the type of normal extension.
!C
!C 		WsNormal is perpendicular to the surface at each point along
!C 			the curve.
!C 		WsPlaneNormal will use the XY normal but keep Z constant.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: nurb edge normal extension")

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

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

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


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

Intersect2:StdWorkplane

Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK EDGE CURVE ON SURFACE ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Surface) THEN Intersect2

GOSUB Which_edge
IF NOT Surf_edge THEN Intersect2
Which1= Surf_edge
Dir1= WsForward
GOSUB Orient_normal

Stat= SetPickMask(WsPickGeometry)

Para_value1= 0.0

Dist= Extend_dist*Norm_sign

Extension_surf= EdgeExtendSurf(Ext_type,WsNurbSurfEnt,Surface,Which1,Dir1,Dist,U_width,V_width,Tol,Para_value)

ActivateUndo
Stat= AdvanceUndoNumber("nurb surface")
Stat= AddItemToUndo(WsUBCreateMode,Extension_surf)
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 



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