.NET

.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 9 (278 Views)
Accepted Solution

Issues adding an Attribute to an Entity

278 Views, 8 Replies
05-10-2012 05:46 AM

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

 

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;

 


Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 2 of 9 (275 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 06:15 AM in reply to: vince1327

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.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 3 of 9 (266 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 07:03 AM in reply to: Alexander.Rivilis

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.

 

Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 4 of 9 (261 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 07:14 AM in reply to: vince1327

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;

 



Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 5 of 9 (253 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 07:19 AM in reply to: Alexander.Rivilis

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

 

***Edited for spelling

Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 6 of 9 (250 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 07:21 AM in reply to: vince1327

vince1327 wrote:

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


:smileyhappy: This is not fantasy - it is a minimal knowledge of the AutoCAD capabilities .


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 7 of 9 (243 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 07:43 AM in reply to: Alexander.Rivilis

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 :smileyhappy:

 

Cheers

Vince

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

Re: Issues adding an Attribute to an Entity

05-10-2012 08:06 AM 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
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 9 of 9 (235 Views)

Re: Issues adding an Attribute to an Entity

05-10-2012 08:08 AM in reply to: Hallex

Thanks Hallex, will do!

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.