cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
Archicad C++ API
About Archicad add-on development using the C++ API.
SOLVED!

Attribute index

poco2013
Mentor
I am trying to obtain the attribute index (Font) by typeID and name in the attribute header. ACAPI_Attribute_Search(...)

Can't figure out the code to set the name -- my 'C' is not so good.

The name can be set either by the attribute name[API_AttrNameLen] or *uniStringNamePtr;

What is the line code to set either --- attr.name = "Courier New" does not work nor attr.uniStringNamePtr = &uniString

pardon my ignorance of 'C'
Gerry

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

Accepted Solutions
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Here is an example for you (copied from Attribute_Test.cpp, part of the Attribute_Test example Add-On):
API_Attribute	attrib;
GSErrCode	err;

BNZeroMemory (&attrib, sizeof (API_Attribute));
attrib.header.typeID = API_LayerID;
strcpy (attrib.header.name, "2D Drafting - General");

err = ACAPI_Attribute_Search (&attrib.header);		/* search by name: index returned in header */
if (err != NoError) {
	WriteReport_Err ("Layer \"2D Drafting - General\" was not found", err);
	return;
}

But if you already have the name in a GS::UniString object, then I recommend you to search by using the uniStringNamePtr member:
short GetAttributeIndex (const API_AttrTypeID typeID, const GS::UniString& attributeName)
{
	API_Attribute	attrib;
	GSErrCode	err;

	BNZeroMemory (&attrib, sizeof (API_Attribute));
	attrib.header.typeID = typeID;
	attrib.header.uniStringNamePtr = &attributeName;

	err = ACAPI_Attribute_Search (&attrib.header);		/* search by unistring name: index returned in header */
	if (err != NoError)
		return -1;
	return attrib.header.index;
}

// You can simply call this function:
GetAttributeIndex (API_LayerID, "2D Drafting - General");

View solution in original post

3 REPLIES 3
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Here is an example for you (copied from Attribute_Test.cpp, part of the Attribute_Test example Add-On):
API_Attribute	attrib;
GSErrCode	err;

BNZeroMemory (&attrib, sizeof (API_Attribute));
attrib.header.typeID = API_LayerID;
strcpy (attrib.header.name, "2D Drafting - General");

err = ACAPI_Attribute_Search (&attrib.header);		/* search by name: index returned in header */
if (err != NoError) {
	WriteReport_Err ("Layer \"2D Drafting - General\" was not found", err);
	return;
}

But if you already have the name in a GS::UniString object, then I recommend you to search by using the uniStringNamePtr member:
short GetAttributeIndex (const API_AttrTypeID typeID, const GS::UniString& attributeName)
{
	API_Attribute	attrib;
	GSErrCode	err;

	BNZeroMemory (&attrib, sizeof (API_Attribute));
	attrib.header.typeID = typeID;
	attrib.header.uniStringNamePtr = &attributeName;

	err = ACAPI_Attribute_Search (&attrib.header);		/* search by unistring name: index returned in header */
	if (err != NoError)
		return -1;
	return attrib.header.index;
}

// You can simply call this function:
GetAttributeIndex (API_LayerID, "2D Drafting - General");
There is some nice example code bundled with the API that can be really when the documentation isn't completely clear. In this case, there is an example for ACAPI_Attribute_Search in the Attribute_Test project:
API_Attribute	attrib;
GSErrCode			err;

BNZeroMemory (&attrib, sizeof (API_Attribute));
attrib.header.typeID = API_LayerID;
strcpy (attrib.header.name, "2D Drafting - General");

err = ACAPI_Attribute_Search (&attrib.header);		/* search by name: index returned in header  
This particular case is looking up a layer, but the same principle applies for any attribute type.

Edit: I see Tibor beat me to it
Ralph Wessel BArch
Central Innovation
poco2013
Mentor
Thanks Ralph --That's what I thought -- But I was having trouble with the compiler. Tried it again this morning and all worked OK. Don't know what combination I must of been using late,late last night, but probably old eyes and bad fingers ?

Thanks again and I will compile and look at the attribute example.
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27