As I explained in the previous tutorial, A quick look inside the Python Node, we need the CLR to add references to .NET libraries. Within that library there are a bunch of goodies we want to use in our code.
The “Boiler Plate” Code that ships inside of the Python Node shows you how this can be done.
To reference a .dll we must first use the CLR function to Add a Reference. This can be done by typing the following:
This method of adding references is adding references in the current directory, this directory has been hardwired to be the directory of the Dynamo Instance you are using. But you can add from another directory by doing the following:
However, most of what you will need is in the Dynamo folder and I will only be covering what is in there or we would be here forever.
We can also find out what references are loaded by doing the following:
These are all the References loaded into the Python Script although you will most likely never have to do this, this was just to show you how you could check that.
But what do we do from here?
Well, we need to access those References and pull out the Modules we need. This can be done by the following:
As mentioned in the previous tutorial, we added a reference to “ProtoGeometry.dll”, and from this we want to import everything from the Namespace “Autodesk.DesignScript.Geometry”.
The “*” after import denotes that we want to import it all, but you can be a little more selective, for instance…
We are importing only the Point Class and its methods/properties
We are importing the Point and Surface Classes only and their methods/properties
We are importing the Point Class only and its methods/properties, but, we are using a local name for this as a variable. So, in this case, we can use the local name of “pt” instead of writing “Point”. Note: Creating local variable names can be handy, but you don’t get intellisense support when scripting (where it auto-predicts what you may want to type. Very useful). So, you need to know what you are writing or you’ll get a few “Object has no Attribute” errors.
A great place to understand importing modules is a post by Fredrk Lundh.
So…What do we need?
Here I have my Boiler Plate code, basically, I use this and take out what I don’t need to use. You probably don’t want all your scripts to start off with all of this code, plus importing unnecessary modules will probably cause your run time to slow down.
I won’t be looking too deep into System Operations or the OS in these tutorials, but, there are a tonne of tutorials on this online. Dot Net Perls has plenty about it as well as Stack Exchange and other sites, just type in what you are trying to do and I guarantee someone has asked the question already and it has been answered. If I need to use anything from the OS/System modules I will explain what and why as and when we need to.
We are using the standard Reference to the ProtoGeometry.dll which gives you access to pretty much all the Geometry Operations in Dynamo. This, as mentioned several times, comes inside the Python Code in any new instance of a Python Node.
- You will need to Reference the RevitServices.dll to gain access to the current Document, UIDocument and Revit Application objects. From there you can use these objects in conjunction with the Revit API.
- This also gives you access to the TransactionManager. In C# this is slightly different but we won’t get into that, suffice to say, if you want to commit a command to Revit, you need to import the TransactionManager from RevitServices. We will look into how to use the TransactionManager in later tutorials when using the Revit API.
- Referencing RevitNodes in will give you access to the Revit Nodes that come OOTB in Dynamo. Have a look at the examples that are in the DynamoPrimer.
- This also gives you access to the Dynamo/Revit Conversion utilities. You will need this when converting between Dynamo and Revit Data Types.
- To really start messing around and creating whatever you want in Revit with Dynamo you will need to reference the RevitAPI.dll. This is a powerful beast and is well documented. We will be looking into this in later tutorials quite a bit as this really is what we want to learn.
- You won’t need to Reference the RevitAPIUI often unless you want Revit Popups to appear or you want to access the UI side of Revit e.g. To pick and object etc. Nonetheless, this is an equally powerful thing to learn given the right context and we will be covering some practical applications using this.
NOTE: You will need to be careful when using the Revit API as this changes between versions of Revit. New things are added and some things are removed (deprecated). You can check what version you are using if you want to be thorough with you code and ensure that the node works in all versions. We will cover this in more detail in later tutorials.
Well, that about sums up References. You may need some that aren’t on the list above, but for pretty much all cases this will be what you need. We will look at The basics of using the Revit API in the next tutorial.
August 13, 2021 at 3:37 am
Hi, great explanation with pictures and examples. I got one major question:
How do you know what modules can be pulled from a reference. I have a reference called “RevitAPISteel”, and I would love to know what the names are of the modules that I can import from that reference.
August 15, 2021 at 9:14 pm
You can use the dir function in python to list its contents.
August 15, 2021 at 9:15 pm
Or if you have api documentation, then use that of course