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

How to get currently selected Library Part

gehairing
Participant
Hi all

I try to get the currently selected library part.
I mean the object selected (in green) by user in the current model.

I have tried following code but it doesn't seem to work
	GSErrCode err; 
	 
	// Get selected elements
	API_SelectionInfo selectionInfo; 
	API_Neig **selNeigs;
	err = ACAPI_Selection_Get (&selectionInfo, &selNeigs, false); 
	if (err == NoError) 
	{ 
		if (selectionInfo.typeID != API_SelEmpty) 
		{ 
			UInt32    ii, nSel; 
			nSel = BMGetHandleSize ((GSHandle) selNeigs) / sizeof (API_Neig); 
			for (ii = 0; ii < nSel && err == NoError; ii++) 
			{ 
				GS::UniString selectedGuid = APIGuidToString((*selNeigs)[ii].guid);
				API_NeigID selectedID = (*selNeigs)[ii].neigID;
			
				API_Element  element;
				BNZeroMemory (&element, sizeof (API_Element));
				element.header.guid  = (*selNeigs)[ii].guid;
				err = ACAPI_Element_Get(&element);
				if(err == NoError)
				{
					// Find the library part
					API_LibPart libpart;
					BNZeroMemory (&libpart, sizeof (libpart));
					libpart.index = element.object.libInd;

					err = ACAPI_LibPart_Search (&libpart, false);
					if (err == NoError) 
					{
						double aParam = 0.0;
						double bParam = 0.0;
						Int32 paramNum = 0;
						API_AddParType** addPars  = NULL;
						err = ACAPI_LibPart_GetParams (libpart.index, &aParam, &bParam, &paramNum, &addPars);
						if (err == NoError) 
						{
							for (Int32 i = 0; i < paramNum; i++) 
							{
								if(CHCompareCStrings ("BimObject id", (*addPars).name) == 0)
								{
									std::wstring objectUrl = L"xxx";
									ShowPlatform(objectUrl);
									BMKillHandle ((GSHandle *) &selNeigs);
									return;
								}
							}
						}
					}
				}
			} 
			BMKillHandle ((GSHandle *) &selNeigs);
		}
	} 
What am i doing wrong in this code ?
(this is a quick and dirty testing code)
1 REPLY 1
gehairing
Participant
Me again

I have finally found out what was wrong.

Here is the working code (draft code...to be cleaned 😉 )
	GSErrCode err; 
	 
	// Get selected elements
	API_SelectionInfo selectionInfo; 
	API_Neig **selNeigs;
	err = ACAPI_Selection_Get (&selectionInfo, &selNeigs, false); 
	if (err == NoError) 
	{ 
		if (selectionInfo.typeID != API_SelEmpty) 
		{ 
			UInt32    ii, nSel; 
			nSel = BMGetHandleSize ((GSHandle) selNeigs) / sizeof (API_Neig); 
			for (ii = 0; ii < nSel && err == NoError; ii++) 
			{ 
				GS::UniString selectedGuid = APIGuidToString((*selNeigs)[ii].guid);
				API_NeigID selectedID = (*selNeigs)[ii].neigID;
			
				API_Element  element;
				BNZeroMemory (&element, sizeof (API_Element));
				element.header.guid  = (*selNeigs)[ii].guid;
				err = ACAPI_Element_Get(&element);
				if(err == NoError)
				{
					// Find the library part
					API_LibPart libpart;
					BNZeroMemory (&libpart, sizeof (libpart));
					libpart.index = element.object.libInd;

					err = ACAPI_LibPart_Get (&libpart);
					if (err == NoError) 
					{
						double aParam = 0.0;
						double bParam = 0.0;
						Int32 paramNum = 0;
						API_AddParType** addPars  = NULL;
						err = ACAPI_LibPart_GetParams (libpart.index, &aParam, &bParam, &paramNum, &addPars);
						if (err == NoError) 
						{
							for (Int32 i = 0; i < paramNum; i++) 
							{
								GS::UniString us = (*addPars).uDescname;
								if(us == L"BimObject id")
								{
									
                // Todo : Do someting here

		BMKillHandle ((GSHandle *) &selNeigs);
									return;
								}
							}
						}
					}
				}
			} 
			BMKillHandle ((GSHandle *) &selNeigs);
		}
	}