
! script name: vpoints.d/closestcendpt
! var_topic: closest element end point on a contour.

!A
!A NAME:
!A
!A 	Closest element end point on a contour. VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script finds the element end point on a contour which is closest 
!A	to an existing point location.
!A
!A
!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 used to variationally find the element endpoint
!C	which is closest to a specific location. It is useful when making
!C	variational strategies which act on arbitrary contours in which
!C	the name and number of elements is unknown.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: closest contour end 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 contour endpoint")
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=ClosestContEndPt(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

