BIM Coordinator Program (INT) April 22, 2024

Find the next step in your career as a Graphisoft Certified BIM Coordinator!

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

[SOLVED] ACAPI_Element_GetMemo error: APIERR_REFUSEDCMD

Anonymous
Not applicable
Hello,

I am getting this error in my Addin: APIERR_REFUSEDCMD while attempting to get the Memo of Rooms(API_ZoneID), ONLY when I call my function from a managed WPF Control (mixed code).

Are there any locks happening automatically when the API is used without an ArchiCAD UI trigger? What could be the problem here? I am a little confused.

I have checked the GUIDs and they are unchanged as when I call the function from a Menu Command, also I can access all Informations at the "element.zone..." level and also the IFC Properties, just the memo throws this error.

I would be very glad to get some suggestions. Thanks in advance!

*DEVENV: Windows 10, VS2012, AC20
13 REPLIES 13
Ralph Wessel
Mentor
mar_kq wrote:
Hello,

I am getting this error in my Addin: APIERR_REFUSEDCMD while attempting to get the Memo of Rooms(API_ZoneID), ONLY when I call my function from a managed WPF Control (mixed code).
Are there any locks happening automatically when the API is used without an ArchiCAD UI trigger? What could be the problem here? I am a little confused.
Can you clarify the context for this error? Are you running managed code and using WPF within an ARCHICAD add-on? And is this code making API calls from a separate thread or process?
Ralph Wessel BArch
Anonymous
Not applicable
Hello Ralph,

My Archicad Addin initializes a WPF Control hosted in a Window. From a button in my WPF I am triggering an API call to get information about rooms in the model.
Akos Somorjai
Graphisoft
Graphisoft
mar_kq wrote:
Hello,

I am getting this error in my Addin: APIERR_REFUSEDCMD while attempting to get the Memo of Rooms(API_ZoneID), ONLY when I call my function from a managed WPF Control (mixed code).

Are there any locks happening automatically when the API is used without an ArchiCAD UI trigger? What could be the problem here? I am a little confused.

I have checked the GUIDs and they are unchanged as when I call the function from a Menu Command, also I can access all Informations at the "element.zone..." level and also the IFC Properties, just the memo throws this error.

I would be very glad to get some suggestions. Thanks in advance!

*DEVENV: Windows 10, VS2012, AC20
Hi,

Could you please post the relevant part of your code?

That function usually gives that error code when it cannot build up the connection to ARCHICAD from the add-on. The API functions mostly run on the main thread of the application, triggered by some user event, so you cannot really throw API calls randomly at ARCHICAD; this won't work. Is this the case you refer to?
If so, then I can recommend two solutions: if you have a palette in ARCHICAD, then enable idle events for that, and call the API from that idle event handler (PanelIdle ()). If you don't have an interface, then use the ACAPI_Command_CallFromEventLoop () mechanism to get what you want.

I'm not sure how the handles allocated in the ACAPI_Element_GetMemo work with managed code, though.

Regards, Akos
Anonymous
Not applicable
Akos wrote:

Hi,

Could you please post the relevant part of your code?

That function usually gives that error code when it cannot build up the connection to ARCHICAD from the add-on. The API functions mostly run on the main thread of the application, triggered by some user event, so you cannot really throw API calls randomly at ARCHICAD; this won't work. Is this the case you refer to?
If so, then I can recommend two solutions: if you have a palette in ARCHICAD, then enable idle events for that, and call the API from that idle event handler (PanelIdle ()). If you don't have an interface, then use the ACAPI_Command_CallFromEventLoop () mechanism to get what you want.

I'm not sure how the handles allocated in the ACAPI_Element_GetMemo work with managed code, though.

Regards, Akos

Hi,

Thanks for the Reply. I guess you describe the Problem correctly. I''ll recap my process:
1- I have registered a Menu command (no Palette)
2- This Menu command calls the managed dll which instantiates a custom WPF Interface (also connecting to other applications)
3- An Event in the WPF is calling the "GetPropertiesSpace" function (below) which uses several API methods.
4- So far I experience issues Only with the ACAPI_Element_GetMemo function.


GetPropertiesSpace:

map<string, string>	GetPropertiesSpace (API_Guid& guid, map<string, string> searchMap)
{
	API_Element			element;
	API_ElementMemo		memo;
	GSErrCode			err;
	map<string, string> targetMap = map<string, string>();

	// GET ELEMENT ------------------------------------------------------------ //
	BNZeroMemory (&element, sizeof (element));
	element.header.typeID = API_ZoneID;
	element.header.guid = guid;	
	err = ACAPI_Element_Get (&element);

	// GET GUID
	//WriteReport ("Room_GUID: %s", APIGuidToString (guid).ToCStr ().Get ());
	GS::UniString strguid = APIGuidToString(guid).ToCStr().Get();
	if(strguid != "")
		targetMap["GUID"] = strguid.ToCStr();

	// GET MEMO ------------------------------------------------------------ //
	BNZeroMemory (&memo, sizeof (memo));
	if (err == NoError && element.header.hasMemo) 
	{
		err = ACAPI_Element_GetMemo (element.header.guid, &memo, APIMemoMask_AddPars);
		//err = ACAPI_Element_GetDefaults (&element, &memo);
		if (err != NoError) 
		{
			ErrorBeep ("ACAPI_Element_Get/Memo (zone)", err);
		}
	}
    
	// GET ELEMENT PARAMETERS -- ROOM
	if (&memo.params != NULL || *memo.params != NULL)
	{
		... get other params
	}
	
	return targetMap;
}

I have attempted to use the ACAPI_Command_CallFromEventLoop you suggested, with the example in the documentation, but so far with no avail.
When I call the Do_CommandCallFromEventLoop(); from the managed Code I get the same Error on the ACAPI_Command_CallFromEventLoop as with the GetMemo.

I also tried with a sample for the DWG IO Add-On but I get the same Error as before):

bool APIHandler::Test () 
{
	// API_ModulID mdid = { developerID, localID };
	API_ModulID	mdid = { 1198731108, 1322668197 };		// DXF/DWG add-on
	try {
		GSErrCode   err = NoError;
		GSHandle    paramHandle;
		err = ACAPI_Goodies (APIAny_InitMDCLParameterListID, &paramHandle, NULL);
		if (err == NoError) {
			err = ACAPI_Command_CallFromEventLoop (&mdid, 'GDCO', 1, paramHandle, false, CommandCallBackProc);
			if (err != NoError) {
                    WriteReport_Err ("ACAPI_Command_CallFromEventLoop failed", err);
                    ACAPI_Goodies (APIAny_FreeMDCLParameterListID, &paramHandle, NULL);
					return false;
			}
		}
		else{
			return false;
		}
	}
    catch (...) {
    }
	return true;
}

I also found a post that was perhaps related: http://archicad-talk.graphisoft.com/viewtopic.php?printertopic=1&t=50872&start=0&postdays=0&postorder=asc&vote=viewresult&sid=dab5266f1e6f57c7fa2d8c06641f8725
Am I moving in the right direction? Would you happen to have a small example of how to achieve this otherwise?

Thanks again.
Oleg
Expert
This Menu command calls the managed dll which creates a WPF Interface
What unterface ?
Do you call the Show or the ShowDialog method ?

I dont know the reason. Just some ideas to try.

Call the your GetPropertiesSpace directly, not from any UI.
Is it works ?

May be better to try the WinForms instead the WPF.
Form.ShowDialog (IWin32Window) looks better. 🙂
Anonymous
Not applicable
Hello Oleg,
Oleg wrote:
What unterface ?
Do you call the Show or the ShowDialog method ?
We have a WPF component used in other applications. I am using a WinForm Window to "host" the WPF, and I use the "Show" method. The WPF UI works just fine.
Oleg wrote:
Call the your GetPropertiesSpace directly, not from any UI.
Is it works ?
If I call the function from a normal Archicad Menu command then it works fine. It has something to do with the communication with the Main Thread like Akos Somorjai suggested.
Anonymous
Not applicable
I believe I have found the issue:

after I initialized my UI from the Add-On i never called the ACAPI_KeepInMemory function.

I added this now and seems to work as expected. I'll post later today the results.
Oleg
Expert
I use the "Show" method.
I guess it is the key reason of the issue.
Your UI is a modeless, possible has own thread.
I dont know a UI message loop and a threading details in you case.
But I think you cannot call any API functions directly.

I have no any ready solutions.
But I think the ACAPI_Command_CallFromEventLoop may help.
Ralph Wessel
Mentor
mar_kq wrote:
Oleg wrote:
What unterface? Do you call the Show or the ShowDialog method ?
We have a WPF component used in other applications. I am using a WinForm Window to "host" the WPF, and I use the "Show" method. The WPF UI works just fine.
I think this is the source of your problem. Your WPF window is running in parallel with ARCHICAD on a separate thread and receiving events directly through the OS, i.e. not through ARCHICAD. Add-ons normally run on the same thread as the core functionality of ARCHICAD, so these processes do not run in parallel (and are not currently designed to do so). Attempting to call into API functions in parallel with ARCHICAD's main thread is unlikely to work and would likely lead to unpredictable behaviour.

I recommend you drop WPF and use the native UI components of the ARCHICAD API.
Ralph Wessel BArch
Learn and get certified!

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!