Dynamo Python issue about creat view and filter

Hi, can I get some help from here?:grinning:
Before many thanks for your any reply.
Question 1::grinning:
I have defined a def in python but its aways with warming - unexpected token ‘newline’, see the below snapshot. (This script is work to creat a default 3d view.)

Question 2::joy:
Here is another script also with some issue - aways with the blow snapshot warming.
(This one is work to create some view filter.)


And here is the Python script includes two def be defined.
import Autodesk
from Autodesk.Revit.DB import *
import System
from System.Collections.Generic import *
doc = revit.ActiveUIDocument.Document

# Get category list of the project parameter
def _GetParamCat(param):

    # Collect parameter and category list
    params = []
    cats = []
    iterator = doc.ParameterBindings.ForwardIterator()
    while iterator.MoveNext():
    	params.append(iterator.Key.Name)
    	thesecats = []
    	for cat in iterator.Current.Categories:
    		thesecats.append(Category.GetCategory(doc,cat.Id))
    	cats.append(thesecats)

    #Return category list
    if param in params:
        return cats[params.index(param)]
    else:
        return ["%s is not a parameters name" %(param)]


def _CreatParamStrEqFilter(param):
    cats = _GetParamCat(param)

    #DB transaction
    tran = Transaction(doc,"Creat ID Filter")
    tran.Start()

    #Get parameter from parameter category List
    Elemcollector = []
    for i in cats:
        collector = FilteredElementCollector(doc).\
                    OfCategoryId(i.Id).\
                    WhereElementIsNotElementType().ToElements()
        Elemcollector.extend(collector)

    #Get all parameter values
    pvalues = [""]
    for elem in Elemcollector:
        para = elem.LookupParameter(param)
        pvalues.append(para.AsString())
    pvalue = list(set(pvalues))

    #Creatd a new rule
    rules = []
    for i in pvalue:
        rules.append(ParameterFilterRuleFactory.CreateEqualsRule(para.Id,i,False))

    #Typed category list ------- Important!!!
    ids1 = list()
    for i in cats:
        ids1.append(i.Id)
    catlist = List[ElementId](ids1)

    rulelist = List[FilterRule](rules)
    
    #Create parameter filter elmenets
    filters = []
    for i in pvalue:
        if len(i) == 0:
            name = 'Id - ' + i
        else:
            name = 'Id - Empty'
        filters.append(ParameterFilterElement.Create(doc,name,catlist,rulelist[pvalue.index(i)])

    tran.Commit()
    
    return filters
    
a = _CreatParamStrEqFilter("ACTIVITY ID")
print a
  1. Line 25 is missing a colon “:” at the end of the definition.
  2. filters.append(ParameterFilterElement.Create(doc,name,catlist, is missing a closing bracket “)” at the end
2 Likes

Many many many many thanks Kibar genius.:grinning::grinning:
How can you so fast find it? If someone has a lot of code experience will get it?~~~ Very appreciate.

I have changed to the script below, but here with a new warming. about it do you have any idea:wink:


The Syntax Errors with traceback... are usually simple mistakes in which you forgot a bracket, a colon, a dot notation or similar.

The error messages themselves help you to locate what/where approximately the error is. If you look at the first 2 lines of your new error, you’ll see that your variable rule is raising an exception. Try to find which of the rules is causing the exception and how you constructed the rules. Maybe by using only 1 rule at a time.

Locate the error, find out what causes it, and fix it. You’ll learn a lot through it.

Google “python try except” and “reading exception messages”. It will help you in the future.
Good luck!

1 Like

Many appreciate your reply:blush:, I think I will learn more knowledge about python and rev it API from your suggestion.
Tomorrow I will try to resolve the problem of this script.
Absolutely here is a genius community.

1 Like