
! script name: circles.d/twoptstangent
! topic: construct circle tangent to element and passing through two points

!A
!A NAME:
!A
!A 	Two Points: Tangent
!A
!A DESCRIPTION:
!A
!A 	This script constructs a circle tangent to an element and passing
!A 	through two defined points.  The tangent element to be picked must
!A 	be a circle, line, or point.
!A
!A~ image location - images/twopointstangent.jpg
!B INSTRUCTIONS:
!B
!B 	INPUT FIRST POINT ABS X,Y: Enter the X,Y coordinates of the
!B 		first point, through which the circle constructed will pass.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	INPUT SECOND POINT ABS X,Y: Enter the X,Y coordinates of
!B 		the second point, through which the circle constructed
!B 		will pass.
!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


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


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

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

Point2: !
P2=XyzInput("INPUT SECOND POINT",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
T2=0

Ptdist=DistTwoPts(P1,P2)
IF Ptdist>.000098 THEN Point3
Stat=DeleteEntity(P2)
GOTO Point2

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

	C1=ThreeTangents(P1,T1,E3,T3,P2,T2,0)

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

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

stat= SetInputFormat("n4b","")
Stat=DynamicEntity(Vnum,C1,Pc,Pt,WsDynThreeTangents,"PICK TANGENT",Params,Num_params,G1)
IF (ExitScript(Destination$)) THEN
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)
	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(P1)
Stat=DeleteEntity(P2)
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(P1)
Stat=DeleteEntity(P2)
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
