Happy new year! After a very lazy and relaxing relatively tech free time in the countryside for 10 days, I’m ready to rock n’ roll… I’m sure this feeling of complete regeneration will last about a week, but gotta enjoy it while it lasts! 🙂
While kicking open wordpress (after several months of hiding in maths books and fun work projects) to my surprise I noticed there were a whole bunch of comments that wordpress decided not to let me know…yeah, tell me about it is exactly the right turn of phrase here.
One of the comments by Cathy was quite interesting and something I never really looked into before. She asked is there a difference between importing modules using the clr.ImportExtensions() method vs. the standard Python way of import from. To be honest, I didn’t actually know the answer to this, so of course, dug around a little to find out…
After some digging I found out what the deal was, and it didn’t really surprise me as it is sort of self explanatory and I had to roll my eyes at myself for not knowing this. So, the difference is simple…
Classes are made up of Methods and Properties (amongst other things), Extension Methods are methods that are sort of stuck onto a Class. These aren’t defined in the Class itself, but usually in another class. The benefit to Extension Methods is that you can extend a Class without having to change the original Class in any way or recompile it. This is really useful as you can make your own custom Helper Methods in your own project that you can use on someone else’s Class. So, with that bit out the way, how does this relate to the question asked…
Well, we still have to load these Extension Methods into python, but the standard python import from doesn’t map the Extension Methods to the Class. This is why iron python has the clr.ImportExtensions() method, this will map the Extension Methods to the correct Classes. This can be demonstrated with the following code examples…
With clr.ImportExtensions() method…
import clr
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
elem = UnwrapElement(IN[0])
OUT = elem.Location.Curve.ToProtoType()
With import from…
import clr
clr.AddReference("RevitNodes")
import Revit
from Revit.GeometryConversion import *
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
elem = UnwrapElement(IN[0])
OUT = elem.Location.Curve.ToProtoType()

In the above image I used a Beam as my test subject, but you can use any type of curve driven Element.
You can see that the With import from node failed with a message saying that ‘Line’ object has no attribute ‘ToProtoType’. However, the With clr.ImportExtensions() node worked just fine. This is because the .ToProtoType() method is an Extension Method and extends on what I assume is most geometric class types of the Revit API – in this case the Revit Line Class. Without using the clr.ImportExtensions() method, these Extension Methods are not mapped to the appropriate Class and therefore fails to run as by default the Revit Line Class does not have a method .ToProtoType().
Well, that about wraps it up…I hope this post was enlightening to you and as always, thanks for reading! 🙂
Leave a Reply