
! script name: vpoints.d/closestcontpt
! var_topic: closest point on a contour.

!A
!A NAME:
!A
!A 	Closest point on a contour. VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script finds a point on a contour which is closest to
!A	an existing point location.
!A
!A
!A~ image location - images/closestpoint.jpg
!B INSTRUCTIONS:
!B
!B 	PICK AN ELEMENT OF CONTOUR: Pick an element on the contour.
!B
!B 	PICK POINT: Pick the point to determine the closest.
!B
!C NOTE:
!C
!C	This script can be useful in making variational strategies
!C	to act on arbitrary contours. The closest point may be
!C	found without knowing either the number of elements in the
!C	contour or the element names.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: closest contour point ")

DIM Destination$[256]
DIM Answer$[256]


Pick_loop:!
DIM Contour$[256]
DIM Name$[256]
DIM Point$[256]

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

ActivateUndo
Pick_contour:!
Stat= SetInputFormat("n4","")
Contour=PickGroupEnt("PICK CONTOUR ")
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Pick_contour
Stat= EntityNameCheck(Contour,Contour$)

Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Pick_point:!
Stat=SetInputFormat("n4","")
E1=PickEntity("PICK POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Base_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Point$)


Stat= GenerateVarName(WsPointEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POINT: closest point on contour")
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Point$,T,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Point$,T,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Point$,T,WsGeomCenter)
END SELECT
Pt=ClosestPtOnCont(Contour$,Pc)
Stat= NameEntity(Pt,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(Point$)
Stat= PlotEntity(Contour$)
Stat= PlotEntity(Name$)
IF NOT ValidGeometry(Pt) THEN
        DelLastVarRecord
ELSE
ActivateUndo
	Stat= AdvanceUndoNumber("closest point")
	Stat= AddItemToUndo(WsUBCreateMode,Pt)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_point

Leave:!
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

