
! script name: 3dfillet.d/cblendflow
! 3dtopic: bicubic blend surface between the flow curves of two surfaces

!A
!A NAME:
!A
!A 	Bicubic Flow
!A
!A DESCRIPTION:
!A	
!A 	This script will create a bicubic surface.  The surface is created
!A 	by using a flow curve on each of the existing 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 along
!B 		the edge.
!B
!B 	PICK SURFACE #1: Pick the first surface to be used for the blend.
!B 		Choose the portion of the surface to be saved after trimming.
!B
!B 	PICK SURFACE #2: Pick the other surface to be used for the blend.
!B 		Choose the portion of the surface to be saved after trimming.
!B
!B 	NOTE: The following questions are asked for each surface.
!B
!B 	BLEND: Ucurve Vcurve, Edge: Select the type of flow curve desired.
!B 		The particular type of surface determines whether a U curve
!B 		or a V curve should be used.
!B
!B 	PICK APPROXIMATE START OF BLEND: Pick the approximate start point of
!B 		the blend.  This start point will be the location of the trim
!B 		curve so accuracy in picking start Location is important.
!B 		NOTE: This question is only asked if Ucurve or Vcurve was the
!B 		selected blend.
!B
!B 	PICK EDGE: Pick the edge of the surface to be used for the blend.
!B 		NOTE: This question is only asked if Edge blend was selected.
!B
!C NOTES:
!C 
!C 	The flow curves should be picked so directions are consistent.
!C 	The starting location will be the end nearest the pick point.
!C 	The pick point determines a U,V coordinate of the pick location.
!C 	If Vcurve has been selected the u coordinate determines which flow
!C 	curve will be used and the v coordinate determines which end will be
!C 	the starting location.  If Ucurve has been selected, the V coordinate
!C 	determines which flow curve will be used and the u coordinate
!C 	determines which end will be 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, or the tolerance may be changed
!C 	permanently by selecting the Constants option in the Settings menu.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: cubic blend flow curves")

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

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

GOSUB Select_curve
IF NOT Surf_edge THEN Pick_surf1

Which1=Surf_edge
Dir1=Edge_dir
Para_value1=Para_value

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

GOSUB Select_curve
IF NOT Surf_edge THEN Pick_surf2

Which2=Surf_edge
Dir2=Edge_dir
Para_value2=Para_value

Stat=SetPickMask(WsPickGeometry)
Weight1=0.3
Weight2=0.3


ActivateUndo
Stat= AdvanceUndoNumber("cubic surface")

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

Stat= AddItemToUndo(WsUBCreateMode,Blend_surf)
SELECT Which1
CASE 5,6
	Stat= AddItemToUndo(WsUBModifyMode,Surface1)
	Stat=TrimSurfToEdge(Surface1,UVpoint1,Blend_surf,1,Tol)
END SELECT

SELECT Which2
CASE 5,6
	Stat= AddItemToUndo(WsUBModifyMode,Surface2)
	Stat=TrimSurfToEdge(Surface2,UVpoint2,Blend_surf,3,Tol)
END SELECT

Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
GOTO Intersect2

Select_curve:!
Stat=SetInputFormat("a1=",",U,V,E,")
Stat=SetEqualsValue(0,"E")
Stat=GetValue("BLEND: Ucurve Vcurve, Edge  ",Answer$)
IF ExitScript(Destination$) THEN Leave
Curve_type$=Answer$

SELECT Curve_type$
CASE "U", "V"
	Stat=SetInputFormat("n4","")
	Pt=InputFreePoint("PICK APPROXIMATE START OF BLEND")
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(Pt) THEN Select_curve

	Stat=DeleteEntity(Pt)
	IF Curve_type$="U" THEN
		GOSUB Ucurve
	ELSE
		GOSUB Vcurve
	END IF
CASE "E"
	Stat=SetInputFormat("n4","")
	Pt=InputFreePoint("PICK EDGE")
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(Pt) THEN Select_curve

	Stat=DeleteEntity(Pt)
	GOSUB Which_edge
END SELECT
RETURN

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
Para_value=0
RETURN 

Ucurve:!
Surf_edge=5
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Para_value=V
IF U>0.5 THEN Edge_dir=WsReverse
RETURN

Vcurve:!
Surf_edge=6
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Para_value=U
IF V>0.5 THEN Edge_dir=WsReverse
RETURN

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