
! script name: vcontours.d/manualcontour
! var_topic: create a contour by picking elements

!A
!A NAME:
!A
!A 	Manual Contour		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	Create a contour by picking elements.
!A

Stat=ChangeLabelStr("ScriptNameStatus","Function: manual contour ")

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

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

DeActivateUndo
Start:Stat=SetInputFormat("a99=","",WsFalse)
Stat= SetEqualsValue(0,"A")
Stat=GetValue("CONTOUR NAME, Autoname ",Name$)
IF ExitScript(Destination$) THEN Leave
IF Name$= "A" OR Name$="a" OR Name$= "Autoname" OR Name$="autoname" THEN
	Auto_name=1
ELSE
	Auto_name=0
END IF

E1= FindNamedGrp(Name$)
IF ValidGeometry(E1) THEN
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("NAME IN USE,  Proceed",Answer$)
        IF ExitScript(Destination$) THEN Leave
        IF Answer$="P" THEN Start
END IF

Contour= CreateGroup("TEMP",WsContourGroup)

Pick_entity: !
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("a1",",P,B,C,")
Element=PickEntity("PICK ELEMENT, Backup, Clear, Proceed ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "P"
	GOTO Make_record
CASE "B"
	Number= GroupCount(Contour)
        Stat=Highlight(Contour,WsHighlightPen,WsDirArrowOff)
        IF Number THEN
                stat= RemoveFromGrp(Contour,Number)
        END IF
        Stat=Highlight(Contour,WsAutoseqPen,WsDirArrowOff)
	GOTO Pick_entity
CASE "C"
        Stat=Highlight(Contour,WsHighlightPen,WsDirArrowOff)
	Stat= DeleteEntity(Contour)
	Contour= CreateGroup("TEMP",WsContourGroup)
	GOTO Pick_entity
END SELECT
IF NOT ValidGeometry(Element) THEN Pick_entity
N1= ElementGrpNum(Contour,Element)
IF N1 THEN Pick_entity
Stat=AddToGroup(Contour,Element)
T=LastPickPara
N1= GroupCount(Contour)
IF N1= 1 THEN Tstart= T
Stat=Highlight(Contour,WsAutoseqPen,WsDirArrowOff)
GOTO Pick_entity

Make_record:!
IF NOT ValidGeometry(Contour) THEN Leave
IF NOT GroupCount(Contour) THEN Leave
Stat=SetPickMask(WsPickAll)
Stat=SetInputFormat("n4","")

E1= GroupElement(Contour,1)
IF Tstart<= 0.5 THEN
	Stat= SetReverseDir(E1,WsForward)
ELSE
	Stat= SetReverseDir(E1,WsReverse)
END IF
Stat= SetNoPathFlag(Contour,WsFalse)

Cont_color= CurrentColor
Cont_layer= CurrentLayer

IF Auto_name THEN
	Stat=GenerateVarName(WsGroupEnt,Name$)
END IF
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Manual select")
G1= CreateGroup(Name$,WsContourGroup)
V1= FreeParameter("Contour_color",Cont_color)
V2= FreeParameter("Contour_layer",Cont_layer)
RecordFunctionOff
Num_elements= GroupCount(Contour)
FOR I= 1 TO Num_elements
        E1= GroupElement(Contour,I)
        IF ValidGeometry(E1) THEN
                Stat= EntityNameCheck(E1,Answer$)
		IF I=1 THEN
			IF IsDirReversed(E1) THEN
                		RecordFunctionOn
				Stat= SetReverseDir(Answer$,WsReverse)
                		RecordFunctionOff
			ELSE
                		RecordFunctionOn
				Stat= SetReverseDir(Answer$,WsForward)
                		RecordFunctionOff
			END IF
		END IF
                RecordFunctionOn
                Stat= AddToContour(G1,Answer$,V1,V2)
                RecordFunctionOff
        END IF
NEXT I
RecordFunctionOn
Stat= NameEntity(G1,Name$)
Stat= SetNoPathFlag(G1,WsTrue)
RecordFunctionOff
CloseVarRecord

IF ValidGeometry(G1) THEN
        Stat=Highlight(G1,Cont_color,WsDirArrowOn)
ELSE
        DelLastVarRecord
END IF
Stat=DeleteEntity(Contour)
Stat=SetPickMask(WsPickAll)
GOTO Start

Leave:!
Stat=DeleteEntity(Contour)
Stat=SetPickMask(WsPickAll)
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END

