Hi,
I'm trying to read the value out of an attribute called "REV" in a title block called "STAR2" in C#. I then want to assign the value as the definition in my dictionary shown below, and return that to the method that eventually will call this one.
Please see a snippet of my code, the places in bold are the areas I [think I] need to address:
// Gets the attributes from the drawing public getDWGInfo() { // Declarations int dwgCount = 0; DocumentCollection docs = Application.DocumentManager; var dwgDict = new Dictionary<string, string>(); foreach (Document doc in docs) { Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; if (acBlkTbl.Has("STAR2")) { // ##### Get the value of attribute "REV" from "STAR2" & Add to dictionary below? dwgDict[doc.Name] = ; dwgCount++; } else { Application.ShowAlertDialog("The drawing does not have a STAR2 titleblock with a REV attribute, couldn't add to list."); } } } // ##### Return dwgDict to the method that called it? }
Could you toss me some bones
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
Are you looking for the (default) value of an attribute definition (AttributeDefinition) contained in a block definition (BlockTableRecord) in the block table (BlockTable) or the value of an attribute reference (AttributeReference) bound to a block reference (BlockReference) inserted in a the model space or paper space of the drawing?
You can try something like this:
public static Dictionary<string, string> GetAttribs(string blockName, string tag) { // create a new instance of Dictionary<string, string> var attribs = new Dictionary<string, string>(); // get the documents collection var docs = Application.DocumentManager; // use an OpenCloseTransaction which is not related to a document or database using (var tr = new OpenCloseTransaction()) { // iterate through the documents foreach (Document doc in docs) { // get the document database var db = doc.Database; // get the database block table var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); // if the block table contains a block definitions named 'blockName'... if (bt.Has(blockName)) { // open the block definition var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead); // get the inserted block references ObjectIds var ids = btr.GetBlockReferenceIds(true, true); // if any... if (0 < ids.Count) { // open the first block reference var br = (BlockReference)tr.GetObject(ids[0], OpenMode.ForRead); // iterate through the attribute collection foreach (ObjectId id in br.AttributeCollection) { // open the attribute reference var attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForRead); // if the attribute tag is equal to 'tag' if (attRef.Tag.Equals(tag, StringComparison.CurrentCultureIgnoreCase)) { // add an entry to the dictionary attribs[doc.Name] = attRef.TextString; // break the loop break; } } } } } } // return the dictionary return attribs; }
Thank you gile.
I want to call this method once, then have the returned dictionary "attribs" be globally accessible to anything in the same namespace for use in my Forms interface which is in another class file.
How can this be accomplished?
It's difficult to reply without knowing your projetc architecture.
Typically, to expose data from a class to other classes, you can use instance or static properties.
You can also pass the data to an overload constructor of the Form (as explain in the 'Modal dialog box' part of this tutorial).
Anyway, you should learn the .NET basics outside of AutoCAD until you're comfortable with OOP, .NET Framework, Visual Studio and C# before trying to use the AutoCAD .NET API.
Can't find what you're looking for? Ask the community or share your knowledge.