Dynamo API Events

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.

http://dynamods.github.io/DynamoAPI/Dynamo_Graph_Workspaces/WorkspaceModel/#events

RequestNodeCentered
CurrentOffsetChanged
Cleared (several have a Cleared event but not sure what it is)
Note (Added Removed Cleared)
Annotation
Connector
MessageLogged

Thanks

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 hope that helps a little.

Cheers,
Dan

5 Likes

@Steven

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.

Cheers,
Dan

11 Likes

Wow while your first explanation was great this was something else. This is definitely going to help with what I am trying to do.

Thanks Daniel,

1 Like

You’re welcome @Steven,

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. :slight_smile:

2 Likes

I will check that stuff out for sure. Every time I go looking through the API I just get lost. It takes me forever to find stuff.

1 Like

How can I search for a code block with a specific name in current workspace and get its content?

Thanks!

@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;
}

CodeBlockNodeModel lives in the namespace…

using Dynamo.Graph.Nodes;

Hope this helps.
D

2 Likes

Thank you very much for your help! I would like to do it in ZeroTouch environment, is it there possible?
I am getting always errors:
grafik

Ah, I assumed wrong. If you are creating ZT nodes, you can use this one liner…

var model = Dynamo.Applications.DynamoRevit.RevitDynamoModel;

Similar to the way you would in python. I don’t do ZT nodes, so this is more of an educated guess and I haven’t tested personally

Mark Thorley has a nice little repo for the designtech.io nodes you can check out…