2014-07-03 04:31 PM - last edited on 2023-08-01 02:28 PM by Doreena Deng
2014-07-04 09:28 AM
2014-07-04 03:35 PM
// Create an own structure which contains all the necessary informations what you want to store in element's user data typedef struct { Int32 myNumbers[5]; char myInfo[API_InfoLen]; } MyElementUserData; // ----------------------------------------------------------------------------- bool SetElementUserData (const API_Guid& guid, const API_ElemTypeID& typeID) // Add extra parameters if needed { API_ElementUserData userData; MyElementUserData** myUserData = NULL; API_Elem_Head elemHead; GSErrCode err = NoError; BNZeroMemory (&userData, sizeof (API_ElementUserData)); userData.dataVersion = 1; userData.platformSign = GS::Act_Platform_Sign; // Set flags if you need: // userData.flags = APIUserDataFlag_FillWith | APIUserDataFlag_Pickup | APIUserDataFlag_UndoAble | APIUserDataFlag_SkipRecalcAndDraw; userData.dataHdl = BMAllocateHandle (sizeof (MyElementUserData), ALLOCATE_CLEAR, 0); if (userData.dataHdl == NULL) return false; myUserData = (MyElementUserData**) (userData.dataHdl); // Fill the allocated userdata as you want: for (Int32 ii = 0; ii < 5; ++ii) { (*myUserData)->myNumbers[ii] = ii + 1; } sprintf ((*myUserData)->myInfo, "My Info!"); // Attach userdata to the element: BNZeroMemory (&elemHead, sizeof (API_Elem_Head)); elemHead.guid = guid; elemHead.typeID = typeID; err = ACAPI_Element_SetUserData (&elemHead, &userData); if (userData.dataHdl != NULL) BMKillHandle (&userData.dataHdl); return err == NoError ? true : false; } // ----------------------------------------------------------------------------- bool GetElementUserData (const API_Guid& guid, const API_ElemTypeID& typeID, MyElementUserData** myUserData) { API_ElementUserData userData; API_Elem_Head elemHead; GSErrCode err = NoError; BNZeroMemory (&userData, sizeof (API_ElementUserData)); // Get userdata from the element: BNZeroMemory (&elemHead, sizeof (API_Elem_Head)); elemHead.guid = guid; elemHead.typeID = typeID; err = ACAPI_Element_GetUserData (&elemHead, &userData); if (err != NoError || userData.dataVersion != 1) { if (userData.dataHdl != NULL) BMKillHandle (&userData.dataHdl); return false; } myUserData = (MyElementUserData**) (userData.dataHdl); return true; } // ----------------------------------------------------------------------------- // Usage example: bool result = false; result = SetElementUserData (myGuid, myType); // ... MyElementUserData** myUserData = NULL; result = GetElementUserData (myGuid, myType, myUserData); if (result) { // read the userdata Int32 myFirstNumber = (*myUserData)->myNumbers[0]; // ... } // Don't forget to release the handle after using! if (myUserData != NULL) BMKillHandle ((GSHandle*) &myUserData);
2014-08-08 03:07 PM
2014-08-08 04:25 PM
paulk wrote:Good question
Is the line "elemHead.typeID = typeID; " required in the example you posted, or is it redundant?
2014-08-09 12:16 AM
Everywhere you can identify an element with only the GUID, you don't need the type+index pair anymore. So it's not required.Thank you.
2014-08-10 03:17 AM
case DG_MSG_CLICK: switch (itemId) { case 1: // Close Button SetElementUserData(params); break;However - the call to...
err = ACAPI_Element_SetUserData (&elemHead, &userData);kills the ArchiCAD selection system. So after that call, I can select another ArchiCAD element, but I cannot then select the element the data was saved for in the 3d view (by mouseclicking the element), even with "Arrow" select. Selection of elements in 2d views is OK though. err is returning NoError. If I move the camera in the 3d view, other elements can be selected again. Rem'ing that line out fixes the problem (but doesn't save the data to the element). This is AC17 5005. This appears to be an ArchiCAD bug.
2017-04-21 12:08 PM
2017-04-21 12:31 PM
ReignBough wrote:
Does element user data per add-on or can I access the same user data with different add-on?
2017-04-23 05:57 PM
Tibor wrote:Additional info: this is for data safety reasons, we don't allow other add-ons to accidentally compromise the data of another add-on.ReignBough wrote:
Does element user data per add-on or can I access the same user data with different add-on?
Each Add-On can attach own userdata to an element, but the attached userdata can be accessed only by the owner Add-On.