
! script name: 3dfillet.d/cblendedges
! 3dtopic: bicubic blend surface between the edges of two surfaces

!A
!A NAME:
!A
!A 	Bicubic Edge
!A
!A DESCRIPTION:
!A
!A 	This script will create a bicubic surface.  The surface is created by
!A 	using the flow curves of the selected edges of the surfaces to generate
!A 	a blend surface.  The blend surface is created in such a way as to be
!A 	tangent to the original surfaces.
!A
!B INSTRUCTIONS:
!B
!B 	PATCH WIDTH ALONG EDGES: Enter the width of the patch to be used
!B 		along the edge.
!B
!B 	PICK EDGE ON SURFACE #1: Pick the first surface near the edge to be 
!B 		used in creating the Bicubic blend surface.
!B
!B 	PICK EDGE ON SURFACE #2: Pick the other surface near the edge to be 
!B 		used in creating the Bicubic blend surface.
!B
!C NOTES:
!C
!C 	The edges should be picked so directions are consistent.
!C 	The starting location will be the end nearest the pick point.  The
!C 	pick point must be a point on the edge curve and should be as close
!C 	as possible to the starting location.
!C
!C 	The "3D Fillet Tol" should be left at its default value unless problems
!C 	occur.  If a problem is encountered, then go to a smaller tolerance.
!C 	If small parts are the normal, then consider changing the default
!C 	tolerance.  The tolerance may be edited temporarily by selecting the
!C 	Variables option in the Settings menu, the tolerance may be changed
!C 	permanently by selecting the Constants option in the Settings menu.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: cubic blend surface edges")

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)

Start:!
StdWorkplane

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

Pick_surf1:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK EDGE ON SURFACE #1",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surf1
Surface1=Surface

GOSUB Which_edge
IF NOT Surf_edge THEN Pick_surf1
Which1=Surf_edge
Dir1=Edge_dir

Pick_surf2:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK EDGE ON SURFACE #2",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surf2
Surface2=Surface

GOSUB Which_edge
IF NOT Surf_edge THEN Pick_surf2
Which2=Surf_edge
Dir2=Edge_dir

Stat=SetPickMask(WsPickGeometry)

Weight1=0.3
Weight2=0.3
Para_value1=0.0
Para_value2=0.0

Blend_surf=BlendSurfaces(WsCubicSurfEnt,Surface1,Which1,Dir1,Weight1,Surface2,Which2,Dir2,Weight2,U_width,V_width,Tol,Para_value1,Para_value2)

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

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