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

Issues adding an Attribute to an Entity

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
vince1327
569 Views, 8 Replies

Issues adding an Attribute to an Entity

Hey everyone,

 

I'm trying to add an attribute to an entity, however it seems that the AttributeDefinition requires a Point3d and as such appear in my modelspace. I don't want this, i just wanted it to appear as an attribute with a value when i click on my entity, is there any way of making it so that i does not add into modelspace?

 

Cheers

Vince

 

 {
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);

                    for (int i = 0; i < prSelectionResult.Value.Count; i++)
                    {
                        // Get the entity the user had selected
                        Entity ent = (Entity)trans.GetObject(prSelectionResult.Value[i].ObjectId, OpenMode.ForWrite);

                        if (ent.GetType() == typeof(BlockReference))
                        {
                            // Add attribute to layer
                            BlockReference blockRef = (BlockReference)ent;
                            AttributeDefinition attDef = new AttributeDefinition(new Point3d(0d, 0d, 0d), "World, I'm an attribute!", "Hello", "", db.Textstyle);
                            
                            // Add the attribute to to the BlockTrableRecord of the blockdefinition of the selected block
                            ObjectId btrId = blockRef.BlockTableRecord;
                            BlockTableRecord btrBlock = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForWrite);
                            btrBlock.AppendEntity(attDef);
                            trans.AddNewlyCreatedDBObject(attDef, true);

                            // Add the AttributeReference to the BlockReference                            
                             AttributeReference attRef = new AttributeReference();
                             attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
                             blockRef.AttributeCollection.AppendAttribute(attRef);
                             trans.AddNewlyCreatedDBObject(attRef, true);
                                          
                        }
                    }

 

8 REPLIES 8
Message 2 of 9

Maybe you can explain in detail what does it do?
It seems to me that this is not the best practice each time to change the number AttributeDefinition in the block.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 9

Hello,

 

I was searching through other posts and examples in the forums and came across this. What it does is add an attribute to an entity, in this case:

 

Hello: World, I'm an Attribute!

 

As you can see in the attached photo however, the attribute is also placed into modelspace as per the lines:

BlockReference blockRef = (BlockReference)ent;
                            AttributeDefinition attDef = new AttributeDefinition(new Point3d(0d, 0d, 0d), "World, I'm an Attribute!", "Hello", "", db.Textstyle);
What i'm trying to do is avoid having the attribute placed in my modelspace, just in the entity.

 

Message 4 of 9

Do I understand correctly that you do not want to attributes were visible on the screen (model space)?
Then it is sufficient to set their property Invisible to true or property Visible to false.

Property Visible is common entity property, but Invisible is only AttributeReference (and AttributeDefinition) property.

attdef.Invisible = true;
attref.Invisible = true;

 


Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 5 of 9

Fantastic!!!! That's exactly what i was looking for!!!! Thank you!

 

***Edited for spelling

Message 6 of 9


@vince1327 wrote:

Fatnastic!!!! That's exactly what i was looking for!!!! Thank you!


Smiley Happy This is not fantasy - it is a minimal knowledge of the AutoCAD capabilities .

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 7 of 9

I've just begun working with attributes and am very new to the AutoCAD API in general so I was not aware of this property. Although it may seem like minimal knowledge to some, to me it's quite new and something I will definetly remember as I continue 🙂

 

Cheers

Vince

Message 8 of 9
Hallex
in reply to: vince1327

Would be good as well to add one line in your code

         

AttributeReference attRef = newAttributeReference();

                            attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);

                            blockRef.AttributeCollection.AppendAttribute(attRef);

                        //  additional line:

                            attRef.AdjustAlignment(db);

 

                            trans.AddNewlyCreatedDBObject(attRef,

true);

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 9 of 9
vince1327
in reply to: Hallex

Thanks Hallex, will do!

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