
! script name: v3dinter.d/flowcircle
! var_topic: make circle on surface at a point.

!A
!A NAME:
!A
!A 	SURFACE FLOW CIRCLE	VARIATIONAL
!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]
DIM Surface$[256]
DIM Point$[256]
DIM Name$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

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:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Intersect2
Stat= EntityNameCheck(Surface,Surface$)

Pick_point:stat= SetInputFormat("n4","")
Stat= SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK POINT ON SURFACE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_point
Stat= EntityNameCheck(E1,Point$)
Pick_para= LastPickPara
Pick_return= CurrPickReturn
Stat= SetPickMask(WsPickAll)



IF Circle_type= 3 OR Circle_type= 4 THEN
       	UorV=1
ELSE
	P1= ParameterReturnPt(Point$,Pick_para,Pick_return)
	UVPoint= PointOnSurface(P1,Surface,Tol,WsReturnUVPoint)
	Stat= DeleteEntity(P1)
        Stat=SetPickMask(WsPickGeometry)
        GOSUB UorV_direction
	Stat= DeleteEntity(UVPoint)
END IF

Stat= GenerateVarName(WsCircle,Name$)
RecordFunctionOn
IF Circle_type= 3 THEN
	Stat= RecordDescr("CIRCLE: fillet circle")
ELSE
	Stat= RecordDescr("CIRCLE: flow circle")
END IF
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
SELECT Pick_return
CASE WsGeometryEnd
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
END SELECT
V1= FreeParameter("Uorv",Uorv)
V2= FreeParameter("Circle type",Circle_type)
V3= FreeParameter("Tolerance",Tol)
UVPoint= PointOnSurface(Pt,Surface$,V3,WsReturnUVPoint)
RecordFunctionOff
IF NOT ValidGeometry(UVPoint) THEN
        Stat= DeleteEntity(UVPoint)
        Stat= DeleteEntity(Pt)
        CloseVarRecord
        DelLastVarRecord
        GOTO Pick_point
END IF
RecordFunctionOn
C1= SurfFlowCircle(Surface$,UVPoint,V1,V2)
Stat= NameEntity(C1,Name$)
Stat= DeleteEntityList(UVPoint,Pt)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(C1) THEN
        DelLastVarRecord
ELSE
ActivateUndo
	Stat= AdvanceUndoNumber("flow circle")
	Stat= AddItemToUndo(WsUBCreateMode,C1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_point



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

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

        Stat=SetColor(U_line,Forward_color,WsTrue)
        Stat=SetColor(Pu,Forward_color,WsTrue)
        Stat=SetColor(V_line,Reverse_color,WsTrue)
        Stat=SetColor(Pv,Reverse_color,WsTrue)
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= DeleteEntity(P1)
Stat=DeleteEntity(U_line)
Stat=DeleteEntity(V_line)
Stat=DeleteEntity(Pu)
Stat=DeleteEntity(Pv)
Stat=DeleteEntity(Pt)
stat= SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
