<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=157406051691882&amp;ev=PageView&amp;noscript=1">

Dynamo Code Blocks Harness Advanced Features

AutoCAD Programmming Dynamo April 20, 2022

CAD Programming: Dynamo, Part 3. Learn how to use Dynamo code blocks in Civil 3D and Revit.

CADProgramming-450

 

In previous articles, we explored the Dynamo environment and how to create AutoCAD objects using Dynamo. Specifically, we learned: 1) how to create a Dynamo script and draw a line and 2) how to draw a polyline using an externally stored point file. These simple examples demonstrated how to use basic Dynamo features and create objects programmatically without typing any code.

You can continue to build more powerful Dynamo applications without typing code, but before proceeding too far, you might want to become familiar with something called the code block. Yes, it involves some simple coding concepts, but you don’t have to type much code. In fact, the code block can help introduce you to coding if you’ve never typed any code. It can also harness advanced Dynamo features while simplifying your Dynamo graphs — keeping them cleaner and more manageable.

 

Getting Started with Code Blocks  

As with the previous articles, this example will be based on a Civil 3D environment, but most concepts also apply to Revit and other Dynamo-compatible products.

1. To get started, open a Civil 3D (or Revit) drawing and start a new Dynamo session. (Refer to the first article if you need a refresher on how to do this.)

2. Double-click anywhere in the workspace to add a code block node. (You can also click and expand the Script category of the Library pane to access the Editor and Code Block.)

 

042022_CADProg-fig1-450

Double-click anywhere in the workspace to add a code block node. Click image to enlarge.

           

3. In the code block, type a phrase enclosed in quotation marks, such as “Hi there.”

4. Click in the shaded area below your phrase to see it displayed back to you.

042022_CADProg-fig2

5.  Replace the text phrase with a mathematical expression, such as 2+2. Do not enclose this expression in quotation marks.

6. Click in the shaded area to see the answer displayed.

042022_CADProg-fig3

As you can see, the code block can serve as a handy calculator or text handler for Dynamo projects. But, this is just one small role of the code block.

 

Using Code Blocks as Flexible Nodes

In addition to handling numbers and text, code blocks can handle other data types, formulas, and scripts. Because of its flexibility, the code block can be a great tool for providing input and simplifying tasks in Dynamo.

For example, in our first article, we demonstrated how to create a line in AutoCAD using pre-defined point coordinates. The Dynamo graph looked like this:

 

042022_CADProg-fig4-450

A Dynamo graph. Click image to enlarge.

 

Instead of creating separate nodes for each coordinate value, you can use code blocks to provide the same input in two nodes instead of six. Dynamo recognizes when the code block contains numbers, providing the correct input format for creating points.

 

042022_CADProg-fig5-450

Dynamo recognizes when the code block contains numbers, providing the correct input format for creating points. Click image to enlarge.

Because code blocks use the same DesignScript language used by other Dynamo nodes, you can essentially access all types of Dynamo nodes with code blocks. As another example, let’s use a code block to replace the Point.ByCoordinates node, as well as the coordinate nodes.

1. Create another code block and type the word “Point” without quotation marks. Dynamo displays a list of nodes including the phrase “Point.”

042022_CADProg-fig6

2. Add a period after the word “Point” and you’ll see ByCoordinates and other choices.

042022_CADProg-fig7

3. Double-click ByCoordinates, then type a left parenthesis and you’ll see that Dynamo needs two values — x- and y-coordinates — to represent the point.

042022_CADProg-fig8

4. Type in numeric values for x and y, separated by a comma followed by a right parenthesis, to define the point location.

This feature allows you to create the Point.ByCoordinates node — and virtually any another Dynamo node — using a code block. In this example, we encapsulated point creation in one node instead of four or more nodes.

 

Node to Code

Taking the code block concept a step further, Dynamo includes a nifty feature called “Node to Code” that quickly converts a series of nodes into a code block. To use this feature, simply select a set of nodes graphically by clicking and dragging a box around the nodes, then right-click in the workspace, and select Node to Code. Dynamo converts the nodes to a single block of code. Here’s what the code block looks like for the line creation nodes shown previously.

 

042022_CADProg-fig9-450

Here's what the code block looks like. Click image to enlarge.

 

Now you can impress your boss, your coworkers, and others by showing them you know how to create code — without ever typing any code! In a more practical sense, when you reduce complex Dynamo graphs to code blocks, you simplify the workspace and enable easier reuse of scripts.

 

Code Block Functions

Code blocks can also serve as functions, where a block of code can be called by another block of code to perform a task, or function. To demonstrate this, let’s create a function that draws a series of AutoCAD lines using the DesignScript language. Syntax rules and other information can be found at dynamobim.org.

1. Create another code block and type three lines of code as shown below. 

def LineDrawer(point1)
{
};

The first line starts with the key word “def” to define the function. The function name is “LineDrawer,” followed by an input parameter (point1) in parentheses. Braces define the body of the function. 

2. Add the following lines of code after the first brace: 

// point0 will always be at origin
point0 = Point.ByCoordinates(0, 0);
line1 = DesignScript.Line.ByStartPointEndPoint(point0, point1);
return=line1;

The two forward slashes identify a comment line, which is a place where you can type any comments you like to document your code. The next line of code defines point0 as always located at the origin (0,0). The next line of code draws an object (a line) using a technique similar to that demonstrated in our first article. In this example, the starting point is always located at the origin. The second point (point1) will be determined by another code block. The remaining line of code tells Dynamo to return the object called line1 as the result of this code block. The first code block, which will be referred to as the parent code block, should now look like this:

042022_CADProg-fig10

 

3. Create another code block and one line of code as shown below.  

042022_CADProg-fig11

This simple code block creates a range of values spanning from 0 to 90, divided into 10 parts.

4. Add a Point.ByCoordinates node as demonstrated in the first article. 

042022_CADProg-fig12

5. Add another code block as shown below. 

042022_CADProg-fig13

6. Connect the nodes as shown below.  Notice that the first code block, which is the parent code block, is not connected to the other nodes. It is the function called by the final code block.

042022_CADProg-fig14-450

The first code block, which is the parent code block, is the function called by the final code block. Click image to enlarge.

 

7. Right-click on the Point.By.Coordinates node and change the Lacing property to Cross Product. Lacing defines how lists are connected. The Cross Product method makes all possible connections to produce the longest list of points.

 

042022_CADProg-fig15

 

8. Click Run in the lower-left corner of the Dynamo window to run your script. (You can skip this step if you’re running Dynamo in Automatic mode.) You should see a series of lines as shown below.

 

042022_CADProg-fig16-450

Once complete, you should see a series of lines. Click image to enlarge.

 

What Next?

Now that you’ve seen how to use code blocks, you can add this to your tool box for creating Dynamo scripts. You can still rely on the graphical approach to create Dynamo scripts and avoid typing code, but code blocks open some significant doors to developing more powerful scripts. As shown in the previous example, you can automate redundant tasks, such as drawing multiple lines with variable end points, instead of manually creating one line at a time.

In a similar manner, you can also tap into more robust programming languages such as Python. By doing so, you can introduce key programming features such as looping and conditional branching (if/then statements) into your scripts. 

For more advanced programmers, you can write external routines in other programming languages such as C# and call them from Dynamo. You can develop these routines in environments such as Visual Studio.NET and save them as dynamic link libraries (DLLs) that can be reused by multiple programs. 

We’ll touch on some of these concepts in future articles. If you have specific ideas for programming topics, feel free to drop us an email. Until then, have fun with Dynamo!

 

CAD-PROGRAMMING_PILLAR_CTA

 

 

 

Andrew G. Roe

Cadalyst contributing editor Andrew G. Roe is a registered civil engineer and president of AGR Associates. He is author of Using Visual Basic with AutoCAD, published by Autodesk Press. He can be reached at editors@cadalyst.com.

View All Articles

MORE ON THIS TOPIC

More Fun with AutoLISP

In aprevious article, we reviewed how AutoLISP can automate repetitive CAD tasks. Specifically, we used the Visual LISP (VLISP) Editor to build a...

Combining Python and .NET

In previous articles, we’ve seen how Python can add value to your programming toolbox. Specifically, we’ve seen: 1)how to get started with Python,...

More Fun with Python and pyautocad

In aprevious article, we introducedPythonas an open-source programming platform. We also saw how to use Python in conjunction withDynamoto...