
! script name: 3dintersect.d/flowcircle
! 3dtopic: make circle on surface at a point.

!A
!A NAME:
!A
!A 	SURFACE FLOW CIRCLE
!A
!A DESCRIPTION:
!A
!A 	This script makes a circle on a surface at a point along a flow curve.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE:
!B
!B 	PICK POINT ON SURFACE:
!B
!C OPTIONS:
!C
!C	Circle_type= 1 : Circle at point
!C	Circle_type= 2 : Circle from edge to edge.
!C	Circle_type= 3 : Fillet circle
!C
!C	The circle constructed for method 1 is the circle that is
!C	close to matching the surface at a point. It may not 
!C	agree with the circles created by the other methods.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: surface flow circle ")

DIM Destination$[256]
DIM Answer$[256]

ActivateUndo
Circle_type= 1
IF ARGC> 1 THEN
	Circle_type= VAL(ARGV(1))
END IF

Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4


Start:!
Intersect2:StdWorkplane

Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Surface) THEN Intersect2

Pick_point:Stat= SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Ploop:P1=XyzInput("INPUT POINT ON SURFACE",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(P1,WsXyzInput)
UVPoint= PointOnSurface(P1,Surface,Tol,WsReturnUVPoint)
IF NOT ValidGeometry(UVpoint) THEN Pick_point

U= XPointValue(UVPoint)
V= YPointValue(UVPoint)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(UVPoint)

IF Circle_type= 3 THEN
       	UorV=1
ELSE
        Stat=SetPickMask(WsPickGeometry)
        GOSUB UorV_direction
END IF

Cent= SurfFlowCircle(Surface,U,V,Uorv,Circle_type)

ActivateUndo
Stat= AdvanceUndoNumber("flow circle")
Stat= AddItemToUndo(WsUBCreateMode,Cent)

GOTO Pick_point

ShowUVDirection:!
        U_line= SurfFlowLine(Surface,U,V,Normal_len,1)
        Pu=ParameterPt(U_line,1.0)
        V_line= SurfFlowLine(Surface,U,V,Normal_len,0)
        Pv=ParameterPt(V_line,1.0)

	Stat= SetForceVisible(U_line,WsTrue)
        Stat= SetForceVisible(Pu,WsTrue)
        Stat= SetForceVisible(V_line,WsTrue)
        Stat= SetForceVisible(Pv,WsTrue)

        Stat=SetColor(U_line,Forward_color)
        Stat=SetColor(Pu,Forward_color)
        Stat=SetColor(V_line,Reverse_color)
        Stat=SetColor(Pv,Reverse_color)
RETURN

UorV_direction:!
        GOSUB ShowUVDirection
        Stat=SetInputFormat("n4","")
Pick_uorv: L1=PickSpecificEnt("PICK DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uorv
        Pt=ParameterPt(L1,1.0)
        D1=DistTwoPts(Pt,Pu)
        D2=DistTwoPts(Pt,Pv)
        Uorv=1
        IF D2<D1 THEN Uorv= 0
        Stat=DeleteEntity(U_line)
        Stat=DeleteEntity(V_line)
        Stat=DeleteEntity(Pu)
        Stat=DeleteEntity(Pv)
        Stat=DeleteEntity(Pt)
RETURN


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