• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Load Newton.dylib file via Ruby

Scheduled Pinned Locked Moved Developers' Forum
14 Posts 5 Posters 1.0k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    x11joex11
    last edited by 25 Mar 2013, 20:26

    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?

    1 Reply Last reply Reply Quote 0
    • X Offline
      x11joex11
      last edited by 26 Mar 2013, 00:35

      If anyone knows to load c-libraries at all in ruby via sketchup I'd be interested to know.

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 26 Mar 2013, 10:11

        @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.)

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 26 Mar 2013, 10:14

          @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.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • X Offline
            x11joex11
            last edited by 26 Mar 2013, 19:13

            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?

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 26 Mar 2013, 22:28

              @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, before DL, we had to use the Win32API 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 the DL library.

              FFI, is supposed to become the successor to DL.

              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.)

              💭

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 26 Mar 2013, 22:36

                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.

                💭

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 26 Mar 2013, 23:23

                  It does load .bundle files.

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • X Offline
                    x11joex11
                    last edited by 27 Mar 2013, 18:48

                    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?

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      driven
                      last edited by 27 Mar 2013, 23:54

                      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

                      learn from the mistakes of others, you may not live long enough to make them all yourself...

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        Dan Rathbun
                        last edited by 30 Mar 2013, 00:49

                        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

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • X Offline
                          x11joex11
                          last edited by 30 Mar 2013, 22:21

                          Does the Google (Trimble) Sketchup Ruby Interpreter work the same as regular Ruby. If not, what differences are there?

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            Dan Rathbun
                            last edited by 30 Mar 2013, 22:42

                            @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.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • A Offline
                              Anton_S
                              last edited by 21 May 2013, 07:28

                              @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?

                              1 Reply Last reply Reply Quote 0
                              • 1 / 1
                              • First post
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement