We value your input!
Please participate in Archicad 28 Home Screen and Tooltips/Quick Tutorials survey

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

Writing to a txt file in the Embedded Library

Rob C
Contributor

Hi all,

 

I've got a .txt file located within my Embedded Library, and I'm looking to write to it using an addon to allow some custom GDL objects to read it and update their parameters when they're placed. My question is whether this is possible at all? Current code is below:

 

 

API_LibPart settingsText = {};
GS::ucscpy(settingsText.file_UName, L("test.txt"));
ACAPI_LibraryPart_Search(&settingsText, false);
GS::UniString loc = "";
settingsText.location->ToPath(&loc);

/* GS::UniString out defined here */

std::ofstream file(loc.ToCStr().Get());
file << out.ToCStr().Get();
file.close();

 

 

I'm aware that this could be done using an addon with an event handler, though we've already got the objects currently reading this text file and we're concerned with the added processing time that might be required when placing a new object (though we're open to ideas to the contrary!).

 

Thanks,

Rob

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Hi Rob,

 

Yes it's possible, but I found it super tricky. Especially with Teamwork projects.

 

My main approach right now is (without TW):
1. Write the file to a temporary folder
2. Delete the file in the embedded library if it already exists

3. Then use APIEnv_CopyFilesIntoLibraryID to copy the temporary file into the embedded library

Hope that helps

View solution in original post

3 REPLIES 3
Solution

Hi Rob,

 

Yes it's possible, but I found it super tricky. Especially with Teamwork projects.

 

My main approach right now is (without TW):
1. Write the file to a temporary folder
2. Delete the file in the embedded library if it already exists

3. Then use APIEnv_CopyFilesIntoLibraryID to copy the temporary file into the embedded library

Hope that helps

Hi Bernd,

 

Thanks! That works perfectly - although admittedly I have skipped the Deletion step as ACAPI_LibraryManagement_CopyFilesIntoLibrary has the ability to overwrite files that match. Teamworking seems to work fine as well, as my addon already makes sure to reserve/release the file at the appropriate times.

 

Thanks again!

Rob

Hi Rob,

 

Yes the deletion step seems silly when you can just overwrite the file. Just a heads up that it was necessary for me at times 😉
Maybe the issues are fixed now, but there were situations when the file seemed to be updated, but when restarting Archicad and checking the file in the embedded lib, it wasn't updated! Deleting it first resolved those issues. But I can't remember now if that was only an issue in TW projects or in general.