I would probably put their values in an array and sort on that.
Or a sorted list box?
in the meantime I'd ask here and hope somebody comes up with a sensible answer.
Registered forum members do not see this ad.
Does anybody know if it's possible to order a selectionset by a specified property in VBA?
What I am trying to do:
I have a collection of polylines on a drawing, all of which are contained in a selection set. I then want to iterate through the selection set, starting with the polyline with the smallest area through to the polyline with the largest area.
Does anybody know of any way to do this?
- Tuoni
Terminat hora diem, terminat author opus
I would probably put their values in an array and sort on that.
Or a sorted list box?
in the meantime I'd ask here and hope somebody comes up with a sensible answer.
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
Thanks for your quick response, I am trying to avoid doing that
as I need several properties on each of the objects - for instance, layer name, area, perimeter/length...
I was wondering if there was a more efficient way?
- Tuoni
Terminat hora diem, terminat author opus
As I was typing that I thought "I bet he doesn't want to do this". But having typed I wasn't going to not post!
However, having now checked the help it doesn't look like there is a method available.
I would still be inclined to do an array. You only have to store an order, not the objects. Something like....
to get an array of order. Then step through byCode:if object(order(i)).area < object(order(i+1)).area then temp = order(i) order(i) = order(i+1) order(i+1) = temp
mySelectionSet.item(order(i))
(totally off the top of my head so probably not practical)![]()
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
I have checked the help and have searched on other forums (such as the AutoDesk discussion forum) but also to no avail, that's why I aired it here.
You should know, as well as I (if not better), that VBA is a very impractical language in AutoCAD due to its... "quirks"... shall we say?
Still, storing (potentially) hundreds of polylines in a selection set, then having to manually trawl through it to find increasing areas is a complete ballache and isn't going to help the execution time of the software
Thanks for your responses anyway, I think I will end up having to do it this wayif you have any other thoughts in the meantime, I'd be grateful
- Tuoni
Terminat hora diem, terminat author opus
I find VBA easier than LISP. But it is frustrating, especially when that one command that would have saved you SO MUCH TIME isn't on the drop down list but does work if you were just brave enough to try it.
I agree, storing hundreds of items in an array and manipulating them isn't ideal. I do sometimes resort to writing my own custom control in VB at times but I can only get them to work on my machine. No matter how closly I follow insructions I just can't seem to get them to work reliably on other machines. (not that I can see a use here)
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
I haven't ever learnt LISP, I must admit (though from doing reading round it seems that VBA is more suited to my task anyway... using ADO and integrating with mirco$0ft office applications etc) but most of my software in recent years has been in languages without drop down lists so I have lost reliance on them- drop down lists being an invention of the devil is shown by the fact that "area" is not on the list for an entity (yes, I understand that it is only applicable in certain cases, but surely the programmer should know what sort of entity is being passed to the function?)
Also, from my experience, a lot of VBA seems to be somewhat of a "Dark Art", there is little true documentation on a lot of the features - which is exactly why I joined usergroups lol
anyways </rant>
I'll keep digging and thinking. Got a few hour's train journey back home yet... I might as well chew it over rather than just purely staring blankly out of the window)
- Tuoni
Terminat hora diem, terminat author opus
The Collection object may be a good alternative to a dynamic array. It doesn't require "redim"ming , and adding items at a specific spot if a relatively easy task.
Code:Option Explicit Sub SortByArea() Dim intCode(0) As Integer Dim varData(0) As Variant Dim entLWP As AcadLWPolyline Dim entLWPSorted As AcadLWPolyline Dim colSmall2Large As New Collection Dim intSSCount As Integer Dim i As Integer Dim j As Integer Dim colSS As AcadSelectionSet intCode(0) = 0: varData(0) = "LWPOLYLINE" intSSCount = SoSSS(intCode, varData) 'create selection Set colSS = ThisDrawing.SelectionSets.Item("TempSSet") If intSSCount > 0 Then Set entLWP = colSS.Item(0) colSmall2Large.Add entLWP 'add first item as baseline For i = 1 To intSSCount - 1 Set entLWP = colSS.Item(i) For j = 1 To colSmall2Large.Count If entLWP.Area <= colSmall2Large.Item(j).Area Then colSmall2Large.Add entLWP, , j Exit For ElseIf j = colSmall2Large.Count Then colSmall2Large.Add entLWP, , , j 'current Pline has largest area End If Next Next End If For Each entLWP In colSmall2Large MsgBox "Area: " & entLWP.Area & vbCr & "Perimeter: " & entLWP.Length & vbCr & "Layer: " & entLWP.Layer Next Set colSS = Nothing Set entLWP = Nothing End Sub '''''''Generic Selection Set creation routine ''''''' Function SoSSS(Optional grpCode As Variant, Optional dataVal As Variant) As Integer Dim TempObjSS As AcadSelectionSet SSClear 'make sure SS doesn't already exist Set TempObjSS = ThisDrawing.SelectionSets.Add("TempSSet") 'Select on Screen Selection Set If IsMissing(grpCode) Then TempObjSS.SelectOnScreen Else TempObjSS.SelectOnScreen grpCode, dataVal End If SoSSS = TempObjSS.Count Set TempObjSS = Nothing End Function Private Sub SSClear() Dim SSS As AcadSelectionSets On Error Resume Next Set SSS = ThisDrawing.SelectionSets If SSS.Count > 0 Then SSS.Item("TempSSet").Delete End If Set SSS = Nothing End Sub
Thankyou both for your quick responses, and thanks SEANT, that code does exactly what I wanted, and on my "test" drawing, it runs quickly enough for me(I was quite worried at the prospect of sorting an array - not a quick process at the best of times >.<).
Thanks again
(Oh, and just so as you know... I did just stare blankly out of the window on the train last night hehe)
- Tuoni
Terminat hora diem, terminat author opus
Registered forum members do not see this ad.
I hope the passing scenery, blankly stared upon, was nice.![]()
Bookmarks