Author Topic: Looping through layers  (Read 2262 times)

0 Members and 1 Guest are viewing this topic.

KEverts

  • Guest
Looping through layers
« on: November 16, 2007, 07:01:13 AM »
I am working on a routine which simplifies the creation of section views using the "solview" & "soldraw" commands. My only hangup is naming the view. I want to name the views SOL-SECT##.

Creating the first section will work fine, but what I need to do is loop through the layers and find the SOL-SECT#-VIS layer with the greatest number so I can then choose the next number for the view's name. Problem...I know how to loop through the layers, however, I'm not sure how to find the SOL-SECT#-VIS layer with the greatest number. Can anyone point me in the right direction?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through layers
« Reply #1 on: November 16, 2007, 09:19:16 AM »
Well, If you always name it SOL-SECT##-VIS, (notice the 2 #'s), you can use Mid() and search the layer name and pull out a 2 digit number, and do a comparison of greater than less than and move thru the dwg.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through layers
« Reply #2 on: November 16, 2007, 09:27:46 AM »
So from the help file
Quote
MID()
Returns a Variant (String) containing a specified number of characters from a string.
Syntax
Mid(string, start[, length])

The Mid function syntax has these named arguments:
Part Description
string Required. String expression from which characters are returned. If string contains Null, Null is returned.
start Required; Long. Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").
length Optional; Variant (Long). Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

So if you had layer SOL-SECT02-VIS & SOL-SECT78-VIS, I would make 2 vars to compare.  FirstNum= Mid(Layername,9,2) & SecNum=Mid(Layername,9,2)

Then do a if FirstNum > SecNum then get next Layername to compare
Else, SecNum was bigger, get next Layername and campare to Secnum using > again
« Last Edit: November 16, 2007, 09:29:24 AM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through layers
« Reply #3 on: November 16, 2007, 09:30:31 AM »
There is probably an easier way to do this, but this is the first thing I could think of before the coffee kicks in
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through layers
« Reply #4 on: November 16, 2007, 09:59:29 AM »
Having had a cup of coffee now, I was thinking about what if you filter your layers on the SOL- part, then sort them alphabetically, and the last one should be the highest number, or reverse sort and the first one is the highest
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Fatty

  • Guest
Re: Looping through layers
« Reply #5 on: November 17, 2007, 01:31:15 PM »
I like to use dictionary for these things

Here is my 2 ¢


Code: [Select]
Option Explicit
' based on original code from here:
' http://support.microsoft.com/default.aspx?scid=kb;en-us;246067
' request reference to Microsoft Scripting Runtime
  Dim d, i
  Const dictKey = 1
  Const dictItem = 2
Function SortDictionary(objDict, intSort)
  ' declare our variables
  Dim strDict()
  Dim objKey
  Dim strKey, strItem
  Dim X, Y, Z

  ' get the dictionary count
  Z = objDict.Count

  ' we need more than one item to warrant sorting
  If Z > 1 Then
    ' create an array to store dictionary information
    ReDim strDict(Z, 2)
    X = 0
    ' populate the string array
    For Each objKey In objDict
        strDict(X, dictKey) = CStr(objKey)
        strDict(X, dictItem) = CStr(objDict(objKey))
        X = X + 1
    Next

    ' perform a a shell sort of the string array
    For X = 0 To (Z - 2)
      For Y = X To (Z - 1)
        If StrComp(strDict(X, intSort), strDict(Y, intSort), vbTextCompare) > 0 Then
            strKey = strDict(X, dictKey)
            strItem = strDict(X, dictItem)
            strDict(X, dictKey) = strDict(Y, dictKey)
            strDict(X, dictItem) = strDict(Y, dictItem)
            strDict(Y, dictKey) = strKey
            strDict(Y, dictItem) = strItem
        End If
      Next
    Next

    ' erase the contents of the dictionary object
    objDict.RemoveAll

    ' repopulate the dictionary with the sorted information
    For X = 0 To (Z - 1)
      objDict.Add strDict(X, dictKey), strDict(X, dictItem)
    Next

  End If

End Function


Sub NextLayerByAscending()
Dim oLayer As AcadLayer
Dim n
  Set d = CreateObject("Scripting.Dictionary")
  For Each oLayer In ThisDrawing.Layers
  If oLayer.Name Like "SOL-SECT#-VIS" Or _
     oLayer.Name Like "SOL-SECT##-VIS" Then

d.Add CStr(n), oLayer.Name
n = n + 1
End If
Next

  SortDictionary d, dictItem
  Dim lastLay As String, nextLay As String
  lastLay = "SOL-SECT12-VIS" 'dummy layers for debug
 
For Each i In d

If d(i) > lastLay Then
nextLay = d(i)
Exit For
End If
Next

MsgBox "Next layer is: " & Chr(34) & nextLay & Chr(34)

End Sub

~'J'~

KEverts

  • Guest
Re: Looping through layers
« Reply #6 on: November 21, 2007, 09:20:20 AM »
Sorry I haven't gotten back to you guys...got sidetracked. Thanks much for the replies.

Fatty, I tried the code you supplied and was pretty excited when I first tried it, however, when ran again it keeps giving the same #. I am not familiar with the dictionary at all. I tried a couple of things myself, but made things worse. Any ideas?

surveyor_randy

  • Guest
Re: Looping through layers
« Reply #7 on: November 22, 2007, 07:17:52 PM »
Okay....  here's some code....  Be sure to reference the Microsoft Scripting Runtime library.

Code: [Select]
Function SortDict(ByVal objDict)
     
Dim i, j, temp As Variant
     
For Each i In objDict
    For Each j In objDict
        If (objDict.Item(i) <= objDict.Item(j)) Then
            temp = objDict.Item(i)
            objDict.Item(i) = objDict.Item(j)
            objDict.Item(j) = temp
        End If
    Next
Next
   
Set SortDict = objDict

End Function
Public Sub try_this()

Dim oLayerDictX As Dictionary
Dim oLayerDictXX As Dictionary

Dim oSortedLayerDictX As Dictionary
Dim oSortedLayerDictXX As Dictionary
Dim oSortedLayerDict As Dictionary

Dim oLayer As AcadLayer
Dim oLayers As AcadLayers

Dim vKey As Variant

Set oLayers = ThisDrawing.Layers
Set oLayerDictX = New Dictionary
Set oLayerDictXX = New Dictionary
Set oSortedLayerDict = New Dictionary

For Each oLayer In oLayers
    If oLayer.Name Like "SOL-SECT#-VIS" Then
        oLayerDictX.Add oLayerDictX.Count + 1, oLayer.Name
    ElseIf oLayer.Name Like "SOL-SECT##-VIS" Then
        oLayerDictXX.Add oLayerDictXX.Count + 1, oLayer.Name
    End If
Next

Set oSortedLayerDictX = SortDict(oLayerDictX)
Set oSortedLayerDictXX = SortDict(oLayerDictXX)

For Each vKey In oSortedLayerDictX
    oSortedLayerDict.Add oSortedLayerDict.Count + 1, oSortedLayerDictX(vKey)
Next

For Each vKey In oSortedLayerDictXX
    oSortedLayerDict.Add oSortedLayerDict.Count + 1, oSortedLayerDictXX(vKey)
Next

Debug.Print "Highest Layer: " & oSortedLayerDict(oSortedLayerDict.Count)

Set oLayerDictX = Nothing
Set oLayerDictXX = Nothing
Set oSortedLayerDictX = Nothing
Set oSortedLayerDictXX = Nothing
Set oSortedLayerDict = Nothing
Set oLayer = Nothing
Set oLayers = Nothing

End Sub

The reason for having multiple dictionaries has to do with the sorting method.  One dictionary (oLayerDictX) contains all layers titled 'SOL-SECT#-VIS', the other dictionary (oLayerDictXX) contains all of the layers titled 'SOL-SECT##-VIS'.  You could eliminate one of the dictionaries by creating a naming scheme using a leading '0' which would hold the place for numbers less than 10.
I hope this helps.

KEverts

  • Guest
Re: Looping through layers
« Reply #8 on: November 26, 2007, 07:20:42 AM »
Randy, Worked great! Thanks much for the help.