10 Replies Latest reply on Oct 31, 2016 12:31 PM by Rob Edwards

    Export Arc entity data to table?

    Justin Lee

      Lets say I have several arcs in a sketch and I want to select them all, then hit a button to produce a spread sheet with the arc radius, length & angle for each entity.

       

      Is there a script or something I can use to extract these data for use in a spreadsheet?

       

      I know cut lists are a thing, but not working with solid models here.  I just want to quickly output table data for multiple arcs in a sketch....

       

      I'm not of the coding/scripting sort, so hopefully someone can lend some guidance... seems like it'd be simple enough.

       

      Thanks

        • Re: Export Arc entity data to table?
          Rob Edwards

          I did something similar, but it wasn't easy.  I'm sure someone who could write code would be best at solving this, my solution was a bit antiquated.  I saved the sketch as an IGES file (this is readable in a spreadsheet but you will have a bit of noise)

           

           

          You will have to research the IGES format

          but all the data you need is in there.  This is the IGES file for the above sketch opened in notepad

          you'll just have to use some excel magic!!

           

          The tricky part is that the IGES format is so ancient the line length in the file is limited and the data you need spans two lines.  Even so I think it would be a lot easier programming job to write a script to parse an IGES file directly than a script that has to use the SW API directly

            • Re: Export Arc entity data to table?
              Justin Lee

              Thanks Rob, but not able to get there with your suggested IGES methodology.

               

              The values I'm looking for are not actually stored in the iges tables.  Rather, it looks like coordinate data for center points & end points.  I dont see the angle value, radius or length at all, and to derive those values from the IGES table would require... well.. derivatives or some other advanced maths, which may be beyond the capabilities of Excel to compute quickly or easily.

               

              So, sadly this is far from the solution i was looking for.  Anyone else have a good method for extracting the values I need (arc length, radius, & angle) from the IGES table data?

               

              Or maybe  there is a way to export data from the measure tool?

               

              Right now I manually select an arc, activate the measure tool and write the radius, length and angle on paper, then enter those values into our excel pricing table.  This methodology is laborious and prone to errors.. Just looking for a better/faster way.

               

              Is there a way to have the measure tool output to a file or anything like that?

               

              ¯\_(ツ)_/¯

               

              Thanks

                • Re: Export Arc entity data to table?
                  Rob Edwards

                  I know it wasn't the exact solution you were looking for- Im sorry but I promise you no advanced maths is required, just some basic trig.

                  If you look on the Solidwork Tricks thread (#198) you will find a spreadsheet I shared that does most of what you require.

                  The arc calculations tab, will show you the excel formulas for computing the radius and start and end angle relative to 3 O'Clock

                  It shouldnt be so hard to compute the arc length when you have these values,, although its got me thinking some care will be required to make sure you get the correct answer.  I made this spreadsheet a few years ago,,, I'd love to be able to write something akin to what you want for my own purposes, but unfortunately I'm a bit old in the tooth and fuzzy in the head to learn it all.

              • Re: Export Arc entity data to table?
                Justin Lee

                I just discovered that the measure tool has a history pop-out with a plain text log of every object measured.

                 

                So, with a combination of notepad++ and a little bash shell script (cygwin for windows works too) I was able to use the measure tool history to transmogrify the data I need into an excel table without TOO much hassle and zero fancy excel maths.

                 

                 

                -First edit the sketch with various arcs, open the measure tool history and be sure to clear any existing history before going any further:

                arc-values-1.jpg

                -Next, individually select, then deselect each arc entity in sequence, taking care not to select any non-arc entities.. do this right and the history will only contain entries that look like this:

                 

                arc-values-2.jpg

                 

                -Next copy and paste the entire measure history into notepad++.  Inspect each entry closely making sure no non-arc entities exist in the list. Any non-arc entries need to be removed manually or your data will not match up in the final spreadsheet.

                 

                -Once you're certain the list contains zero non-arc entries, you can use the notepad++ find/replace command (ctrl+h) to find both "in" & "deg" characters and replace them with an empty character.

                 

                arc-values-4.jpg

                 

                Do this right and each of your entries will look like this in notepad++:

                ===========================

                Arc Length: 4.33

                Chord Length: 4.24

                Radius: 6.00

                Angle: 41.35

                part1.SLDPRT

                ===========================

                If you nailed it, save as foo.txt

                 

                -Now we'll move to a bash shell (or cygwin on windows) for a little shell script to extract the values we need need.  In short-hand the script does the following..  Grep for "Rad" then awk/print column 2 & output to radius.txt;  Next, grep for "Arc" then awk/print column 3 & output to length.txt.  Finally, grep for "Ang" then awk/print column 2 & output to angle.txt.

                 

                [CODE:]

                #!/bin/sh radlenang.sh

                grep Rad foo.txt |awk '{print $2}' > radius.txt; grep Arc foo.txt |awk '{print $3}' > length.txt; grep Ang foo.txt |awk '{print $2}' > angle.txt

                [/CODE]

                 

                After running the script you'll have 3 new text files, open each one with notepad++ to double check they only contain numeric data & compare the values to those in foo.txt, the entries should match up.

                 

                If everything matches you just need to create a new spreadsheet and paste these values into their respective columns.

                arc-values-5.jpg

                 

                 

                Not too many steps here, and when working with dozens of arcs (as i do) this is definitely going to save me time & errors going forward.

                 

                Thanks & Enjoy!

                 

                Edit: Use notepad++ or another rich text editor.  Do not use windows "notepad.exe"  as it does not properly detect unix carriage returns and will display all lines on a single line... Yuk!

                  • Re: Export Arc entity data to table?
                    Rob Edwards

                    wicked! I like that.

                      • Re: Export Arc entity data to table?
                        Justin Lee

                        Cool!.. Thanks Rob.

                         

                        Although, I can hold my own in bash, I'm not not a giant shell script guru or anything.  So, there is probably a way to automate more of this job within the shell script itself, rather than relying on copy/past to notepad++, etc. 

                         

                        I'll leave that part up to someone else.. Lemme know if you can help!

                         

                        Thanks again!

                          • Re: Export Arc entity data to table?
                            Rob Edwards

                            Nice 1 Justin, I have a smattering of programming experience, I learnt a bit of C 20+ years ago, and I played with Ruby for a bit.  I'd love to learn more, there's just so much to learn.  Even with a little bit of skills I think it can still be powerful, especially now that you can just google stack overflow for instant advice.

                             

                            I really like your discovery of the history in measurements, that will come in useful to me.

                             

                            I've been toying with my idea a bit tonight and I've got it kind of working.

                             

                            With apologies to any real programmers I wrote the following ruby script.

                            I've not commented it, but I think you'll be able to follow.

                             

                            I adopt a file processing technique, where I first create a file of just the points, and then a file of the data you want

                             

                            #igesarcs.rb

                            #quick and dirty extract raw arc data from IGES file and calculate radius, angle and length

                             

                             

                            read_file_name = ARGV[0]

                            write_file_name = read_file_name.gsub('igs','arcpts')

                            arc_count = 0

                            p "opening #{read_file_name} and creating #{write_file_name}"

                             

                             

                            File.open(read_file_name, 'r') do |read_file|

                             

                            File.open(write_file_name,'w') do |write_file|

                             

                              write_file.puts 'x center, y center, x start, y start, x end, y end'

                             

                             

                              while current_line = read_file.gets

                               if current_line[0..2]=='100'

                                 arc_count += 1

                                 data = current_line.rpartition(',')[0][4..-1] + ',' + read_file.gets.rpartition(';')[0]

                                 write_file.puts data.split(',')[1..6].map{|x| x[0] == '.' ? x.prepend('0') : x[-1] == '.' ? x[0..-2] : x }.join(',')

                               end

                              end

                            end

                            p "found #{arc_count} arc#{arc_count == 1 ? '' : 's'}."

                            end

                             

                             

                            include Math

                             

                             

                            def get_angle(x,y)

                              a = (y == 0) ? PI/2 : atan(x.abs/y.abs)

                              case ((x < 0) ? 'L' : 'R') + ((y < 0) ? 'B' : 'T')

                                when "RT" then return a

                              when "RB" then return PI - a

                              when "LB" then return PI + a

                              when "LT" then return 2*PI - a        

                              end

                            end

                             

                             

                            read_file_name = write_file_name

                            write_file_name = write_file_name.gsub('arcpts','arcdata')

                             

                             

                            p "opening #{read_file_name} and creating #{write_file_name}"

                             

                             

                            File.open(read_file_name,'r') do |read_file|

                              p "file headers: " + read_file.gets.chomp

                             

                              File.open(write_file_name,'w') do |write_file|

                              write_file.puts 'radius, angle, length'

                             

                              while current_line = read_file.gets

                               xc , yc, xs, ys, xe, ye = current_line.chomp.split(',').map{|n| n.to_f}

                                  xs , ys , xe , ye = xs - xc , ys - yc , xe -xc , ye -yc

                                  average_radius = ((xs**2 + ys**2)**0.5 + (xe**2 + ye**2)**0.5) /2

                               start_angle, end_angle = get_angle(xs,ys), get_angle(xe,ye)

                               angle = (end_angle < start_angle) ? start_angle - end_angle : start_angle + (2*PI - end_angle)

                               write_file.puts [average_radius, angle*180/PI, angle*average_radius].join(',')

                                end

                              end

                            end 

                             

                             

                             

                             

                            for the above test sketch I got a good result

                             

                            radius, angle, length

                            159.99999999995396,222.53728899853922,621.4413442376874

                             

                            It's not fully tested but its looking promising

                             

                            One problem I know exists is that I dont parse the IGES file properly, I just read in two lines.  If the arc is centered at the origin the IGES file only needs 1 line to store the entity data, so it corrupts.  I'd have to develop a more sophisticated parser that reads the IGES Data Table correctly....  buts thats a job for another day

                              • Re: Export Arc entity data to table?
                                Justin Lee

                                This is awesome!

                                 

                                I haven't used Ruby before, so I guess I'll need to grab the SDK, & get started..

                                I can avoid placing centers at the origin pretty easily, so no worries there!

                                 

                                Looking like a busy week, but when I find a minute to test this out I'll report back..

                                 

                                Thanks again!

                                  • Re: Export Arc entity data to table?
                                    Rob Edwards

                                    I use Ruby because before I got Solidworks I was using Sketchup and that is the supported language.  I really like it as its free and (at the basic level) easy.  I think any language you are comfortable with would do.  It would be best if you could run it directly from SW but thats beyond me at the moment .. maybe next year or the year after lol.  I've got a busy week coming up too, but the forums more fun 

                                    It should be easy enough to read the IGES file correctly.  When I first did this I just guessed, but I have since read the IGES file specification here IGES file Specification - Eclipsepedia

                                    The separators are specified in the Global section of the file  (SW seems to use , for data separator and ; for end line)

                                    There are Data Entry and Parameter Data Sections.  They all link to each other through line numbers (on the right)

                                    The Data Entry table will tell you how many lines of data there are, but I don't think you need that, just look for a ;

                                    All the best any questions I'd be happy to help.