• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011
    Accepted Solution

    Issues adding an Attribute to an Entity

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

     

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,167
    Registered: ‎04-09-2008

    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

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    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.

     

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,167
    Registered: ‎04-09-2008

    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

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    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

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,167
    Registered: ‎04-09-2008

    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

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    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

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    Re: Issues adding an Attribute to an Entity

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

    Thanks Hallex, will do!

    Please use plain text.