6 Replies Latest reply on Aug 17, 2017 4:45 PM by Alex Burnett

    Getting material category

    M. Doga Dogan

      Based on this example, I would like to get the bodies' material categories.

       

      Debug.Print ("Body " & itr & "'s material name: " & sMatName)

      Debug.Print ("Body " & itr & "'s material database: " & sMatDB)

       

      With these commands I can get the name and database but not the category of the specific material. For example, for the selected material below, I'd like to get the category, which is "DIN Aluminum Alloys".

      Screenshot 2017-08-04 17.40.31.png

       

      Is that possible to do with the API?

       

      Thanks in advance!

        • Re: Getting material category
          Peter Kennedy

          The line:

           

          vMatDBarr = swApp.GetMaterialDatabases

           

          Doesn't that populate the data you require?

           

          • Re: Getting material category
            Alex Burnett

            There is a root element in the material database that has this listed as the part classification. The only way to access this is to open the material library (.sldmat) file and parse through it with an XML reader in order to gather the information you're looking for specifically.

             

            The following code was written by AngelSix and made public as a library (SolidDNA) to help make the solidworks API much easier to understand and use. His method of gathering this information is as follows:

             

            using (var stream = File.Open(database, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                // Try and parse the Xml
                                var xmlDoc = XDocument.Load(stream);
            
            
                                // Make sure we got something
                                if (xmlDoc == null)
                                    throw new ArgumentNullException(Localization.GetString("SolidWorksApplicationGetMaterialsXmlNotLoadedError"));
            
            
                                var materials = new List<Material>();
            
            
                                // Iterate all classification nodes and inside are the materials
                                xmlDoc.Root.Elements("classification")?.ToList()?.ForEach(f => 
                                {
                                    // Get classification name
                                    var classification = f.Attribute("name")?.Value;
            
            
                                    // Iterate all materials
                                    f.Elements("material").ToList().ForEach(material =>
                                    {
                                        // Add them to the list
                                        materials.Add(new Material
                                        {
                                            Database = database,
                                            DatabaseFileFound = true,
                                            Classification = classification,
                                            Name = material.Attribute("name")?.Value,
                                            Description = material.Attribute("description")?.Value,
                                        });
                                    });
                                });
            
            
                                // If we found any materials, add them
                                if (materials.Count > 0)
                                    list.AddRange(materials);
                            }
            

             

             

            He created a class called Material and populated it with the information from the SLDMAT database.