
! script name: 3dextend.d/cedgepatch
! 3DTOPIC: patch edge curve with tangent bicubic surface

!A
!A NAME:
!A
!A 	Bicubic Edge Patch Surface: Tangent Patch
!A
!A DESCRIPTION:
!A
!A 	This script will create a new bicubic surface by extending the edge of
!A 	the surface the specified distance in both directions.  
!A	The new surface, which extends the edge of the original surface, 
!A	will be tangent to the original surface.
!A	This surface is used to "patch" a join between two surfaces when
!A	a gap large enough to cause problems in cutter paths results.
!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

stat=ChangeLabelStr("ScriptNameStatus","Function: cubic edge tangent patch")

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)
Extend_dist= Patch_width/2

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=Edge_dir

Stat=SetPickMask(WsPickGeometry)

Para_value1=0.0

Dist1=-Esign*Extend_dist
Dist2=Esign*Extend_dist

Extension_surf=EdgePatchSurface(WsTangent,WsCubicSurfEnt,Surface,Which1,Dir1,Dist1,Dist2,U_width,V_width,Tol,Para_value)

ActivateUndo
Stat= AdvanceUndoNumber("cubic 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)

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 

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