Searching trough objects in active layer

Searching trough objects in active layer

Kahva
Contributor Contributor
916 Views
11 Replies
Message 1 of 12

Searching trough objects in active layer

Kahva
Contributor
Contributor

Hello VBA comunity, 

 

I am trying to access objects in active layer without having to filter through MyDrawing.ModelSpace with for loop. Is that possible?

 

My dwg drawing has a lot of objects so the search is slow.

Is there a way to eliminate certain objects so that the for loop doesn't see these objects?

 

Thanks in advance, 

Best regards!

0 Likes
Accepted solutions (1)
917 Views
11 Replies
Replies (11)
Message 2 of 12

seabrahenrique
Advocate
Advocate

Hello,

 

Yes. You must to use a Selection Set to do this.

 

A example can be:

 

Sub LoopObjectsInActiveLayer()

    Dim SS As AcadSelectionSet: Set SS = FA_SSL(ThisDrawing.ActiveLayer.Name) 'You also can put another layer name here
    Dim AcdEnty As AcadEntity

    For Each AcdEnty In SS

        'Do your things here
        Debug.Print AcdEnty.Handle

    Next AcdEnty

End Sub

Function FA_SSL(NomeL As String) As AcadSelectionSet 'Create SelectionSet by LAYER

    On Error Resume Next: ThisDrawing.SelectionSets.Item("SS00").Delete: On Error GoTo 0

    Dim filterType(0 To 1) As Integer, filterData(0 To 1) As Variant
    Set FA_SSL = ThisDrawing.SelectionSets.Add("SS00")
    filterType(0) = 8: filterData(0) = NomeL
    filterType(1) = 67: filterData(1) = 0 'Only in model space
    FA_SSL.Select acSelectionSetAll, , , filterType, filterData

End Function

 

I hope can help u.

Message 3 of 12

Ed__Jobe
Mentor
Mentor

Looping through model space should be comparable to a filtered selection set. Are you trying to perform some logic inside your loop? That could slow down execution. For example, break it up into two blocks. First iterate model space and save the entities to a collection then iterate the collection to process the reduced number of entities.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 4 of 12

Kahva
Contributor
Contributor

Hi Ed.Jobe, you are right, I am trying to do some work on filtered objects. I'm new to VBA, can you please explain what is a selection set? And why is it faster to do logic work on already filtered data over to do some logic while filtering?

Thanks in Advance!

Best regards!

0 Likes
Message 5 of 12

Kahva
Contributor
Contributor

seabrahenrique, thank you for help. Can you clarify the code a little bit?

 

If I understand clearly, by creating a selection set and using a function, I can filter through  objects that are on a certain and defined layer, without filtering through all objects on a drawing using "For Each Obj In ThisDrawing.ModelSpace --- Next" Loop?

 

Where can I look up further on this topic? 

 

Thanks in advance!

Best Regards!

0 Likes
Message 6 of 12

seabrahenrique
Advocate
Advocate

Your understanding was perfect.

 

A selection set is a temporary collection of itens created by some rules. You can create a selection set by a especific layer (as my example) but also can create with:

 

  • Types - for example, filtering only Polylines in your dwg.
  • Colors - for example filtering only objects with a color 7 in your dwg.
  • and etc..

Is a very common and useful resource in a AutoCAD programming. I'll put some contents that u can learn more about bellow:

 

https://documentation.help/AutoCAD-ActiveX-AAG/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-6c1c.htm

https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-F23DA291-5F68-4DAA-BA2B-A92A4A8D2942

 

I hope can help.

0 Likes
Message 7 of 12

Kahva
Contributor
Contributor

seabrahenrique .. Wow, Thanks man, very good and fast reply! Only two more questions.. What is defined by temporary? Just during macro/function operation or the temporary files exist until the document is closed? What if i want to call in that data after some period of time?

 

And lastly, where to find VBA documentation and references? Saw something on AutoDesk, but it seems complicated and unorganised...

 

Thanks again!

Best Regards!

0 Likes
Message 8 of 12

seabrahenrique
Advocate
Advocate
Accepted solution

You're welcome!

 

About the Selection Set Lifetime:

 

When you created a selection set like this:

 

Sub CreateSS()

Dim ss As AcadSelectionSet
Set ss = ThisDrawing.SelectionSets.Add("MySS")

End Sub

 

He gonna exist in your VBA project until the drawing is close, or until you delete it by that way:

 

Sub DeleteSS()

ThisDrawing.SelectionSets.Item("MySS").Delete

End Sub

 

You also can access the selection set using the created name with a "key" in other parts of you code (if you want) by that way:

 

Sub GetMySS()

Dim ss As AcadSelectionSet
Set ss = ThisDrawing.SelectionSets.Item("MySS")

End Sub

 

But to access by that way above, you need before create it like my first example code.

 

If you notice in function "FA_SLL" in my first post, first i delete a selection set named by "SS00" and after that i created another one with the same name. Obviously, you also can created several selection set with different names (or keys) in your drawing, and as i told: they gonna exist until this drawing is closed or until they are deleted.

 

About learning VBA for AutoCAD:

 

Well, about this subject in fact i don't have a 'good news' for you... Actually there are not many contents about it.

 

Personally i can recommend  the AutoCAD ActiveX Developer Guide here: https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-36BF58F3-537D-4B59-BEFE-2D0FEF5A4443

 

And another one is this book:

https://www.wiley.com/en-us/AutoCAD+Platform+Customization:+VBA-p-9781118798935

 

I have one and is awesome 🙂

 

And another tip (like every programming language) is use this forum and sometimes 'google' your doubt... Nothing too different in my onion.

 

I hope can help.

 

0 Likes
Message 9 of 12

Kahva
Contributor
Contributor
Man, u are a MVP. Thanks for help and all clarifications!
Message 10 of 12

Ed__Jobe
Mentor
Mentor

@Kahva wrote:

Hi Ed.Jobe, you are right, I am trying to do some work on filtered objects. I'm new to VBA, can you please explain what is a selection set? And why is it faster to do logic work on already filtered data over to do some logic while filtering?

Thanks in Advance!

Best regards!


In addtion to what @seabrahenriquesaid a selection set is a collection of entities. The collection is valid until the drawing closes or you delete the ss. Deleting the ss doesn’t delete the entities, just the pointers to them. Also even though the ss may still be in the Selectionsets collection, the entities could have been erased. So the ss may no longer refer to the same entities.

As to iterating all entities, you save time not running logic on entities you’re not interested in. For example, suppose you want to change the diameter of certain circles. Why waste time testing lines to see if they have a Radius property? 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 11 of 12

Kahva
Contributor
Contributor

Ed.Jobe, Thank you for your reply! I'm slowly catching up to the said concepts... 

I tried to put above code in VBA, but I get 424 Object required error in selection set function.

Dim filterType(0 To 1) As Integer, filterData(0 To 1) As Variant
    Set FA_SSL = ThisDrawing.SelectionSets.Add("SS00")
    filterType(0) = 8: filterData(0) = NomeL
    filterType(1) = 67: filterData(1) = 0 'Only in model space
    FA_SSL.Select acSelectionSetAll, , , filterType, filterData

 

 The following line in highlighted by debugger: 

Set FA_SSL = ThisDrawing.SelectionSets.Add("SS00") 

 

Tried to resolve it, but i'm missing something. Could you please clarify? Is something missing in declarations? I know the Set statement should be used with objects. 

 

Thank you again!

Best Regards!

 

0 Likes
Message 12 of 12

seabrahenrique
Advocate
Advocate

You have to declare the Selection Set before set it, like this:

 

Dim filterType(0 To 1) As Integer, filterData(0 To 1) As Variant
Dim FA_SSL As AcadSelectionSet 'LOOK HERE
Set FA_SSL = ThisDrawing.SelectionSets.Add("SS00")
filterType(0) = 8: filterData(0) = NomeL
filterType(1) = 67: filterData(1) = 0 'Only in model space
FA_SSL.Select acSelectionSetAll, , , filterType, filterData

 

See the line 2 of code above.

 

(Only if you did not use like a function of my first post).

 

I hope can help!