问题
Our Revit Add-In lets the user browse and set type parameters, including those in linked models. It worked fine in Revit 2011 and earlier, but in Revit 2012 and 2013 we can no longer set these. When constructing the transaction on the linked document I get: "Autodesk.Revit.Exceptions.ArguementException: Document is a linked file. Transactions can only be used in primary documents (projects or families.)"
OK, so we can't use transactions on linked models. So I tried setting the parameter without a transaction, but then I got an exception saying we cannot modify the model outside of a transaction.
Can't create a transaction on linked models and can't modify a model outside of a transaction--so how does one modify a linked model in Revit 2012/2013? It worked fine in 2011 with the same code. Did a fair amount of searching online including The Building Coder blog, but found no mention of this breaking change or how to work around it. Can anyone lend a hand?
Our code is straightforward--we get a parameter in the model, start a transaction and attempt to set a parameter value. Again the same code works without error in Revit 2011.
// elementType is an ElementType in document doc
// for which we want to set a type parameter.
Parameter typeParameter = elementType.get_Parameter(pararmeterName);
Transaction transaction = new Transaction(doc, "Update Revit Type"); // Exception thrown here if doc is a linked model
transaction.Start();
typeParameter.Set("FooValue");
transaction.Commit();
回答1:
Since Revit 2014, you can unload the linked files. So just unload your files before starting the transaction, then reload them after the transaction has ended.
// Unload all links
var loadedExternalFilesRef = new List<RevitLinkType>();
var collector = new FilteredElementCollector(document);
foreach (Element element in collector.OfClass(typeof(RevitLinkType)))
{
ExternalFileReference extFileRef = element.GetExternalFileReference();
if (null == extFileRef || extFileRef.GetLinkedFileStatus() != LinkedFileStatus.Loaded)
continue;
var revitLinkType = (RevitLinkType)element;
loadedExternalFilesRef.Add(revitLinkType);
revitLinkType.Unload(null);
}
// Do your stuff in a transaction
// Reload links
foreach (RevitLinkType revitLinkType in loadedExternalFilesRef)
revitLinkType.Load();
回答2:
I started work with Revit 2012 and didn't know the behavior with the transactions in linked files. But I also needed set parameter to the linked files and didn't find the way how to do it properly. And the RevitAPI help tells Transactions can only be used in primary documents (projects or families.)
I can guess that setting parameters in the linked file in the Revit 2011 was a bug, not a feature, because it is potentially unsafe.
回答3:
2 years later...I think you'll have to open the document in question, make it the activedocument and then try applying the code you want in there. if you need a code example, let me know and i'll try and whip something up
来源:https://stackoverflow.com/questions/11417713/revit-set-type-parameter-in-linked-models