Archicad C++ API
About Archicad add-on development using the C++ API.

Creating a mesh from scratch using the API

Anonymous
Not applicable
Hi. I'm supposed to write a function that generates a mesh starting from a (x,y,z) coord set. So far the correct usage of the mesh element's memo has eluded me. Could someone help me with a piece of souce code that correctly creates a mesh so that I might get some clues about it? Thank you!
2 REPLIES 2
Ralph Wessel
Mentor
Marius wrote:
Hi. I'm supposed to write a function that generates a mesh starting from a (x,y,z) coord set. So far the correct usage of the mesh element's memo has eluded me. Could someone help me with a piece of souce code that correctly creates a mesh so that I might get some clues about it? Thank you!
Have you looked at the documentation for ACAPI_Element_GetMemo and API_Polygon? Although it doesn't describe the contents of meshPolyZ, meshLevelCoords, and meshLevelEnds, their meaning echoes the equivalent structures for API_Polygon.
Ralph Wessel BArch
Software Engineer Speckle Systems
Anonymous
Not applicable
I too am trying to create a mesh from scratch. From what i can tell from the documentation this should be enough info to create a mesh? It is returning the APIERR_BADPOLY error. This same exact poly definition works when i create a polyline but not in a mesh. Heres my code.

BNZeroMemory(&element,sizeof(API_Element));
BNZeroMemory(&memo,sizeof(API_ElementMemo));

element.header.typeID = API_MeshID;
err = ACAPI_Element_GetDefaults (&element, &memo);
element.header.layer = 1;


element.mesh.poly.nArcs = 0; //no arcs
element.mesh.poly.nSubPolys = 1; //no holes
element.mesh.poly.nCoords = 5; //rectangle


BMAllocateHandle(sizeof(long) * (6), ALLOCATE_CLEAR, 0);

memo.meshPolyZ = (double **) BMAllocateHandle(sizeof(double) * 6, ALLOCATE_CLEAR, 0);

//allocate space for 4points + final point to close shape + space for storing max vertID = 5
memo.vertexIDs = (unsigned long **) BMAllocateHandle(sizeof(long) * (6), ALLOCATE_CLEAR, 0);
(*memo.vertexIDs)[0] = 4; //max vertexID

//allocate space for pends. no holes, so the 5th vert is the only endpoint.
memo.pends = (long **) BMAllocateHandle(sizeof(long) * 2, ALLOCATE_CLEAR, 0);
(*memo.pends)[0] = 0; //required
(*memo.pends)[1] = 5; //5th vert closes

//allocate space for the coord array. 5 vertices + the dummy .
memo.coords = (API_Coord **) BMAllocateHandle(sizeof(API_Coord) * 6, ALLOCATE_CLEAR, 0);
(*memo.coords)[0].x = -1.0; //polylines start with a "dummy" coord of -1,0
(*memo.coords)[0].y = 0.0;
(*memo.meshPolyZ)[0] = 0.0; //
//setup complete....add vertices.

//1st point:
(*memo.coords)[1].x = nHouseMinx;
(*memo.coords)[1].y = nHouseMiny;
(*memo.meshPolyZ)[1] = 2; //
(*memo.vertexIDs)[1] = 1;

//2nd point:
(*memo.coords)[2].x = nHouseMinx;
(*memo.coords)[2].y = nHouseMaxy;
(*memo.meshPolyZ)[2] = 1; //
(*memo.vertexIDs)[2] = 2;

//3rd point:
(*memo.coords)[3].x = nHouseMaxx;
(*memo.coords)[3].y = nHouseMaxy;
(*memo.meshPolyZ)[3] = 2; //
(*memo.vertexIDs)[3] = 3;

//4th point. Same as point 1.
(*memo.coords)[4].x = nHouseMaxx;
(*memo.coords)[4].y = nHouseMiny;
(*memo.meshPolyZ)[4] = 1; //
(*memo.vertexIDs)[4] = 4;

//4th point. Same as point 1.
(*memo.coords)[5].x = nHouseMinx;
(*memo.coords)[5].y = nHouseMiny;
(*memo.meshPolyZ)[5] = 1; //
(*memo.vertexIDs)[5] = 1;

err = ACAPI_Element_Create(&element,&memo);
if (err != NoError){
WriteReport (ErrID_To_Name(err));
}
//handle return code...
ACAPI_DisposeElemMemoHdls(&memo);

Thanks in advance.