Hello all,
I have been trying to find a method to extract custom properties from the custom property bag.
I have been able to extract the property value, using the "GetProperty" and the associated "GetValue" method, this method works when I know exactly what property name I am looking for. What do you do when you dont know the propery name?
here is my code snippet..
dim myProps as acsmcustompropertybag = mysheet.getcustompropertybag
dim myProp as iacsmcustompropertyvalue = myprops.getproperty("SOMENAME")
dim myPV as string = myProp.GetValue
what I want to do is:
dim myProps as acsmcustompropertybag = mysheet.getcustompropertybag
dim myPN as string = ??????????.tostring
dim myProp as iacsmcustompropertyvalue = myprops.getproperty(myPN)
dim myPV as string = myProp.GetValue
Thanks for your help
Solved! Go to Solution.
Solved by adamkalajzich. Go to Solution.
You may want to look into "AcSmEnumProperty"
That is, something like:
Dim enumProp As AcSmEnumProperty=mysheet.GetCustomPropertyBag().GetPropertyEnumerator()
Dim propName as String
Dim propVal As AcSmCustomPropertyValue
Do
enumProp.Next(propName,propVal)
MessageBox.Show("PropName=" & propName & vbcrlf & "PropVal=" & propVal.ToString())
Loop Until (String.IsNullOrEmpty(propName)
Just some idea. I did not actually run the code, so it may or may not run (meaning need some change).
Try this blog post by Kean Walmsley.
Norman,
I came up with something like what you have posted, but I am unable to cast the propertyenumerator to myEnumComponent
any ideas:
Dim myEnumComponent As IAcSmEnumComponent = TryCast(myCustomPropertyBag.GetPropertyEnumerator, IAcSmEnumComponent)
Dim myComponent As IAcSmComponent = myEnumComponent.Next
Dim myPropName As String
Dim IterProp As IAcSmCustomPropertyValue
While myComponent IsNot Nothing
myPropName = myComponent.GetName
IterProp = myCustomPropertyBag.GetProperty(myPropName)
MsgBox("Prop Name: " + myPropName.ToString + ControlChars.CrLf + "Prop Value: " + IterProp.GetValue.ToString)
myComponent = myEnumComponent.Next
End While
Of course you are "unable to cast the propertyenumerator to myEnumComponent", because PropertyEnumerator IS NOT an IacSmEnumComponent, it is IAcSmEnumProperty.
That is,
Dim myEnumComponent As IAcSmEnumComponent = TryCast(myCustomPropertyBag.GetPropertyEnumerator, IAcSmEnumProperty)
Here is the code to show names of all custom properties (in C#):
using System; using System.Collections.Generic; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; using ACSMCOMPONENTS18Lib; [assembly: CommandClass(typeof(SheetSetAddInTest.MyCommands))] namespace SheetSetAddInTest { public class MyCommands { private const string SHEET_FILE = @"D:\Norm Documents\AutoCAD Sheet Sets\110900.dst"; [CommandMethod("ShCustProp")] public static void ShowSheetCustomProperties() { Document dwg = Application.DocumentManager.MdiActiveDocument; Editor ed = dwg.Editor; //Get SheetSet ACSMCOMPONENTS18Lib.AcSmSheetSetMgr ssmgr = new AcSmSheetSetMgr(); ACSMCOMPONENTS18Lib.IAcSmDatabase db = ssmgr.OpenDatabase(SHEET_FILE, true); ACSMCOMPONENTS18Lib.IAcSmSheetSet ss = db.GetSheetSet(); List<string> propNames = GetSheetSetCustomProperties(ss); foreach (string name in propNames) { ed.WriteMessage("\nCustom Property Name: {0}", name); } ed.WriteMessage("\n"); } #region private methods private static List<string> GetSheetSetCustomProperties( ACSMCOMPONENTS18Lib.IAcSmSheetSet sheetSet) { List<string> propNames = new List<string>(); IAcSmEnumProperty propEnum = sheetSet.GetCustomPropertyBag().GetPropertyEnumerator(); string propName; AcSmCustomPropertyValue propVal; do { propEnum.Next(out propName, out propVal); if (!String.IsNullOrEmpty(propName)) propNames.Add(propName); } while (!String.IsNullOrEmpty(propName)); return propNames; } #endregion } }
HTH
Thankyou norman,
your explination and the code example is greatly apreciated.
i must confess, that your origional explination should have resolved my problem but I was confused with the
propEnum.Next(out propName, out propVal);
and its use because no value was assigned to the propname / propval did not relise the function assigns values to them.
As a final question, where or what enumerator holds the custom property type. ie: sheet custom property or sheet set custom property.
I have a feeling its a boolean but have not found the associated reference in the custompropertybag object.
All,
Sorry for replying to myself. I have worked it out. I forgot that I previously worked out the last part (about sheet or sheet set) property about 2 years ago. Just looked up my previous code.
get/set (flag) options.
the resultant code is a fully working solution :
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim ed As Editor = dwg.Editor Dim myPropEnum As IAcSmEnumProperty = mySheetSet.GetCustomPropertyBag.GetPropertyEnumerator Dim myPropList As New List(Of String) Dim myPropName As String = Nothing Dim myPropValue As AcSmCustomPropertyValue = Nothing Dim myPropType As String = Nothing Do myPropEnum.Next(myPropName, myPropValue) If Not [String].IsNullOrEmpty(myPropName) Then myPropList.Add(myPropName + " = " + myPropValue.GetValue.ToString + "(" + myPropValue.GetFlags.ToString + ")") End If Loop While Not [String].IsNullOrEmpty(myPropName) For Each Prop As String In myPropList ed.WriteMessage(vbLf + "Custom Property Name = Value (type): " + Prop) Next ed.WriteMessage(vbLf)
I hope others will find this usefull.
Thanks again for your help.