! script name: contrib/ascript/tsm.d/autosequence
! topic: auto link, create contour that is a chain of connected elements
! Version_270423.a

!A
!A NAME:
!A
!A      Auto Link
!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.
!B              This 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, Unlink, Single, New:
!B      or,
!B      PICK NEXT, Backup, Unlink, Multiple, 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              U - Unlink.  Backs up to the beginning.
!B              M - Default Mode.  Choosing Multiple switches from Single pick
!B                  mode.  
!B              S - Auto link defaults into Multiple mode.  Single allows the
!B                  Auto link to proceed one element at a time and should be
!B                  used if there are a lot of places where a decision about
!B                  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
!B              is picked twice, the Auto Link will proceed around
!B              the contour as far as it can, back to the first
!B              element if the contour is closed and there are
!B              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
!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]

Stat= SetCurrentColor(6)
Stat= SetCurrentLayer(5)



EStart:!
DEG
DeActivateUndo
Contour=CreateGroup("CONTOUR",WsContourGroup)

Smode= WsMultipleAuto
Stat= SetDefaults(WsSelectedEntPen,WsAutoseqPen)

Pick_loop:!
Number= GroupCount(Contour)
IF Number  THEN
        Stat= SetEqualsValue(0,"N")
        if(Smode= WsMultipleAuto) THEN
                Question$= "PICK NEXT , Backup, Unlink, Single, New" 
                Stat= SetInputFormat("a1=",",B,S,N,U,")
        ELSE
                Question$= "PICK NEXT , Backup, Unlink, Multiple, New" 
                Stat= SetInputFormat("a1=",",B,M,N,U,")
        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

SELECT Answer$
CASE "N"
        Destination$= "tsm.d/autosequence"
        GOTO Leave
CASE "S","s"
        Smode= WsSingleAuto
        GOTO Pick_loop
CASE "M","m"
        Smode= WsMultipleAuto
        GOTO Pick_loop
CASE "U","u"
        Stat= SetSelectedFlag(Contour,WsFalse)
        Stat= PlotEntity(Contour)
        FOR L= GroupCount(Contour) TO 1 STEP -1
                Stat= RemoveFromGrp(Contour,L)
        NEXT L
        Stat= SetSelectedFlag(Contour,WsTrue)
        Stat= SetHighlightGroup(0)
        Stat= DeleteEntity(Contour)
        GOTO Start
CASE "B","b"  ! Backup one element
        Number= GroupCount(Contour)
        Stat= SetSelectedFlag(Contour,WsFalse)
        Stat= PlotEntity(GroupElement(Contour,Number))
        IF Number THEN
                Stat= RemoveFromGrp(Contour,Number)
        END IF
        Stat= SetSelectedFlag(Contour,WsTrue)
        Stat= SetHighlightGroup(Contour)
        Stat= PlotEntity(Contour)
        GOTO Pick_loop
END SELECT

IF NOT ValidGeometry(Estart) THEN Pick_loop
IF IsEntVariational(Estart) THEN
        Stat= SetInputFormat("a1=",",Y,N,")
        Stat= GetValue("VDM ENTITY. RUN VDM AUTO-LINK? Yes or No ",Answer$)
        IF Answer$= "Y" THEN
                Destination$= "vcontours.d/vautoseq"
                GOTO Leave
        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
        IF 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= SetHighlightGroup(Contour)
END IF

GOTO Pick_loop

Leave:!
        Stat= SetHighlightGroup(0)
        N1=GroupCount(Contour)
        IF (N1=0) THEN
                Stat=DeleteEntity(Contour)
        ELSE
                Stat= SetSelectedFlag(Contour,WsFalse)
                Stat= ColorContour(Contour)
                Stat= AutoNumberCont(Contour)
                Stat= PlotEntity(Contour)
        END IF
Stat=SetInputFormat("n4=","")


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


