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

!A
!A NAME:
!A
!A 	Tangent 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 tangent line there.
!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
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: contour tangent 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

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


Pick_loop:!
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tangent_len)
Tangent_len=GetValue("TANGENT 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("Tangent length",Tangent_len)
ELSE
        V1= FreeParameter("Tangent length",Length_var$)
END IF
V2= FreeParameter("Parameter",ContourParameter(Contour$,Pc))
L1= ContourParaLine(Contour$,V2,V1,WsTangent)
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("tangent 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

