From the course: Dynamo for Revit: Python Scripting

Creating line-driven elements

- [Instructor] In an earlier video, we've used Python to place a point-based family using points from Dynamo. Now let's have a look at creating line-based elements such as walls and floors by utilizing what we've learned so far. I currently have the Revit and Dynamo exercise files open for this video. In this graph, I've created a polygon from a set of points that are driven by two lists of integers. Let's use these lines to create a new floor in the Revit project. Then we'll use the same lines to create some walls. To find out how to create a floor, let's switch over to Visual Studio and have a look. Go ahead and search for floor. We can see in the floor class that there is no method to create a floor. This is a quirk of the Revit API, in which some of the creation methods are within this namespace from when the first Revit API was released. So let's go ahead and search for new floor. As you can see, there are three different ways to create a floor. Let's use the method that takes a curve array and a boolean. The curve array is a Revit API object similar to a list that can be created with a curve array constructor. Lines or curves can then be appended to it. The boolean in this method simply determines whether the floor is structural or non-structural in nature. So let's use the new floor method to create a floor. Go ahead and open up the Python script. As you can see, I've already imported the libraries we need, created an empty list, and retrieved the document. So let's store the incoming lines into the variable lines, making sure to convert it to Revit type geometry by using the ToRevitType extension method. Next, let's create a curve array object which we can append these to. To do this, let's use the variable carray, and we'll simply assign to it a new curve array using the constructor that takes no parameters. Next, we need to loop through each of the incoming lines and append each one to this empty array. So for line in lines, carray.Append(line). Now that we have our curve array object, we can create a new floor. As you can see, I've already started and closed a transaction. So let's go ahead and use the new floor method by creating a variable named floor, and assign to it doc.Create.NewFloor. For the parameter, let's use our curve array, and false, for non-structural. Then, let's append that floor to the output list and wrap it so that it's Dynamo controlled. So, floor.ToDSType, with false as the parameter. Then hit save changes. Great, so it looks like we have a new floor, and if we switch over to Revit, you can see our new floor is in the project. Now let's use those same lines to create walls within our project. To do this, let's switch over to Visual Studio again, and have a look at how to create walls. Go ahead and search for wall. Within the wall class, you can see that there are a few different ways to create walls. Let's use this method that simply takes the document, a curve for the location of the wall, and element ID for the level the wall is on, and a boolean for structural or non-structural. Go ahead and open Python back up, and as we already have the document and lines, all we need to do is get the level ID that the floor is to be created on. We can get this from the level parameter on the floor we've just created. So let's create a variable named level, and we'll assign to this the level we need. Let's use the lookup parameter method, using the string level as the parameter. The level parameter will store an element ID for the level that it's referencing. So let's retrieve it as an element ID. Great, so now that we have our level ID, let's go ahead and create some walls. As our Revit lines are a type of curve, we can loop through each of these again and create a wall. So let's start another loop through our lines, using the variable line again. And with each loop, let's create a variable named wall, and we'll assign to this a new wall using the create method from the wall class. For the parameters, we need the document, the line, the level ID, and a false boolean, so that it's non-structural. Then let's append this to the output list, and wrap it so that it's Dynamo owned. And go ahead and hit save changes. Awesome, so it looks like we have some new walls output from the Python script. Let's have a look in Revit. Perfect, we have our walls in Revit. As we have wrapped the walls and floor, if we update the incoming points, our floor and walls will update automatically. For example, let's change this second integer to 12000. And if we switch over to Revit, we can see that the wall has been updated.

Contents