Making changes to the document through a websocket
-
Hi, I am currently working on a way, to trigger different actions through a websocket, for example triggering the undo/redo or to create a simple cube, I searched through the documentation a lot, and found the relevant functions, like creating a cube:
BaseDocument* const document = GetActiveDocument(); if (document == nullptr) return false; BaseObject* const cubeObject = BaseObject::Alloc(Ocube); if (cubeObject == nullptr) return false; cubeObject->SetName("This is a new object"_s); document->InsertObject(cubeObject, nullptr, nullptr); EventAdd();
but I am not quite sure about the context of how to use them. For example I tried making a hidden CommandData plugin to create a simple cube when it is called, to also include the creation in the undo/redo system but if the plugin is hidden from the user, its Execute function obviously is never called. Similarily, I tried calling the
BaseDocument *doc = GetActiveDocument(); if (doc) doc->DoUndo();
from within the websocket OnMessage function directly, this resultet in a breakpoint being hit inside the DoUndo() function. Probably caused by the networking layer being in a different thread than the main thread (I assume) and thus can not modify the BaseDocument in any way.
How would a design look like, to trigger such actions from within a websocket? Any help would be greatly appreciated!
-
I made some progress now, I changed the message type to be MessageData, to retrieve Core messages and in the websocket layer I now use SpecialEventAdd() with my own event ids, I see that the CoreMessage() method also supports the BaseContainer as a parameter, is it possible to fill it with custom data when calling SpecialEventAdd()? The function only seems to take two Uints as additional parameters, how would I get some custom data into my own CoreMessage method call?
Thank you again!
-
Hi using MessageData is correct, however the best way is to call ExecuteOnMainThread as you can see in the websocket example I share with you here.
This lambda will be executed on main thread, therefor it will be safe to create object and perform undo from it. One note make sure to pass a copy of the data to your lambda, since this lambda will be executed when the main thread is free and you do not know exactly when it can happen. And it can be latter than your OnMessage function, so with a copy you are sure that object are not nullptr.
Cheers,
Maxime.