! script name: lines.d/dynlinepttan
! topic: construct line through point tangent to entity

!A
!A NAME:
!A
!A 	Point: Tangent
!A
!A DESCRIPTION:
!A
!A 	This script constructs a line through a point tangent to an entity.
!A
!A~ image location - images/linepttan.gif
!A
!B INSTRUCTIONS:
!B
!B 	INPUT ABS X,Y: Enter the X,Y coordinates of the point, through
!B 		which the line tangent to the entity will be constructed.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK TANGENT ELEMENT: Pick the entity that will be tangent to
!B 		the line constructed.
!B
!# see also - points.d/xyzinput.hlp


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

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

DEG

Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)
ActivateUndo
Ploop:!
stat= SetInputFormat("n4","")
P1=XyzInput("INPUT",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(P1) THEN Ploop
Param1= 0.0

IF NOT Graphical_mode THEN
E2=PickEntity("PICK TANGENT ELEMENT")
IF (ExitScript(Destination$)) THEN Leave
P2=LastPickPoint

C1=SelectTwoTan(P1,P1,E2,P2,WsTRIMMED)

Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
ActivateUndo
Stat= AdvanceUndoNumber("line pt tangent")
Stat= AddItemToUndo(WsUBCreateMode,C1)
GOTO Ploop

ELSE
Place_line:!
PlotOff
Pref= CopyEntity(P1)
Pdest= CopyEntity(Pref)
L1=PointAngle(Pref,0.0)
Stat= MakeInvisible(L1)
Stat= MakeInvisible(Pref)
Stat= MakeInvisible(Pdest)
PlotOn
Stat=SetLastPoint(Pdest)

Vnum= LastPickView
Params(0)= Param1
Params(1)= Param2
Params(2)= 0.0
Params(3)= WsTrimmed
G1= CreateGroup("temp")
Stat= AddToGroup(G1,P1)
Stat= AddToGroup(G1,E1)
Num_params=4
Stat=SetInputFormat("n4b","")
Stat=DynamicEntity(Vnum,L1,Pref,Pdest,WsDynLineTwoTan,"PICK TANGENT",Params,Num_params,G1)
IF (ExitScript(Destination$)) THEN
        Stat=DeleteEntity(L1,WsTrue)
	Stat=SetLastPoint(Pdest)
	Stat=DeleteEntity(Pref)
	Stat=DeleteEntity(Pdest)
	Stat=DeleteEntity(G1)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)
        IF Destination$= "BACKUP" THEN Ploop
        GOTO Leave
END IF
Stat= MakeVisible(L1)
Stat=SetLastPoint(Pdest)
Stat=DeleteEntity(Pref)
Stat=DeleteEntity(Pdest)
Stat=DeleteEntity(G1)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
IF IsEntityMarked(L1) THEN
	Stat= DeleteEntity(L1)
ELSE
	Stat= PlotEntity(L1)
	ActivateUndo
	Stat= AdvanceUndoNumber("line pt tangent")
	Stat= AddItemToUndo(WsUBCreateMode,L1)
END IF
GOTO Ploop

END IF


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