Ruby API "Error opening AutoCad import file"

Hello All,

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.

My code is:EDDXF.rb (196 Bytes)

And here is the DXF:
NoNest.dxf (73.5 KB)

Why can I not import the DXF using this script?

Huge Thanks Ed

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.


Hi,

Thanks for your reply!

I can’t see what I am doing wrong. I have tried different location for the file with no success.

I will try using UI.openpanel to pass the file location. This is what I intend to do in the long run.

I am new to ruby and a programming novice so it may take a little while.

Thanks Ed

Hi All!

Well…
That was easier than expected.
This code works:

UI.menu("Extensions").add_item("ED DXF"){    
    ed_DXF
    }

def ed_DXF        
    model = Sketchup.active_model
    dxfLocation = UI.openpanel("Open DXF", "","DXF|*.dxf||")
    model.import dxfLocation,true    
end

I don’t understand what I was doing wrong before.

Now onto adding a face and pulling into shape.

Thanks!

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.

Yup, Bruce is correct.

See the intro docs for literals. (Scroll down to the Strings section.)
[url]http://ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html[/url]

“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”

Hello All,

Thank you all for your input!

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.

I am going to have a look at the Example Files now.

Sorry for my noobie nature.

Thanks!

Ed

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.

Hi Steve,

Thanks for your pointers. I am quite lost so I have opened thread with wider scope.

Thanks again,

Ed