1 Reply Latest reply on Dec 21, 2018 8:55 AM by Dan Bovinich

    Sorting Materials in c#

    Jerome De San Nicolás

      Hi,

      I have seen some post complaining on the impossibility to sort the material database by name.

      As I'm learning the c# I've make a little code to sort the database that I want to share with you.

      It seems to work correctly but the process is kind of wanky. (you have to rename files extensions )

       

      Be sure to make a BACKUP before testing as I'm a noob developer. And to leave a feedback on what could I improve.

       

      Tanks

       

      Ps the code :

       

      using System.Linq;
      using System.Xml.Linq;
      
      
      namespace SortXlm
      {
      class Program
      {
      static void Main(string[] args)
      {
      //Open the xml doc
      XDocument doc = XDocument.Load("C:\\NAME OF THE BASE.XML");
      //you have to change the extention of the material batabase from .sldmat to .xml (MAKE BACKUP!)
      
      
      //sort the stuff
      SortByName(doc.Root);
      
      
      //save xml in another file
      doc.Save("C:\\NEW SORTED XML.xml");
      //you have to change the extention of the material batabase from .xml to .sldmat and put the file in the library directory
      }
      
      
      static void SortByName(XContainer container)
      
      {
      container.ReplaceNodes(
      from childEl in container.Elements()
      orderby childEl.Attributes("name").Select(a => a.Value).FirstOrDefault()
      select childEl
      );
      
      
      foreach (XElement childEl in container.Elements().Where(e => e.HasElements))
      {
      if (childEl.Name == "classification" || childEl.Name == "material")
      {
      SortByName(childEl);
      }
      }
      }
      }
      }
      
        • Re: Sorting Materials in c#
          Dan Bovinich

          I'm interested in this. File extension naming is easy.

           

          But I need the "rest of the story". What do I do with this code? (I have never written a macro or done API ~ whatever that is). Do I copy this text you wrote, put it in notepad and save it as a file with a macro extension? Then run the macro inside of SW?

           

          Thank you,

           

          Dan