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

Reading attributes which are not related to any block from AutoCAD drawing

15 REPLIES 15
Reply
Message 1 of 16
manishasac
863 Views, 15 Replies

Reading attributes which are not related to any block from AutoCAD drawing

hi all,
I am currently workign on reading the attributes from AutoCAD drawing.
I am able to read the attributes related to any block.
But I am not able to read those attributes which are not related to any block.
I am trying the following line of code in c#
foreach (Autodesk.AutoCAD.Interop.Common.AcadAttribute att in adoc.ModelSpace)
{
}
Bot it is not working
Will anyone help me?
15 REPLIES 15
Message 2 of 16
Anonymous
in reply to: manishasac

Do not mix Attributes with AttributeDefinitions

See how it will works for you,
tested on A2007 only
You can covert the code with help
of this page:
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

~'J'~
---------------------------------------------------
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Data
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 AttDefUtils
_
Public Sub GetAttDefs()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Using db As Database = HostApplicationServices.WorkingDatabase()
Using trans As Transaction = db.TransactionManager.StartTransaction()
Try
Dim bTable As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim bRecord As BlockTableRecord = CType(trans.GetObject(bTable("*MODEL_SPACE"), OpenMode.ForWrite), BlockTableRecord)
Dim iter As IEnumerator = bRecord.GetEnumerator
iter.Reset()
Dim i As Integer = 0
While iter.MoveNext
Dim id As ObjectId = iter.Current
Dim ent As Entity = CType(trans.GetObject(id, OpenMode.ForWrite), Entity)
If (TypeOf ent Is AttributeDefinition) Then
Dim atdef As AttributeDefinition = CType(trans.GetObject(ent.ObjectId, OpenMode.ForWrite), AttributeDefinition)
If atdef.Tag.ToUpper = "MYTAG" Then
atdef.UpgradeOpen()
If atdef.IsWriteEnabled = True Then
atdef.TextString = "MyNewValueForAttributeDefinition"
i += 1
End If
ent.Dispose()
End If
End If
End While
MsgBox(String.Format("Changed AttributeDefinition(s): {0}", i))
trans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.StackTrace)
Finally
trans.Dispose()
End Try
End Using
End Using
End Sub
End Class
End Namespace
Message 3 of 16
Anonymous
in reply to: manishasac

>> Dim iter As IEnumerator = bRecord.GetEnumerator
>> iter.Reset()
>> Dim i As Integer = 0
>> While iter.MoveNext
>> Dim id As ObjectId = iter.Current
>> Dim ent As Entity = CType(trans.GetObject(id,......

Fatty, does the VB 'For Each' statement also
not work for you?

Dim id As ObjectId
For Each id in bRecord
' use id here
Next

--
http://www.caddzone.com

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

wrote in message news:5624488@discussion.autodesk.com...
Do not mix Attributes with AttributeDefinitions

See how it will works for you,
tested on A2007 only
You can covert the code with help
of this page:
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

~'J'~
---------------------------------------------------
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Data
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 AttDefUtils
_
Public Sub GetAttDefs()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Using db As Database = HostApplicationServices.WorkingDatabase()
Using trans As Transaction = db.TransactionManager.StartTransaction()
Try
Dim bTable As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim bRecord As BlockTableRecord = CType(trans.GetObject(bTable("*MODEL_SPACE"), OpenMode.ForWrite), BlockTableRecord)
Dim iter As IEnumerator = bRecord.GetEnumerator
iter.Reset()
Dim i As Integer = 0
While iter.MoveNext
Dim id As ObjectId = iter.Current
Dim ent As Entity = CType(trans.GetObject(id, OpenMode.ForWrite), Entity)
If (TypeOf ent Is AttributeDefinition) Then
Dim atdef As AttributeDefinition = CType(trans.GetObject(ent.ObjectId, OpenMode.ForWrite), AttributeDefinition)
If atdef.Tag.ToUpper = "MYTAG" Then
atdef.UpgradeOpen()
If atdef.IsWriteEnabled = True Then
atdef.TextString = "MyNewValueForAttributeDefinition"
i += 1
End If
ent.Dispose()
End If
End If
End While
MsgBox(String.Format("Changed AttributeDefinition(s): {0}", i))
trans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.StackTrace)
Finally
trans.Dispose()
End Try
End Using
End Using
End Sub
End Class
End Namespace
Message 4 of 16
Anonymous
in reply to: manishasac

There are no attributes that are not
related to a block insertion. Those are not
attributes, they're attribute definitions.

So, perhaps you can clarify what it is
you're trying to do?

--
http://www.caddzone.com

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

wrote in message news:5624460@discussion.autodesk.com...
hi all,
I am currently workign on reading the attributes from AutoCAD drawing.
I am able to read the attributes related to any block.
But I am not able to read those attributes which are not related to any block.
I am trying the following line of code in c#
foreach (Autodesk.AutoCAD.Interop.Common.AcadAttribute att in adoc.ModelSpace)
{
}
Bot it is not working
Will anyone help me?
Message 5 of 16
Anonymous
in reply to: manishasac

Yes, you are right, it could be to do with
For Each statement, but I like to use IEnumerator
for my own enjoy 🙂

~'J'~
Message 6 of 16
manishasac
in reply to: manishasac

Actually I have some AutoCAD drawings which doesnt contain any blocks.
But it has some attributes such as Drawing Number, TITLE etc.
I dont know whether they are attributes or attribute definitions since I am new to AutoCAD.
But when I select that Drawing Number(or TITLE) and select the option 'Properties' from Right Mouse Button click menu- It displays as Attribute in the Properties dialog.

I have tried the following code:
int cnt = 0;
foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in adoc.ModelSpace)
{
if (ent is Autodesk.AutoCAD.Interop.Common.AcadAttribute)
{
cnt++;
MessageBox.Show("Its a attribute");
}
}

The code gets executed properly but it doesnt go inside the IF condition ie cnt is not getting incremented.
Please suggest me whether I am missing something?
Message 7 of 16
manishasac
in reply to: manishasac

I think you are right.
Those are attribute definitions not attribute
so is it possible to read those attribute definitions?
Message 8 of 16
Anonymous
in reply to: manishasac

Feel free to do it anyway you wish.

The purpose of pointing it out is so that others
who are using the code posted here as learning
examples are not mislead to believe that it is
the correct way to iterate a collection, because
it is not.


--
http://www.caddzone.com

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

wrote in message news:5624494@discussion.autodesk.com...
Yes, you are right, it could be to do with
For Each statement, but I like to use IEnumerator
for my own enjoy 🙂

~'J'~
Message 9 of 16
Anonymous
in reply to: manishasac

Watch out for the code Fatty posted.

It's loaded with problems that will only crash
AutoCAD. Also, Fatty doesn't seem to realize
that you're using the ActiveX API, not the
managed API.

Attributes do not exist in the drawing as top-
level entities, so you can't access them directly
as elements of the ModelSpace collection.

Attributes are child objects of AcadBlockReference
objects (block insertions), so you have to get them
from the block reference that contains them.

There is sample code in the help for doing that.


--
http://www.caddzone.com

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

wrote in message news:5624533@discussion.autodesk.com...
Actually I have some AutoCAD drawings which doesnt contain any blocks.
But it has some attributes such as Drawing Number, TITLE etc.
I dont know whether they are attributes or attribute definitions since I am new to AutoCAD.
But when I select that Drawing Number(or TITLE) and select the option 'Properties' from Right Mouse Button click menu- It displays as Attribute in the Properties dialog.

I have tried the following code:
int cnt = 0;
foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in adoc.ModelSpace)
{
if (ent is Autodesk.AutoCAD.Interop.Common.AcadAttribute)
{
cnt++;
MessageBox.Show("Its a attribute");
}
}

The code gets executed properly but it doesnt go inside the IF condition ie cnt is not getting incremented.
Please suggest me whether I am missing something?
Message 10 of 16
Anonymous
in reply to: manishasac

I used it before to because it was there - even posted some code using it.
Sorry... Listen to T.T.
wrote in message news:5624494@discussion.autodesk.com...
Yes, you are right, it could be to do with
For Each statement, but I like to use IEnumerator
for my own enjoy 🙂

~'J'~
Message 11 of 16
manishasac
in reply to: manishasac

Hi,
as you said I tried using AcadBlockReference

The code is:

int cnt = 0;
Autodesk.AutoCAD.Interop.Common.AcadBlockReference blockReference;
foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in adoc.ModelSpace)
{
if (ent is Autodesk.AutoCAD.Interop.Common.AcadBlockReference)
{
blockReference = (Autodesk.AutoCAD.Interop.Common.AcadBlockReference)ent;
if (blockReference.HasAttributes)
{
cnt++;
MessageBox.Show("Its a attribute");
}
}
}

Here I am unable to get any AcadBlockReference.
And hence not able to read those attributes.

Please tell me whether I am not going in the right direction?
Message 12 of 16
Anonymous
in reply to: manishasac

>> Here I am unable to get any AcadBlockReference.
>> And hence not able to read those attributes.

If there are block references in model space, then
your code will find them.

If the block reference is in a paper space layout,
then you need to search through the layout's
block, not the model space block.


--
http://www.caddzone.com

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

wrote in message news:5624756@discussion.autodesk.com...
Hi,
as you said I tried using AcadBlockReference

The code is:

int cnt = 0;
Autodesk.AutoCAD.Interop.Common.AcadBlockReference blockReference;
foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in adoc.ModelSpace)
{
if (ent is Autodesk.AutoCAD.Interop.Common.AcadBlockReference)
{
blockReference = (Autodesk.AutoCAD.Interop.Common.AcadBlockReference)ent;
if (blockReference.HasAttributes)
{
cnt++;
MessageBox.Show("Its a attribute");
}
}
}

Here I am unable to get any AcadBlockReference.
And hence not able to read those attributes.

Please tell me whether I am not going in the right direction?
Message 13 of 16
manishasac
in reply to: manishasac

Hi,
Your suggestions are really helpful.
Thoase are not attributes but attribute definitiond and not related to any block.
I am able to reach upto that attribute definition using loop of AcadObject.
It gives me name of AcadObject as AcDbAttributeDefinition.

The code is as:

foreach (Autodesk.AutoCAD.Interop.Common.AcadObject acadObj in adoc.ModelSpace)
{
string objName = acadObj.ObjectName;
if (objName == "AcDbAttributeDefinition")
{
cnt++;
string h = acadObj.Handle);
}
}

Also I am getting handle of that AcadObject.
But still I am not finding any way to get the name and value of that AcadObject.

Any suggestions are really welcome.
Message 14 of 16
Anonymous
in reply to: manishasac

You have to cast the AcadObject to the correct
type. Did you not know that?

--
http://www.caddzone.com

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

wrote in message news:5626017@discussion.autodesk.com...
Hi,
Your suggestions are really helpful.
Thoase are not attributes but attribute definitiond and not related to any block.
I am able to reach upto that attribute definition using loop of AcadObject.
It gives me name of AcadObject as AcDbAttributeDefinition.

The code is as:

foreach (Autodesk.AutoCAD.Interop.Common.AcadObject acadObj in adoc.ModelSpace)
{
string objName = acadObj.ObjectName;
if (objName == "AcDbAttributeDefinition")
{
cnt++;
string h = acadObj.Handle);
}
}

Also I am getting handle of that AcadObject.
But still I am not finding any way to get the name and value of that AcadObject.

Any suggestions are really welcome.
Message 15 of 16
Anonymous
in reply to: manishasac

If you want to use COM interop try this one
It worked for me nice

~'J'~

Public Shared Sub ReadAtts()

Dim acadApp As Autodesk.AutoCAD.Interop.AcadApplication = _
CType(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, Autodesk.AutoCAD.Interop.AcadApplication)
Try
Dim adoc As AcadDocument = acadApp.ActiveDocument
Dim cnt As Integer = 0
For Each objEnt As Common.IAcadObject In adoc.ModelSpace
If (TypeOf objEnt Is Common.IAcadAttribute) Then
cnt += 1
Dim id As Integer = objEnt.ObjectID
Dim atdefobj As Common.IAcadAttribute = objEnt
Dim info(2) As String
info(0) = atdefobj.TagString
info(1) = atdefobj.PromptString
info(2) = atdefobj.TextString
MsgBox(String.Format("Tag: {0}" & Microsoft.VisualBasic.vbCr & _
"Prompt: {1}" & Microsoft.VisualBasic.vbCr & _
"Value: {2}", info))
End If
Next
MsgBox("Selected: " & cnt & " attributes")
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.StackTrace)
Finally
End Try
End Sub

Convert to C# with help by:
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

//
public static void ReadAtts()
{

Autodesk.AutoCAD.Interop.AcadApplication acadApp = (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
try {
AcadDocument adoc = acadApp.ActiveDocument;
int cnt = 0;
foreach (Common.IAcadObject objEnt in adoc.ModelSpace) {
if ((objEnt is Common.IAcadAttribute))
{
cnt += 1;
int id = objEnt.ObjectID;
Common.IAcadAttribute atdefobj = objEnt;
string[] info = new string[3];
info(0) = atdefobj.TagString;
info(1) = atdefobj.PromptString;
info(2) = atdefobj.TextString;
Interaction.MsgBox(string.Format("Tag: {0}" + Microsoft.VisualBasic.vbCr + "Prompt: {1}" + Microsoft.VisualBasic.vbCr + "Value: {2}", info));
}
}
Interaction.MsgBox("Selected: " + cnt + " attributes");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex) {
Interaction.MsgBox(ex.StackTrace);
}
finally {
////
////TODO
////
}
}
Message 16 of 16
manishasac
in reply to: manishasac

Hi,
Now I am able to read the Attribute definitions by typecasting the AcadObject.
Your suggestions are really helpful for a newcomer like me.

Thank you so much.

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