
! script name: vpoints.d/closestbdypt
! var_topic: closest point on a boundary.

!A
!A NAME:
!A
!A 	Closest point on a boundary. VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script finds a point on a boundary which is closest to
!A	an existing point location.
!A
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR IN BOUNDARY: Pick a contour in the boundary.
!B
!B 	PICK POINT: Pick the point to determine the closest.
!B

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

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


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

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

ActivateUndo
Pick_boundary:!
Stat=SetInputFormat("n4","")
Contour=PickGroupEnt("PICK CONTOUR IN BOUNDARY")
IF (ExitScript(Destination$)) THEN Leave
Boundary1= FindBoundary(Contour)
IF (NOT ValidDependency(Boundary1)) THEN Start
Stat= EntityNameCheck(Boundary1,Boundary$)
Stat= SetSelectedFlag(Boundary1,WsTrue)
Stat= PlotEntity(Boundary1)

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 boundary")
Stat= SetRecDelLastExec
Stat= NullReference(Boundary$)
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=ClosestPtOnBdy(Boundary$,Pc)
Stat= NameEntity(Pt,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(Point$)
Stat= PlotEntity(Boundary$)
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:!
Stat= SetSelectedFlag(Boundary1,WsFalse)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

