Get Legends in project

Hello everybody,

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)

Assign your output to the OUT variable.

OUT = newlist

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
3 Likes

@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. :slight_smile:

Cheers,
Dan

4 Likes

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…

2 Likes

It’s not that difficult at it’s core. Python I mean.

The general idea from what I’ve learned so far is simple: you call libraries whit functions and then execute them. The functions that you can execute within Revit are listed in the API docs https://www.revitapidocs.com/2019/7a3b5ac1-c66d-9f81-a11d-9bcd4e026295.htm

Take this for example Spot coordinates

  1. I’ve imported libraries like RevitAPI, RevitNodes, RevitServices
  2. Then I’ve assigned the input values
  3. 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.

1 Like

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.

1 Like

guys, you are amazing but how can I know to use that keys… it is not enough to know Python… I can’t somehow get it… or I need much time :S

We are a little off topic and on well trodden ground… Haha! :slight_smile:

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.

But most importantly don’t give up! :slight_smile:

6 Likes

Thank you Daniell! You are an amazing motivator! I won’t give up! I am really enjoying when I learn something new on this way!

Perfect to have you guys on this forum!

Best greetings!

1 Like

to avoid python :renfrogné:

2 Likes

Sometimes I think if I learn DesignScript more than Python will be better for me. It is interestingly very effective!

1 Like

I learned so much reading https://danimosite.wordpress.com/2016/12/28/a-quick-look-inside-the-python-node/ and just realized you wrote it - so here a very big THANK YOU, amazing work!! :trophy:

2 Likes

Thanks @bialmeida! Much appreciated! :blush:

1 Like

Will this be able to copy legends between projects?

We’re using 2019 and it doesn’t have any feature to copy legends between projects…

In R22 “View” doesnt work but this does : )

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]



It works on my side…

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())

1 Like