简体   繁体   中英

How to pick the right LoadFamily function in revitpythonshell

revitpythonshell provides two very similar methods to load a family.

LoadFamily(self: Document, filename:str) -> (bool, Family)
LoadFamily(self: Document, filename:str) -> bool

So it seems like only the return values are different. I have tried to calling it in several different ways:

(success, newFamily) = doc.LoadFamily(path)
success, newFamily = doc.LoadFamily(path)
o = doc.LoadFamily(path)

But I always just get a bool back. I want the Family too.

You can get at the overload you are looking for like this:

import clr
family = clr.Reference[Family]()
# family is now an Object reference (not set to an instance of an object!)
success = doc.LoadFamily(path, family)  # explicitly choose the overload
# family is now a Revit Family object and can be used as you wish

This works by creating an object reference to pass into the function and the method overload resultion thingy now knows which one to look for.

Working under the assumption that the list of overloads shown in the RPS help is the same order as they appear - which I think is a pretty safe assumption to make, you can also do this:

success, family = doc.LoadFamily.Overloads.Functions[0](path)

and that will, indeed, return a tuple (bool, Autodesk.Revit.DB.Family) .

Note, that this has to happen inside a transaction, so a complete example might be:

t = Transaction(doc, 'loadfamily')
t.Start()
try:
    success, family = doc.LoadFamily.Overloads.Functions[0](path)
    # do stuff with the family
    t.Commit()
except:
    t.Rollback()

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