c# code to add new attribute value to a block

c# code to add new attribute value to a block

Anonymous
Not applicable
7,972 Views
22 Replies
Message 1 of 23

c# code to add new attribute value to a block

Anonymous
Not applicable

I wanted to add new attribute value to block by opening a autocad drawing & selecting block for which the new attribute value should be added

0 Likes
Accepted solutions (1)
7,973 Views
22 Replies
Replies (22)
Message 2 of 23

_gile
Consultant
Consultant

Hi,

 

In view of all your recent posts, could you elaborate about the context in which you wish to do this (in-process vs out-of-process)?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 23

Anonymous
Not applicable

I want to open a autocad drawing and select the block & in the program I want a textbox & a button the value given the user in the textbox should be added to the selected block by clicking on the button

0 Likes
Message 4 of 23

_gile
Consultant
Consultant

Sorry, but it's still not clear to me.
Do you want to create a custom AutoCAD command or, again, do that from a standalone executable, which is a completely different (and not so easy) process?
Is the block selected by the user a particular block? If so, is it dynamic? What's its name? Does it contain one or more attributes?
Is it to add an attribute or modify the value of an existing attribute? In any case, what is the label of the attribute?

 

And, if possible, show what you have done so far.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 23

Anonymous
Not applicable

imagine I have a autocad drawing it contains "n" number of block & each block has many attributes but now the user wants to add a new attribute value for a particular block which he wants to be added so he has to select the block first & then add the new attribute to that block

0 Likes
Message 6 of 23

_gile
Consultant
Consultant

Do you want to only add an attribute reference to the selected block reference or add an attribute definition to the block definition and then synchronize all the references?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 23

Anonymous
Not applicable

I would like to add an attribute reference to the selected block reference. 

0 Likes
Message 8 of 23

_gile
Consultant
Consultant

You certainly know that this kind of attribute reference will be erased if the ATTSYNC command is run for this block.

 

Here's an example of AutoCAD plugin (i.e., a class library DLL which have to be netloaded in AutoCAD) containg custom command which shows a little dialog box to let the user enter the attribute value, then it prompts the user to select a block reference and specify the attribute insertion point.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace AmazingRequestSample
{
    public class Commands
    {
        [CommandMethod("ADDATTREF")]
        public static void AddAttributeReference()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            // display a dialog box to get the attribute value
            string attValue;
            using (var dialog = new AttributeValueDialog())
            {
                if (Application.ShowModalDialog(dialog) != System.Windows.Forms.DialogResult.OK)
                    return;
                attValue = dialog.AttributeValue;
            }

            // prompts the user to select a block reference
            var peo = new PromptEntityOptions("\nSelect block reference: ");
            peo.SetRejectMessage("\nSelected object is not a block reference.");
            peo.AddAllowedClass(typeof(BlockReference), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;

            // prompts the user to specify the attribute insertion point
            var ppr = ed.GetPoint("\nSpecify the attribute insertion point: ");
            if (ppr.Status != PromptStatus.OK)
                return;
            var insPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);

            // create the attribute reference and add it to the attribute collection of the selected block
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var br = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
                var attRef = new AttributeReference(insPoint, attValue, "TAG", db.Textstyle)
                { LockPositionInBlock = false };
                br.AttributeCollection.AppendAttribute(attRef);
                tr.AddNewlyCreatedDBObject(attRef, true);
                tr.Commit();
            }
        }
    }
}

The dialog box

AttributeValueDialog.png

using System.Windows.Forms;

namespace AmazingRequestSample
{
    public partial class AttributeValueDialog : Form
    {
        public AttributeValueDialog()
        {
            InitializeComponent();
            AcceptButton = buttonOk;
            CancelButton = buttonCancel;
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;
        }

        public string AttributeValue => textBoxAttValue.Text;
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 23

Anonymous
Not applicable

Thank you so much for ur quick response

0 Likes
Message 10 of 23

Anonymous
Not applicable

I'm getting error in the below line

 

var attRef = new AttributeReference(insPoint, attValue, "TAG", db.Textstyle) { LockPositionInBlock = false };

 

Use of unassigned local variable "attValue" but i have assigned the variable

0 Likes
Message 11 of 23

_gile
Consultant
Consultant

I do not have such error.

Is it an error at edition time (which prevent the compilation in Visual Studio) or an exception thrown at running time?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 23

Anonymous
Not applicable

It is an error at edition time

0 Likes
Message 13 of 23

_gile
Consultant
Consultant

Your project should contains 2 classes:

  • Commands.cs where the command is defined
  • AttributeValueDialog.cs the dialog box decription

AmazingRequestSample.png



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 23

Anonymous
Not applicable

Even I have done the same thing. But I m getting a errorError.jpg

0 Likes
Message 15 of 23

_gile
Consultant
Consultant

You didn't correctly the code i posted.

Replace:

            string attValue;
            using (var dialog = new AttributeValueDialog())
            {
                if (Application.ShowModalDialog(dialog) != System.Windows.Forms.DialogResult.OK)
                    attValue = dialog.AttributeValue;
            }

with:

            string attValue;
            using (var dialog = new AttributeValueDialog())
            {
                if (Application.ShowModalDialog(dialog) != System.Windows.Forms.DialogResult.OK)
                    return;
                attValue = dialog.AttributeValue;
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 16 of 23

Anonymous
Not applicable

Ya I m sorry. Thank you for correcting me

0 Likes
Message 17 of 23

Anonymous
Not applicable

Thank you sooo much it worked successfully

Now I want an enhancement in that code. I want to add the attribute value by selecting multiple blocks how can we do that??

0 Likes
Message 18 of 23

_gile
Consultant
Consultant

You can use a selection set got with Editor.GetSelection() and a selection filter instead of Editor.GetEntity() and then, for each selected entity, prompt the user to specify the insertion point and add the attribute reference.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 19 of 23

Anonymous
Not applicable

Can u send me the code

0 Likes
Message 20 of 23

_gile
Consultant
Consultant
Accepted solution

Claiming a code is not a good way to learn.

 

        [CommandMethod("ADDATTREF")]
        public static void AddAttributeReference()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            // display a dialog box to get the attribute value
            string attValue;
            using (var dialog = new AttributeValueDialog())
            {
                if (Application.ShowModalDialog(dialog) != System.Windows.Forms.DialogResult.OK)
                    return;
                attValue = dialog.AttributeValue;
            }

            // prompts the user to select block references
            var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            var psr = ed.GetSelection(filter);
            if (psr.Status != PromptStatus.OK)
                return;
            dynamic acadApp = Application.AcadApplication;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                foreach (SelectedObject so in psr.Value)
                {
                    var br = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForWrite);
                    br.Highlight();
                    // prompts the user to specify the attribute insertion point
                    var ppr = ed.GetPoint("\nSpecify the attribute insertion point: ");
                    br.Unhighlight();
                    if (ppr.Status != PromptStatus.OK)
                        continue;
                    var insPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                    var attRef = new AttributeReference(insPoint, attValue, "TAG", db.Textstyle)
                    { LockPositionInBlock = false };
                    br.AttributeCollection.AppendAttribute(attRef);
                    tr.AddNewlyCreatedDBObject(attRef, true);
                    db.TransactionManager.QueueForGraphicsFlush();
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes