How to know in wich space is a selected object ?

How to know in wich space is a selected object ?

TONELLAL
Collaborator Collaborator
1,165 Views
6 Replies
Message 1 of 7

How to know in wich space is a selected object ?

TONELLAL
Collaborator
Collaborator

Hello,

All is in the title...

I have to select blocks, then replace them by other blocks.

I select them by :

 

FilterType(0) = 0       
FilterData(0) = "Insert"

oSS.SelectOnScreen FilterType, FilterData

 

This is ok, I select only Block entities.

 

Then I replace blocks :

oBlock.delete

Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionpoint, blockname, 1#, 1#, 1#, rotateAngle)

...but this always insert the block in ModelSpace.

 

How can I detect in wich space is the original block, so I can insert the new blocx using Modelspace or Papersace (i) ?

0 Likes
Accepted solutions (1)
1,166 Views
6 Replies
Replies (6)
Message 2 of 7

Norman_Yuan
Mentor
Mentor

You can use AcadDocument.ActiveSpace to test what the current space the selection is performed, because you do selecting on screen, which only allows you select entities in current/active space (either model, or paper)

 

So, you can do something like:

 

If ThisDrawing.ActiveSpace=AcModelSpace Then

  Set BlockRefObj=ThisDrawing.ModelSpace.Insert(....)

Else

  Set BlockRefObj=ThisDrawing.PaperSpace.Insert(....)

End If

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 7

TONELLAL
Collaborator
Collaborator

"... because you do selecting on screen, which only allows you select entities in current/active space " :

This is exactly where my problem is : if you select All, you select in paperspace AND in modelspace...

0 Likes
Message 4 of 7

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

each entity has an OwnerID which points to the block which holds the object, that can be the blockdefinition called modelspace, it can also be the blockdefinition of every layout existing in the drawing.

 

Let's see if that helps (code goes through "Point" objects in a selection set and places a new blockreference at the position of the point (and in the modelspace or layout(s) of that point(s).

 

Dim tEnt As AcadPoint
For Each tEnt In tSelSet
   Debug.Print ("")
   Dim tBlock As AcadBlock
   Set tBlock = ThisDrawing.ObjectIdToObject(tEnt.OwnerID)  'that is the owner (parent) of the point
   Dim tBlRef As AcadBlockReference
   Set tBlRef = tBlock.InsertBlock(tEnt.Coordinates, "NewBlockName", 1, 1, 1, 0)
Next

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 7

TONELLAL
Collaborator
Collaborator

Thanks Alfred !

I did something like this :

 

Space = ThisDrawing.ObjectIdToObject(tEnt.OwnerID)

If Space.Name = "*Paper_Space" Then
    Set BlockRefObj = ThisDrawing.PaperSpace.InsertBlock(insertionpoint, blockname, 1#, 1#, 1#, rotateAngle)
Else
    Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionpoint, blockname, 1#, 1#, 1#, rotateAngle)
End If

 

 

Alain

0 Likes
Message 6 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> I did something like this [...]

Sorry, that might fail. If you have more than one layouts then the "ThisDrawing.Paperspace" points to the first/active layout,

In case your blockreference is on layout3 your insertion would be done on layout1 instead of layout3

 

That's why I suggested do use (using your variables) Space.InsertBlock....

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 7

TONELLAL
Collaborator
Collaborator

Hi,

Ok, not understood very well first time.

I noticed the problem if I have several layouts, but strangely it seems to function...

0 Likes