9 Replies Latest reply on Sep 8, 2016 11:08 PM by Artem Taturevych

    SwDMConfigurationMgr GetConfigurationByName Fails

    Andrew Johnson

      OS: Windows 10

      SolidWorks Version: SolidWorks 2016 SP4

      IDE / Language: Visual Studio 2015 / C#

       

      I have some code simply reading configuration-specific custom properties using the Document Manager API. I first get the document's SwDMConfigurationMgr object, then get the configuration names. Now I want to loop through the names and get the Configuration-Specific custom properties for each configuration. This is where I run into trouble.

       

      If I call GetConfigurationByName before my foreach loop, it works fine. If I call it within my foreach loop, it fails. Error message: "'object' does not contain a definition for 'GetConfigurationByName'". See screenshot.

       

      ConfigurationManagerError.png

        • Re: SwDMConfigurationMgr GetConfigurationByName Fails
          Andrew Johnson

          Update: If I change the foreach loop to a for loop, the problem goes away.. At least there is a workaround, but I definitely prefer foreach where possible.

           

          private static IEnumerable<SwCustomProperty> GetConfigurationCustomProperties(SwDMDocument20 swdmDocument)
                  {
                      var customProperties = new List<SwCustomProperty>();
          
          
                      var configurationManager = swdmDocument.ConfigurationManager;
                      var configurationNames = configurationManager.GetConfigurationNames() as string[];
                      if (configurationNames == null) return customProperties;
                      
                      for (var i = 0; i < configurationNames.Length; i++)
                      {
                          var configurationName = configurationNames[i];
                          var configuration = configurationManager.GetConfigurationByName(configurationName) as SwDMConfiguration14;
                          var propertyNames = configuration?.GetCustomPropertyNames() as string[];
                          if (propertyNames != null && propertyNames.Length > 0)
                          {
                              foreach (var propertyName in propertyNames)
                              {
                                  SwDmCustomInfoType type;
                                  string linkedTo;
                                  var resolvedValue = configuration.GetCustomPropertyValues(propertyName, out type, out linkedTo);
                                  var value = string.IsNullOrEmpty(linkedTo) ? resolvedValue : linkedTo;
          
          
                                  var newProperty = new SwCustomProperty(propertyName, value, resolvedValue, SwCustomPropertyTypeConverter.GetPropertyType(type));
                                  customProperties.Add(newProperty);
                              }
                          }
                      }
          
          
                      return customProperties;
                  }
          
          • Re: SwDMConfigurationMgr GetConfigurationByName Fails
            Andrew Johnson

            Thanks for all of your suggestions. I changed no code, but switched Embed Interop Types to False and that fixed the problem. I should have thought to try it before posting, it has caused me pain before.

             

            Just FYI, using var vs specifying a string explicitly has no effect on the compiled code. The type is inferred by the collection type, which is a string array. 'var' is a keyword, really just a shortcut in the IDE. If you hover over 'var' in Visual Studio you will see in the tooltip that it knows it is a string. This is different from 'dynamic', where type isn't determined until runtime. This can cause issues for sure.

             

            Thanks again!