简体   繁体   English

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

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

I'm trying to create a new family parameter by calling a family's document in a project document and using the FamilyManager method to edit the family. 我正在尝试通过在项目文档中调用家族文档并使用FamilyManager方法来编辑家族来创建新的家族参数。 There have been about 10 people asking for this on the Dynamo forums, so I figured I'd give it a shot. 在Dynamo论坛上大约有10个人要求这样做,所以我想我会试一试。 Here's my Python script below: 以下是我的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()

When I run the script, I get this error: 运行脚本时,出现以下错误:

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.

I'm assuming that this error is because I am opening a family document that already exists through the script and then never sending the information back to the project document? 我以为这个错误是因为我正在打开通过脚本已经存在的族文档,然后再从不将信息发送回项目文档? Or something similar to that. 或类似的东西。 Any tips on how to get around this? 关于如何解决这个问题的任何提示?

Building up on our discussion from the forum: 建立在论坛的讨论基础上:

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 发电机图的图像

The error message is already telling you exactly what the problem is: "The document is currently modifiable! Close the transaction before calling EditFamily". 该错误消息已经在告诉您确切的问题是:“文档当前可修改!在调用EditFamily之前关闭事务”。

I assume that TransactionManager.Instance.EnsureInTransaction opens a transaction on the given document. 我假设TransactionManager.Instance.EnsureInTransaction打开给定文档上的事务。 You cannot call EditFamily with an open transaction. 您不能通过未清事务调用EditFamily。

That is clearly documented in the help file: 帮助文件中明确记录了这一点:

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

Close the transaction before calling EditFamily, or, in this case, don't open it at all to start with. 在调用EditFamily之前关闭事务,或者在这种情况下,根本不要打开它。

Oh, and then, of course, you wish to modify the family document. 哦,然后,当然,您希望修改家庭文件。 That will indeed require a transaction, but on the family document 'familyDoc', NOT on the project document 'doc'. 这确实需要进行交易,但是需要在家庭文档“ familyDoc”上,而不是在项目文档“ doc”上。

I don't know whether this will be the final solution, but it might help: 我不知道这是否将是最终的解决方案,但这可能会有所帮助:

familyDoc = doc.EditFamily(familySymbol)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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