A few days ago (while flicking through the Dynamo Forum) I spotted this post about the Dynamo API and some confusion as to what the Workspace Events do, and my interest was piqued as I too had the same question a while back.

I have been messing around a fair bit with the Dynamo API for just over a year now and it’s super fun. I have written several custom ViewExtensions for my firm and personal ViewExtensions which I hope to release at some point soon so all can enjoy.

But, I do remember the pain and frustration of just not getting it at first. The documentation is rather sparse (if you are not familiar with MVVM – I’ll get to this later), not much info is given about what does what and how it all works together. There were no tutorials at the time and I spent a substantial amount of time just reading the GitHub repos trying to get some semblance of understanding and waiting for that eureka moment when I totally got it (this didn’t happen – I understood, but not enough). I almost gave up with it altogether, but was re-inspired by Long Nguyen’s DynaShapes Extension -one of the first 3rd party ViewExtensions out there. Mind blown!

So, seeing someone else totally own it with the DynamoAPI and blowing everyone’s socks off, I decided to not give up and really try get how it all worked. This brings me to WPF and MVVM.

I had never used WPF before, up to this point I generally used the Built-In Revit Task Dialog or Winforms in my code. I also had never used MVVM or even knew what it really was at that point. However, from reading the Dynamo Wiki pages and a couple of people mentioning the use of the MVVM pattern, I researched it.

What is WPF/MVVM?

WPF stands for Windows Presentation Foundation and is a combination of two languages, xaml and c#, xaml is a type of markup language and is used specifically to describe the View, that is to say, the graphical aspect of the application. It is similar in a sense to html where you are describing the layout of a page or form in code.

MVVM however, is a design pattern used with WPF (although not exclusively) and C# (sort of like a workflow or framework to structure your code). MVVM stands for Model-View-ViewModel. Hmmm, and that means what specifically? Let me elaborate as simply as possible…

Model

This is where you have classes that describe your data objects/business layer. This could be a Class that represents say a Movie Library, with sub-classes that represent Movies which have properties like Movie Title, Genre, Director, Actors, Description and other metadata.

View

This is the UI layer, where you graphically represent the data in your Model. This could be a full blown Application UI or a measly dialog/pop-up showing some snarky message. Say for instance we stuck to a Movie app (Like Microsoft’s Films and TV app), We could use the View to display all the Movies in the library by say Genre, showing the Movies Title, Description, Runtime etc.

ViewModel

Now, this is where the magic happens. Consider the ViewModel as a messenger/coordinator/executioner. The purpose of the ViewModel is to take data from the View, do something with it and send to the Model and similarly take the Model data, interpret it and relay it to the View. The ViewModel also handles all the commands that may be triggered in the View (i.e. Buy Movie button) and also hooked into Events that may be triggered from the View (i.e. PlayMovieRequest Event).

Well, that’s my simplified take on MVVM, but needless to say, it is a very powerful and nice way of doing things. Another benefit for MVVM is that the View does not need to know about the ViewModel or Model, in other words, the View only knows what it’s told, it is not responsible for executing any code as that is done by the ViewModel (in most cases). Similarly, the Model need not know anything about anything other than itself as again, the ViewModel does all the hard work. This gives us a very scalable framework to build upon and separates out concerns nicely. You can read more about MVVM here with more explicit technical language and far deeper explanation.

And back to DynamoAPI

So, having learnt all that, and creating a couple of simple applications (<cough> – they might have been heavily based on the course content), on coming back to the DynamoAPI it started to make a whole lot more sense. The lack of descriptions in the API docs kind of made sense in context after writing my stuff. After looking back on the GitHub I could finally understand to some degree how it was set up and then start using it for my own needs!

Now… if you made it this far, you might be thinking to write your own ViewExtension, but might not have a good understanding of MVVM (my basic explanation is certainly not enough for that), I would totally recommend taking the following online courses that I took…

WPF Masterclass By Eduardo Rosas on Udemy

WPF MVVM In Depth By Brian Noyes on PluralSight

Udemy usually has flash sales where you can get the courses for like £12 so if it is not reduced, definitely check it out when there is a sale on.

Pluralsight is strictly subscription only, but well worth the money in my opinion as they cover pretty much everything to do with Programming and Digital Design.

Both courses are very good, Eduardo’s has some really good projects to follow (like creating OneNote from scratch) and a great section on C# first principles. Whereas, Brian’s course I initially found harder to follow, but it is more in depth and follows the Prism framework that is oh-so-popular. In my code I mostly use Brian’s methods, but I feel for a complete beginner, Eduardo explains it better.

If you already have a good grasp of MVVM, you probably switched off on the second paragraph and have skipped this bit altogether! 🙂

In any Event…

Getting back to the Dynamo Forum post. I tried to explain what each of the workspace events do to the best of my knowledge, but it is so much better to just show when the are triggered. So, I wrote a very quick ViewExtension in my lunch break (took just over an hour perhaps). Here it is in action…

A ViewExtension to Watch Workspace Events as they happen.

And of course, the full source code should you want to see how it is done or build upon it yourselves.

If you still have a thirst for learning more, I would recommend rifling though other GitHub resources which I think are great…

Andreas Dieckmann’s GitHub Repo for Monito

Long Nguyen’s GitHub Repo for DynaShape

Dynamo Workspace Upgrader – Dynamo GitHub

In a future post, I will upload a Visual Studio Template for Dynamo ViewExtensions that will have all that you need to get started. You can then use that to start up a project fully loaded and ready to go!

Also, a shout out to Long Nguyen for helping me out with a couple of very noob questions back when I started to learn.

As always, thanks for reading! And I look forward to seeing all your cool ViewExtensions! 😉

Advertisement