
! script name: 3dextend.d/ctrimcircle
! 3dtopic: extend trim curve with circular arc

!A
!A NAME:
!A
!A 	Circle Extend Trim Curve: Bicubic
!A
!A DESCRIPTION:
!A
!A 	This script will create a new bicubic surface by extending the trim
!A 	curve of the surface along a circular arc.
!A
!B INSTRUCTIONS:
!B
!B 	CIRCLE RADIUS: Enter the radius of the circles.
!B
!B 	EXTENSION ANGLE ( less than 180 ): Enter the extension angle.
!B 		Note: It 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 TRIM CURVE ON SURFACE: Pick the trim curve on surface.
!B

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

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

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=","")
Radius=InputRadius("CIRCLE RADIUS")
IF ExitScript(Destination$) THEN Leave
IF Radius=0 THEN Start
Radius=ABS(Radius)

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

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 TRIM CURVE ON SURFACE ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Intersect2

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)

Dir1=1
Radius1=Radius*Norm_sign

Extension_surf=TrimExtendSurf(WsCircleExt,WsCubicSurfEnt,Surface,Which1,Dir1,Radius1,U_width,V_width,Tol,Angle)

ActivateUndo
Stat= AdvanceUndoNumber("cubic surface")
Stat= AddItemToUndo(WsUBCreateMode,Extension_surf)
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)
	Stat=SetColor(Forward_normal,Forward_color)
	Stat=SetColor(Pforward,Forward_color)

	Reverse_normal=SurfNormalLine(Surface,U,v,Normal_len,WsReverse)
	Preverse=ParameterPt(Reverse_normal,1.0)
	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
