cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
2024 Technology Preview Program

2024 Technology Preview Program:
Master powerful new features and shape the latest BIM-enabled innovations

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

API Database_control example Error

Anonymous
Not applicable
I am practicing database control to practice the addon which outputs element attribute value to excel file in Archicad. (Static void Do_ExportWallsToExcel (void) function )There is problem that it is not saved in the place where Excel file is saved in executable file.

static void	Do_ExportWallsToExcel (void)
{
	DBPrintf ("Exporting walls to Excel document...\n");

	libxl::Book* book = xlCreateXMLBook ();
	libxl::Sheet* sheet = book->addSheet (UNISTR_TO_LIBXLSTR (GS::UniString("Walls exported from Archicad")));

	libxl::Font* guidFormatFont = book->addFont ();
	guidFormatFont->setSize (6);
	guidFormatFont->setColor (libxl::COLOR_GRAY50);
	libxl::Format* guidFormat = book->addFormat ();
	guidFormat->setFont (guidFormatFont);

	GS::Array<GS::UniString> titles;
	titles.Push ("GUID");
	titles.Push ("Height");
	titles.Push ("BeginCoordX");
	titles.Push ("BeginCoordY");
	titles.Push ("EndCoordX");
	titles.Push ("EndCoordY");

	for (UIndex i = 0; i < titles.GetSize (); ++i) {
		sheet->writeStr (0, i, UNISTR_TO_LIBXLSTR (titles));
	}

	GS::Array<API_Guid> apiguids;
	ACAPI_Element_GetElemList (API_WallID, &apiguids);
	for (UIndex i = 0; i < apiguids.GetSize (); ++i) {
		GS::Guid gsguid = APIGuid2GSGuid (apiguids);

		API_Element element = {};
		element.header.guid = apiguids;
		GSErrCode err = ACAPI_Element_Get (&element);
		if (err != NoError) {
			DBPrintf ("ACAPI_Element_Get returned error (%d) for GUID '%s'\n", err,
					  gsguid.ToUniString ().ToCStr ().Get ());
			continue;
		}

		API_WallType& wall = element.wall;
		//DBPrintf ("GUID '%s'\n", gsguid.ToUniString ().ToCStr ().Get ());

		sheet->writeStr (i + 1, 0, UNISTR_TO_LIBXLSTR (gsguid.ToUniString ()), guidFormat);
		sheet->writeNum (i + 1, 1, wall.height);
		sheet->writeNum (i + 1, 2, wall.begC.x);
		sheet->writeNum (i + 1, 3, wall.begC.y);
		sheet->writeNum (i + 1, 4, wall.endC.x);
		sheet->writeNum (i + 1, 5, wall.endC.y);
	}

	IO::Location location;
	IO::fileSystem.GetSpecialLocation (IO::FileSystem::UserDocuments, &location);
	
	location.AppendToLocal (IO::Name ("export.xlsx"));
	GS::UniString filepath;
	location.ToPath (&filepath);
	
	DBASSERT (book->save (UNISTR_TO_LIBXLSTR(filepath)));
	book->release ();

	DBPrintf ("Export operation finished\n");
}
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Hi,

This example code saves the excel file into the user's documentation folder. Of course you can configure it as you want.

This part of the code gets the documents folder and sets the excel filename to "export.xlsx":
	IO::Location location;
	IO::fileSystem.GetSpecialLocation (IO::FileSystem::UserDocuments, &location);

	location.AppendToLocal (IO::Name ("export.xlsx"));
Don't you find the "export.xlsx" file in you documentation folder after executing this operation?

View solution in original post

6 REPLIES 6
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Hi,

This example code saves the excel file into the user's documentation folder. Of course you can configure it as you want.

This part of the code gets the documents folder and sets the excel filename to "export.xlsx":
	IO::Location location;
	IO::fileSystem.GetSpecialLocation (IO::FileSystem::UserDocuments, &location);

	location.AppendToLocal (IO::Name ("export.xlsx"));
Don't you find the "export.xlsx" file in you documentation folder after executing this operation?
Anonymous
Not applicable
before
DBASSERT (book->save (UNISTR_TO_LIBXLSTR(filepath)));
book->release ();

after
bool aaaa= book->save (UNISTR_TO_LIBXLSTR(filepath))
book->release();

After making the above changes, an Excel file was created in the Documents folder.
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Yes, you're absolutely right. The code inside DBASSERT runs only if you built your AddOn using Debug configuration.
park wrote:
DBASSERT (book->save (UNISTR_TO_LIBXLSTR(filepath)));
I'm glad that you solved the issue and you can continue the AddOn development!
Karl Ottenstein
Moderator
Tibor wrote:
Yes, you're absolutely right. The code inside DBASSERT runs only if you built your AddOn using Debug configuration.
park wrote:
DBASSERT (book->save (UNISTR_TO_LIBXLSTR(filepath)));
I'm glad that you solved the issue and you can continue the AddOn development!
This is an excellent reason for anyone posting here in the Developer Forum to make clear if they are experimenting with the API using ARCHICAD in Demo mode - or if they have an API license key and are testing in a production license.

A reminder to all - without a valid developer ID, your add-on can only operate in Demo mode - which has save, print and other features disabled ... including the normal creation of files as seen here. Mr Park's workaround for saving information while in demo mode is an intriguing tip that may help others. 😉
One of the forum moderators
AC 27 USA and earlier   •   macOS Ventura 13.6.7, MacBook Pro M2 Max 12CPU/30GPU cores, 32GB
Anonymous
Not applicable
I'm testing on valid ID and still had to use this trick. It works anyway
SajW
Participant
park wrote:
before
DBASSERT (book->save (UNISTR_TO_LIBXLSTR(filepath)));
book->release ();

after
bool aaaa= book->save (UNISTR_TO_LIBXLSTR(filepath))
book->release();

After making the above changes, an Excel file was created in the Documents folder.
I get the following errors when the change mentioned is made, any suggestions why?

Severity Code Description Project File Line Suppression State
Warning C4189 'aaaa': local variable is initialized but not referenced Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Examples\Database_Control\Src\Database_Control.cpp 1458

Severity Code Description Project File Line Suppression State
Error (active) E0028 expression must have a constant value Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Support\Modules\GSRoot\GSException.hpp 62

Severity Code Description Project File Line Suppression State
Error (active) E0028 expression must have a constant value Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Support\Modules\GSRoot\GSException.hpp 62

Severity Code Description Project File Line Suppression State
Error (active) E0145 member "GS::GSException::AlignOfUniString" may not be initialized Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Support\Modules\GSRoot\GSException.hpp 56

Severity Code Description Project File Line Suppression State
Error (active) E0145 member "GS::GSException::SizeOfUniString" may not be initialized Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Support\Modules\GSRoot\GSException.hpp 57

Severity Code Description Project File Line Suppression State
Error C2220 warning treated as error - no 'object' file generated Database_Control C:\Program Files\GRAPHISOFT\API Development Kit 22.3004\Examples\Database_Control\Src\Database_Control.cpp 1458