Where to get the LibraryServices for creation of CodeBlockNodeModel in view extention

Hello,

Does anyone have a suggestion how to get the ‘Dynamo.Engine.LibraryServices’ that is needed to create a ‘Dynamo.Graph.Nodes.CodeBlockNodeModel’ from within a view extention?

so far as i can tell is a singleton in the Dynamo.Engine but im not sure how to get that one either from a view extention

ViewLoadedParams vlp = ExtentionDefaults.ViewLoadedParameters;
//Need a library service to create a codeblocknodemodel, where do i get it from?
Dynamo.Engine.LibraryServices libraryServices = new Dynamo.Engine.LibraryServices(new Core(new Options()), vlp.StartupParams.PathManager, vlp.StartupParams.Preferences);
Dynamo.Graph.Nodes.CodeBlockNodeModel inputNode = new CodeBlockNodeModel(libraryServices);
inputNode.Name = “INPUT”;
inputNode.GUID = Guid.NewGuid();
//Set position to left top of group
double _x = this.AnnotationModel.X;
double _y = this.AnnotationModel.Y + this.AnnotationModel.Height;
ExtentionDefaults.ViewLoadedParameters.CommandExecutive.ExecuteCommand(new DynamoModel.CreateNodeCommand(inputNode, _x,_y,false,false), ExtentionDefaults.UniqueId, ExtentionDefaults.Name);

Have you checked out the CodeBlockNodeTests on the Dynamo core tests Github? From memory I’m sure you can get to the LibraryServices instance from the current DynamoViewModel instance.

I have tested in a quick ViewExtension and works fine for me. Here is some code you can use…

A static NodeUtils Class…

using Dynamo.ViewModels;
using Dynamo.Models;
using Dynamo.Graph.Nodes;

namespace CodeBlockTest
{
    public static class NodeUtils
    {
        public static void CreateCodeBlock(DynamoViewModel _dvm, double xPos, double yPos, string code)
        {
            var cbm = new CodeBlockNodeModel(_dvm.Model.LibraryServices);
            var cmd = new DynamoModel.CreateNodeCommand(cbm, xPos, yPos, true, false);
            cbm.SetCodeContent(code, _dvm.Model.CurrentWorkspace.ElementResolver);            
            _dvm.ExecuteCommand(cmd);            
        }
    }
}

ViewExtension Class inheriting iViewExtension…

using System;
using System.Windows.Controls;

using Dynamo.ViewModels;
using Dynamo.Wpf.Extensions;

namespace CodeBlockTest
{
    public class CodeBlockTest : IViewExtension
    {
          public string UniqueId
        {
            get
            {
                return Guid.NewGuid().ToString();
            }
        }

        // Edit string when starting a new ViewExtension...
        public string Name
        {
            get
            {
                return "CodeBlockTest";
            }
        }

        public void Startup(ViewStartupParams p)
        {

        }

        public void Loaded(ViewLoadedParams p)
        {
            // Get current DataContext as DynamoViewModel...
            var _dvm = (DynamoViewModel)p.DynamoWindow.DataContext;

            // Create Menu Items...
            Menu MainMenu = p.dynamoMenu;

            MenuItem _myMenu = new MenuItem
            {
                Header = "Code Block Test"
            };
            MainMenu.Items.Add(_myMenu);

            MenuItem _viewExtensionMenuItem = new MenuItem
            {
                Header = "Create Code Block"
            };
            _myMenu.Items.Add(_viewExtensionMenuItem);

            // Add click event to MenuItem...
            _viewExtensionMenuItem.Click += (sender, args) =>
             {
                 NodeUtils.CreateCodeBlock(_dvm, 0, 0, "HelloCB");
             };


        }

        public void Dispose()
        {

        }

        public void Shutdown()
        {

        }
    }
}

EDIT: Make sure you have the following Dynamo .dlls referenced in VS…

  • DynamoCore.dll
  • DynamoCoreWpf.dll
  • ProtoCore.dll

Hope this helps.

Cheers,
Dan

1 Like

Many thanks this seems to work perfect for me!