2016-12-16 04:55 PM - last edited on 2023-07-12 08:59 PM by Doreena Deng
2016-12-16 11:13 PM
mar_kq wrote: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?
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.
2016-12-17 11:51 PM
2016-12-18 11:50 AM
mar_kq wrote:Hi,
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
2016-12-28 06:53 PM
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
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; }
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, ¶mHandle, 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, ¶mHandle, NULL); return false; } } else{ return false; } } catch (...) { } return true; }
2016-12-29 10:32 AM
This Menu command calls the managed dll which creates a WPF InterfaceWhat unterface ?
2016-12-29 10:51 AM
Oleg wrote: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.
What unterface ?
Do you call the Show or the ShowDialog method ?
Oleg wrote: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.
Call the your GetPropertiesSpace directly, not from any UI.
Is it works ?
2016-12-29 11:37 AM
2016-12-29 11:53 AM
I use the "Show" method.I guess it is the key reason of the issue.
2016-12-29 09:37 PM
mar_kq wrote: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.Oleg wrote: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.
What unterface? Do you call the Show or the ShowDialog method ?