.NET

Reply
New Member
quamper
Posts: 2
Registered: ‎12-29-2006
Message 1 of 7 (64 Views)

Next available block in sequence

64 Views, 6 Replies
12-29-2006 06:19 AM
Does someone have a sample of, or can point in the right direction for searching through blocks in the current file to determine the most recent one.

For example. The current document has blocks called Base1, Base2, Base3, Wall1, Wall2, Wall3 and I want to find out what the last Base# is so that I can create the next in the sequence?

VB code would great but I could probably pick through C# as well. Thanks.
Valued Contributor
McSwiller
Posts: 64
Registered: ‎11-06-2006
Message 2 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 12:25 PM in reply to: quamper
Using a snippet like this:


Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = sourceDb.TransactionManager
Dim blT As Transaction = tm.StartTransaction
Dim dbt As BlockTable = CType(blT.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, True), BlockTable) 'destination block table (this current drawing's block table) Dim btr As BlockTableRecord

Try
Dim boId As ObjectId 'block record Id holder
boId = dbt(blkTblRecName) 'this is where you grab a block table record id by name....
Dim btr As BlockTableRecord = CType(tm.GetObject(boId, OpenMode.ForRead, False), BlockTableRecord)


btr.Dispose()

dbt.Dispose()
' No need to commit
blT.Dispose()
tm.Dispose()

Catch ex As Exception

End Try

-You can find a block table record id by name; looping though incrementally, and not finding one by its name could be caught in the Catch area of the Try block - at least then you'd have the current number to use next.

Cheers.
*Paul Richardson
Message 3 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 12:26 PM in reply to: quamper
try this idea.

wrote in message news:5437283@discussion.autodesk.com...
Does someone have a sample of, or can point in the right direction for
searching through blocks in the current file to determine the most recent
one.

For example. The current document has blocks called Base1, Base2, Base3,
Wall1, Wall2, Wall3 and I want to find out what the last Base# is so that I
can create the next in the sequence?

VB code would great but I could probably pick through C# as well. Thanks.
*Paul Richardson
Message 4 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 12:35 PM in reply to: quamper
Not need to have opened the block for write, I should have
just opened for read.

"Paul Richardson" wrote in message
news:5437646@discussion.autodesk.com...
try this idea.

wrote in message news:5437283@discussion.autodesk.com...
Does someone have a sample of, or can point in the right direction for
searching through blocks in the current file to determine the most recent
one.

For example. The current document has blocks called Base1, Base2, Base3,
Wall1, Wall2, Wall3 and I want to find out what the last Base# is so that I
can create the next in the sequence?

VB code would great but I could probably pick through C# as
well. Thanks.
*Tony Tanzillo
Message 5 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 01:10 PM in reply to: quamper
public static class MyClass
{
public static string GetBlockName( ObjectId tableId, string prefix )
{
ArrayList names = new ArrayList();
using( Transaction tr = tableId.Database.TransactionManager.StartTransaction() )
{
SymbolTable table = (SymbolTable) tr.GetObject( TableId, OpenMode.ForRead );
foreach(ObjectId id in table)
{
if( ! id.IsErased )
{
SymbolTableRecord rec = (SymbolTableRecord) tr.GetObject( id, OpenMode.ForRead );
if( rec.Name.StartsWith( prefix, StringComparison.InvariantCultureIgnoreCase ) )
names.Add( rec.Name );
}
}
}
names.Sort();
int n = 0;
while( names.BinarySearch( string.Format( "{0}{1}", prefix, n)) > -1 )
{
++n;
}
return string.Format( "{0}{1}", prefix, n );
}
}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5437283@discussion.autodesk.com...
Does someone have a sample of, or can point in the right direction for searching through blocks in the current file to determine the most recent one.

For example. The current document has blocks called Base1, Base2, Base3, Wall1, Wall2, Wall3 and I want to find out what the last Base# is so that I can create the next in the sequence?

VB code would great but I could probably pick through C# as well. Thanks.
*Tony Tanzillo
Message 6 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 01:23 PM in reply to: quamper
BTW, the name I gave to that method ("GetBlockName"), is
a bit misleading, as it works for any kind of symbol table.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

"Tony Tanzillo" wrote

>> public static string GetBlockName( ObjectId tableId, string prefix )
*Paul Richardson
Message 7 of 7 (64 Views)

Re: Next available block in sequence

12-29-2006 04:05 PM in reply to: quamper
Nice... Thanks.
"Tony Tanzillo" wrote in message
news:5437715@discussion.autodesk.com...
BTW, the name I gave to that method ("GetBlockName"), is
a bit misleading, as it works for any kind of symbol table.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

"Tony Tanzillo" wrote

>> public static string GetBlockName( ObjectId tableId, string prefix )
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.