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

时间:2016-02-19 19:38:15

标签: revit-api revitpythonshell

我尝试通过在项目文档中调用系列文档并使用FamilyManager方法编辑系列来创建新的系列参数。在迪纳摩论坛上有大约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.

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

2 个答案:

答案 0 :(得分:1)

加强我们在论坛上的讨论:

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

答案 1 :(得分:0)

错误消息已经准确地告诉您问题所在:&#34;该文档目前可以修改!在调用EditFamily&#34;。

之前关闭交易

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

帮助文件中清楚地记录了这一点:

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

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

哦,然后,当然,您希望修改家庭文件。这确实需要交易,但是在家庭文件“家庭文件”上,不在项目文档&#39; doc&#39;。

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

familyDoc = doc.EditFamily(familySymbol)

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