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

c# and DWG Attributes

9 REPLIES 9
Reply
Message 1 of 10
alberich2k5
3320 Views, 9 Replies

c# and DWG Attributes

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);

        */
    }
}

 

9 REPLIES 9
Message 2 of 10
Hallex
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
Message 3 of 10

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) 😉

 

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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 4 of 10
alberich2k5
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?

 

 

 

 

 

 

 

 

 


Message 5 of 10

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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 10

I will check this out,

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

 

Message 7 of 10

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 ....". 😉

 

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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 10

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.

Message 9 of 10

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
Message 10 of 10
alberich2k5
in reply to: alberich2k5

Thanks to both of you for your time and patience 🙂

 

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

 

thanks again

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