Author Topic: Looping through the entire drawing  (Read 3025 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Looping through the entire drawing
« on: April 02, 2007, 09:31:33 AM »

I am having a little difficulty, In the below example, I am looping through Modelspace and populating my listbox with Block References in the drawing. However, I am not sure how to loop through the entire drawing (paper and modelspace) all at the sametime.
If someone could help me, I'd appreciate it.
Thank you
Mark

Code: [Select]
For Each Obj In ThisDrawing.ModelSpace
 If TypeOf Obj Is AcadBlockReference Then
  ListBox1.AddItem (Obj.Name)
 End If
Next Obj

Arizona

  • Guest
Re: Looping through the entire drawing
« Reply #1 on: April 02, 2007, 10:19:46 AM »
Are you utilizing a selection set? or have you considered that?
Code: [Select]
Set objSelCol = objDoc.SelectionSets
  For Each objSelSet In objSelCol
    If objSelSet.Name = "blk_out" Then
      objSelSet.Delete
      Exit For
    End If
  Next objSelSet
  Set objSelSet = objSelCol.Add("blk_out")
  intType(0) = 0
  varData(0) = "INSERT"
  objSelSet.Select 5, filtertype:=intType, _
  filterdata:=varData
  For Each objEnt In objSelSet
« Last Edit: April 02, 2007, 10:23:56 AM by Arizona »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Looping through the entire drawing
« Reply #2 on: April 02, 2007, 11:09:52 AM »
And if you don't want to use a selection set, which would be the case when working with ObjectDBX, you need to loop through the Layouts collection and then each of their blocks.

Code: [Select]
For Each oLayout in Doc
   For Each oEnt in oLayout.Block

.....

ML

  • Guest
Re: Looping through the entire drawing
« Reply #3 on: April 03, 2007, 10:52:52 AM »

Cool. Both approaches make sense.

Thank you  :-)

ML

  • Guest
Re: Looping through the entire drawing
« Reply #4 on: April 03, 2007, 10:55:22 AM »


Actually, I don't think you can access the blocks collection through Layouts

Mark

Chuck Gabriel

  • Guest
Re: Looping through the entire drawing
« Reply #5 on: April 03, 2007, 12:18:43 PM »


Actually, I don't think you can access the blocks collection through Layouts

Mark

I don't think that is what Jeff meant.

The "Block" property of a layout object is how you access the block table record that represents that layout.

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Looping through the entire drawing
« Reply #6 on: April 03, 2007, 12:51:23 PM »
That's correct, Chuck.

Now, if I had inadvertently added an s to the oLayout.Block then that would give an error as Mark thought....but I do try to not make those kind of typing mistakes :-)

ML

  • Guest
Re: Looping through the entire drawing
« Reply #7 on: April 03, 2007, 01:15:51 PM »

Oh, I see, that will return the name of the layout (I think)
No what I was looking for was Blockreferences inserted into paper.
The selection set method above would work just fine.

THank you

Mark

Dave R

  • Guest
Re: Looping through the entire drawing
« Reply #8 on: April 04, 2007, 08:31:10 AM »
Mark -

Put this code in a command button on your form and see what happens:

Code: [Select]
Dim Doc As AcadDocument
Dim oLayout As AcadLayout
Dim oEnt As AcadEntity

Set Doc = ThisDrawing

For Each oLayout In Doc.Layouts
   For Each oEnt In oLayout.Block
      If TypeOf oEnt Is AcadBlockReference Then
         ListBox1.AddItem (oEnt.Name)
      End If
   Next oEnt
Next oLayout

This will loop through each layout in the drawing and add any block references to the listbox.

This is where Jeff and Chuck were trying to lead you. Each layout in a drawing is, in fact, a block which can contain objects. You can loop through the block and look for objects contained in the block just like looping through modelspace or paperspace.

HTH


Oh, I see, that will return the name of the layout (I think)
No what I was looking for was Blockreferences inserted into paper.
The selection set method above would work just fine.

THank you

Mark

DaveW

  • Guest
Re: Looping through the entire drawing
« Reply #9 on: April 04, 2007, 10:12:36 AM »
This is great information. I finally sort of get it. This is very similar to the way it is done in .NET, if I am not mistaken. Someone please correct me if I am wrong. It is a good thing to understand this, even though it appears to be a bit more complicated, because if you want to eventually move to let's say, VB.NET, you will have to a have a firm understanding in using this approach.

ML

  • Guest
Re: Looping through the entire drawing
« Reply #10 on: April 04, 2007, 11:19:45 AM »

Dave

That does make a lot of sense. Thank you very much
I am going to try it then I will post the results.

Mark

ML

  • Guest
Re: Looping through the entire drawing
« Reply #11 on: April 04, 2007, 06:09:00 PM »

Dave,

That worked great, exactly what i was looking for

Thank you

Mark

Arizona

  • Guest
Re: Looping through the entire drawing
« Reply #12 on: April 05, 2007, 06:27:16 AM »
Mark,

While any of these methods would work, what Jeff and Chuck were showing you would be a much quicker, more efficient way of doing what you were trying to do. Rather than search through every entity in the drawing looking for blockreferences, they suggested looking through the layouts for the blockreference. much quicker! :-)

ML

  • Guest
Re: Looping through the entire drawing
« Reply #13 on: April 05, 2007, 09:19:21 AM »

Yes, I would agree to that and I appreciate the help. However, some might say that the sset method is quicker still. Either way, it works great and it isn't slo at all. I will remember that tip for future reference though

Thank you

ML

  • Guest
Re: Looping through the entire drawing
« Reply #14 on: April 05, 2007, 09:45:09 AM »

I was actually advised that this would be a better method: Looping through by objects (Graphical) as opposed to entities which are everything. I have to agree ---->
Mark

Code: [Select]
Dim Layout As AcadLayout
Dim Obj As Object


For Each Layout In ThisDrawing.Layouts
   For Each Obj In Layout.Block
      If TypeOf Obj Is AcadBlockReference Then
         ListBox1.AddItem (Obj.Name)
      End If
   Next Obj
Next Layout