
! script name: vlines.d/contnormalline
! var_topic: normal line at the closest point on a contour.

!A
!A NAME:
!A
!A 	Normal line at the 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 and places a normal line there.
!A
!A
!A~ image location - images/contnormal.gif
!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	For a closed contour a positive normal length will be to the
!C	exterior of the contour. A negative length will be to the 
!C	inside. For an open contour positive is to the left and negative
!C	is to the right.
!C
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: contour normal line ")

DIM Destination$[256]
DIM Answer$[256]
DIM Contour$[256]
DIM Name$[256]
DIM Point$[256]
DIM Length_var$[256]

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

Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)


ActivateUndo
Pick_loop:!
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Normal_len)
Normal_len=GetValue("NORMAL LINE LENGTH")
IF ExitScript(Destination$) THEN Leave
No_length_var= LastVariableInput(Length_var$)

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(WsLineEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LINE: normal line to a 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
IF No_length_var THEN
        V1= FreeParameter("Normal length",Normal_len)
ELSE
        V1= FreeParameter("Normal length",Length_var$)
END IF
V2= FreeParameter("Parameter",ContourParameter(Contour$,Pc))
L1= ContourParaLine(Contour$,V2,V1,WsNormal)
Stat= NameEntity(L1,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(Point$)
Stat= PlotEntity(Contour$)
Stat= PlotEntity(L1)
IF NOT ValidGeometry(L1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("normal line")
	Stat= AddItemToUndo(WsUBCreateMode,L1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_point

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


