.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

loop thru entity in a block

7 REPLIES 7
Reply
Message 1 of 8
michael_vanhoose
742 Views, 7 Replies

loop thru entity in a block

Does any body know how to loop thru all the entity in a block.

Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database = ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction
Try
Dim tbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim bfound As Boolean = False

For Each id As ObjectId In tbl
Dim btr As BlockTableRecord = tr.GetObject(id, OpenMode.ForRead)
If btr.IsAnonymous = False And btr.IsLayout = False Then


If UCase(btr.Name) = UCase("Test") Then

Dim sp As ObjectId = btr.BlockBeginId
Dim ep As ObjectId = btr.BlockEndId


For p As Integer = sp To ep step 1.0
'I think something like this
Next

tr.Commit()

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Error...")
tr.Abort()
End Try
End Using
End Using
End Using
7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: michael_vanhoose

This will count all circles in a block including any in blocks nested in the
block. Should give you some ideas.

{code}
Private Shared Sub NestedCircles _
(ByVal tr As Transaction, ByVal br As BlockReference, ByRef count As
Integer)
Dim btr As BlockTableRecord = tr.GetObject(br.BlockTableRecord, 0)
If (Not btr Is Nothing) Then
Dim id As ObjectId
For Each id In btr
Dim e As Entity = tr.GetObject(id, 0)
If TypeOf e Is Circle Then
count += 1
End If
'recursive call for nested blocks.
If TypeOf e Is BlockReference Then
NestedCircles(tr, DirectCast(e, BlockReference), count)
End If
Next
End If
End Sub
{code}

wrote in message
news:6233414@discussion.autodesk.com...
Does any body know how to loop thru all the entity in a block.

Using docLock As DocumentLock =
Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction =
db.TransactionManager.StartTransaction
Try
Dim tbl As BlockTable =
tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim bfound As Boolean = False

For Each id As ObjectId In tbl
Dim btr As BlockTableRecord = tr.GetObject(id,
OpenMode.ForRead)
If btr.IsAnonymous = False And btr.IsLayout =
False Then


If UCase(btr.Name) = UCase("Test") Then

Dim sp As ObjectId = btr.BlockBeginId
Dim ep As ObjectId = btr.BlockEndId


For p As Integer = sp To ep step 1.0
'I think something like
this
Next

tr.Commit()

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical,
"Error...")
tr.Abort()
End Try
End Using
End Using
End Using
Message 3 of 8
Anonymous
in reply to: michael_vanhoose

Or using C# and LINQ:

{code}

// Count the total number of block insertions
// in the current space:

[CommandMethod( "COUNTBLOCKREFS" )]
public static void CountBlockRefsWithLinq()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );

using( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
OpenMode.ForRead ) as BlockTableRecord;
var items = from id in btr.Cast()
where id.ObjectClass.IsDerivedFrom(
blockrefClass )
select id;

int result = items.Count();

doc.Editor.WriteMessage(
"Found {0} block insertions in current space", result );
tr.Commit();
}
}

// Count the number of insertions of blocks named
// "DOOR" in the current space:

[CommandMethod( "COUNTDOORS" )]
public static void CountDoorBlockRefsWithLinq()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
using( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
OpenMode.ForRead ) as BlockTableRecord;

var blockrefs = from id in btr.Cast()
where id.ObjectClass.IsDerivedFrom(
blockrefClass )
select id.GetObject( OpenMode.ForRead ) as
BlockReference;

var doors = from blockref in blockrefs
where blockref.Name.ToUpper() == "DOOR"
select blockref;

int result = doors.Count();

doc.Editor.WriteMessage(
"Found {0} insertions of DOOR in current space", result );

tr.Commit();

}
}



{code}

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


"Paul Richardson" wrote in message
news:6233545@discussion.autodesk.com...
This will count all circles in a block including any in blocks nested in the
block. Should give you some ideas.

{code}
Private Shared Sub NestedCircles _
(ByVal tr As Transaction, ByVal br As BlockReference, ByRef count As
Integer)
Dim btr As BlockTableRecord = tr.GetObject(br.BlockTableRecord, 0)
If (Not btr Is Nothing) Then
Dim id As ObjectId
For Each id In btr
Dim e As Entity = tr.GetObject(id, 0)
If TypeOf e Is Circle Then
count += 1
End If
'recursive call for nested blocks.
If TypeOf e Is BlockReference Then
NestedCircles(tr, DirectCast(e, BlockReference), count)
End If
Next
End If
End Sub
{code}

wrote in message
news:6233414@discussion.autodesk.com...
Does any body know how to loop thru all the entity in a block.

Using docLock As DocumentLock =
Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction =
db.TransactionManager.StartTransaction
Try
Dim tbl As BlockTable =
tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim bfound As Boolean = False

For Each id As ObjectId In tbl
Dim btr As BlockTableRecord = tr.GetObject(id,
OpenMode.ForRead)
If btr.IsAnonymous = False And btr.IsLayout =
False Then


If UCase(btr.Name) = UCase("Test") Then

Dim sp As ObjectId = btr.BlockBeginId
Dim ep As ObjectId = btr.BlockEndId


For p As Integer = sp To ep step 1.0
'I think something like
this
Next

tr.Commit()

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical,
"Error...")
tr.Abort()
End Try
End Using
End Using
End Using
Message 4 of 8
Anonymous
in reply to: michael_vanhoose

I just picked up 'Essential Linq' - Been playing with applying to AutoCAD -
Thanks.
"Tony Tanzillo" wrote in message
news:6233581@discussion.autodesk.com...
Or using C# and LINQ:

{code}

// Count the total number of block insertions
// in the current space:

[CommandMethod( "COUNTBLOCKREFS" )]
public static void CountBlockRefsWithLinq()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );

using( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
OpenMode.ForRead ) as BlockTableRecord;
var items = from id in btr.Cast()
where id.ObjectClass.IsDerivedFrom(
blockrefClass )
select id;

int result = items.Count();

doc.Editor.WriteMessage(
"Found {0} block insertions in current space", result );
tr.Commit();
}
}

// Count the number of insertions of blocks named
// "DOOR" in the current space:

[CommandMethod( "COUNTDOORS" )]
public static void CountDoorBlockRefsWithLinq()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Application.DocumentManager.MdiActiveDocument;
RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
using( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
OpenMode.ForRead ) as BlockTableRecord;

var blockrefs = from id in btr.Cast()
where id.ObjectClass.IsDerivedFrom(
blockrefClass )
select id.GetObject( OpenMode.ForRead ) as
BlockReference;

var doors = from blockref in blockrefs
where blockref.Name.ToUpper() == "DOOR"
select blockref;

int result = doors.Count();

doc.Editor.WriteMessage(
"Found {0} insertions of DOOR in current space", result );

tr.Commit();

}
}



{code}

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


"Paul Richardson" wrote in message
news:6233545@discussion.autodesk.com...
This will count all circles in a block including any in blocks nested in the
block. Should give you some ideas.

{code}
Private Shared Sub NestedCircles _
(ByVal tr As Transaction, ByVal br As BlockReference, ByRef count As
Integer)
Dim btr As BlockTableRecord = tr.GetObject(br.BlockTableRecord, 0)
If (Not btr Is Nothing) Then
Dim id As ObjectId
For Each id In btr
Dim e As Entity = tr.GetObject(id, 0)
If TypeOf e Is Circle Then
count += 1
End If
'recursive call for nested blocks.
If TypeOf e Is BlockReference Then
NestedCircles(tr, DirectCast(e, BlockReference), count)
End If
Next
End If
End Sub
{code}

wrote in message
news:6233414@discussion.autodesk.com...
Does any body know how to loop thru all the entity in a block.

Using docLock As DocumentLock =
Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction =
db.TransactionManager.StartTransaction
Try
Dim tbl As BlockTable =
tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim bfound As Boolean = False

For Each id As ObjectId In tbl
Dim btr As BlockTableRecord = tr.GetObject(id,
OpenMode.ForRead)
If btr.IsAnonymous = False And btr.IsLayout =
False Then


If UCase(btr.Name) = UCase("Test") Then

Dim sp As ObjectId = btr.BlockBeginId
Dim ep As ObjectId = btr.BlockEndId


For p As Integer = sp To ep step 1.0
'I think something like
this
Next

tr.Commit()

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical,
"Error...")
tr.Abort()
End Try
End Using
End Using
End Using
Message 5 of 8
Anonymous
in reply to: michael_vanhoose

Nice! Thanks Tony, I hadn't taken notice of LINQ yet. I have now.

Have you noticed any significant performance difference using a LINQ
query like that instead of a recursive function like Paul posted? I
read somewhere after a quick search that it may add some overhead, which
I think makes sense.

--
James Allen
Malicoat-Winslow Engineers, P.C.
Columbia, MO



Tony Tanzillo wrote:
> Or using C# and LINQ:
>
> {code}
>
> // Count the total number of block insertions
> // in the current space:
>
> [CommandMethod( "COUNTBLOCKREFS" )]
> public static void CountBlockRefsWithLinq()
> {
> Database db = HostApplicationServices.WorkingDatabase;
> Document doc = Application.DocumentManager.MdiActiveDocument;
> RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
>
> using( Transaction tr = db.TransactionManager.StartTransaction() )
> {
> BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
> OpenMode.ForRead ) as BlockTableRecord;
> var items = from id in btr.Cast()
> where id.ObjectClass.IsDerivedFrom(
> blockrefClass )
> select id;
>
> int result = items.Count();
>
> doc.Editor.WriteMessage(
> "Found {0} block insertions in current space", result );
> tr.Commit();
> }
> }
>
> // Count the number of insertions of blocks named
> // "DOOR" in the current space:
>
> [CommandMethod( "COUNTDOORS" )]
> public static void CountDoorBlockRefsWithLinq()
> {
> Database db = HostApplicationServices.WorkingDatabase;
> Document doc = Application.DocumentManager.MdiActiveDocument;
> RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
> using( Transaction tr = db.TransactionManager.StartTransaction() )
> {
> BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
> OpenMode.ForRead ) as BlockTableRecord;
>
> var blockrefs = from id in btr.Cast()
> where id.ObjectClass.IsDerivedFrom(
> blockrefClass )
> select id.GetObject( OpenMode.ForRead ) as
> BlockReference;
>
> var doors = from blockref in blockrefs
> where blockref.Name.ToUpper() == "DOOR"
> select blockref;
>
> int result = doors.Count();
>
> doc.Editor.WriteMessage(
> "Found {0} insertions of DOOR in current space", result );
>
> tr.Commit();
>
> }
> }
>
>
>
> {code}
>
>
Message 6 of 8
Anonymous
in reply to: michael_vanhoose

Here is a bit cleaner version. When using Reflector to convert code it
changes enum names to their value - hence the '0' instead of
'OpenMode.ForRead'. Also I should have added a 'Continue For' to avoid the
overhead of the second 'If' statement.
{code}
Private Shared Sub _
NestedCircles(ByVal tr As Transaction, ByVal br As BlockReference, ByRef
count As Integer)

Dim btr As BlockTableRecord = _
DirectCast(tr.GetObject(br.BlockTableRecord, OpenMode.ForRead),
BlockTableRecord)

If btr IsNot Nothing Then
For Each id As ObjectId In btr
Dim e As Entity = DirectCast(tr.GetObject(id, OpenMode.ForRead),
Entity)

If TypeOf e Is Circle Then
count += 1
Continue For
End If

'recursive call if nested block ref is found.
If TypeOf e Is BlockReference Then
NestedCircles(tr, DirectCast(e, BlockReference), count)
End If
Next
End If
End Sub
{code}

"Paul Richardson" wrote in message
news:6233545@discussion.autodesk.com...
This will count all circles in a block including any in blocks nested in the
block. Should give you some ideas.

{code}
Private Shared Sub NestedCircles _
(ByVal tr As Transaction, ByVal br As BlockReference, ByRef count As
Integer)
Dim btr As BlockTableRecord = tr.GetObject(br.BlockTableRecord, 0)
If (Not btr Is Nothing) Then
Dim id As ObjectId
For Each id In btr
Dim e As Entity = tr.GetObject(id, 0)
If TypeOf e Is Circle Then
count += 1
End If
'recursive call for nested blocks.
If TypeOf e Is BlockReference Then
NestedCircles(tr, DirectCast(e, BlockReference), count)
End If
Next
End If
End Sub
{code}

wrote in message
news:6233414@discussion.autodesk.com...
Does any body know how to loop thru all the entity in a block.

Using docLock As DocumentLock =
Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction =
db.TransactionManager.StartTransaction
Try
Dim tbl As BlockTable =
tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim bfound As Boolean = False

For Each id As ObjectId In tbl
Dim btr As BlockTableRecord = tr.GetObject(id,
OpenMode.ForRead)
If btr.IsAnonymous = False And btr.IsLayout =
False Then


If UCase(btr.Name) = UCase("Test") Then

Dim sp As ObjectId = btr.BlockBeginId
Dim ep As ObjectId = btr.BlockEndId


For p As Integer = sp To ep step 1.0
'I think something like
this
Next

tr.Commit()

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical,
"Error...")
tr.Abort()
End Try
End Using
End Using
End Using
Message 7 of 8
Anonymous
in reply to: michael_vanhoose

LINQ is just syntactical sugar.

LINQ expressions are translated by the compiler into iterative functions
that essentially have the same performance characteristics as hand-written
ones.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


"James Allen" wrote in message
news:6233785@discussion.autodesk.com...
Nice! Thanks Tony, I hadn't taken notice of LINQ yet. I have now.

Have you noticed any significant performance difference using a LINQ
query like that instead of a recursive function like Paul posted? I
read somewhere after a quick search that it may add some overhead, which
I think makes sense.

--
James Allen
Malicoat-Winslow Engineers, P.C.
Columbia, MO



Tony Tanzillo wrote:
> Or using C# and LINQ:
>
> {code}
>
> // Count the total number of block insertions
> // in the current space:
>
> [CommandMethod( "COUNTBLOCKREFS" )]
> public static void CountBlockRefsWithLinq()
> {
> Database db = HostApplicationServices.WorkingDatabase;
> Document doc = Application.DocumentManager.MdiActiveDocument;
> RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
>
> using( Transaction tr = db.TransactionManager.StartTransaction() )
> {
> BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
> OpenMode.ForRead ) as BlockTableRecord;
> var items = from id in btr.Cast()
> where id.ObjectClass.IsDerivedFrom(
> blockrefClass )
> select id;
>
> int result = items.Count();
>
> doc.Editor.WriteMessage(
> "Found {0} block insertions in current space", result );
> tr.Commit();
> }
> }
>
> // Count the number of insertions of blocks named
> // "DOOR" in the current space:
>
> [CommandMethod( "COUNTDOORS" )]
> public static void CountDoorBlockRefsWithLinq()
> {
> Database db = HostApplicationServices.WorkingDatabase;
> Document doc = Application.DocumentManager.MdiActiveDocument;
> RXClass blockrefClass = RXClass.GetClass( typeof( BlockReference ) );
> using( Transaction tr = db.TransactionManager.StartTransaction() )
> {
> BlockTableRecord btr = tr.GetObject( db.CurrentSpaceId,
> OpenMode.ForRead ) as BlockTableRecord;
>
> var blockrefs = from id in btr.Cast()
> where id.ObjectClass.IsDerivedFrom(
> blockrefClass )
> select id.GetObject( OpenMode.ForRead ) as
> BlockReference;
>
> var doors = from blockref in blockrefs
> where blockref.Name.ToUpper() == "DOOR"
> select blockref;
>
> int result = doors.Count();
>
> doc.Editor.WriteMessage(
> "Found {0} insertions of DOOR in current space", result );
>
> tr.Commit();
>
> }
> }
>
>
>
> {code}
>
>
Message 8 of 8
Anonymous
in reply to: michael_vanhoose

Okay, thank you Tony.

--
James Allen
Malicoat-Winslow Engineers, P.C.
Columbia, MO



Tony Tanzillo wrote:
> LINQ is just syntactical sugar.
>
> LINQ expressions are translated by the compiler into iterative functions
> that essentially have the same performance characteristics as hand-written
> ones.
>
>

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost