
! script name: 3dtrim.d/fasttrmfillet
! 3dtopic: fast fillet and trim surfaces

!A
!A NAME:
!A
!A 	Fast Fillet + Trim Surfaces
!A
!A DESCRIPTION:
!A
!A 	This script combines a fillet and trim procedure of two intersecting
!A 	surfaces using a fast algorithm.
!A
!B INSTRUCTIONS:
!B
!B 	FILLET RADIUS: Enter the radius of the fillet to be constructed.
!B
!B 	PATCH WIDTH: Enter the width of the patch to be used.  The patch width
!B 		is along the length of the fillet and the radii that will be
!B 		encountered should be used to determine the patch width.
!B
!B 	PICK SURFACE #1: Pick one of the surfaces to be used for the fillet
!B 		and trim procedure.  The pick location will determine the part
!B 		to be saved.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #1 to be trimmed.
!B 		The normal vectors will be displayed to allow the side to be
!B 		picked.
!B
!B 	PICK SURFACE #2: Pick the other surface to be used for the fillet and
!B 		trim procedure.  The pick location will determine the part to
!B 		be saved.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #2 to be trimmed.
!B 		The normal vectors will be displayed to allow the side to be
!B 		picked.
!B
!C NOTES:
!C
!C 	The fast fillet is mathematically the same as the regular fillet.
!C
!C 	The fast fillet is fast because it uses the pick locations and it
!C 	does not search as exhaustively for all possible intersections.
!C 	The extent of the search is controlled by the patch density
!C 	and the plot density of the surface.
!C
!C 	The fast fillet may fail to give the desired fillet. This method may
!C 	fail if at least one of the surfaces has many small radii through
!C 	which the fillet must propagate.
!C
!C 	If this method fails increasing the plot density on one or both
!C 	surfaces forces a more exhaustive search.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: fast trim fillet ")

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



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

Ask_fillet:stat= SetInputFormat("n4=","")
Radius= InputRadius("FILLET RADIUS")
IF (ExitScript(Destination$)) THEN Leave

IF Radius<= 0 THEN Ask_fillet

stat= SetInputFormat("n4=","")
stat= SetEqualsValue(Patch_width)
Patch_width= GetValue("PATCH WIDTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Intersect2:StdWorkplane
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

UVpoint1= LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint1) THEN Pick_surf1
U= XPointValue(UVpoint1)
V= YPointValue(UVpoint1)

GOSUB Orient_normal
Surface1= Surface
Normal_sign1= norm_sign


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

UVpoint2= LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
U= XPointValue(UVpoint2)
V= YPointValue(UVpoint2)

GOSUB Orient_normal
Surface2= Surface
Normal_sign2= norm_sign

Stat= SetPickMask(WsPickGeometry)

ActivateUndo
Stat= AdvanceUndoNumber("fillet")
Stat= AddItemToUndo(WsUBModifyMode,Surface1)
Stat= AddItemToUndo(WsUBModifyMode,Surface2)

Trim1=1
Trim2=1
Fillet_group= FastTrimFillet(Radius,Surface1,Normal_sign1,UVpoint1,Trim1,Surface2,Normal_sign2,UVpoint2,Trim2,Patch_width,Tol)

Stat= AddGroupToUndo(WsUBCreateMode,Fillet_group,0,WsFalse)
stat= DeleteEntity(fillet_group)

stat= DeleteEntity(UVpoint1)
stat= DeleteEntity(UVpoint2)

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)

        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= SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
stat= DeleteEntity(Forward_normal)
stat= DeleteEntity(Reverse_normal)
stat= DeleteEntity(Pt)
stat= DeleteEntity(Pforward)
stat= DeleteEntity(Preverse)
stat= DeleteEntity(UVpoint1)
stat= DeleteEntity(UVpoint2)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
