I’m trying to automate DXF import using ruby. The plan is to import a DXF and pull the imported geometry to a predefined thickness. The DXF I am importing is exported from my PCB software.
I can manually import the DXF using file>import without issue. If I use my script I get an error.
I think the problem is most likely with how you are passing the path to the file. I’m on a Mac, so I had to edit your code for where I downloaded your file, but then it ran and loaded without issue.
When I open your original file in the Sublime text 2 editor, it appears as follows:
For my configurations, a simple text string should be all colored red. The green characters are back slashed escape characters. Ruby uses forward slash (i.e. division symbol) for directory paths, not back slashes for all operating systems. Sketchup for Windows has its version of Ruby “tolerate” backslashes somewhat. The culprit may be the “\n”, which is a newline character.
I used backslashes to my download folder on your DXF, and I see the same error. I can make it work by changing the double quote to single quote. I can also make it work if changed the back slashes to forward slashes.
Your new changes though are now Mac compatible, and have no hard coded paths. (You are not handling the situation if the user selects the cancel button from the open dialog box though).
On Windows, Sketchup’s “openpanel” method does return paths with back slashes, but they are all “escaped” correctly.
“C:\Users\ed\Desktop\DemoPCB\NoNest.dxf”
will fail as the single \ is read as an escape character, not a \ path separator
“C:\\Users\\ed\\Desktop\\DemoPCB\\NoNest.dxf”
with "double " will work as they are then read as \ path separators
‘C:\Users\ed\Desktop\DemoPCB\NoNest.dxf’
with single quotes will work without escaping
“C:/Users/ed/Desktop/DemoPCB/NoNest.dxf”
and ‘C:/Users/ed/Desktop/DemoPCB/NoNest.dxf’
will work as the / is taken as the path separators
MAC’s always expect / path separators
PCs accept / or \ path separators [some VBS etc expect ]
Both types work in Ruby, provided any \ inside " " are properly escaped as \
I think it’s best to avoid anything but /
I would always use something to make sure there are no \ characters in any returned path. path = “C:\Users\ed\Desktop\DemoPCB\NoNest.dxf” path.tr!("\", “/”) path >>> “C:/Users/ed/Desktop/DemoPCB/NoNest.dxf”
I see now what I was doing wrong. I have not done a lot of coding in the past (a bit of c for embedded and C# for desktop) and I am very new to Ruby.
This project is my first real effort at coding with Ruby so I am at the bottom of the learning curve.
I could do with following a couple more tutorials.
Now I can import my DXF I don’t know what to do with it.
I need to work out how to add a face and pull to thickness.
Are there any more tutorials no the Ruby API? I have followed the Creating Geometry tutorial.
To fill in the face, take any of the imported edges and invoke the Edge#find_faces method on it. Then use Face#pushpull on the resulting Face. One point to be aware of: check the direction of the normal to the Face to make sure pushpull will extrude it in the direction you want. Positive argument to pushpull extrudes in the positive direction of the Face’s normal.