.NET

.NET

Reply
Contributor
lukasz.taraszka
Posts: 13
Registered: ‎10-07-2011
Message 1 of 3 (623 Views)
Accepted Solution

How to get AcadBlock and AcadEntity in transaction?

623 Views, 2 Replies
10-24-2011 04:56 AM

Welcome 

 

I have a little problem - how to get AcadBlock or AcadEntities by using transaction.

 

In a current open document I can do something like this:

 

Dim document As Document = Application.DocumentManager.MdiActiveDocument
        Dim AcadDrawing As AcadDocument = DirectCast(document.AcadDocument, AcadDocument)
        For Each block As AcadBlock In AcadDrawing.Blocks
            For Each ent As AcadEntity In block
                'here I can access all of the entities in block
            Next
        Next

It's working fine, but when I tried open a document in backgroud I can't get the AcadBlock or AcadEntities, only a BlockTableRecord.

 

Dim path As String = "c:\path to dwg file"
        Dim db As Database = New Database(TrueTrue)
        db.ReadDwgFile(path, FileOpenMode.OpenForReadAndAllShare, TrueNothing)
        Dim tr As Transaction = db.TransactionManager.StartTransaction()
        Dim blockTable As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
        For Each blockRecordId As ObjectId In blockTable
            Dim blockRecord As BlockTableRecord = DirectCast(tr.GetObject(blockRecordId, OpenMode.ForRead), BlockTableRecord)'I can't get AcadEntity or AcadBlock here :/
        Next

Is there any posibility to get AcadBlocks or AcadEntities by using TransactionManager?

what's the difference between AcadBlock and BlockTableRecord?

 

I will be very grateful if anyone would like to help me with this problem :smileyhappy:

 

Hi,

 

am I right that you try to scan through all Blocks?

Look at this code. ... does it help?

 

Dim blockTable As BlockTable = ctype(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each blockRecordId As ObjectId In blockTable
if (blockRecordID.isValid) andalso (not blockRecordID.isErased) Then
'now we can get the BlockTableRecord for this ID
Dim blockRecord As BlockTableRecord = cType(tr.GetObject(blockRecordId, OpenMode.ForRead), BlockTableRecord)
'now scan through the entities in the BlockTableRecord
for each EntID as ObjectID in blockRecord
if (EntID.isValid) andalso (not EntID.isErased) Then
dim tEnt as Entity = cType(tr.GetObject(EntID,OpenMode.ForRead),Entity)
'whatever you want to do with this Entity now ...
end if
Next
end if
Next

 HTH, - alfred -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,607
Registered: ‎06-29-2007
Message 2 of 3 (621 Views)

Re: How to get AcadBlock and AcadEntity in transaction?

10-24-2011 05:18 AM in reply to: lukasz.taraszka

Hi,

 

am I right that you try to scan through all Blocks?

Look at this code. ... does it help?

 

Dim blockTable As BlockTable = ctype(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each blockRecordId As ObjectId In blockTable
  if (blockRecordID.isValid) andalso (not blockRecordID.isErased) Then
    'now we can get the BlockTableRecord for this ID
    Dim blockRecord As BlockTableRecord = cType(tr.GetObject(blockRecordId, OpenMode.ForRead), BlockTableRecord)
    'now scan through the entities in the BlockTableRecord
    for each EntID as ObjectID in blockRecord
      if (EntID.isValid) andalso (not EntID.isErased) Then
        dim tEnt as Entity = cType(tr.GetObject(EntID,OpenMode.ForRead),Entity)
        'whatever you want to do with this Entity now ...
      end if
    Next
  end if
Next

 HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
lukasz.taraszka
Posts: 13
Registered: ‎10-07-2011
Message 3 of 3 (615 Views)

Re: How to get AcadBlock and AcadEntity in transaction?

10-24-2011 05:25 AM in reply to: Alfred.NESWADBA

That's correct - Your code is doing exactly what I meant.

Thanks for Your help and support.

Best regards

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.