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

Draw Text

poco2013
Mentor
Trying to draw some simple text at the origin. I'm guessing I need to use the function API_Element_Create() but I'm obviously doing something wrong as I get a compile message of 'no suitable conversion function from API_TextType to API_Element'

Can't seem to find a example in the Examples files. Here's the code I'm using -- Obviously wrong -- Any one have a example?
API_TextType element;
	BNZeroMemory(&element , sizeof(API_TextType));
	API_ElementMemo memo;
	BNZeroMemory(&memo, sizeof(API_ElementMemo));
	element.head.typeID = API_TextID;
	element.head.hasMemo = true;
	element.head.guid = APINULLGuid;
	element.loc.x = 0;
	element.loc.y = 0;
	element.font = 131;
	CHCopyC(*memo.textContent , "some text");
	ACAPI_Element_Create(&element, &memo);
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
You have to bother with paragraphs only if you want to create a text with multi style.
Here is a simple function to create text element (extracted from Communication_Client example Add-On):
static GSErrCode	CreateAText (const char* content, short pen, double size, double xPos, double yPos)
{
	API_Element element = {};
	element.header.typeID = API_TextID;

	GSErrCode err = ACAPI_Element_GetDefaults (&element, nullptr);
	if (err != NoError) {
		ErrorBeep ("ACAPI_Element_GetDefaults (text)", err);
		return err;
	}

	element.text.pen    = pen;
	element.text.size   = size;
	element.text.anchor = APIAnc_LT;
	element.text.just   = APIJust_Left;
	element.text.loc.x = xPos;
	element.text.loc.y = yPos;
	element.text.nonBreaking = true;
	element.text.nLine = 1;

	API_ElementMemo	memo = {};
	memo.textContent = BMAllocateHandle (Strlen32 (content) + 1, ALLOCATE_CLEAR, 0);

	strcpy (*memo.textContent, content);

	err = ACAPI_CallUndoableCommand ("Create text",
		[&] () -> GSErrCode {
			return ACAPI_Element_Create (&element, &memo);
		});
	if (err != NoError)
		ErrorBeep ("ACAPI_Element_Create (text)", err);

	ACAPI_DisposeElemMemoHdls (&memo);

	return err;
}		// CreateAText

View solution in original post

3 REPLIES 3
poco2013
Mentor
OK -- I found the example in Element_test under mutistyle text and found the paragraph stuff to be immensely confusing. I'd appreciate some direction for an explanation of paragraphs in the documentation -- if someone could direct me to that section. I could not find any pertinent documentation to explain paragraphs in the API?

In the mean time, I think I'll just stick to the non-associated label example for just one/lines of text.
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
You have to bother with paragraphs only if you want to create a text with multi style.
Here is a simple function to create text element (extracted from Communication_Client example Add-On):
static GSErrCode	CreateAText (const char* content, short pen, double size, double xPos, double yPos)
{
	API_Element element = {};
	element.header.typeID = API_TextID;

	GSErrCode err = ACAPI_Element_GetDefaults (&element, nullptr);
	if (err != NoError) {
		ErrorBeep ("ACAPI_Element_GetDefaults (text)", err);
		return err;
	}

	element.text.pen    = pen;
	element.text.size   = size;
	element.text.anchor = APIAnc_LT;
	element.text.just   = APIJust_Left;
	element.text.loc.x = xPos;
	element.text.loc.y = yPos;
	element.text.nonBreaking = true;
	element.text.nLine = 1;

	API_ElementMemo	memo = {};
	memo.textContent = BMAllocateHandle (Strlen32 (content) + 1, ALLOCATE_CLEAR, 0);

	strcpy (*memo.textContent, content);

	err = ACAPI_CallUndoableCommand ("Create text",
		[&] () -> GSErrCode {
			return ACAPI_Element_Create (&element, &memo);
		});
	if (err != NoError)
		ErrorBeep ("ACAPI_Element_Create (text)", err);

	ACAPI_DisposeElemMemoHdls (&memo);

	return err;
}		// CreateAText
Ralph Wessel
Mentor
Ákos also wrote a nice "Hello World" example on the API blog that shows how to place a text element: http://archicadapi.graphisoft.com/hello-world
Ralph Wessel BArch
Active Thread Ltd