Code Red > .NET

adding/deleting block attributes

(1/8) > >>

ssdiesel:
Hello everyone!
I have block with attributes , which have some values.
I need to add one or more attributes in this block, but values of existing attributes must be saved.
The same for deleting.
i am using ACAD2012 (objectARX 2012).

Thanks.

MexicanCustard:

--- Code: ---//To add a new AttributeDefintion to an existing block
Database db = Application.DocumentManager.MDIActiveDocument.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    var ad = new AttributeDefinition();
    ad.SetDatabaseDefaults(db);
    ad.Tag = "YOUR_TAG";
    ad.Prompt = "Your Prompt";

    var bt = db.BlockTableId.GetObject(OpenMode:ForRead);
    var btr = bt["SOMEBLOCK"].GetObject(OpenMode:ForWrite);
    btr.AppendEntity(ad);
    tr.AddNewlyCreatedDBObject(ad, true);

    tr.Commit();
}


--- End code ---

fixo:
Here is my 2 cents
Change the block name and attribute tag


--- Code - C#: ---       [CommandMethod("dewa")]        public static void DelAttribute()        {            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;             Database db = doc.Database;             Editor ed = doc.Editor;             using (Transaction tr = db.TransactionManager.StartTransaction())            {                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);                 if (!bt.Has("IMP"))                {                    ed.WriteMessage("\nBlock definition IMP does not  exist");                    return;                }                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["IMP"], OpenMode.ForRead, false);                 AttributeDefinition foundDef = new AttributeDefinition();                 foreach (ObjectId id in btr)                {                    DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead, false);                     AttributeDefinition attDef = obj as AttributeDefinition;                     if ((attDef != null) && (!attDef.Constant))                    {                         if (attDef.Tag == "METRIC")                        {                            foundDef = attDef;                             //break;                        }                          bt.UpgradeOpen();                         btr.UpgradeOpen();                         foundDef.UpgradeOpen();                         foundDef.Erase();                         btr.DowngradeOpen();                         bt.DowngradeOpen();                     }                  }                 tr.Commit();                 ed.WriteMessage("\nPerform command \"ATTSYNC\" manually");            }        }         [CommandMethod("newa")]        public static void AddNewAtt()        {             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;            Database db = doc.Database;            Editor ed = doc.Editor;            using (Transaction tr = db.TransactionManager.StartTransaction())            {                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);                 if (!bt.Has("PART"))                {                    ed.WriteMessage("\nBlock definition PART does not  exist");                    return;                }                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["PART"], OpenMode.ForRead, false);                 // location of the AttributeDefinition in the                  // block definition                  Point3d ptloc = new Point3d(0, 2, 0);                // create a AttributeDefinition                // specify the text,tag and prompt                 string strvalue = "NEW VALUE ADDED";                 string strtag = "MYTAG";                 string strprompt = "Enter a new value";                // used current text style                AttributeDefinition attDef = new AttributeDefinition(ptloc, strvalue, strtag, strprompt, db.Textstyle);                attDef.Height = 0.12;                attDef.Layer = "0";                attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);                attDef.LinetypeId = db.ContinuousLinetype;                // append the AttributeDefinition to the definition                btr.UpgradeOpen();                btr.AppendEntity(attDef);                tr.AddNewlyCreatedDBObject(attDef, true);                btr.DowngradeOpen();                tr.Commit();            }        }
edit->kdub code formatting

ssdiesel:
thx MexicanCustard it works!
Fixo, the adding procedure is working, thx! But delete attribute isnt working(
i make a little changes, but it show me an error.

--- Code - C#: ---public void delAttribute(string blockName, string delTagName)        {            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;            Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;            Editor ed = doc.Editor;            try            {                using (doc.LockDocument())                {                    using (Transaction tr = db.TransactionManager.StartTransaction())                    {                        BlockTable bt = (BlockTable)(tr.GetObject(db.BlockTableId, OpenMode.ForRead, false));                        BlockTableRecord btr = (BlockTableRecord)(tr.GetObject(bt[blockName], OpenMode.ForRead, false));                        AttributeDefinition foundedDef = new AttributeDefinition();                        foreach (ObjectId id in btr)                        {                            DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead, false);                            AttributeDefinition attDef = obj as AttributeDefinition;                            if ((attDef != null) && (!attDef.Constant))                            {                                if (attDef.Tag == delTagName)                                    foundedDef = attDef;                            }                            ////bt.UpgradeOpen();                            ////btr.UpgradeOpen();                            //foundedDef.UpgradeOpen();                            foundedDef.Erase();                            //btr.DowngradeOpen();                            //bt.DowngradeOpen();                        }                        tr.Commit();                    }                }            }            catch (System.Exception ex)            {                MessageBox.Show("Error: "+ ex.Message);            }         }the error is:  eNoDatabase


edit->kdub code formatting code = csharp.

fixo:
Sorry but I've deleted an attribute from block with no problems on my machine,
I'm using A2010,

~'J'~

Navigation

[0] Message Index

[#] Next page

Go to full version