Call C++ from Ruby in SketchUp

I’m trying to enable panel optimisation in my plugin and the code to do this properly is exceedingly complex so I’ve been trying to track down some code that already exists for this… I’ve have managed to track down a couple of c++ libraries that supposedly will do what i’m looking for. One in particular is…

http://optimalprograms.com/cut_2d_x.htm

So my question is… If this does work (i’ve sent an email to the company to enquire), can I run / access the c++ file / function from within ruby and obtain the results in ruby…?

would really appreciate it if anyone has any ideas on this? or in general the whole panel optimisation / 2d bin backing / 2d stock cutting problem thing???

Thanks

See this topic about using the Ruby Fiddle library to load external binary libraries:
http://forums.sketchup.com/t/export-dll-not-in-exporters-folder/14345/4)

2 Likes

Building a Ruby C extension is well suited for using C/C++ libraries. We have example projects for Visual Studio and Xcode matching the setup we use in-office: https://github.com/SketchUp/ruby-c-extension-examples

Especially if you are doing complex calculations you will also find that doing this in C/C++ will be many times faster than Ruby.

2 Likes

I’ve been trying to get that example (https://github.com/SketchUp/ruby-c-extension-examples) to work with the 64 bit version of SketchUp 2016. When I try to build a 64 bit version of the .so it throws a bunch of errors. And of course the 32 bit versions are giving me the following message in SketchUp:

Error: # LoadError: 193: %1 is not a valid Win32 application.

… which I understand is its way of saying I need a 64 bit .so file for the 64 bit version of SketchUp.

Is there any other examples that compile to a 64 bit .so file? I can’t for the life of me figure out why I can’t get this example to work in 64 bit.

For reference-- what I get when I try to compile the 64 bit version of the example using VS2015:

1>------ Build started: Project: SUEX_HelloWorld, Configuration: Release (2.0) x64 ------
1> SUEX_HelloWorld.cpp
1>C:\Users\User\Desktop\ruby-c-extension-examples-master\ThirdParty\include\ruby\2.0\win32_x64\x64-mswin64_100\ruby/config.h(4): fatal error C1189: #error: MSC version unmatch: _MSC_VER: 1600 is expected.
1> RubyUtils.cpp
1>C:\Users\User\Desktop\ruby-c-extension-examples-master\ThirdParty\include\ruby\2.0\win32_x64\x64-mswin64_100\ruby/config.h(4): fatal error C1189: #error: MSC version unmatch: _MSC_VER: 1600 is expected.
1> Generating Code…
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The version of Ruby included in the GitHub example predates VS2015, as you can tell by the _MSC_VER value expected by the ruby headers. I think we had manually upgraded those to VS2013 but have not done so for VS2015. If you can get it to compile, we’d love to see your changes be contributed back to the project.

Thanks!

Hm… after some hunting, it looks like I may have to stick with VS2013 for now. Apparently there is an outstanding bug where you can’t build Ruby with VS2015.

https://bugs.ruby-lang.org/issues/11118

1 Like

Thanks for the link. It makes sense since VS2015 changes the VC runtime DLL organization pretty drastically.

You can keep using VS2015 if you want as long as you don’t update the compiler for the project.

So if I understand, VS2013 can be used to compile this example. VS2015 can be configured as a front-end for the VS2013 build/compile/link tool chain. But since I never had VS2013 installed, I would need to install it so it is available to VS2015.

Not really a question I guess, just thinkin out loud. I’ll try it this evening.

Actually the example was compiled with VS2010, not VS2013.

So I went back to VS2013, but realized I had to rebuild Ruby 2.0 for VS2013 as the library in the example is for VS2010. I tried to do that in Windows 10… and it failed. Took the files over to a Windows 7 machine and was able to rebuild Ruby 2.0 for VS2013 there. Then took those libraries back to my Windows 10 machine to get it to work.

It finally did work. I’m almost back to getting some actual coding done. :wink:

Correct, the examples where originally VS2010. I got a branch with VS2013 tweaks, but I don’t think I pushed it back because I messed up something else in the configs.

The actual commit:


Relevant files:

  • Hello World/src/RubyUtils/RubyLib.h
  • ThirdParty/include/ruby/2.0/win32/i386-mswin32_100/ruby/config.h

For my personal projects I’ll be updating to VS2015 after the New Year, so I’ll see if I can do a new branch for VS2015.

Hi Thomthom,

did you already find some time to update to VS2015? It would be really great if you’d upload a new branch with the VS2015 version, or give me some hints what I’d have to do to get it compiling under VS2015 natively.

Thanks in advance!

Sebastian

From thomthom Visual Studio 2013 project, I was able to open and compile it under Visual Studio 2015.

I had to fix one error that came across by adding this preprocessor HAVE_STRUCT_TIMESPEC to each project.