Revit API & Dynamo,从项目文档创建族参数

Revit API & Dynamo, Creating a Family Parameter from Project Document

我正在尝试通过调用项目文档中的族文档并使用 FamilyManager 方法编辑族来创建新的族参数。在 Dynamo 论坛上已经有大约 10 个人要求这个,所以我想我会试一试。下面是我的 Python 脚本:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
#The inputs to this node will be stored as a list in the IN variables.

familyInput = UnwrapElement(IN[0])

familySymbol = familyInput.Symbol.Family
doc = familySymbol.Document

par_name = IN[1]
par_type = ParameterType.Text
par_grp = BuiltInParameterGroup.PG_DATA


TransactionManager.Instance.EnsureInTransaction(doc)

familyDoc = doc.EditFamily(familySymbol)
OUT = familyDoc.FamilyManager.AddParameter(par_name,par_grp,par_type,False)

TransactionManager.Instance.TransactionTaskDone()

当我 运行 脚本时,出现此错误:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 26, in <module>
Exception: The document is currently modifiable! Close the transaction before calling EditFamily.

我假设这个错误是因为我打开了一个已经通过脚本存在的家庭文档,然后从未将信息发送回项目文档?或者类似的东西。关于如何解决这个问题的任何提示?

错误消息已经准确地告诉您问题所在:"The document is currently modifiable! Close the transaction before calling EditFamily"。

我假设 TransactionManager.Instance.EnsureInTransaction 在给定的文档上打开了一个事务。您不能使用打开的事务调用 EditFamily。

帮助文件中有明确记载:

http://thebuildingcoder.typepad.com/blog/2012/05/edit-family-requires-no-transaction.html

在调用 EditFamily 之前关闭事务,或者,在这种情况下,根本不要打开它。

哦,然后,当然,你想修改家庭文件。这确实需要交易,但在家庭文件 'familyDoc' 上,而不是在项目文件 'doc'.

我不知道这是否是最终解决方案,但它可能会有所帮助:

familyDoc = doc.EditFamily(familySymbol)

TransactionManager.Instance.EnsureInTransaction(familyDoc)
OUT = familyDoc.FamilyManager.AddParameter(par_name,par_grp,par_type,False)
TransactionManager.Instance.TransactionTaskDone()

基于我们在论坛中的讨论:

import clr

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

par_name = IN[0]
exec("par_type = ParameterType.%s" % IN[1])
exec("par_grp = BuiltInParameterGroup.%s" % IN[2])
inst_or_typ = IN[3]
families = UnwrapElement(IN[4])

# class for overwriting loaded families in the project
class FamOpt1(IFamilyLoadOptions):
    def __init__(self): pass
    def OnFamilyFound(self,familyInUse, overwriteParameterValues): return True
    def OnSharedFamilyFound(self,familyInUse, source, overwriteParameterValues): return True

trans1 = TransactionManager.Instance
trans1.ForceCloseTransaction() #just to make sure everything is closed down
# Dynamo's transaction handling is pretty poor for
# multiple documents, so we'll need to force close
# every single transaction we open
result = []

for f1 in families:
    famdoc = doc.EditFamily(f1)
    try: # this might fail if the parameter exists or for some other reason
        trans1.EnsureInTransaction(famdoc)
        famdoc.FamilyManager.AddParameter(par_name, par_grp, par_type, inst_or_typ)
        trans1.ForceCloseTransaction()
        famdoc.LoadFamily(doc, FamOpt1())
        result.append(True)
    except: #you might want to import traceback for a more detailed error report
        result.append(False)
        trans1.ForceCloseTransaction()      
    famdoc.Close(False)

OUT = result

image of the Dynamo graph