sketchucation logo sketchucation
    • Login
    ⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

    Load Newton.dylib file via Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 1.1k 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

      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
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @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
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          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
          • thomthomT Offline
            thomthom
            last edited by

            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

              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

                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
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  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

                    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
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @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

                        @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