
! script name: vcontours.d/vautoseq
! var_topic: auto link, create contour that is a chain of connected elements

!A
!A NAME:
!A
!A 	Auto Link VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates a contour (a special type of group), that is a
!A 	chain of connected elements.  An Auto Link should only be done ONCE
!A 	for any group of elements.  Each time Auto Link is used the new contour
!A 	is added to the list of groups.  To check to see if something has
!A 	already had the Auto Link option used, use the HIGHLIGHT option or
!A 	check the list of named groups.
!A
!A~ image location - images/autolink1.jpg
!A~ image location - images/autolink2.jpg
!B INSTRUCTIONS:
!B
!B 	PICK START ELEMENT, Single: Pick a point on the
!B 		starting element near the desired start point.  This
!B 		pick will determine the direction of the Auto Link.
!B
!B 		S - Single allows the Auto Link to proceed one element at a
!B 		    time and should be used if there are a lot of places where
!B 		    a decision about which direction to continue.
!B
!B 	PICK NEXT, Backup, Single, New:
!B
!B 		B - Backup allows the Auto Link to be moved back one element
!B 		    at a time, starting at the ending element.  This element
!B 		    will no longer be included in the contour created by the
!B 		    Auto Link.
!B 		S - Single allows the Auto Link to proceed one element at a
!B 		    time and should be used if there are a lot of places where
!B 		    a decision about which direction to continue.
!B 		N - New allows a new group of elements to use the Auto Link
!B 		    option and creates a new contour.  The new contour is
!B 		    automatically named contour#, where # is incremented each
!B 		    time a contour is created.  NOTE: Contours may be renamed.
!B
!B 	PICK END ELEMENT: Pick the element where the
!B 		Auto Link process should stop.  If the first element is
!B 		picked twice, the Auto Link will proceed around the
!B 		contour as far as it can, back to the first element if the
!B 		contour is closed and there are no choices of a direction.
!B
!C NOTES:
!C
!C 	If Auto Link does not work, a couple of common problems are:
!C
!C 		1) Auto link has already been used on the contour and may not
!C 		   be repeated.
!C 		2) There may be double elements, which may happen with type #'s,
!C 		    and this will prevent a successful Auto Link.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: auto sequence ")

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

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

Highlight_color= WsAutoseqPen
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

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
	IF NOT LEN(Name$) THEN Auto_name=1
	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
END IF

Contour=CreateGroup("TEMP",WsContourGroup)

Smode= WsMultipleAuto

Pick_loop:!
Number= GroupCount(Contour)
IF Number  THEN
	Stat= SetEqualsValue(0,"N")
	if(smode= WsMultipleMode) THEN
		Question$= "PICK NEXT , Backup, Single, New" 
		stat= SetInputFormat("a1=",",B,S,N,")
	ELSE
		Question$= "PICK NEXT , Backup, Multiple, New" 
		stat= SetInputFormat("a1=",",B,M,N,")
	END IF
ELSE
	if(smode= WsMultipleAuto) THEN
		stat= SetInputFormat("a1=",",S,")
		stat= SetEqualsValue(0,"S")
		Question$= "PICK START ELEMENT, Single" 
	ELSE
		stat= SetInputFormat("a1=",",M,")
		stat= SetEqualsValue(0,"M")
		Question$= "PICK START ELEMENT, Multiple" 
	END IF
END IF
	
Estart=PickEntity(Question$,Answer$)
IF (ExitScript(Destination$)) THEN Leave_record

SELECT Answer$
CASE "N"
	Destination$= "contours.d/autosequence"
	GOTO Make_record
CASE "S","s"
	Smode= WsSingleAuto
	goto pick_loop
CASE "M","m"
	Smode= WsMultipleAuto
	goto pick_loop
CASE "B","b"  ! Backup one element
	Number= GroupCount(Contour)
	IF Number THEN
		Temp_e1=GroupElement(Contour,Number)
		stat= RemoveFromGrp(Contour,Number)
		Stat= SetSelectedFlag(Temp_e1,WsFalse)
        	Stat=PlotEntity(Temp_e1)
	END IF
	Stat= SetSelectedFlag(Contour,WsTrue)
	Stat=Highlight(Contour,WsAutoseqPen,WsDirArrowOn)
	Stat= SetHighlightGroup(Contour)
	GOTO Pick_loop
END SELECT

IF NOT ValidGeometry(Estart) THEN Pick_loop
IF NOT IsEntVariational(Estart) THEN
	Stat= SetInputFormat("a1=",",Y,N,")
	Stat= GetValue("NON-VDM ENTITY. RUN NON-VDM AUTO-LINK? Yes or No ",Answer$)
	IF Answer$= "Y" THEN
		Destination$= "contours.d/autosequence"
		GOTO Leave_record
	ELSE
		GOTO Pick_loop
	END IF 
END IF

T=LastPickPara
IF T<=0.5 THEN
	Direction=WsForward
ELSE
	Direction=WsReverse
END IF

IF Smode= WsMultipleAuto THEN
New_end_ent:!
	Eend=PickEntity("PICK END ELEMENT ")
	IF (ExitScript(Destination$)) THEN Leave_record
	IF NOT IsEntVariational(Eend) THEN
		Stat= SetInputFormat("a1=",",C,")
		Stat= GetValue("ELEMENT MUST BE A NON-VDM ENTITY. Continue ",Answer$)
		GOTO New_end_ent
	END IF 
ELSE 
	Eend= Estart
END IF

Number= GroupCount(Contour)
Econnect= GroupElement(Contour,number)

Group=AutoSequence("TempContour",Estart,Eend,Econnect,Smode,Direction,2,WsAutoseqPen)

IF (ValidGeometry(Group)) THEN
		Stat=CombineGroups(Contour,Group)
		Stat=DeleteEntity(Group)
		Stat= SetSelectedFlag(Contour,WsTrue)
		Stat=Highlight(Contour,WsAutoseqPen,WsDirArrowOn)
		Stat= SetHighlightGroup(Contour)
END IF

GOTO Pick_loop

Leave_record:On_leave= 1
Make_record:!
Stat= SetHighlightGroup(0)
N1=GroupCount(Contour)
IF (N1=0) THEN 
	Stat=DeleteEntity(Contour)
	GOTO Leave
END IF
Stat=SetPickMask(WsPickAll)
Stat=SetInputFormat("n4","")

Stat= SetSelectedFlag(Contour,WsFalse)
Stat=PlotEntity(Contour)

Cont_color= CurrentColor
Cont_layer= CurrentLayer

IF Auto_name THEN
	Stat=GenerateVarName(WsGroupEnt,Name$)
END IF
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Auto link")
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
	IF Cont_color THEN
        	Stat=Highlight(G1,Cont_color,WsDirArrowOn)
	ELSE
        	Stat=Highlight(G1,WsAutoseqPen,WsDirArrowOn)
	END IF
ELSE
        DelLastVarRecord
END IF
Stat=DeleteEntity(Contour)
IF NOT On_leave THEN Destination$= "vcontours.d/vautoseq"
GOTO Leave


Leave:!
UnSelectAll
Stat= SetHighlightGroup(0)
IF ValidGeometry(Contour) THEN
	N1=GroupCount(Contour)
	IF (N1=0) THEN Stat=DeleteEntity(Contour)
END IF
Stat=SetInputFormat("n4=","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END


