
! script name: interrogate.d/distptentity
! topic: distance between a point and an entity

!A
!A NAME:
!A
!A 	Distance between a point and an entity
!A
!A DESCRIPTION:
!A
!A 	This script measures the perpendicular distance between a point 
!A 	and an entity.
!A
!B INSTRUCTIONS:
!B
!B 	INPUT X,Y,Z: Input the coordinates of the point to be used as a
!B 		reference in calculating the distance between the selected
!B 		point and an entity.
!B
!B 	PICK Element, Point: Pick the element to be used in calculating
!B 		the perpendicular distance between the selected point
!B 		and the selected element.
!B
!B 		P - Select P to enter another point to be used.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: distptentity ")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM File$[256]

DEG

File$= "tempdata"

Current_pen= CurrentColor
Stat= SetCurrentColor(2)




I_loop:!
ActivateUndo

Stat=SetInputFormat("n4","")
Pt=XyzInput("INPUT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave


Stat=SetInputFormat("a1",",P,")
Element=PickEntity("PICK ELEMENT, Point ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "P" THEN
    Stat=SetInputFormat("n4","")
    Point2=XyzInput("INPUT POINT ",WsXyzInput)
    IF (ExitScript(Destination$)) THEN Leave
    Element= Point2
END IF
IF (NOT ValidGeometry(Element)) THEN I_loop

Pinter= Intersect(Element,Pt)
IF ValidGeometry(Pinter) THEN
	Dist= DistTwoPts(Pinter,Pt)
	L1= LineTwoPoints(Pinter,Pt)
ELSE
	P1= ParameterPt(Element,0.0)
	P2= ParameterPt(Element,1.0)
	D1= DistTwoPts(P1,Pt)
	D2= DistTwoPts(P1,Pt)
	IF D1< D2 THEN
		Stat= DeleteEntity(P2)
		Pinter= P1
		Dist= D1
	ELSE
		Stat= DeleteEntity(P1)
		Pinter= P2
		Dist= D2
	END IF
	Dist= DistTwoPts(Pinter,Pt)
	L1= LineTwoPoints(Pinter,Pt)
END IF

X1= XPointValue(Pt)
Y1= YPointValue(Pt)
Z1= ZPointValue(Pt)

X2= XPointValue(Pinter)
Y2= YPointValue(Pinter)
Z2= ZPointValue(Pinter)

Title$= "DISTANCE PT ENTITY"
stat= OutputString(File$,CHR$(10),WsNewFile)

OUTPUT A$ USING "(10A,8D.6D)"; "Distance = ",Dist
stat= OutputString(File$,A$&CHR$(10),WsAppend)

OUTPUT A$ USING "(5A,8D.8D,5A,8D.8D,5A,8D.8D)"; " DX = ",X2-X1," DY = ",Y2-Y1," DZ = ",Z2-Z1
stat= OutputString(File$,A$&CHR$(10),WsAppend)

stat= OutputString(File$,"Picked Pt"&CHR$(10),WsAppend)
OUTPUT A$ USING "(5A,8D.8D,5A,8D.8D,5A,8D.8D)"; " X = ",X1," Y = ",Y1," Z = ",Z1
stat= OutputString(File$,A$&CHR$(10),WsAppend)

stat= OutputString(File$,"Closest Pt"&CHR$(10),WsAppend)
OUTPUT A$ USING "(5A,8D.8D,5A,8D.8D,5A,8D.8D)"; " X = ",X2," Y = ",Y2," Z = ",Z2
stat= OutputString(File$,A$&CHR$(10),WsAppend)

stat= OutputString(File$,CHR$(10),WsAppend)
Stat=MessageFileBox(File$,Title$)


PlotOff
Stat= DeleteEntity(Pinter)
Stat= DeleteEntity(Point2)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(L1)
PlotOn

GOTO I_loop

Leave:!
Stat=SetInputFormat("n4","")
Stat= SetCurrentColor(Current_pen)
Stat= DeleteEntity(Point2)
Stat= DeleteEntity(Pinter)
Stat= DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
