I'm using the ModelDoc2.SelectionManager to obtain the currently selected component in an assembly. With that I get the ModelDoc2 of the selected component, then the ModelDocExtension, and finally the CustomPropertyManager from that. I use the CustomPropertyManager to query some of the custom properties and then use it to add a new one. The properties I query are all correct for the selected part so I know I am working with the correct component. I use the CustomPropertyManager's Add2 function to add a new property and it returns a 1 implying it was added successfully. If I click the More Properties button to view the component's custom properties, I see my newly added property in the list. From the File Menu I select Save All & then close the assembly. I immediately open the same assembly back up, select the same component, view its custom properties, and the new property I just added is no longer listed!
Anyone have any ideas what the problem is?
Thanks!
Below is the code I'm calling. I use the same code for dealing with both Part files and Assemblies. It works fine for Part files (AsmMode=false), but behaves as described above for Assemblies. In this case my parameters have the following values: CustPropName="TEST_PROPERTY", CustPropVal="true", AsmMode=true, swMdlDoc=null
static public bool SetCustomProperty(string CustPropName, string CustPropVal, bool AsmMode = false, ModelDoc2 swMdlDoc = null)
{
try
{
if (swMdlDoc == null)
{
swMdlDoc = (ModelDoc2)iSwApp.ActiveDoc;
}
CustomPropertyManager swPropMgr;
if (AsmMode)
{
// Need to obtain the active feature's ModelDoc SelectionManager
SelectionMgr swSelMgr = (SelectionMgr)swMdlDoc.SelectionManager;
int cnt = swSelMgr.GetSelectedObjectCount2(-1);
if (cnt > 1)
{
MessageBox.Show("Please ensure that only the object you are attempting to set Custom Property data for is currently selected.",
"Multiple Objects Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (cnt == 0)
{
ModelDocExtension swMdlExt = swMdlDoc.Extension;
swPropMgr = (CustomPropertyManager)swMdlExt.CustomPropertyManager[""];
}
else
{
Component2 swSelComp = (Component2)swSelMgr.GetSelectedObjectsComponent3(1, -1);
ModelDoc2 swSelMdlDoc = (ModelDoc2)swSelComp.GetModelDoc2();
ModelDocExtension swSelMdlExt = swSelMdlDoc.Extension;
swPropMgr = (CustomPropertyManager)swSelMdlExt.CustomPropertyManager[""];
}
}
else
{
ModelDocExtension swMdlExt = swMdlDoc.Extension;
swPropMgr = (CustomPropertyManager)swMdlExt.CustomPropertyManager[""];
}
bool PropertyExists = false;
int WasAdded = 0;
object[] nms = (object[])swPropMgr.GetNames();
foreach(string nm in nms)
{
if (string.Compare(nm, CustPropName, true) == 0)
{
PropertyExists = true;
break;
}
}
DocumentEventHandler.In_BK_ITEM_DESC_Reset = true;
if (PropertyExists)
{
swPropMgr.Set(CustPropName, CustPropVal);
}
else
{
WasAdded = swPropMgr.Add2(CustPropName, (int)swCustomInfoType_e.swCustomInfoText, CustPropVal);
}
DocumentEventHandler.In_BK_ITEM_DESC_Reset = false;
return WasAdded == 0 ? false : true;
}
catch(Exception)
{
return false;
}
}
SolidworksApi macros