
! script name: circles.d/pttwotangents
! topic: construct circle through point and tangent to two elements

!A
!A NAME:
!A
!A 	Point: Two Tangents
!A
!A DESCRIPTION:
!A
!A 	This script constructs a circle of unknown radius through a point
!A 	and tangent to two elements.  The tangent element to be picked must
!A 	be a circle, line, or point.
!A
!A~ image location - images/pt2tangents.jpg
!B INSTRUCTIONS:
!B
!B 	INPUT ABS X,Y: Enter the X,Y coordinates of the point, through
!B 		which the circle constructed will pass.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK SECOND TANGENT ELEMENT: Pick the second of the three
!B 		elements that will be tangent to the circle constructed.
!B
!B 	PICK THIRD TANGENT ELEMENT: Pick the third element that
!B 		will be tangent to the circle constructed.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: point: two tangents ")

DIM Destination$[256]
DIM Answer$[256]
REAL Params(0:10)


Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)

DEG
Start:!
ActivateUndo
stat= SetInputFormat("n4","")
Circle_loop:!
Pt=XyzInput("INPUT POINT",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
T1=0

E2=PickEntity("PICK FIRST TANGENT ELEMENT")
IF (ExitScript(Destination$)) THEN Leave
T2=LastPickPara

IF NOT Graphical_mode THEN 
	E3=PickEntity("PICK SECOND TANGENT ELEMENT")
	IF (ExitScript(Destination$)) THEN Leave
	T3=LastPickPara

	C1=ThreeTangents(E2,T2,E3,T3,Pt,T1,0)

	Stat=DeleteEntity(Pt)
	ActivateUndo
	Stat= AdvanceUndoNumber("circle 3 tangents")
	Stat= AddItemToUndo(WsUBCreateMode,C1)
	GOTO Circle_loop
ELSE
PlotOff
Pc= ParameterPt(E2,T2)
C1= RadiusCenter(1.0,Pc)
Stat= MakeInvisible(C1)
PlotOn

Vnum= LastPickView
Params(0)= T2
Params(1)= T1
Params(2)= T1
Params(3)= 0
G1= CreateGroup("temp")
Stat= AddToGroup(G1,E2)
Stat= AddToGroup(G1,Pt) 
Stat= AddToGroup(G1,Pt)
Num_params=4

stat= SetInputFormat("n4b","")
Stat=DynamicEntity(Vnum,C1,Pc,Pt,WsDynThreeTangents,"PICK SECOND TANGENT",Params,Num_params,G1)
IF (ExitScript(Destination$)) THEN
	Stat=DeleteEntity(Pt)
	Stat=DeleteEntity(Pc)
        Stat=DeleteEntity(C1,WsTrue)
	Stat=DeleteEntity(G1)
        IF Destination$= "BACKUP" THEN Start
        GOTO Leave
END IF
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(G1)
Stat= MakeVisible(C1)
IF IsEntityMarked(C1) THEN
	Stat= DeleteEntity(C1)
ELSE
	Stat= PlotEntity(C1)
	ActivateUndo
	Stat= AdvanceUndoNumber("circle 3 tangents")
	Stat= AddItemToUndo(WsUBCreateMode,C1)
END IF
GOTO Circle_loop
END IF


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