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

Roof creation

Anonymous
Not applicable
Hi All,

Do you have any sample code how to create a roof from a set of points on a single plane?

Thanks,
Andor
2 REPLIES 2
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Hi Andor,

I wrote a short sample code for you:
// -----------------------------------------------------------------------------  
// Create a Single-plane Roof  
// -----------------------------------------------------------------------------  
void	Do_CreatePlaneRoof (API_Coord centerPoint)  
{  
	API_Element element;  
	BNZeroMemory (&element, sizeof (API_Element));  
	element.header.typeID = API_RoofID;  
	element.roof.roofClass = API_PlaneRoofID;  
	GSErrCode err = ACAPI_Element_GetDefaults (&element, NULL);  
	if (err != NoError) {  
		ErrorBeep ("ACAPI_Element_GetDefaults (roof)", err);  
		return;  
	}  
  
	API_ElementMemo memo;  
	BNZeroMemory (&memo, sizeof (API_ElementMemo));  
  
	// constructing planeRoof data  
	element.roof.u.planeRoof.poly.nCoords	= 8;  
	element.roof.u.planeRoof.poly.nSubPolys	= 2;  
	element.roof.u.planeRoof.poly.nArcs		= 2;  
  
	memo.coords = reinterpret_cast<API_Coord**> (BMAllocateHandle ((element.roof.u.planeRoof.poly.nCoords + 1) * sizeof (API_Coord), ALLOCATE_CLEAR, 0));  
	memo.pends = reinterpret_cast<Int32**> (BMAllocateHandle ((element.roof.u.planeRoof.poly.nSubPolys + 1) * sizeof (Int32), ALLOCATE_CLEAR, 0));  
	memo.parcs = reinterpret_cast<API_PolyArc**> (BMAllocateHandle (element.roof.u.planeRoof.poly.nArcs * sizeof (API_PolyArc), ALLOCATE_CLEAR, 0));  
	if (memo.coords == NULL || memo.pends == NULL || memo.parcs == NULL) {  
		ErrorBeep ("Not enough memory to create roof polygon data", APIERR_MEMFULL);  
		ACAPI_DisposeElemMemoHdls (&memo);  
		return;  
	}  
  
	// contour points:  
	(*memo.coords)[1].x = centerPoint.x + 10.0;  
	(*memo.coords)[1].y = centerPoint.y + 10.0;  
	(*memo.coords)[2].x = centerPoint.x + 10.0;  
	(*memo.coords)[2].y = centerPoint.y - 10.0;  
	(*memo.coords)[3].x = centerPoint.x - 10.0;  
	(*memo.coords)[3].y = centerPoint.y - 10.0;  
	(*memo.coords)[4].x = centerPoint.x - 10.0;  
	(*memo.coords)[4].y = centerPoint.y + 10.0;  
	(*memo.coords)[5] = (*memo.coords)[1];  
	(*memo.pends)[1] = 5;  
	// hole points:  
	(*memo.coords)[6].x = centerPoint.x + 5.0;  
	(*memo.coords)[6].y = centerPoint.y + 0.0;  
	(*memo.coords)[7].x = centerPoint.x - 5.0;  
	(*memo.coords)[7].y = centerPoint.y + 0.0;  
	(*memo.coords)[8] = (*memo.coords)[6];  
	(*memo.pends)[2] = 8;  
	// arcs:  
	(*memo.parcs)[0].begIndex = 6;							// makes a circle-shaped hole  
	(*memo.parcs)[0].endIndex = 7;  
	(*memo.parcs)[0].arcAngle = PI;  
	(*memo.parcs)[1].begIndex = 7;  
	(*memo.parcs)[1].endIndex = 8;  
	(*memo.parcs)[1].arcAngle = PI;  
  
	// roof baseline  
	element.roof.u.planeRoof.baseLine.c1.x = (*memo.coords)[1].x;  
	element.roof.u.planeRoof.baseLine.c1.y = (*memo.coords)[1].y;  
	element.roof.u.planeRoof.baseLine.c2.x = (*memo.coords)[3].x;  
	element.roof.u.planeRoof.baseLine.c2.y = (*memo.coords)[3].y;  
  
	// roof angle  
	element.roof.u.planeRoof.angle = 5.0 * DEGRAD;  
  
	// create the roof element  
	err = ACAPI_Element_Create (&element, &memo);  
	if (err != NoError)  
		ErrorBeep ("ACAPI_Element_Create (roof)", err);  
  
	ACAPI_DisposeElemMemoHdls (&memo);  
  
	return;  
}		// Do_CreatePlaneRoof


Sorry, I couldn't test it today. But hope it works, tomorrow I'll check it.

Regards,
Tibor
Anonymous
Not applicable
Hi Tibor,

Thanks very much, really helpful.

Andor