Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dynamo script in Revit API

32 REPLIES 32
Reply
Message 1 of 33
Anonymous
11738 Views, 32 Replies

Dynamo script in Revit API

Hi,

 

Does anyone know where I can find the code to run a dynamo script from within the Revit API ? I want to write a C# add-in that can run a dynamo script in the background. I know there is dyno browser, so there is a way to acchieve this. I just can't figure out how.

 

 

32 REPLIES 32
Message 21 of 33
Kevin.Lawson.PE
in reply to: LeeFried

Okay I have it running. 

 

Don't use a transaction in the Plugin code, let dynamo handle the transaction independently. 

I had to reference a lot of .dlls (see the image below) I may have went overboard. 

You can only have one version of dynamo installed (2.x).

 

References.JPG

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Dynamo.Applications;
using Dynamo.Core;
using RevitServices;

namespace DynamoTest
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class RunDynamo : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get application and documnet objects and start transaction
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;           

            string Journal_Dynamo_Path = @"C:\Path To .Dyn\Test.dyn";
            DynamoRevit dynamoRevit = new DynamoRevit();

            DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
            dynamoRevitCommandData.Application = commandData.Application;
            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                { Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                { Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, //run journal automatically
                { Dynamo.Applications.JournalKeys.DynPathKey, Journal_Dynamo_Path }, //run node at this file path
                { Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not. If we are in automation mode the workspace will be executed regardless of this key.
                { Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
                { Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                { Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }

            };

            dynamoRevitCommandData.JournalData = journalData;           
            Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
            return externalCommandResult;

        }
    }
}
-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
Message 22 of 33
LeeFried
in reply to: Kevin.Lawson.PE

I'm out of office this week, but I will try this as soon as I can, even if by RDP. This is awesome!

Message 23 of 33
tnegrin18
in reply to: Kevin.Lawson.PE

Hi @Kevin.Lawson.PE ,

I'm using the code and references from your post from 10-15-2019.

The ".dyn" script run, but doesn't affect the Revit model.

The script should place a single Cylinder at the origin. When running from Dynamo Player it works, but when running  from the C# it doesn't work.

 

Should this code be able to add something to the Revit model ? 

Attached are the script and screenshot of the code. 

 

Thank you,

Tal

Message 24 of 33
Kevin.Lawson.PE
in reply to: tnegrin18

I'm not sure why your code isn't running, but here are some things you can try:

1) May sure that you only have 1 version of Dynamo installed that is at least 2.X

2) Make sure that dynamo is in automatic mode, you have to manually set it in dynamo, I would think that:

Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString()

 would force it to automatic mode, but it doesn't, you have to manually change that. 

3) Make sure you've referenced all of the .dlls in the list.

 

 

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
Message 25 of 33
Anonymous
in reply to: Kevin.Lawson.PE

Hi Klawson, I have followed this problem from a to z and I've tried to implement it into my own project. As mentioned above I'm also trying to run a dynamo script through my own button I've created in Revit. The script I am running, it's working just fine through Dynamo, but the issue is that this script starts with popping out new UI Window asking for some parameters and also to selecting some elements and by those choices it runs the script.  As I have followed your block of code you guys put there, I was able to run it, but after I click on my button, it just runs, but won't actually do anything. I have check also if the Dynamo is set in Automatic mode and it is. Is there any function, that I need to evoke first, so that UI Form from Dynamo is popped up, so I can continue and see if it's actually working?    This is the code I had followed through this communication: 

[Transaction(TransactionMode.Manual)]
    public class RunDynamo : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;

            string Dynamo_Journal_Path = @"C:\Users\PeterMihok\AB Clausen\BIM Team - Dynamo Scripts\02 Scripts\02_01 All Scripts\ABC_Elements_SetElevation.dyn";
            DynamoRevit dynamoRevit = new DynamoRevit();

            DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
            dynamoRevitCommandData.Application = commandData.Application;
            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                {Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                {Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, // run journal automatically
                {Dynamo.Applications.JournalKeys.DynPathKey, Dynamo_Journal_Path }, // run node at this file path
                {Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened
                {Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
                {Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                {Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }
            };

            dynamoRevitCommandData.JournalData = journalData;
            Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
            return externalCommandResult;
        }
    } 

I have tried to debug it, but I am not really a programmer so I don't really understand what should I be watching for etc...I would really like to solve this problem as this really interest me and I would like to understand more of it. Thanks in advance for your time guys and responses!

Message 26 of 33
Kevin.Lawson.PE
in reply to: Anonymous

Hi Pete, I'm not sure. Did you try everything in this list?  Otherwise post your .sln and dynamo files. 

 

1) May sure that you only have 1 version of Dynamo installed that is at least 2.X

2) Make sure that dynamo is in automatic mode, you have to manually set it in dynamo, I would think that:

Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString()

 would force it to automatic mode, but it doesn't, you have to manually change that. 

3) Make sure you've referenced all of the .dlls in the list.

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
Message 27 of 33
Anonymous
in reply to: Kevin.Lawson.PE

Hi,
Thanks for replying. I am pretty sure I did everything what was in the list, I might missed something though...I will put here the code.

Command.cs : 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Dynamo.Core;
using Dynamo.Applications;
using System.Reflection;
#endregion

namespace PETE.Revit.RibbonButton
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            TaskDialog.Show("PETE", "Hello Revit");
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class LinkRedirect : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            Process.Start("http://www.google.com/");
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class KEAHomePage : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            Process.Start("http://www.kea.dk/");
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class BIMCafeLinkedin : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            Process.Start("https://www.linkedin.com/company/kea-bim-cafe/");
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)] 
    public class KEAByg : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            Process.Start("https://kea.dk/en/programmes/bachelor-degree/architectural-technology-and-construction-management");
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class RunDynamo : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;

            string Dynamo_Journal_Path = @"C:\Users\PeterMihok\AB Clausen\BIM Team - Dynamo Scripts\02 Scripts\02_01 All Scripts\ABC_Elements_SetElevation.dyn";
            DynamoRevit dynamoRevit = new DynamoRevit();

            DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
            dynamoRevitCommandData.Application = commandData.Application;
            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                {Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                {Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, // run journal automatically
                {Dynamo.Applications.JournalKeys.DynPathKey, Dynamo_Journal_Path }, // run node at this file path
                {Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened
                {Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
                {Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                {Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }
            };

            dynamoRevitCommandData.JournalData = journalData;
            Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
            return externalCommandResult;
        }
    }
    // public class UIUIDocument OpenAndActivateDocument ()

}

 

App.cs: 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Dynamo.Core;
using Dynamo.Applications;
using System.Windows;
#endregion

namespace PETE.Revit.RibbonButton
{
    class App : IExternalApplication
    {
        const string RIBBON_TAB = "KEA";
        const string RIBBON_PANEL1 = "Bim Cafe";
        const string RIBBON_PANEL2 = "Q&A";
        const string RIBBON_PANEL3 = "Dynamo";
        const string RIBBON_PANEL4 = "AB";

        public Result OnStartup(UIControlledApplication a)
        {
            // get the ribbon tab
            try
            {
                a.CreateRibbonTab(RIBBON_TAB);
            }
            catch (Exception) { } // tab already exists

            // get or create the panel
            RibbonPanel panel1 = null;
            RibbonPanel panel2 = null;
            RibbonPanel panel3 = null;
            RibbonPanel panel4 = null;
            List<RibbonPanel> panels = a.GetRibbonPanels(RIBBON_TAB);
            foreach (RibbonPanel pnl in panels)
            {
                if (pnl.Name == RIBBON_PANEL1)
                {
                    panel1 = pnl;
                    break;
                }
                else if (pnl.Name == RIBBON_PANEL2)
                {
                    panel2 = pnl;
                    break;
                }
                else if (pnl.Name == RIBBON_PANEL3)
                {
                    panel3 = pnl;
                    break;
                }
                else if (pnl.Name == RIBBON_PANEL4)
                {
                    panel4 = pnl;
                    break;
                }
            }

            // couldn't find the panel, create it
            if (panel1 == null)
            {
                panel1 = a.CreateRibbonPanel(RIBBON_TAB, RIBBON_PANEL1);
            }
            if (panel2 == null)
            {
                panel2 = a.CreateRibbonPanel(RIBBON_TAB, RIBBON_PANEL2);
            }
            if (panel3 == null)
            {
                panel3 = a.CreateRibbonPanel(RIBBON_TAB, RIBBON_PANEL3);

            }
            if (panel4 == null)
            {
                panel4 = a.CreateRibbonPanel(RIBBON_TAB, RIBBON_PANEL4);
            }

            // get the image for the button
            Image img = Properties.Resources.BimLogo;
            ImageSource imgsrc=GetImageSource(img);

            Image img1 = Properties.Resources.QA1;
            ImageSource imgSrc1 = GetImageSource(img1);

            // create the button data
            PushButtonData btnData = new PushButtonData(
                "Library",
                "Library",
                Assembly.GetExecutingAssembly().Location,
                "PETE.Revit.RibbonButton.RunDynamo"
                )
            {
                ToolTip = "KEA BIM Cafe Library",
                LongDescription = "Here you can find all the necessary documents for your studies at KEA",
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData1 = new PushButtonData(
                "Button 2",
                "Button 2",
                Assembly.GetExecutingAssembly().Location,
                "PETE.Revit.RibbonButton.LinkRedirect"
                )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData3 = new PushButtonData(
                "FAQ",
                "FAQ",
                Assembly.GetExecutingAssembly().Location,
                "PETE.Revit.RibbonButton.Command"
                )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData4 = new PushButtonData(
              "Teacher",
              "Teacher",
              Assembly.GetExecutingAssembly().Location,
              "PETE.Revit.RibbonButton.Command"
              )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData5 = new PushButtonData(
             "Student",
             "Student",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData6 = new PushButtonData(
             "A",
             "A",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData7 = new PushButtonData(
             "B",
             "B",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData8 = new PushButtonData(
             "C",
             "C",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData9 = new PushButtonData(
             "D",
             "D",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData10 = new PushButtonData(
             "Push",
             "Push",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData btnData11 = new PushButtonData(
             "Run",
             "Run",
             Assembly.GetExecutingAssembly().Location,
             "PETE.Revit.RibbonButton.Command"
             )
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PulldownButtonData group1Data = new PulldownButtonData("PulldownGroup1", "Q&A")
            {
                Image = imgSrc,
                LargeImage = imgSrc
            };
            PushButtonData itemData11 = new PushButtonData("itemName11", "About KEA", Assembly.GetExecutingAssembly().Location, "PETE.Revit.RibbonButton.KEAHomePage")
            {
                ToolTip = "KEA BIM Cafe Library",
                LongDescription = "Here you can find all the necessary documents for your studies at KEA",
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData itemData12 = new PushButtonData("itemName12", "About BIM Cafe", Assembly.GetExecutingAssembly().Location, "PETE.Revit.RibbonButton.BIMCafeLinkedin")
            {
                ToolTip = "KEA BIM Cafe Library",
                LongDescription = "Here you can find all the necessary documents for your studies at KEA",
                Image = imgSrc,
                LargeImage = imgSrc
            };
            PushButtonData itemData13 = new PushButtonData("itemName13", "About BYG", Assembly.GetExecutingAssembly().Location, "PETE.Revit.RibbonButton.KEAByg")
            {
                ToolTip = "KEA BIM Cafe Library",
                LongDescription = "Here you can find all the necessary documents for your studies at KEA",
                Image = imgSrc,
                LargeImage = imgSrc
            };

            PushButtonData group2Data = new PushButtonData(
              "TaskDialog",
              "TaskDialog",
              Assembly.GetExecutingAssembly().Location,
              "PETE.Revit.RibbonButton.Command"
                );


            // add the button to the ribbon
            PushButton button = panel1.AddItem(btnData) as PushButton;
            button.Enabled = true;
            PushButton button1 = panel1.AddItem(btnData1) as PushButton;
            button1.Enabled = true;
            PushButton button3 = panel1.AddItem(btnData3) as PushButton;
            button3.Enabled = true;

            PulldownButton group1 = panel1.AddItem(group1Data) as PulldownButton;
            PushButton item11 = group1.AddPushButton(itemData11) as PushButton;
            item11.ToolTip = itemData11.Text; // Can be changed to a more descriptive text.
            PushButton item12 = group1.AddPushButton(itemData12) as PushButton;
            item12.ToolTip = itemData12.Text;
            PushButton item13 = group1.AddPushButton(itemData13) as PushButton;
            item13.ToolTip = itemData13.Text;

            PushButton group2 = panel4.AddItem(group2Data) as PushButton;
            PushButton item17 = group1.AddPushButton(group2Data) as PushButton;
            item17.ToolTip = group2Data.Text;  // Can be changed to a more descriptive text

            PushButton button4 = panel2.AddItem(btnData4) as PushButton;
            button4.Enabled = true;
            PushButton button5 = panel2.AddItem(btnData5) as PushButton;
            button5.Enabled = true;
            PushButton button6 = panel2.AddItem(btnData6) as PushButton;
            button6.Enabled = true;
            PushButton button7 = panel2.AddItem(btnData7) as PushButton;
            button7.Enabled = true;

            PushButton button8 = panel3.AddItem(btnData8) as PushButton;
            button8.Enabled = true;
            PushButton button9 = panel3.AddItem(btnData9) as PushButton;
            button9.Enabled = true;
            PushButton button10 = panel3.AddItem(btnData10) as PushButton;
            button10.Enabled = true;
            PushButton button11 = panel3.AddItem(btnData11) as PushButton;
            button11.Enabled = true;




            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }

       /* protected void BtnConfirm_Click(object sender, EventArgs e)  // Website redirect test for the button  
        {
            Response.Redirect("Confirm.apx");
        } */

        private BitmapSource GetImageSource(Image img)
        {
            BitmapImage bmp = new BitmapImage();

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Png);
                ms.Position = 0;

                bmp.BeginInit();

                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.UriSource = null;
                bmp.StreamSource = ms;

                bmp.EndInit();
            }

            return bmp;
        }
    }
}

 Should I put here the whole Dynamo script, because the script it self is working, so I am not sure if it will be much help.

Thanks once again!
Peter

Message 28 of 33
Kevin.Lawson.PE
in reply to: Anonymous

Were you able to reference all the .dlls in the list? In you're solution under references, all of the dynamo .dlls should be there and set to "Copy Local = false".  You reference them just like you referenced the Revit .dlls. 

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
Message 29 of 33
Anonymous
in reply to: Kevin.Lawson.PE

Hi, I think I might have missing "DesignScriptBuiltin" reference, but I can't really find it among other references, but I don't really know if that reference is necessary at all. I have also changed copy local to false on them, but it did not help. 

 

references.PNG

Message 30 of 33

Hi

 

Just a question. Why should it not run in manual mode?

Message 31 of 33
Anonymous
in reply to: Anonymous

@Anonymous Hi, are there any updates? Did you figure out the problem?

Message 32 of 33
rtg.dev015A9H9
in reply to: C.Utzinger

I know this is an old topic, but I've managed to run a dynamo script in manual mode via addin.

Just change the dictionary to this one:

 

            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                { Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                { Dynamo.Applications.JournalKeys.AutomationModeKey, false.ToString() }, //run journal automatically
                { Dynamo.Applications.JournalKeys.DynPathKey, Journal_Dynamo_Path }, //run node at this file path
                {
                    Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString()
                }, // The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not. If we are in automation mode the workspace will be executed regardless of this key.
                { Dynamo.Applications.JournalKeys.ForceManualRunKey, true.ToString() }, // don't run in manual mode
                { Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                { Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }

            };

 

Message 33 of 33
manhgt214
in reply to: rtg.dev015A9H9

It's worked for me.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report