
! script name: v3dextend.d/cedgecircle
! var_topic: extend edge curve with circular arc extension

!A
!A NAME:
!A
!A 	Circle Extend Edge: Bicubic
!A
!A DESCRIPTION:
!A
!A 	This script will create a new bicubic surface by extending the edge of
!A 	the surface along circular arcs.  The new surface, which extends the
!A 	edge of the original surface, will be tangent to the original surface.
!A
!B INSTRUCTIONS:
!B
!B 	CIRCLE RADIUS: Enter the radius.
!B
!B 	EXTENSION ANGLE ( less than 180 ): Enter the extension angle. This
!B 		must be less than 180 deg.
!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

Stat=ChangeLabelStr("ScriptNameStatus","Function: cubic edge circle extension")

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


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

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

Start:!

stat= SetInputFormat("n4=","")
Radius= InputRadius("CIRCLE RADIUS")
IF (ExitScript(Destination$)) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)
IF Radius=0 THEN Start
Radius= ABS(Radius)


Angle=InputAngle("EXTENSION ANGLE ( less than 180 )")
IF (ExitScript(Destination$)) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)


stat= SetInputFormat("n4=","")
stat= SetEqualsValue(Patch_width)
V_width= GetValue("PATCH WIDTH ALONG EDGES",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 EDGE CURVE ON SURFACE ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_loop
Stat= EntityNameCheck(Surface,Surface$)
Stat= SetPickMask(WsPickGeometry)

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

Para_value= 0.0
Dir1= WsForward
Radius1= Radius*Norm_sign


Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("BICUBIC: Edge circle extension")
Stat= SetRecDelLastExec
Stat= Set3dInterOffset(0.0)
V1= FreeParameter("Which edge",Which1)
V2= FreeParameter("Direction",Dir1)
V3= FreeParameter("Tolerance",Tol)
IF No_radius_var THEN
        V4= FreeParameter("Radius",Radius)
ELSE
        V4= FreeParameter("Radius",Radius_var$)
END IF
IF No_angle_var THEN
        V5= FreeParameter("Angle",Angle)
ELSE
        V5= FreeParameter("Angle",Angle_var$)
END IF
IF No_patch_var THEN
        V6= FreeParameter("Patch width",Patch_width)
ELSE
        V6= FreeParameter("Patch width",Patch_var$)
END IF
S1=EdgeExtendSurf(WsCircleExt,WsCubicSurfEnt,Surface$,V1,V2,Norm_sign*GetVariableVal(V4),V6,V6,V3,Para_value,V5)
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


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)

Esign= 1
SELECT Surf_edge
CASE 1,3
        Edge_dir= WsForward
        IF(U> 0.5) THEN Edge_dir= WsReverse
	IF(V< 0.5) THEN Esign= -1
CASE 2,4
        Edge_dir= WsForward
        IF(V> 0.5) THEN Edge_dir= WsReverse
	IF(U< 0.5) THEN Esign= -1
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= SetPickMask(WsPickGeometry)
stat= SetInputFormat("n4","")
stat= DeleteEntity(Forward_normal)
stat= DeleteEntity(Reverse_normal)
stat= DeleteEntity(Pt)
stat= DeleteEntity(Pforward)
stat= DeleteEntity(Preverse)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
