
! script name: vcircles.d/radpointent
! var_topic: construct circle defined by radius, tangent element and point

!A
!A NAME:
!A
!A 	Rad: Point: Tangent	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script constructs a circle of a known radius tangent to an
!A 	element and passing though a specified point.
!A
!A~ image location - images/radpttan.gif
!A 
!B INSTRUCTIONS:
!B
!B 	CIRCLE RADIUS: Enter radius of circle to be constructed.
!B 
!B 	POINT: Pick the point that the circle will pass through.
!B
!B 	PICK TANGENT ELEMENT: Pick element that will be tangent to the circle
!B 		constructed.
!B
!B 	PICK LEFT/RIGHT OF TANGENT ELEMENT: Pick side of tangent element that
!B 		the circle should be constructed on.  NOTE: This question will
!B 		only be asked if there is a choice.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: rad: point: tangent ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tangent$[256]
DIM Point1$[256]
DIM Name$[256]
DIM Radius_var$[256]


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

ActivateUndo
Ask_radius:Stat= SetInputFormat("n4","")
Radius=InputRadius("CIRCLE RADIUS")
IF (ExitScript(Destination$)) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
	IF NOT ValidDependency(C1) THEN Ask_radius
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_var$)
END IF


Circle_loop:!
E1=PickEntity("POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(E1) THEN Circle_loop
IF NOT ValidDependency(E1) THEN Circle_loop
T1= LastPickPara
Pick_return1= CurrPickReturn
Stat= EntityNameCheck(E1,Point1$)
P1= ParameterReturnPt(E1,T1,Pick_return1)


Pick_tangent:Stat= SetInputFormat("n4","")
E2=PickEntity("PICK TANGENT ELEMENT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E2) THEN Pick_tangent
Para2=LastPickPara
Stat= EntityNameCheck(E2,Tangent$)

Dist=DistPtEntity(P1,E2)
IF (Dist>0.0001) THEN
	Sign2= 1
	Sign1=PtLeftRight(E2,P1)
	Stat= DeleteEntity(P1)
	IF Sign1=1 THEN
		Which_one=1
	ELSE
		Which_one=2
	END IF
ELSE
	P2=InputFreePoint("PICK LEFT/RIGHT OF TANGENT ELEMENT")
	IF (ExitScript(Destination$)) THEN Leave

	Sign2=1
	Sign1=PtLeftRight(E2,P2)
	IF Sign1=1 THEN
		Which_one=1
	ELSE
		Which_one=2
	END IF
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)
END IF

Stat= GenerateVarName(WsCircleEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CIRCLE: Radius point tangent")
Stat= SetRecDelLastExec
SELECT Pick_return1
CASE WsGeometryEnd
	Pt= ParameterReturnPt(Point1$,T1,WsGeometryEnd)
CASE WsGeometryMid
	Pt= ParameterReturnPt(Point1$,T1,WsGeometryMid)
CASE WsGeomCenter
	Pt= ParameterReturnPt(Point1$,T1,WsGeomCenter)
END SELECT
P1= ParameterPt(Tangent$,Para2)
V1= FreeParameter("Sign1",Sign1)
V2= FreeParameter("Which",0)
IF No_radius_var THEN
	V3= FreeParameter("Radius",Radius)
	C1=Fillet(V3,Tangent$,V1,Pt,Sign2,P1,WsUNTRIMMED,V2)
ELSE
	C1=Fillet(Radius_var$,Tangent$,V1,Pt,Sign2,P1,WsUNTRIMMED,V2)
END IF
Stat= NameEntity(C1,Name$)
Stat= DeleteEntityList(Pt,P1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(C1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("circle")
	Stat= AddItemToUndo(WsUBCreateMode,C1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat= PlotEntity(C1)
Stat= PlotEntity(Point1$)
Stat= PlotEntity(Tangent$)
GOTO Circle_loop

Leave:!
Stat=DeleteEntity(P1)
Stat=DeleteEntity(Pt)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END

