i was wondering if its possible to get a list of all legends in a project without using any extra packages? Would it be possible to filter the view list for example? I tried to filter with python and was unfortunately not succesful… thanks!
Place your code below this line
list = IN[0]
newlist =
for x in list:
if “Legend” in x:
newlist.append(x)
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
a = FilteredElementCollector(DocumentManager.Instance.CurrentDBDocument).OfCategory(BuiltInCategory.OST_Views).ToElements()
b = []
for i in a:
if i.ViewType.ToString() == "Legend":
b.append(i)
OUT = b
@fluffyhugger has the right of it so should be marked as solution. However, a slightly neater way to write this would be…
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
OUT = [v for v in FilteredElementCollector(doc).OfClass(View).ToElements() if v.ViewType == ViewType.Legend]
This is a tad cleaner, but also gets rid of the need for String Literal Comparison by using the ViewType enumarable, this has all the ViewTypes in it (have a look by typing ViewType. and you can see all the ViewTypes come up in the intelisense ). TIP: The reason I mention String Literals is that they can lead to errors (especially with BuiltIn Parameters) as the user visible name in the Revit UI may not always be the same depending on the users language settings. In this case it may work, but it is just good practice to steer away from them wherever you can.
I have been trying to learn Dynamo since over one year, I went to codecademy to learn Python, then back to Dynamo and I am still having trouble understanding how to use it with revit specifically. That scripts are impressive for me, how did you guys get there?! I would appreciate suggestions of how and where to learn Dynamo + Python for Revit very much! Cheers and thanks!
Yeah, it can be a little challenging to learn… There are a whole bunch of posts about learning on this forum, just type in learn python and you’ll get about 20 separate threads. Her is one of them…
I’ve imported libraries like RevitAPI, RevitNodes, RevitServices
Then I’ve assigned the input values
Then just executed an action within the TransactionManager
Just look inside the python scripts of Clockwork. It’s all the same pattern, almost every time. If the script has a lot of stuff in it, at it’s core it’s still execution of a action within the TransactionManager. The rest come naturally.
To clarify, TransactionManager you need to do something within Revit. If you just doing some logic stuff in Dynamo, you don’t need that. The best technique to learn is just to snoop in all of those scripts from Clockwork, Archilab and many others, and try to see the logic.
We are a little off topic and on well trodden ground… Haha!
However, it’s about time spent really, some people pick up python and the Revit API really quickly and have that moment where it clicks quite quickly, I wasn’t one of them, it took me a long time everyday trying to understand how it all works. The Revit API is NOT python, a lot of people think that by learning python you will automatically understand the Revit API, but this is simply not the case, the Revit API is a little beast in its own right and needs time to understand, but is easier to grasp if you have some basic knowledge in Object Orientated Programming or OOP.
The best suggestions I can offer are the following…
Do a course in OOP in the language of your choice (python maybe better in the context of the Dynamo - I learnt in c# as Dynamo didn’t exist at the time but the concepts are pretty much the same).
Also, as @fluffyhugger suggests, to look at what others do in their nodes. Just like in a lot of things, we learn by imitation. Pick apart other peoples code and understand what they are doing in each step.
Google is your friend and so is this forum! Don’t be afraid to ask questions! We are here to help and won’t judge, we were all where you are once. Also, you will find some good resources by looking.
Read the RevitAPIDocs. I spent a lot of time just reading this (booorrrring maybe… did it pay off? Definitely!)
Help other people with their code, use the Questions on this forum as a challenge. This will help you improve quicker as you will learn faster if you have some context or goal.
There are a lot of added references that can probably be culled…
import sys ##Standard system input
import clr
##https://gist.github.com/gtalarico/e6be055472dfcb6f597e3dcd20d11f37
from Autodesk.Revit.DB import FilteredElementCollector
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Drafting Views
from Autodesk.Revit.DB import BuiltInCategory, BuiltInParameter
from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element, ViewFamily
from Autodesk.Revit.DB import ViewFamily
from Autodesk.Revit.DB import ViewType
from Autodesk.Revit.DB import Transaction
###Creates a Drafting View###
from Autodesk.Revit.DB import Transaction, Element, ElementTransformUtils
##https://forum.dynamobim.com/t/collecting-all-elements-of-family-types-in-active-view/19838/2
from Autodesk.Revit.DB import BuiltInCategory, BuiltInParameter
# ViewFamilyTypes and Drafting views creation
from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element
from Autodesk.Revit.DB import ViewFamily
import System ##filterAnnot = System.Predicate <<Work on removing
from System.Collections.Generic import List ##Not same as type() = List <<Work on removing
import math ##For truncate to integer-RA
import re ##Regular expressions for 'natural' sort
doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
#ViewsAll = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
OUT=[v for v in FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements() if v.ViewType == ViewType.Legend]
import sys ##Standard system input
import clr
##https://gist.github.com/gtalarico/e6be055472dfcb6f597e3dcd20d11f37
clr.AddReference("RevitAPI","RevitServices")
from Autodesk.Revit.DB import FilteredElementCollector
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Drafting Views
from Autodesk.Revit.DB import BuiltInCategory, BuiltInParameter
from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element, ViewFamily
from Autodesk.Revit.DB import ViewFamily,View
from Autodesk.Revit.DB import ViewType
from Autodesk.Revit.DB import Transaction
###Creates a Drafting View###
from Autodesk.Revit.DB import Transaction, Element, ElementTransformUtils
##https://forum.dynamobim.com/t/collecting-all-elements-of-family-types-in-active-view/19838/2
from Autodesk.Revit.DB import BuiltInCategory, BuiltInParameter
# ViewFamilyTypes and Drafting views creation
from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element
from Autodesk.Revit.DB import ViewFamily
import System ##filterAnnot = System.Predicate <<Work on removing
from System.Collections.Generic import List ##Not same as type() = List <<Work on removing
import math ##For truncate to integer-RA
import re ##Regular expressions for 'natural' sort
doc = DocumentManager.Instance.CurrentDBDocument
OUT = filter(lambda x: x.ViewType == ViewType.Legend,FilteredElementCollector(doc).OfClass(View).ToElements())