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

Ruby c extension and Sketchup Objects

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 4 Posters 718 Views 4 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.
  • M Offline
    Marc477
    last edited by 7 May 2014, 16:38

    I created a ruby c extension and I know how to use primitive types (int arrays...) as parameter in the functions I created in c.

    But is there any way to use a Sketchup object in a parameter? (like an entity) How can I decode the VALUE on the c side? Do I need a struct that fits the EXACT implementation of the object? Can I find that implementation?

    Thank you

    1 Reply Last reply Reply Quote 0
    • M Offline
      Marc477
      last edited by 7 May 2014, 21:01

      Ok I found a way to achieve that. Here is an exemple with the Sketchup class Geom::Point3d

      static VALUE AddOne( VALUE self, VALUE p1 )
      {
          VALUE point;
          VALUE rbGeom, rbPoint3d;
          float x,y,z;
          
          //Convert to c float
          x = NUM2DBL(rb_funcall( p1, rb_intern( "x" ), 0 )) + 1;
          y = NUM2DBL(rb_funcall( p1, rb_intern( "y" ), 0 )) + 1;
          z = NUM2DBL(rb_funcall( p1, rb_intern( "z" ), 0 )) + 1;
          
          //Convert back to ruby VALUE
          rbGeom = rb_define_module( "Geom" );
          rbPoint3d = rb_define_class_under( rbGeom, "Point3d", rb_cObject );
          point = rb_funcall( rbPoint3d, rb_intern( "new" ), 3, rb_float_new(x), rb_float_new(y), rb_float_new(z) );
          return point;
      }
      

      This code takes a Point3d as parameter, adds one to each value and return the resulting Sketchup ruby object.

      1 Reply Last reply Reply Quote 0
      • T Offline
        tt_su
        last edited by 8 May 2014, 09:35

        You might want to make sure the user passed a Point3d object to your function.

        Chris Lalancette got a nice series that digs into creating Ruby C Extensions:
        http://clalance.blogspot.no/2011/01/writing-ruby-extensions-in-c-part-1.html

        We recently released Ruby C++ Extension examples that include Visual Studio and Xcode projects as well as the exact binaries we use for SketchUp:
        https://github.com/SketchUp/ruby-c-extension-examples

        1 Reply Last reply Reply Quote 0
        • A Offline
          Anton_S
          last edited by 19 Feb 2015, 21:26

          I find this point conversion useful, along with SketchUp Ruby C Extension / src / Example - Basics / SXBasics.c by Thom, which demonstrates conversion from SU Point3d to C.

          @tt_su said:

          You might want to make sure the user passed a Point3d object to your function.

          Is there a way to check if passed item is a Point3d object?

          Edit: I think it really doesn't matter because user may also pass their custom point3d classes or just an array. The rb_funcall( v_point, rb_intern("x"), 0 ) should do the job and raise an error if v_point doesn't respond to .x method.

          1 Reply Last reply Reply Quote 0
          • A Offline
            Anton_S
            last edited by 19 Feb 2015, 22:19

            Also, is SketchUp Length class handled well when it comes to converting it using NUM2DBL, or I should first convert it to float before passing to a c extension?

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 20 Feb 2015, 00:24

              @anton_s said:

              @tt_su said:

              You might want to make sure the user passed a Point3d object to your function.

              Is there a way to check if passed item is a Point3d object?

              Get a reference tot he Geom::Point3d class and invoke a call to is_a? or kind_of?.

              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 20 Feb 2015, 00:26

                @anton_s said:

                Also, is SketchUp Length class handled well when it comes to converting it using NUM2DBL, or I should first convert it to float before passing to a c extension?

                Yes. Prior to SU2015 Length was hacked to be a subclass of Float. Now - that's not really possible in Ruby, but it was still done. 😄
                In 64bit Ruby that broke and we had to change it, it is now a Numeric which forwards to Float. Because of duck typing NUM2DBL should work.

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

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Anton_S
                  last edited by 20 Feb 2015, 00:45

                  Thank you Thom.

                  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