www.dynoscript.xyz

Video Tutorial: Turn your Dynamo Scripts into Add-Ins using C #

Luis Alonso Otero Seminario

--

Have you ever wanted to develop an Add-In that works just like your Dynamo routines? Many times we want to share our Scripts within our office or among some colleagues sending only the file (*.dyn). This is very useful most of the time but rarely the routine goes perfectly the first time it is executed, and it's necessary that the person who implements it has the Dynamo knowledge, to could get into the nodes and modify something if is necessary. It gets even more complicated when you don’t have the correct version of Dynamo or the same packages downloaded and installed on your computer.

Having an external Add-in solves the implementation problems and execution of the routine for a Revit user that is not handled by using Dynamo.

Here we explain the procedure we use to transform our routines in Add-ins ready to send, install, and execute.

Step 1: First of all we need the Dynamo routine we want to transform. In this case, we would use the “SetViewOnSheetByCode.dyn” routine.

We built our routine and we use the nodes “Tool.PlaceViewsOnExistingSheets” from the “Steam Nodes” package. This node does the desired task of placing the view on the indicated sheet. If we open the nodes, we see it is a Custom Node with the written code on Python. This code will serve as a reference later when we write our Add-in in C#.

We try the Script placing the “Level 2 — Lighting Plan” view on the sheet “E201”, then we open Dynamo and we write the code on the node “String” as shown below.

The Script works properly and places the view on the indicated sheet.

Step 2: Now we want to do the same but this time instead of using Dynamo, we are using an Add-in located in the upper flanges. The first to do is write the code in C# and for this, we open the Macro Manager that Revit gives us, and about this Development Framework called SharpDevelop, we will write our code.

The Revit’s Development Framework is called SharpDevelop, and when you open it, it shows us a screen as is the bottom image.

In the middle part is where we will write our code, for which we will use the Revit API Docs as a guide and reference, you can download it or consult for free from here: http://www.revitapidocs.com/

It is necessary to have knowledge about the C# programing language to be able to find the algorithm that does the same that the Dynamo’s script does.

The method: Public Void PlaceActiveView()

It is the one that places the view inside the indicated sheet, and it will be the one we will use later when we write our code for the Add-in.

Once developed the code, the SharpDevelop remains as shown above and now we are going to prove it. When executing the macro (Run Bottom), a window opens, where we will enter the code of the sheet where we want to place the view: E201.

This is the developed code at C# to our Macro:

Step 3: We see the Macro keeps inside of our software or inside of our model file. Which is uncomfortable if what we want is to share it easily with other users.

That is why we have to keep this written code inside of the Macro towards an extern Add-in and we will use the Microsoft Visual Studio’s Development Framework, to writing again our code at C#. In spite of the language is the same, the code for our app it is not exactly the same inside the macro, as within the Visual Studio, although it keeps the same logic and order for the command that executes our task: Place the view on the indicated sheet.

Visual Studio Development Interface:

We create the classification:

Class ThisApplication : IExternalCommand

And within it is the method:

public Result Execute (ExternalCommandData, ref string message, ElementSet elements)

Where the code previously obtained in the Macro method goes:

public void placeActiveView()

Developed code at C# for our Add-in on Visual Studio:

Step 4: We try placing a button on the top of the flanges inside the Revit. We assign it an image as an icon and some content in the description about what task or command it is.

A form is reopened asking about the code on the sheet (Sheet number).

And it works properly again placing the view on the sheet we indicate before: E201

If we want to place again the same view on another sheet, it will send to us a message saying the view is already on a different sheet.

And if we write the sheet code wrong, also it will send us an alert saying that these sheet number does not exist.

Having these clear processes, we can develop and convert any Scripts we have developed on Dynamo (*.dyn) towards an internal Add-in and thus be able to distribute them to all the users we want ready to be installed automatically.

if you have C# and Visual Studio knowledge, see the code at GitHub: https://github.com/alonsooteroseminario/myMacros

Developer: 👨‍💻 Luis Alonso Otero Seminario

Email: 📩 alonsooteroseminario@gmail.com

Consulting: 📍 https://luisotero.vercel.app/

Sources:

  1. http://generativecomponents.blogspot.com/2017/11/from-dynamo-to-macro-to-external.html
  2. https://forums.autodesk.com/t5/revit-api-forum/how-convert-simple-macro-to-revit-api/m-p/7003337
  3. https://boostyourbim.wordpress.com/2012/12/24/converting-a-macro-into-an-add-in/
  4. https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/simplecontent/content/lesson-1-the-basic-plug.html

--

--