[Revit 2013] Transaction error when editing families using API
Hi,
I am now testing a script to modify family instances using API, but transaction errors occur.
Transaction was set to manual, and I think I set proper transaction start and commit.
The code simply takes a family instances, create new type, and reload the instance.
Could anyone please look at my code and give me some help?
in advance, Thank for your help!
Code:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
class familyExtract : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref String massage, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
Reference selected = null;
FamSelectionFilter famFilter = new FamSelectionFilter();
selected = sel.PickObject(ObjectType.Element, famFilter, "select family");
Element elem = doc.GetElement(selected);
FamilyInstance familyInstance = elem as FamilyInstance;
Transaction trans = new Transaction(doc, "excomm");
trans.Start();
editFamilyProperties(doc, familyInstance);
trans.Commit();
return Result.Succeeded;
}
public void editFamilyProperties(Document document, FamilyInstance familyInstance)
{
if (null != familyInstance.Symbol)
{
Family family = familyInstance.Symbol.Family;
Document familyDoc = document.EditFamily(family);
if (null != familyDoc)
{
FamilyManager familyManager = familyDoc.FamilyManager;
FamilyType newFamType = familyManager.NewType("Test");
FamilyParameter familyParam = familyManager.get_Parameter("width");
if (null != familyParam)
{
familyManager.Set(familyParam, 200);
}
family = familyDoc.LoadFamily(document);
FamilySymbolSetIterator symbolsItor = family.Symbols.ForwardIterator();
symbolsItor.Reset();
while (symbolsItor.MoveNext())
{
FamilySymbol familySymbol = symbolsItor.Current as FamilySymbol;
if (familySymbol.Name == "mass_Ctype")
{
familyInstance.Symbol = familySymbol;
break;
}
}
}
}
}
public class FamSelectionFilter : ISelectionFilter
{
public bool AllowElement(Element element)
{
if (element is FamilyInstance)
{
return true;
}
return false;
}
public bool AllowReference(Reference refer, XYZ point)
{
return false;
}
}
}
Re: [Revit 2013] Transaction error when editing families using API
You actually have two documents open, "doc" and "familyDoc." So far, you are not making changes to "doc" but you are attempting to make changes to "familyDoc."
Therefore, you need to add a transaction in familyDoc, inside editFamilyProperties(), whenever you make a change to the family: add type, change width- not sure about mass_type.
Re: [Revit 2013] Transaction error when editing families using API
I agree with Peter that you need a transaction within FamilyDoc to make that change.
But there's also a new wrinkle in 2013 that will make the code need to be even more complicated:
I believe now, you cannot perform an "EditFamily" while you've got a transaction open on the project document - is that where it's failing now? on the EditFamily()?
I'm honestly not sure how you'll need to structure your code for this, but focus on separating the two actions:
EditFamily()
The Family Transaction
The Family Change
Commit the Family Transaction
Start a Project Transaction
Do the Load Family
Commit the Project Transaction
(that's what seems like it would work).
Good Luck,
Matt