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

How to access ThisDrawing.Blocks in VB.NET

1 REPLY 1
SOLVED
Reply
Message 1 of 2
TTIII
1096 Views, 1 Reply

How to access ThisDrawing.Blocks in VB.NET

Continuing from my prior post (thank you those who replied), the equavilent of ThisDrawing from VBA in VB.NET, spawned a new question:

 

I poked around in the help but it didnt really answer my question.

 

Anyway, now I am using MdiActiveDocument

Application.DocumentManager.MdiActiveDocument

However there is no .Blocks


In VBA I was able to do this:

 

For Each entry In ThisDrawing.Blocks
                If entry.Name = BADBLOCK01 Or entry.Name = BADBLOCK02 Or entry.Name = BADBLOCK03 Then
                    found = True
                End If
Next

 How can I access/do this in VB.NET?

 

 

Anyone?

By the way, if you do manage to find the answer posted in some help somewhere please post a link.

 

 

Tony

 

1 REPLY 1
Message 2 of 2
_gile
in reply to: TTIII

Hi,

 

IMHO, if you want to learn the AutoCAD .NET API, you'd better forget your VBA knowledge and start from scratch rather than searching for equivalencies (perhaps learning C# rather than VB should be helpfull in this way).

You'll find some tutorials here.

 

The .NET API have some more layers than the COM one. For example, the equivalent of the COM Document object (ThisDrawing in VBA) is sometimes the .NET Document object, but may also be the Database one.

The common way to access to a database resident object in .NET is openning it from its ObjectId within a transaction. The equivalent for the COM Blocks (the drawing blocks collection) is a BlockTable object.

 

C#

Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
bool found = false;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    found = bt.Has("BADBLOCK01") || bt.Has("BADBLOCK02") || bt.Has("BADBLOCK03");
    tr.Commit();
}

 VB

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim found As Boolean = False
Using tr As Transaction = db.TransactionManager.StartTransaction()
    Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
    found = bt.Has("BADBLOCK01") OrElse bt.Has("BADBLOCK02") OrElse bt.Has("BADBLOCK03")
    tr.Commit()
End Using


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

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