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

Reading Block Attribute Values

15 REPLIES 15
Reply
Message 1 of 16
Anonymous
1275 Views, 15 Replies

Reading Block Attribute Values

Hey Everyone,

I'm busy developing a standalone application in VB.NET to read the contents out of a DWG file. So far i was able to open DWG file and search for the titleblock. But I'm having problems accessing the title block attributes and it's values. Can someone tell me how this can be done?

have already took a look at the object model from Autocad and also examples written online on how to get attributes but the way it's done is by inserting a block and then requesting it's attributes. In my case i don't need to insert a block, i would like to retrieve the block attributes and it's values.

Thanks in advance
15 REPLIES 15
Message 2 of 16
Anonymous
in reply to: Anonymous

this is getting it by the tag name

For Each ID2 In blkRef.AttributeCollection
attrefIndex += 1
Dim attref As AttributeReference = Trans.GetObject(ID2, OpenMode.ForRead)
'Dim attdef As AttributeDefinition = GetAttributeDefinition(attrefIndex, blkRef, Trans)
If Not attref Is Nothing Then
If attref.Tag.Equals(strTag) Then
Return attref.TextString
End If
End If
Next


pat
Message 3 of 16
Anonymous
in reply to: Anonymous

I was reading trough your code and i saw the object Trans.. Here is what got me confuse. In this post http://discussion.autodesk.com/thread.jspa?messageID=5362441. At first i did this but i was getting the same error message. So i thought this was the caused of my problem. My Question is , the code you posted, how can i implement this in my standalone application (.EXE).
Message 4 of 16
Anonymous
in reply to: Anonymous

The code I sent you was for in process and you are looking for out of process.

Right now I do not have anything available for out of process.

pat
Message 5 of 16
brucebruce
in reply to: Anonymous

Here is a function I use to get an attribute call SIZE by looping through all the block in a drawing, very slow.

Add a reference to AXDBLib

Imports Autodesk
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

call Get_size("c:\mydrawing.dwg")

Function Get_size(ByVal SourceFile As String) As String
'Dim PaperSize As String
'Dim odbxdoc As AxDbDocument
'Dim OutputSize As PageSizeEnum
'Dim AcadApp As AcadApplication
'Dim Paper As String
Dim blkref As AcadBlockReference
Dim AttArray As Object
Dim obj As Object
Dim Msg, Style, Title, Help, Ctxt, response, MyString
Dim i
On Error Resume Next

' Check for Open AutoCAD
AcadApp = GetObject(, "AutoCAD.Application.16.2")
If Err.Number 0 Then
Err.Clear()
' if not Open run new AutoCAD
AcadApp = CreateObject("AutoCAD.Application.16.2")
End If
' AvDbDocument

Dim Block As AcadBlock
Dim strOut As String
PaperSize = ""
odbxdoc = AcadApp.GetInterfaceObject("ObjectDBX.AxDbDocument.16")
odbxdoc.Open(SourceFile)
Console.WriteLine("Blockcount = " & odbxdoc.Blocks.Count)
Dim count = 0
For Each Block In odbxdoc.Blocks
'___________________________________________________________________________
If Not Block.Name.StartsWith("*") Then
'look in all blocks for the attribute "size"
PaperSize = getAttribute_Info("SIZE", Block.Name)
'___________________________________________________________________________

If PaperSize "" Then
Get_size = PaperSize
odbxdoc.close()
Exit Function
End If
End If
Next Block
odbxdoc.close()
Console.WriteLine(strOut)
AcadApp.Documents.Close()
'CrawlDrawingEx(odbxdoc)

End Function
Message 6 of 16
Anonymous
in reply to: Anonymous

Will be this help for you?

~'J'~

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices

Namespace BlockUtils
Public Class AttributeUtils
_
Public Sub ChangeAttributes()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim blockName As String = "Title_Block"
Dim attag As String = "REV_NO"
Dim atvalue As String = "001-002-003-004-005"
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockName) Then
ed.WriteMessage(vbCr & "Block does exist: " & blockName)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(blockName), OpenMode.ForWrite), BlockTableRecord)
Dim blockRefIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
If blockRefIds.Count > 0 Then
For Each id As ObjectId In blockRefIds
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
Dim attcol As AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
'Dim attref As AttributeReference = tr.GetObject(attId, OpenMode.ForWrite, True)
Dim attref As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference)
If attag.Equals(attref.Tag) Then
attref.TextString = atvalue
End If
Next
Next
End If
End If
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage("Exception: " + ex.Message)
Finally
tr.Dispose()
End Try
End Using
db.Dispose()
End Using
End Sub
End Class
End Namespace
Message 7 of 16
Anonymous
in reply to: Anonymous

Fatty - Your code has a lot of problems.

You are disposing the current database (via the
Using directive), and you are disposing the
transaction even though the Using directive is
doing that for you.

You might want to consider spending a little
time learning to use the language and the
API yourself before you try to teach others to
use it.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5624566@discussion.autodesk.com...
Will be this help for you?

~'J'~

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices

Namespace BlockUtils
Public Class AttributeUtils
_
Public Sub ChangeAttributes()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim blockName As String = "Title_Block"
Dim attag As String = "REV_NO"
Dim atvalue As String = "001-002-003-004-005"
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockName) Then
ed.WriteMessage(vbCr & "Block does exist: " & blockName)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(blockName), OpenMode.ForWrite), BlockTableRecord)
Dim blockRefIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
If blockRefIds.Count > 0 Then
For Each id As ObjectId In blockRefIds
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
Dim attcol As AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
'Dim attref As AttributeReference = tr.GetObject(attId, OpenMode.ForWrite, True)
Dim attref As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference)
If attag.Equals(attref.Tag) Then
attref.TextString = atvalue
End If
Next
Next
End If
End If
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage("Exception: " + ex.Message)
Finally
tr.Dispose()
End Try
End Using
db.Dispose()
End Using
End Sub
End Class
End Namespace
Message 8 of 16
Anonymous
in reply to: Anonymous

Tony It's your deal to teach others 🙂
but I try to write the code only
Again, I have not have any books on
my native language therefore is problem here

Here is revised version, let me know, please,
what is will be wrong again

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices

Namespace BlockUtils
Public Class AttributeUtils
_
Public Sub ChangeAttributes()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim blockName As String = "Title_Block"
Dim attag As String = "REV_NO"
Dim atvalue As String = "001-002-003-004-005"
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockName) Then
ed.WriteMessage(vbCr & "Block does exist: " & blockName)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(blockName), OpenMode.ForWrite), BlockTableRecord)
Dim blockRefIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
If blockRefIds.Count > 0 Then
For Each id As ObjectId In blockRefIds
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
Dim attcol As AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
'Dim attref As AttributeReference = tr.GetObject(attId, OpenMode.ForWrite, True)
Dim attref As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference)
If attag.Equals(attref.Tag) Then
attref.TextString = atvalue
End If
Next
Next
End If
End If
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage("Exception: " + ex.Message)
Finally
End Try
End Using
End Using
End Sub
End Class
End Namespace
Message 9 of 16
NathTay
in reply to: Anonymous

What is your reason for using Using in the following line:

Using db As Database = HostApplicationServices.WorkingDatabase()
Message 10 of 16
Anonymous
in reply to: Anonymous

Fatty - You don't understand what the 'Using'
directive is. If you don't understand it, then
you need to learn about it, not guess about it.

There's no VB.NET learning resources in your
language? That's hard to believe.

The book you need is about VB.NET, not about
AutoCAD. You can't learn to program AutoCAD
with .NET API before you first learn to use the
VB.NET language.

If you go back into this newsgroup you will see
posts I've made here about 'Using' and Dispose,
and that should tell you what's wrong with your
code.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5624583@discussion.autodesk.com...
Tony It's your deal to teach others 🙂
but I try to write the code only
Again, I have not have any books on
my native language therefore is problem here

Here is revised version, let me know, please,
what is will be wrong again

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices

Namespace BlockUtils
Public Class AttributeUtils
_
Public Sub ChangeAttributes()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim blockName As String = "Title_Block"
Dim attag As String = "REV_NO"
Dim atvalue As String = "001-002-003-004-005"
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockName) Then
ed.WriteMessage(vbCr & "Block does exist: " & blockName)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(blockName), OpenMode.ForWrite), BlockTableRecord)
Dim blockRefIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
If blockRefIds.Count > 0 Then
For Each id As ObjectId In blockRefIds
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
Dim attcol As AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
'Dim attref As AttributeReference = tr.GetObject(attId, OpenMode.ForWrite, True)
Dim attref As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference)
If attag.Equals(attref.Tag) Then
attref.TextString = atvalue
End If
Next
Next
End If
End If
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage("Exception: " + ex.Message)
Finally
End Try
End Using
End Using
End Sub
End Class
End Namespace
Message 11 of 16
Anonymous
in reply to: Anonymous

Fatty - If you try testing your code before you post it,
you will learn from your own mistakes, and others will
not have to pay for your mistakes.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5624583@discussion.autodesk.com...
Tony It's your deal to teach others 🙂
but I try to write the code only
Again, I have not have any books on
my native language therefore is problem here

Here is revised version, let me know, please,
what is will be wrong again

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices

Namespace BlockUtils
Public Class AttributeUtils
_
Public Sub ChangeAttributes()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim blockName As String = "Title_Block"
Dim attag As String = "REV_NO"
Dim atvalue As String = "001-002-003-004-005"
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockName) Then
ed.WriteMessage(vbCr & "Block does exist: " & blockName)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(blockName), OpenMode.ForWrite), BlockTableRecord)
Dim blockRefIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
If blockRefIds.Count > 0 Then
For Each id As ObjectId In blockRefIds
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
Dim attcol As AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
'Dim attref As AttributeReference = tr.GetObject(attId, OpenMode.ForWrite, True)
Dim attref As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference)
If attag.Equals(attref.Tag) Then
attref.TextString = atvalue
End If
Next
Next
End If
End If
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage("Exception: " + ex.Message)
Finally
End Try
End Using
End Using
End Sub
End Class
End Namespace
Message 12 of 16
Anonymous
in reply to: Anonymous

Hi Nathan, yes you are right, I did
it mechanically

~'J'~
Message 13 of 16
Anonymous
in reply to: Anonymous

Tony, thanks a lot again for your explanation,
of course, I'll learn further
But at the moment my code worked good
for me with no any mistakes
I tested it extensivelly

~'J'~
Message 14 of 16
Anonymous
in reply to: Anonymous

wrote

>> What is your reason for using Using in the following line:

>> Using db As Database = HostApplicationServices.WorkingDatabase()

He's not supposed to do it, but the runtime knows
that, so it catches the mistake and there is no harm
done, but it doesn't change the fact that it shouldn't
be done to begin with, and there are other managed
objects that are not as forgiving.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 15 of 16
Rudedog
in reply to: Anonymous

Check this out.

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627

There is a sample application in most of the packages to read block attributes. Just pick your preferred programming style. I would post an example I customized, but it would be way too long. YOu could take one of the examples and simply change the property or attribute names to what you need.

(PSST. Don't forget to delete the stuff you don't want.)..........=8^D

Rudedog
"Fooling computers since 1971."
Message 16 of 16
Rudedog
in reply to: Anonymous

Why not insert it, read it, then delete it when you are done.

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