Using GeometryInputRef functions is messy - has anyone made it simpler?


#1

I am building a plugin that manipulates (quite a lot of) geometry through the C API. As I see it, the C API was originally designed for exporters and importers, and the “manipulation” side of things is not well supported.

It looks like when creating more complex geometry, you are really only left with using functions around GeometryInputRef. Now I have just understood how it works, and to me it is horrendous. A lot of complexity, and a lot of potential pitfalls. Just to copy a face from one model to another, I might have to write 30 lines of code. Messy. This is how far I got before I thought there must be another way:

size_t face_num = m_faces.size();
SUFaceRef faces_to_add[face_num];
SUGeometryInputRef geom_input = SU_INVALID;
for (size_t i = 0; i < face_num; ++i) {
SULoopInputRef outer_loop = SU_INVALID;
SULoopInputCreate(&outer_loop);
SULoopRef old_face_loop = SU_INVALID;
SUFaceGetOuterLoop(m_faces[i]->face, &old_face_loop);
size_t num_vertices = 0;
SULoopGetNumVertices(old_face_loop, &num_vertices);
SUVertexRef vertices[num_vertices];
SULoopGetVertices(old_face_loop, num_vertices, &vertices[0], &num_vertices);
for (size_t j = 0; j < num_vertices; ++j) {
// TODO: more work needed!
SULoopInputAddVertexIndex(old_face_loop, j);
} // Only a quarter of the way through. Give up.

I am of an Object Orientated Programming inclination (C++), and it looks like I will be able to create a wrapper class around GeometryInputRef to make the programmer’s task much easier.

Before I go ahead, I should ask if anyone has made such a wrapper already? Or maybe a suite of helper functions?


#2

This is something I’ve really wanted. I think it would be a great open source project.

I got some extensions where I need to use Ruby C Extensions. The Ruby C API is also cumbersome - so I added a C++ wrapper layer on top of that which now makes it a lot easier to write Ruby C extensions. I can write things very similar to how I do in Ruby - greatly speeding up development.

I got plans to write something using the SketchUp C API - and for that I wanted a C++ wrapper, exactly as what you describe.

I don’t know if anyone who have made a C++ wrapper yet, but there are C# wrappers. In fact, someone donated it to us which we plan to make open source. But things have been really busy recently so it’s been on the back burner.

In a few weeks time I might have some better time, maybe I can look at starting a base for such a wrapper - open source.


#3

Good to hear that I am not the only one thinking it!

Would be happy to help on an open source effort, since I will probably be doing some of the work anyway.

I’ve never collaborated on code before, I guess you’d use Github? I just need to get up to speed with git. When you have the time and start it off - get in touch, I’ll share what I have.

EDIT:
Just so I know we are on the same page, I was thinking that it could end up working something like this:

GeometryInput geom_input;
SUEntitiesRef entities; // This is the entities object that we wish to add the geometry to.
std::vector outer_loop{SUPoint3D(0.0,0.0,0.0), SUPoint3D(1.0,0.0,0.0), SUPoint3D(0.0,1.0,0.0)};
std::vector<std::vector> inner_loops{{SUPoint3D(0.1,0.1,0.0), SUPoint3D(0.1,0.2,0.0), SUPoint3D(0.2,0.3,0.0)}};
GIFace* face = geom_input.add_face(outer_loop, inner_loops); // GeometryInput handles the interface with Sketchup C API when creating geometry. The returned pointer to the object GIFace can be manipulated if you wish, as it is stored in the GeometryInput object.
SU_RESULT result = geom_input.output(entities); // the geometry must be output into a SUEntitiesRef object.

…generally trying to look like Ruby as much as possible.


#4

I thought that before I launch headlong into this, I should check that you approve of how I am starting. I’ve only been at C++ for a few months, and it would be reassuring to know that you and I are in agreement about the approach. Please see a header file for the Entity object attached:
Entity.txt (3.1 KB)
The approach is to basically do everything possible that the Ruby API does, in the same style as the Ruby API. I aim to use std libraries a lot instead of SUTypedValueRef and other silly types.


#5

Yea, GitHub would be a natural choice.

Yea, something like that.

If you want wait a couple of weeks - we can sync up better. Right now it’s really busy. For an open source project we should probably nail down a few general things. (Style guide, structure etc.)


#6

I’m just getting started learning c++ and the SketchUp C API - cumbersome is an understatement. Has there been progress getting these projects online?


#7

Not yet. Though I’m finally about to release SUbD v2. After that I can look into extracting the C++ wrapper on top of Ruby.

(@ChrisFullmer - just an FYI. Maybe we should make this wrapper available under the SketchUp account?)

And as for the C++ wrapper on top of the C API - we have has some progress there, thanks to @sal. But we still need some work before we feel we can release it publicly.)


#8

I did find @TommyK’s code repo here


#9

@jim_foltz - I am making an independent effort for a C++ wrapper here:

It is not complete yet. There are plenty of bugs, I am sure, and I am in the process of ironing them out. If anyone feels like helping me complete this, then I will be happy for you to get commit access.

No offence, @thomthom - I couldn’t wait around to develop my program any longer.


#10

Thanks, @TommyK. I have been studying-up on and wanted to implement something independently before looking at your code. When I did look, it turns out my wrapper approach is nearly identical to yours. I’ll give your wrapper a try.

I am mostly interested in getting geometry into SketchUp. I have been using assimp to this end with some success - it supports quite a few model formats.


#11

No problems, Jim. Do keep in touch, as now that I know that someone might be using it, I’ll know to be more descriptive with my changes, and make sure I only commit finished work!