Can anyone explain what each workspacemodel event does in Dynamo? I have been playing with them but I cannot seem to figure out what event triggers them. The descriptions are rather vague too.
RequestNodeCentered
CurrentOffsetChanged
Cleared (several have a Cleared event but not sure what it is)
Note (Added Removed Cleared)
Annotation
Connector
MessageLogged
The added and removed events are for when some type of element is added to the workspace (ie nodes, notes annotations and connectors). This is useful if you want to know if the user has added or removed something and react to it… for instance, i have an extension that listens to new groups (annotations) being made so i can automatically change the colour. I also have another extension that tracks nodes being used, so i need to know when they are added/removed.
When a graph is closing/clearing, from what i understand all elements are cleared, that’s to say, unloaded from the graph space. I haven’t found a need for this event, but you might.
I havent used the request node centered event nor the current offset changed, but i can only assume the first is when the user focuses on a node via the [ ] button and the latter is when the user is panning in the graph (not sure on these though, just a guess until I test or someone else corrects me, try fire up a message box or put a breakpoint to flag when the event is fired).
There’s also the PropertyChanged event which is very useful and allows you to watch for certain changes to named properties. I would suggest leaning about MVVM patterns if you dont know what this is.
I would recommend searching on the Dynamo Github as there is usally something that has what you need in it, maybe in the tests directory. Pretty much how i started learning. I also think there is some learning material on there too and also on AU website from the past AU Vegas classes.
I wrote something very quick in my lunch you might find useful…
This tracks all the WorkspaceModel Events and lists them in the window as they happen. You can find the source code here. Feel free to copy it completely and make it your own. It is built using Dynamo API v2.0.2 and is ‘as is’…
This should hopefully help you with understanding what is happening and when.
I remember starting to learn all this just over a year back and how frustrated I was with the lack of info, the lack of descriptions in the API and no tutorials/documentation. However, I found learning MVVM/WPF very useful to understanding the API though and how Model, ViewModels and Views interact. Once you learn that, the API doesn’t seem that flaky tbh.
@Deniz_Maral, I’m assuming you are using .net here and writing a View Extension…
var codeBlocks = DynamoViewModel.CurrentSpace.Nodes.Where(n => n is CodeBlockNodeModel && n.Name == "My Awesome CodeBlock").ToList();
foreach (CodeBlockNodeModel cb in codeBlocks)
{
var code = cb.Code;
// Do whatever...
}
You can get the DynamoViewModel from the ViewLoadedParams in the entry point class (which inherits IViewExtension/IExtension) like this…
public void Loaded(ViewLoadedParams p)
{
DynamoViewModel = (DynamoViewModel)p.DynamoWindow.DataContext;
}