Active space for an Autocad Object

Active space for an Autocad Object

NGVBA
Contributor Contributor
1,656 Views
8 Replies
Message 1 of 9

Active space for an Autocad Object

NGVBA
Contributor
Contributor

Hi All,

I am working on a project with Autocad VBA.

My requirement is to gather the "ActiveSpace" of an object.

What I am doing is ......... based on a certain criteria (Using Filters) I am making a selection set and then transferring the entity info (Which are in selection set) to excel e.g. handle, layer etc.

I want to also know the active space i.e. either "Model" or "Paper".

At the moment I tried to use oSset.items(i).document.ActiveSpace but it only gives me the active space of the document and not where the object resides.

Any help would be much appreciated.

0 Likes
Accepted solutions (2)
1,657 Views
8 Replies
Replies (8)
Message 2 of 9

Ed__Jobe
Mentor
Mentor
Accepted solution

For entities, the OwnerID property will be a pointer to the container object, which is either the modelspace block, the paperspace block or the block table.

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 3 of 9

norman.yuan
Mentor
Mentor

 

Well, since you use SelectionSet with filter to select entities, you can certainly add filter to filter entities in ModelSpace or PaperSpace, or entities on specific layout:

 

Filter for ModelSpace/PaperSpace

Group code: 67

Value: 0 (or omitted) - ModelSpace; 1 - PaperSpace

 

Filter to Layout Name

Group code: 410

Value: a string value of Layout name, such as "Layout1"

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 9

NGVBA
Contributor
Contributor

Thanks for the quick reply.

I used oSset.items(i).document.Ownerid but strange thing happened, when I run the Subroutine by pressing either F5 or F8, excel instance crashes. 

I tried this many times but this does not work

Strangely if I use oSset.items(i).document.ActiveSpace it executes (but it is not what I want).

Any clues ??

 

Thanks in advance.

0 Likes
Message 5 of 9

NGVBA
Contributor
Contributor

Thanks Norman.

On this forum I have been benefitted many a times by your posts.

You are right I can filter for the space but I want the Entities both from paper AND model space along with the entity "Residing" space to be captured through iteration of the selection set entities.

Jobe has suggested to use "OwnerId" but I can't execute that command, Excel crashes when I run it even if I try to press F8 for the subroutine.

Any clues ??

 

0 Likes
Message 6 of 9

norman.yuan
Mentor
Mentor
Accepted solution

You need to use AcadEntity.OwnerId, not AcadDocument.OwnerId (e.g. oSset.Item(i).OwnerId) to trace back to the space/layout the entity belongs to. Something like:

 

Private Function GetLayoutName(ent As AcadEntity) As String

 

  '' If the entity belongs to one of the layouts

  Dim lay As AcadLayout

  For Each lay in ThisDrawing.Layouts

    If lay.Block.ObjectId = ent.OwnerId Then

      GetLayoutName=lay.Name

      Exit Function

    End If

  Next

 

  '' Then it must be from ModelSpace

  GetLayoutName="Model"

 

End Function

 

To use it:

 

Dim ent As AcadEntity

Dim spaceName As String

For Each ent in oSset

  spaceName=GetLayoutName(ent)

  MsgBox "This entity is in " & spaceName

Next

 

I see you mentioned that your code is actually run in Excel. So, to be warned: if you have large SelectionSet, the code would run fairly slow in comparison to running in AutoCAD VBA.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 9

NGVBA
Contributor
Contributor

Thanks for the code Norman.

I understand that this shall work perfectly but my code is acting weird......

I put the function GetLayoutName(ent As AcadEntity) in the code module and when the subroutine code calls the function, my excel crashes.

As I explained earlier as soon as I put "OwnerId" any where  in the code, it crashes the excel.

 

Have you experience anything like this before and what could be the remedy for it?

 

0 Likes
Message 8 of 9

NGVBA
Contributor
Contributor

I discovered while browsing through the past forum discussions and found the below one :

 

https://forums.autodesk.com/t5/vba/automation-error-ownerid-in-excel/td-p/8018188

 

This is really good and I could achieve the desired result through late binding.

 

Thanks a lot for your support.

0 Likes
Message 9 of 9

norman.yuan
Mentor
Mentor

The crash you get is likely that your Excel is 32-bit (thus its VBA also 32-bit), while ObjectId  (e.g. OwnerId property) in AutoCAD COM API is 64-bit long (longPtr type, which is not available in 32-bit VBA), therefore the crash. 

 

So, if you have to do it from Excel VBA side, you have to avoid to touch the data type that is not available in 32-bit VBA, which is not possible in this case (because you need to access OwnerId to decide the layout an entity belongs to). I suspect that the late binding would not help in this case. SO, you either use 64-bit Excel, or doing the work on AutoCAD side. IMO, automating AutoCAD from external app (Excel, in your case) is hardly a good approach. If the work involves quite some AutoCAD operations, it should be done from AutoCAD side (e.g. AutoCAD VBA, or lisp, or .NET plugin...).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes