I'm trying to open a family document do something inside it, switch to project document using UIDocument.ShowElements and close the family document, but i keep getting that there is an open transaction even though the transaction was already committed, also the snippet code attached results in having the same effect, even though there is no transaction at all.
I would appreciate any hints of how to solve it.
public class OpenAndClose : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiApp = commandData.Application;
var uiDoc = uiApp.ActiveUIDocument;
var doc = uiDoc.Document;
try
{
ICollection<ElementId> eLiD = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_ElectricalFixtures).
WhereElementIsNotElementType().ToElementIds();
var firstId = eLiD.First();
FilteredElementCollector symbols = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_ElectricalFixtures)
.WhereElementIsElementType();
var families = new HashSet<Family>();
var Ids = new HashSet<ElementId>();
foreach (FamilySymbol symbol in symbols)
{
Family fam = symbol.Family;
var id = fam.Id;
Ids.Add(id);
}
var uniquFam = new HashSet<Family>();
foreach (ElementId id1 in Ids)
{
var fam2 = doc.GetElement(id1) as Family;
uniquFam.Add(fam2);
}
foreach (Family family in uniquFam)
{
var editFamDoc = doc.EditFamily(family);
var famUIDoc = uiApp.OpenAndActivateDocument(editFamDoc.PathName);
var famDoc = famUIDoc.Document;
uiDoc.ShowElements(eLiD);
famDoc.Close(false);
}
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}
Solved! Go to Solution.
Solved by mohamedelimam. Go to Solution.
Try to use OpenAndActivateDocument instead of ShowElements to change the active document before you close the active family document
uiApp.OpenAndActivateDocument(doc.PathName);
Do you have any explanation why this happens? I face the same issue, but I can't use OpenAndActivateDocument if it has not yet been saved.
Can't find what you're looking for? Ask the community or share your knowledge.