C newbies question

Hi all,

I trying to migrate slowing , step by step to C coding as a 100% newbies !

I download the Bugra’s sample from github https://github.com/SketchUp/ruby-c-extension-examples

I adapt it for Xcode 6.0.1 by changing the architecture (i’m very proud of that ), compile it and run successfully it on Sketchup 2014 !
Wooooooohoooo.

I read some book about C++ programming and I feel pretty close to made my first C plugin.

Now my question is very simple from a newbies:
Is there a way to call Sketchup ruby api from C ? ( eg add_face , entities, …)
or Should I take informations from ruby, send it to C, treat them in C and send it back to ruby to interact with the SU Model ?

I’m not sure to be very clear but don’t hesitate to ask question.

Thanks !

Let have some fun calling C! Lets use funcall! :smiley: See what I did right there? :stuck_out_tongue: (Sorry - still having release euphoria from the SU2015 launch)

VALUE CreateRubyPoint(double x, double y, double z) {
  // Get handle to the Geom:::Point3d class
  VALUE Geom = rb_const_get(rb_cObject, rb_intern("Geom"));
  VALUE Geom_Point3d = rb_const_get(Geom, rb_intern("Point3d"));
  // Then create new instance.
  ID id_new  = rb_intern("new"); # EDIT: Better to use rb_class_new_instance
  return rb_funcall(Geom_Point3d, id_new, 3, DBL2NUM(x), DBL2NUM(y), DBL2NUM(z));
}

VALUE GetActiveEntities() {
  VALUE Sketchup = rb_const_get(rb_cObject, rb_intern("Sketchup"));
  VALUE model = rb_funcall(Sketchup , rb_intern("active_model"), 0);
  return rb_funcall(model, rb_intern("active_entities"), 0);
}

void AddEdge() {
  VALUE point1 = CreateRubyPoint(2, 3, 0);
  VALUE point2 = CreateRubyPoint(5, 8, 2);
  VALUE entities = GetActiveEntities();
  VALUE edge = rb_funcall(entities, rb_intern("add_line"), 1, point1, point2);
}

Now - this code example is just proof on concept. I’m omitting error checking etc so don’t consider it best practice or anything.

In my own extension I create C++ wrapper classes on top of the Ruby C API which allows me to interact with Ruby and the SketchUp Ruby API in a more object oriented manner similar to how I write the code in Ruby.

2 Likes

It‘s interesting, But how to get the instance of Geom::Pointed?" ID = rb_intern(“new”) " can’t work

That line should be …

ID id_new = rb_intern("new");

The Geom::Point3d object is returned by the CreateRubyPoint() function in Thomas’ example.


See the book:

Extending Ruby 1.9: Writing Extensions in C” by Dave Thomas

thanks,but I use “ rb_class_new_instance” rather than “id_new = rb_intern(“new”)”,now it can work well,I think that the way of ext_ruby is cool, but there is too little information here. :grinning:

(1) This is just a rudimentary example.

(2) The example is 3 and 1/2 years old !

(3) It is not really Trimble’s (nor their employees) responsibility to teach anyone C programming or Ruby C extensions. So we should be thanking Thomas for taking the time to write up this simple example.

(4) They have since then begun posting better examples on their GitHub site:

The best resource I’ve found so far is this site: The Ruby C API
I use that all the time for reference when doing C Ruby.

1 Like