Load Newton.dylib file via Ruby
-
Hi everyone, nice to meet you. My name is Joe and this is my first post. Hopefully someone can help me put this in the right place.
Currently the mac version of Sketchy Physics is broken so I've made it my task to bring it back as I need it for a physics project I'm working on involving magnetic forces.
I got the sourcecode to the newton physics library and compiled it into a .dylib after some trouble. This is the dynamic library equivlant to .DLL on the windows platform.
Now I'm trying to figure out how to load the functions into Ruby.
Looking at the previous sketchy physics tool, it appears the used a library called DL. I did some research into this and apparently there is a better way to do this now using (https://github.com/ffi/ffi) FFI.
I really like how the interface looks how FFI and it seems like it will be easy to use.
Only problem is, unlike the newton source code which compiles to a C-dynamic library, I'm not sure how to compile something like ffi or use it. I noticed the lib folder had some ruby files so I put it in my sketchup's plugin folder, and tried making a require statement to ffi.rb, but it appears it's missing ffi_c. I'm not sure what that is, I assume something I was supposed to have compiled.
I'm running Mac Os 10.8.2, with the latest version of xCode and latest version of Google SketchUp. Any help is appreciated if anyone has an idea how to make this work. Maybe DL is the only way to load external c library commands?
-
If anyone knows to load c-libraries at all in ruby via sketchup I'd be interested to know.
-
@x11joex11 said:
I'm running Mac Os 10.8.2, with the latest version of xCode and latest version of Google SketchUp. Any help is appreciated if anyone has an idea how to make this work. Maybe DL is the only way to load external c library commands?
I'm a bit confused because you talk about OSX and DLL files. I thought DLL files was entirely Windows domain. Or is the term used for OSX libraries as well?
(Btw, it's not Google SketchUp any more, it's Trimble SketchUp. If your SketchUp version still says Google then you need to update.)
-
@x11joex11 said:
If anyone knows to load c-libraries at all in ruby via sketchup I'd be interested to know.
If you write Ruby C Extensions you can load c libraries - making bridging functions between C and Ruby land. On Windows you get a .so file and under OSX you get a .bundle file.
-
I guess I should have been more clear. DL is NOT DLL. DL is a library apparently which helps you load dynamic files (either .DYLIB for mac or DLL for windows) (http://www.ruby-doc.org/stdlib-2.0/libdoc/dl/rdoc/DL.html), not sure how to use this either though, but I noticed SketchyPhysics used it. FFI is a library which apparently makes this even easier.
I do have the newest version of Sketchup, but I assumed it was still owned by Google since I downloaded it from there. But I do remember seeing Trimble. I will look into that to see what it is.
Is the only way to get Ruby C Extensions to write them myself in C? What do you think of the ruby library FFI?
-
@x11joex11 said:
I guess I should have been more clear.
DL
is NOT DLL.DL
is a library ...Yes.
DL
is Ruby bridge to using dynamic compiled libraries.
On PC, beforeDL
, we had to use theWin32API
library.FYI.. starting with Ruby v1.9.0, the Win32API.so compiled file was replaced with a Win32API.rb file, that converted the
Win32API
class into a backward compatible wrapper INTO theDL
library.FFI
, is supposed to become the successor toDL
.ALL of the above libraries either come with, or can be installed as Gems, into a FULL Ruby install.
SketchUp does not come with a Full Ruby install.
BUT you can install Full Ruby on PC (the Mac comes with some patchlevel of v1.8.7 Full Ruby,) and then push the paths to those full Ruby library directories, into SketchUp's
$LOAD_PATH
array.See my post: [ Code ] Ruby LOAD PATHs script (Win32) : ver 3.0.1 for ideas.
There is another topic (somewhere here,) with a link to a StackOverflow topic, that shows how to create a symbolic link, to load your OS edition of Ruby (rather than the obsolete bare-bone v1.8.5 Ruby interperter that comes with Mac SketchUp.)
-
Now all that said ...
Ruby's global require() method is capable of loading .rb, .o, .so, and .dll files.
I do not have a Mac, so I cannot test whether it can load .bundle or .dylib files.
Try it. (both with and without the filetype extension.)
@unknownuser said:
](http://www.ruby-doc.org/core-1.8.6/Kernel.html#method-i-require)":bpd1ecn6] If the file has the extension “.rb**”, it is loaded as a source file; if the extension is “.so”, “.o”, or “.dll”, or whatever the default shared library extension is on the current platform, Ruby loads the shared library as a Ruby extension.
-
It does load .bundle files.
-
Thanks for your answers in advance so far. Some more questions...
How would you go about including the newton dynamic library? What would the code look like to do this.
Could I just say...
require('newton.dylib')
Make sure it's in the same folder as my .rb plugin code and it should work?
Then the question would be, how do I let ruby know about the functions that exists in the dynamic library, especially if those functions were c-based functions. I vaguely remember declaring externs, but don't know the syntax.
As for loading .bundle files, I'm not entirely sure how I would compile the newton library into a .bundle, any ideas for this?
I can get it to compile to a .a (static library), and I had to do some tricks to get it to compile to a dynamic library (.dylib). Not sure something like that could be a .bundle, since .bundle is usually for applications?
-
Hi,
a couple of us have a ruby to switch Ruby Version from SU with and auto restart.
read this thread and let me know if you want to try 'our' ruby.
http://sketchucation.com/forums/viewtopic.php?f=180&t=34219#p301178
I had it loading gems at one stage, but only to play...john
-
Joe read the "Pick-Axe" Programming Ruby book to learn how Ruby works.
The CHM format is posted HERE in the "Ruby Resources" topic.
It is also online in HTML format HERE
-
Does the Google (Trimble) Sketchup Ruby Interpreter work the same as regular Ruby. If not, what differences are there?
-
@x11joex11 said:
Does the Google (Trimble) Sketchup Ruby Interpreter work the same as regular Ruby.
The interpreter DLL (or framework on the Mac,) IS the regular Ruby interpreter
@x11joex11 said:
..., what differences are there?
Under SketchUp, Ruby runs in SketchUp's process as an embedded command interpreter, meaning that ALL plugin code shares this ONE Ruby session.
In normal Ruby, scripts are usually run from the OS shell, which associates them with ruby.exe (a loader for the interpreter,) which runs the script within it's OWN Ruby session. This session exits when the script is done running.
So in Ruby that runs embedded as a scripting engine (under SketchUp, DoubleCAD, etc.,) all code needs to run within a uniquely named company or author namespace (a toplevel Ruby module,) so that plugins do not interfere with one another.
Also in a shared Ruby session, plugins and authors should NOT modify any Ruby base classes/modules, and MUST not modify the SketchUp API (per the Terms of Use,) in any code that is distributed.
-
@dan rathbun said:
Yes. DL is Ruby bridge to using dynamic compiled libraries.
On PC, before DL, we had to use the Win32API library.Interesting topic
What about the win32-api 1.4.8 by Daniel J. Berger, Park Heesob?
In dl you load the dll file once and then extern as many functions.
<span class="syntaxdefault">dlload</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_support_file</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"NewtonServer3.dll"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"plugins\\SketchyPhysics3\\"</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">extern </span><span class="syntaxstring">"void init()"<br /></span><span class="syntaxdefault">extern </span><span class="syntaxstring">"void stop()"<br /></span><span class="syntaxdefault">extern </span><span class="syntaxstring">"void update(int)"<br /></span><span class="syntaxcomment">#...<br /></span><span class="syntaxdefault"> </span>
But, in win32api.so and win32-api you load the library every time you create a function, right?
<span class="syntaxdefault">api </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> AMS</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Win32</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">API<br />newton </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">join</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dir</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'lib/Win32/newton.dll'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> <br />Create </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> api</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">'NewtonCreate'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'PP'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'P'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> newton</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Destroy </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> api</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">'NewtonDestroy'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'P'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'V'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> newton</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Update </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> api</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">'Update'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'P'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'V'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> newton</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span>
Am I thinking right? Which one is better dl or win32-api?
Advertisement