What am I doing wrong - rectangles defined by 3d vectors?

rectangle

#1

I want to output rectangles but for some reason I am unable to - here is the crucial part of the code (The rest is part of an example found in the SDK so I’m sure it’s ok - the code works fine but it always shows a single rectangle when I drop the created model into my browser):

	// Create the face
	SUFaceRef face[2] = SU_INVALID;
	struct SUPoint3D vertices[4] = { { 0,   0,   0 },
	{ 100, 100, 0 },
	{ 0, 0, 0 },
	{ 200,   200,   100 } };
	SUFaceCreateSimple(face, vertices, 2);

	SUFaceCreateSimple(face+1, vertices+2, 2);
	// Add the face to the entities
	SUEntitiesAddFaces(entities, 2, face);

Any ideas what’s wrong with the above code - remember I want to add sequentially rectangles defined by 2 points in the 3d space.

I’ve also looked into SUPlane3D but I don’t seem to find any way to use such a structure then with a Section Plane which I obviously don’t need.

I’m really desperate now - even after removing all of the ‘draw’ calls (ie. All the code posted above) I still get a single rectangle. How is that possible?


#2
  1. The vertices would not be coplanar.

  2. If they were, they’d might produce a bowtie effect as you have the origin point as 2 of the vertices.

  3. Always check your objects after doing a geometry creation call before using the object later in code. Ie, you are not getting the return value from the create function call and testing it for errors.

Sorry, but you’d have to write your own function. The API call needs 1 point for every vertex of the face, and since rectangles have 4 edges, there must be 4 points passed in the points array, if you are to get a rectangular face.


#3

You are trying to create two faces out of only four vertices. Each face you try to create out of two vertices. Check the return value and you’ll see error codes. (Always check these). You need to provide all the vertices for the face loop - meaning you need at least 3.

Also, notice the warning for SUEntitiesAddFaces (http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/entities_8h.html#af644b4c5ce421c5e0c0db6c5c478e8c6)

NOTE: This function does not merge geometry, which will likely create an invalid SketchUp model. It is recommended to use SUGeometryInput instead which does correctly merge geometry.

The faces it creates doesn’t merge properly. We should probably put some compile time warnings to these functions to make it clearer they are bugged.

Look into SUGeometryInput instead for creating geometry in the C API. It ensures to merge geometry.


#4

I’m trying something like this:

#define e ,SUEntitiesFill(entities, a, 1)

SUGeometryInputRef a = SU_INVALID;
SUGeometryInputCreate(&a);

SUGeometryInputSetVertices(a, 2, vertices)e;

SUGeometryInputSetVertices(a, 2, vertices + 2)e;

However I get “Can’t insert empty component” in the browser sketchup free app.


#5

What are you doing with the #define there? Calling SUEntitiesFill after each SUGeometryInputSetVertices?

Looks like you are trying to call SUEntitiesFill for each face you are trying to create. (Again after setting only 2 vertices at a time. Note my comment earlier, you need at least 3 points to define a face. And you need 4 points to define a rectangle.)

With SUEntitiesFill you call it once, after you built all your geometry using SUGeometryInputRef.

Make sure you check the return values from your calls. They give you information when you have provided incorrect input. That will give you clues.

Without seeing the whole code I cannot say exactly why you get that message about inserting empty component. But from the snipped it looks like your calls are failing - hence not generating any geometry.

Note that we have examples in the SDK package. Have you looked at those?


#6

Well here is my code and obviously no function fails:

#include <setjmp.h>


#include <stdbool.h>
#include <Windows.h>
#include <dbghelp.h>

#include <SketchUpAPI/defs.h>


#include <SketchUpAPI/common.h>
#include <SketchUpAPI/geometry.h>
#include <SketchUpAPI/initialize.h>
#include <SketchUpAPI/model/model.h>
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/face.h>
#include <SketchUpAPI/model/edge.h>
#include <SketchUpAPI/model/vertex.h>

#define cast(type, p) ((type) (p))
#define casta(type, p) *((type *) (p))

#define vX(a, ...) ((a)(__VA_ARGS__) ? longjmp(env, __LINE__) : 0)

int main() {

	jmp_buf env;

	int p;

	if (p = setjmp(env))
	{
		printf("%d\n", p);


			exit(-1);

	}


  // Always initialize the API before using it
	SUInitialize();
  // Create an empty model
  SUModelRef model = SU_INVALID;

  
  vX(SUModelCreate, &model);


  SUEntitiesRef entities = SU_INVALID;
  vX(SUModelGetEntities, model, &entities);
 

  struct SUPoint3D vertices[4] = { { 0,   0,   0 },
  { 100, 100, 0 },
  { 100, 100, 100 },
  { 0,   0,   100 } };


  SUGeometryInputRef a = SU_INVALID;
  vX(SUGeometryInputCreate,&a);

  vX(SUGeometryInputSetVertices, a, 2, vertices), vX(SUEntitiesFill, entities, a, 1);

  // Save the in-memory model to a file
  vX(SUModelSaveToFile,model, "new_model.skp");
  // Must release the model or there will be memory leaks
  vX(SUModelRelease,&model);
  // Always terminate the API when done using it
  SUTerminate();
  return 0;
}

I want to create a rectangle from 2 points - that’s why you suggested me to use Geometry Input but that doesn’t seem to work.


#7

That’s not what I meant. The API never allows you to create a face form 2 points. At all times you must provide all the points that makes up the face. If you want a face with four vertices, you must provide four points.