I have a custom drawing property called: MyCustom and set it value to "This". Via C# I want to change the value to "That". Currently I have the code to enumerate through the Database.SummaryInfo.CustomProperties which gets me down to MyCustom, but now "how" to change the value. That I cannot figure out. I can find how to create new custom properties, but I can't seem to find how to just change the value. I hoped it would be as simple as just setting the value, but that isn't working. So below is my code, that alas, is not working. I experimented around using:
DatabaseSummaryInfoBuilder dpbuilder = new DatabaseSummaryInfoBuilder();
But can't see how to use it except for creating new custom properties.
public static void setDwgProp(Database acDb, string dwgprop, string propval)
{
IDictionaryEnumerator denum = acDb.SummaryInfo.CustomProperties;
using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
{
while (denum.MoveNext())
{
DictionaryEntry entry = denum.Entry;
if (entry.Key.ToString().ToUpper() == dwgprop.ToUpper())
{
entry.Value = propval;
}
}
acTrans.Commit();
}
}
Tangent question, how can I copy/paste my code so that it formats for easy reading?
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
Here're some extension methods
using System.Collections; using System.Collections.Generic; namespace Autodesk.AutoCAD.DatabaseServices { /// <summary> /// Provides extension methods for the Database type /// </summary> public static class DatabaseExtension { /// <summary> /// Gets the drawing custom properties. /// </summary> /// <param name="db">Database instance this method applies to.</param> /// <returns>A strongly typed dictionary containing the entries.</returns> public static Dictionary<string, string> GetCustomProperties(this Database db) { Dictionary<string, string> result = new Dictionary<string, string>(); IDictionaryEnumerator dictEnum = db.SummaryInfo.CustomProperties; while (dictEnum.MoveNext()) { DictionaryEntry entry = dictEnum.Entry; result.Add((string)entry.Key, (string)entry.Value); } return result; } /// <summary> /// Gets a drawing custom property. /// </summary> /// <param name="db">Database instance this method applies to.</param> /// <param name="key">Property key.</param> /// <returns>The property value or null if not found</returns> public static string GetCustomProperty(this Database db, string key) { DatabaseSummaryInfoBuilder sumInfo = new DatabaseSummaryInfoBuilder(db.SummaryInfo); IDictionary custProps = sumInfo.CustomPropertyTable; return (string)custProps[key]; } /// <summary> /// Sets a property value /// </summary> /// <param name="db">Database instance this method applies to.</param> /// <param name="key">Property key.</param> /// <param name="value">Property value.</param> public static void SetCustomProperty(this Database db, string key, string value) { DatabaseSummaryInfoBuilder infoBuilder = new DatabaseSummaryInfoBuilder(db.SummaryInfo); IDictionary custProps = infoBuilder.CustomPropertyTable; if (custProps.Contains(key)) custProps[key] = value; else custProps.Add(key, value); db.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo(); } } }
dennis a écrit :
Tangent question, how can I copy/paste my code so that it formats for easy reading?
When wrting your message, click on the "insert code" button [</>] and paste your code in the dialog box.
Thank you Giles. Solution looks simple now that you showed me. I did keep some of the code I wrote using the Enumerator because I wanted to avoid the uppercase/lowercase problem.
public static void setDwgProp(Database acDb, string propkey, string propval) { IDictionaryEnumerator denum = acDb.SummaryInfo.CustomProperties; while (denum.MoveNext()) { DictionaryEntry entry = denum.Entry; if (entry.Key.ToString().ToUpper() == propkey.ToUpper()) { DatabaseSummaryInfoBuilder dpbuilder = new DatabaseSummaryInfoBuilder(acDb.SummaryInfo); IDictionary customProps = dpbuilder.CustomPropertyTable; if (customProps.Contains(entry.Key)) { customProps[entry.Key] = propval; } acDb.SummaryInfo = dpbuilder.ToDatabaseSummaryInfo(); } } }