简体   繁体   中英

revit python macros vs scripts

Why does the following code work as a script inRevitPython Shell but throw errors as a Macro when added to a Module? the idea is to collect the doors, query their host object for fire rating and frame details, then asign them to the doors.

The error is "None type has no attribute set()". So it must be the door elements throwing this error. But still, why does the very same code (only modifying how doc is defined) perform as a script but not a macro?

def updateDoors(self):
    doc = self.Document
    doors = FilteredElementCollector(doc).OfCategory( BuiltInCategory.OST_Doors ).WhereElementIsNotElementType().ToElements()

    t = Transaction(doc, 'Door Update')
    t.Start()

    for d in doors:

        wallRating = "---"
        doorRating = "---"
        doorNumber = "---"
        wallFr = "---"
        wallH = "---"
        wallJ = "---"

        if d.Host is not None and d is not None:

            wallTypeId = d.Host.GetTypeId() 
            wall = doc.GetElement(wallTypeId)
            if wall.LookupParameter('Fire Rating') is not None:
                wallFr = wall.LookupParameter('Fire Rating').AsString()
            # Get Door Jamb from the wall
            if wall.LookupParameter('Frame Jamb') is not None:
                wallJ = wall.LookupParameter('Frame Jamb').AsString()
            # Get Door Head from the wall
            if wall.LookupParameter('Frame Head') is not None:
                wallH = wall.LookupParameter('Frame Head').AsString()

            if str(wallFr) == '':
                d.LookupParameter('Fire Rating-Instance').Set('--')
            if str(wallFr) == '0':
                d.LookupParameter('Fire Rating-Instance').Set('--')
            if str(wallFr) == '1':
                d.LookupParameter('Fire Rating-Instance').Set('45')
            if str(wallFr) == '2':
                d.LookupParameter('Fire Rating-Instance').Set('90')
            if str(wallFr) == '3':
                d.LookupParameter('Fire Rating-Instance').Set('120')

            if d.LookupParameter('Over ride wall assigned details').AsValueString() == 'No':    
                d.LookupParameter('Jamb').Set(wallJ)
                d.LookupParameter('Head').Set(wallH)

    t.Commit()

Run it in the SharpDevelop debugger and you will see for yourself which line of code causes the problem. That will enable you to see the values of all the variables, and you can examine which of them is None.

Furthermore, this line is very weird:

wall = doc.GetElement(wallTypeId)

It is assigning the wall type to the variable wall.

All parameter values are being read from the wall type , not the wall instance.

Is that your intention?

If so, I recommend renaming the wall variable to wallType to avoid confusing yourself and others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM