TheSwamp
Code Red => VB(A) => Topic started by: KEverts 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?
-
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.
-
So from the help file
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
-
There is probably an easier way to do this, but this is the first thing I could think of before the coffee kicks in
-
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
-
I like to use dictionary for these things
Here is my 2 ¢
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'~
-
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?
-
Okay.... here's some code.... Be sure to reference the Microsoft Scripting Runtime library.
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.
-
Randy, Worked great! Thanks much for the help.