Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions...

32
Super Powered Dynamo Dynamo Extensions - “with great power comes great responsibility” - uncle ben

Transcript of Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions...

Page 1: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Super Powered DynamoDynamo Extensions - “with great power comes great

responsibility” - uncle ben

Page 2: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

OverView For Today● 8:30 - 9:00 Breakfast and Setup● 9:00 - 9:30 Introductions and Extension Ideas Exchange● 9:30 - 11:00 Extensions Presentation● 11:00 - 12:00 Make an Extension (Part 1)● 12:00 - 13:00 Lunch● 13:00 - 16:00 Make an Extension (Part 2)● 16:00 - 17:00 Share-Out/ Conclusion

Page 3: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Difficulty

ExpressivePower

Graph Custom NodesDS

Zero Touch‘UI’ Nodes

Host integrationExtensions

Page 4: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

When to Use an Extension

● Control over the graph (add,remove nodes etc).● Add new UI or functionality that is external to a single node.● Core functionality that needs improvement - simple to add an

extension which will ship with DynamoCore- good model for contribution to Dynamo itself.

● Don’t feel like learning about abstract syntax trees today…

Page 5: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

What can we do with them?

● Create an instance of your extension when Dynamo starts.● Add MenuItems to Dynamo Window.● Get a reference to CurrentWorkspace.● Execute Model commands on The Dynamo Model (application).● Get a reference to the DynamoView - now you have as much control as Dynamo

Sandbox or DynamoRevit.

I.E. Control Dynamo like a application.

Page 6: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Extensions Architecture

Extensions are composed of 2 parts:

1. a .Net assembly that contains a class which implements IViewExtension or IExtension.

Page 7: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Extensions Architecture

2. An xml Extension manifest file which tells Dynamo which class to instantiate to start your extension.

<ViewExtensionDefinition>

<AssemblyPath>..\Notifications.dll</AssemblyPath>

<TypeName>Dynamo.Notifications.NotificationsViewExtension</Ty

peName>

</ViewExtensionDefinition>

Page 8: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Extensions Architecture

Dynamo looks for extension manifest files at startup in these folders in the Dynamo core install folder.

If it finds one, it tries to create an instance of it.

Naming is important!

Dynamo/ViewExtensions

MyExtension_ViewExtension.xml

Dynamo/Extensions

MyExtension_Extension.xml

Page 9: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Dynamo Architecture(a brief aside)

DynamoCoreWPF.dll

DynamoCore.dll

Page 10: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Extensions ArchitectureAt Startup Dynamo calls two methods on your extension:

StartUp(Params): called when Dynamo starts loading.

Loaded(Params): called when Dynamo is finished loading.

When these are called exactly, depends on if you are writing an Extension or ViewExtension

Dynamo Model Startup

Instantiate Extensions

Call StartUp() on Extensions

Call Loaded() on Extensions

DynamoView Startup

Call StartUp() on ViewExtensions

Instantiate ViewExtensions

Call Loaded() on ViewExtensions Shutdown() View

Extensions

Shutdown() on Extensions

Page 11: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Composition of ExtensionsTaking advantage of MVVM

(model)Extension

ViewExtension1 ViewExtension2 ViewExtension3

Extensions Manager

Page 12: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Extension Startup + Loaded ParamsMaking stuff happen. (dynamo APIs)

ViewLoadedParams

ReadyParams

ViewStartupParams

StartupParams

WorkspaceModelsCurrentWorkspaceModel

CommandExecutiveNotificationRecieved

CurrentWorkspaceChanged

ExtensionManager

DynamoMenuBackGroundPreviewViewModel

RenderPackageFactoryDynamoWindow*

CommandExecutiveAddMenuItem

SelectionCollectionChanged

AuthProviderPreferences

PathManagerLibraryLoader

CustomNodeManagerDynamoVersion

Page 13: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Dirty TricksMaking more stuff happen.ViewLoadedParams.DynamoWindow.DataContext as DynamoViewModel

Don’t do this unless you have to - it may stop working.

Page 14: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Let’s get started.

For this demo we’re going to run our extension in Dynamo Sandbox, so make sure you can build and run Dynamo Sandbox using the Dynamo repo: https://github.com/DynamoDS/Dynamo

Page 15: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Let’s get started.

DynamoSamples/ SampleViewExtension

https://github.com/DynamoDS/DynamoSamples

Page 16: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension(from DynamoSamples repo)

● Create a new visual studio project.

● Reference some Dynamo nugets.

● Implement an IViewExtension.

● Add a menu item.● Load it in Dynamo.

Simple! ;)

Page 17: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension diagram

View Extension

Window

ViewModel

Dynamo

Current Workspace Model

Data Binding

Workspace.NodesReference

Page 18: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension

● Create a new Visual studio class library project. (target .Net 4.5 minimum)

● Reference required Dynamo Nugets: DynamoCore and DynamoCoreWPFUI

● Project-> Manage Nuget Packages->Browse

Page 19: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension

● create a class that implements IViewExtension

● Loaded(viewParams) is used to add our main logic and menu Item

Page 20: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension● Create our

viewModel which will contain our actual logic and raise events to update the view.

● For now we just save the ready params.

Page 21: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension

● Use the ready params to respond to events from Dynamo and update our UI.

Page 22: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension

Window

ViewModel

Current Workspace Model

Data Binding : ActiveNodeTypes

Event: Nodes Added/Removed

2. RaisePropertyChanged("ActiveNodeTypes");

Display a string “some node names”...

1 . Recalculate Nodes in graph.

Page 23: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension● Implement our

window using XAML.● Add new WPF

userControl to project.

● Bind to a property called ‘ActiveNodeTypes’

Page 24: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension

● Implement our Loaded() method logic using our viewModel and window.

Page 25: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extension● Add an xml file to the project

(SampleViewExtension_ViewExtension.xml)

<ViewExtensionDefinition>

<AssemblyPath>..\SampleViewExtension.dll</AssemblyPath>

<TypeName>SampleViewExtension.SampleViewExtension</TypeName>

</ViewExtensionDefinition>

Page 26: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Sample WPF extensionDynamo/bin/AnyCPU/Debug/ViewExtensions/SampleView_ViewExtension.xmlDynamo/bin/AnyCPU/Debug/SampleViewExtension.dll

Page 27: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Advanced SamplesExecuting Commands on the Dynamo Model and Dynamo View Model.

var VM = ViewLoadedParams.DynamoWindow.DataContext as DynamoViewModel;

//workspace view model command

VM.CurrentSpaceViewModel.ResetFitViewToggleCommand.Execute(null);

//dynamoViewModel command

VM.AddToSelectionCommand.Execute(someNode);

p.CommandExecutive.ExecuteCommand

(new DynamoModel.ForceRunCancelCommand(true, false),

this.UniqueId,

this.Name)

Execute model commands Execute viewModel commands … not guaranteed.

See:Dynamo\src\DynamoCore\Models\RecordableCommands.cs

Page 28: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Advanced SamplesInjecting UI into other views.

Page 29: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Advanced SamplesInjecting UI into other views.

https://github.com/mjkkirschner/DynamoSamples/tree/extensionWorkshop

A hack to illustrate injecting some wpf control whenever the view is updated… performance implications and other caching issues with this code.

Page 30: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Incoming Changes:

● Investigating Package Manager distribution of extensions.● Loading sequence might change a bit.● Loaded Parameters will likely also contain a reference to StartUp

Params.● It would be good to know now- or soon, before 2.0 release what

APIs you are missing from the extensions APIs.

Page 31: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Project Ideas:

● Tracking graph changes by watching workspace collection modified events.

● An extension that finds missing dependencies and reports what package they are in.

● Write your own graph UI.● Execution results view.● MiniMap.● Inject UI into base nodeView.● Improve background preview.

Page 32: Super Powered DynamoMVVM (model)Extension ViewExtension1 ViewExtension2 ViewExtension3 Extensions Manager. Extension Startup + Loaded Params Making stuff happen. (dynamo APIs) ViewLoadedParams

Your Turn!