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.
Let have some fun calling C! Lets use funcall! See what I did right there? (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.
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.
(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: