.NET

Reply
Active Member
alberich2k5
Posts: 6
Registered: ‎05-10-2012
Message 1 of 10 (1,431 Views)

c# and DWG Attributes

1431 Views, 9 Replies
05-10-2012 11:48 AM

Hello Guys!

I'm new to autocad and visual studio. I'm trying to build a simple aplication that uses com interop with autocad. It creates an acad aplication. uses it to open a file, get the blocks and finally its attributes.

My problem is that on the attributes, i'm not able to get the value of the attribute. When i try att.textvalue I get and empty string.  Can anyone help me on this one?

Thanks!!

Here is my very simple code:

 

Autodesk.AutoCAD.Interop.AcadDocument doc = _acad.Documents.Open(dwgpath, true, string.Empty);
Autodesk.AutoCAD.Interop.Common.AcadDatabase myDB = doc.Database;
Autodesk.AutoCAD.Interop.Common.AcadBlocks blocks = myDB.Blocks;

Autodesk.AutoCAD.Interop.Common.AcadBlock selblk = null;


foreach (Autodesk.AutoCAD.Interop.Common.AcadBlock myblock in blocks)
{
    String myBlkName = myblock.Name.ToString();
    
    if (String.Compare(myBlkName.ToLower(), "BOTTOMBLK_2") == 0)
    {
        selblk = myblock;
    }

}

if (selblk == null)
{
    return;
}

foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in selblk)
{
    Autodesk.AutoCAD.Interop.Common.AcadAttribute att = ent as Autodesk.AutoCAD.Interop.Common.AcadAttribute;

    if (att != null)
    {
        MessageBox.Show("EntityName = " + att.TextString);
        MessageBox.Show("EntityName = " + att.TagString);
        /*
        MessageBox.Show("EntityName = " + att.TagString +
                        "\n Entity Prompt String = " + att.PromptString +
                       "\n Entity Text String = " + att.TextString.ToCharArray() +
                        "\n Entity Text String = " + att.TextString);

        */
    }
}

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 10 (1,421 Views)

Re: c# and DWG Attributes

05-10-2012 12:31 PM in reply to: alberich2k5

Just a hint,

Compare with your code:

      foreach (Autodesk.AutoCAD.Interop.Common.AcadBlock myblock in blocks)
            {
                String myBlkName = myblock.Name.ToString();

                if (String.Compare(myBlkName.ToUpper(), "BOTTOMBLK_2") == 0)
                {
                    selblk = myblock;
                }

            }

            if (selblk == null)
            {
                return;
            }

            foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity ent in selblk)
            {
                Autodesk.AutoCAD.Interop.Common.AcadAttribute att = ent as Autodesk.AutoCAD.Interop.Common.AcadAttribute;

                if (att != null)
                {
                    Autodesk.AutoCAD.Interop.AcadUtility util = doc.Utility;
                    util.Prompt("\n" + att.TextString + "\t" + att.TagString);

                }
            }

 

You write ToLower() might be ToUpper()

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 3 of 10 (1,420 Views)

Re: c# and DWG Attributes

05-10-2012 12:34 PM in reply to: alberich2k5

Hi,

 

First check that codeline:

if (String.Compare(myBlkName.ToLower(), "BOTTOMBLK_2") == 0)

Using .ToLower and compare it with uppercase characters does not make that sense (even if the comparision is set to see uppercase equal to lowercase) :smileywink:

 

Next I'm not sure you see the difference between BLOCK and BLOCKREFERENCE?

If you really want to have the block-definitions (Interop.Common.AcadBlock) then your code does mistyping when scaning through the elements of the block-definition:

ent as Autodesk.AutoCAD.Interop.Common.AcadAttribute;

Within your block-definition there may be objects like AcadLine, AcadCircle, ..... and also AcadAttribute.

You try to cast every object of type AcadEntity to AcadAttribtue and that does not work and at least it needs to be handled by some error-handler (better is to check the type of ent before casting it).

 

If you thought more about AcadBlockReference (the inserted Blocks in ModelSpace or PaperSpace) then let us know that, because then you are on the wrong way.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Member
alberich2k5
Posts: 6
Registered: ‎05-10-2012
Message 4 of 10 (1,412 Views)

Re: c# and DWG Attributes

05-10-2012 12:58 PM in reply to: alberich2k5

Hello!

 

Yes, sorry. I mistake copy pasting the code.

    if (String.Compare(myBlkName.ToLower(), "BOTTOMBLK_2") == 0)

 

The thing is, with my code (without the typos), and as I explaines in the first post, I'm able to scan and reach the block BOTTOMBLK_2. When scanning the atributes inside this block, I can see the tag name, the prompt string, but no the value string of this attribute. These 3 attributes I can see them in the autocad app (the real autocad I mean).

 

I want to reach to these attributes, and I need the value. Maybe eventually I will need to edit some value. That's what I was trying to do. Do I need the blockreference then?

 

 

 

 

 

 

 

 

 


*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 5 of 10 (1,410 Views)

Re: c# and DWG Attributes

05-10-2012 01:02 PM in reply to: alberich2k5

Hi,

 

>> I can see the tag name, the prompt string, but no the value string of this attribute

Are you sure that you have a predefined value in the definition of the attribute (within the block-definition)? Otherwise you won't see a value. Verify that by starting the blockeditor with that blockdefinition and double-click the attribute-definition.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Member
alberich2k5
Posts: 6
Registered: ‎05-10-2012
Message 6 of 10 (1,405 Views)

Re: c# and DWG Attributes

05-10-2012 01:29 PM in reply to: Alfred.NESWADBA

I will check this out,

meanwhile, you talk about of doing it using blockreference. How should I do it?

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 7 of 10 (1,400 Views)

Re: c# and DWG Attributes

05-10-2012 01:35 PM in reply to: alberich2k5

Hi,

 

>> meanwhile, you talk about of doing it using blockreference. How should I do it?

I'm not sure about what you want to do, and as long as this is not clear I don't want to spend time in "I will check this out, meanwhile ....". :smileywink:

 

When you are sure if you want to have the value of an attribute within a block-defintion or you want to have the value of an attribute within a block-insertion in modelspace or paperspace. In second case you would also have to be prepared that there may be more than one insertion and so more than just one attribute-value.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Member
alberich2k5
Posts: 6
Registered: ‎05-10-2012
Message 8 of 10 (1,397 Views)

Re: c# and DWG Attributes

05-10-2012 02:03 PM in reply to: Alfred.NESWADBA

which are the difference between attribute within a block-defintion and an attribute within a block-insertion in modelspace or paperspace? I though the block I created & filled in the autocad application, was the one I found in the code above.

 

thanks for your replies and time.

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 9 of 10 (1,386 Views)

Re: c# and DWG Attributes

05-10-2012 02:48 PM in reply to: alberich2k5

When you loop through the BlockTable looking at objects of type AcadBlock, those are Block definitions, there is only on of each name, and they may contain geometric objects, and/or AcadAttributes (Attribute Definitions).  When a Block is Inserted into the model or paper space, this definition is what AutoCAD looks at to tell it what is supposed to be drawn for that block, and what AcadAttributeReferences should be added to the AcadBlockReference.

 

The smallest change that needs to made to your code, is that you don't want to loop through the BlockTable, you want to loop through the ModelSpace or PaperSpace.  And you shouldn't be looking for objects of AcadBlock you should be looking for AcadBlockReference, and inside the BlockReference, loop looking for AcadAttributeReference.

 

Better still would be to drop the loop of the ModelSpace, and use a selection filter to obtain the BlockReferences you are looking for directly.

Dave O.                                                                  Sig-Logos32.png
Active Member
alberich2k5
Posts: 6
Registered: ‎05-10-2012
Message 10 of 10 (1,361 Views)

Re: c# and DWG Attributes

05-11-2012 08:18 AM in reply to: alberich2k5

Thanks to both of you for your time and patience :smileyhappy:

 

I will look at the selection filter to see better how can I do it.

 

thanks again

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.