Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Sorting XML file


Recommended Posts

Sorry I posted this in the wrong section. It should have gone in the regular post section.  I do want to manipulate the XML from within the post.  I have always had a hard time wrapping my head around creating NEThooks even though I use VB.net recreationally.  I need to force myself to work with it I guess. Thanks for the response.

Link to comment
Share on other sites

You can launch a NET-Hook from within a PST.

 

For manipulating XML data, .NET is the easy way to go.

 

This (C#) method reorders the <record> items within each <record_block> by the Material Thickness.

You can find examples of the data this works with in ->

C:\Users\Public\Documents\shared mcamx9\wire\Power

Those .TECH files you see there are XML data.

 

You can use the free Visual Studio Community Edition to build NET-Hooks ->

 
bool ReorderByMaterialThickness(string inFile, string outFile)
    {
        bool result = false;
        try
        {
            XDocument document = XDocument.Load(inFile);

            foreach (var rb in document.Descendants("record_block"))
            {
                // A LINQ statement to select and sort the record items in the TECH (XML) data file.
                var records = from record in rb.Elements("record")
                    let t = Convert.ToDouble(record.Element("thickness").Value)
                    orderby (t)
                    select record;

                int cnt = 1;
                foreach (XElement rec in records)
                {
                    // reset the num attribute on the record
                    rec.Attribute("num").Value = (cnt++).ToString();

                    // We've done something, so assume that it's good. ;)
                    result = true;
                }

                // We need the reordered data to be an Array format.
                var reordered = records.ToArray();

                // Reove the old records.
                rb.Elements("record").Remove();

                // And repalce them with the new reordered ones.
                rb.Add(reordered);
            }

            document.Save(outFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "ReorderByMaterialThickness");
        }

        return result;
    }
Link to comment
Share on other sites

Thanks for sharing the idea Roger,

 

However, the code above falls in the 'catch (Exception ex)' section and returns  "Input string was not in a correct format." in at least two large .TECH files in that folder...

 

Makino (SP43,SP64).tech

Makino DUO-Ver6-INCH-V Guide.TECH

 

Cheers,

Link to comment
Share on other sites

Indeed it does fail when attempting to process a Makino TECH file.

There are some thickness values in those Makino TECH files that fail this 'convert text to double' -

let t = Convert.ToDouble(record.Element("thickness").Value)

So... if wish wish to process those files, we'll need some additional logic it determine the thickness value(s).

 

Try it on one the Mitsubishi named TECH files and it works. [e.g. Mitsubishi (FA-S).tech]

*You may not see any difference the _OUT file, as most (if not all) of the TECH files are already ordered by increasing material 'thickness'.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...