Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Closing active Family document

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Ahmad.AbuZnait
504 Views, 3 Replies

Closing active Family document

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;
            }
        }
    }
}

 

 

AhmadAbuZnait_0-1665237816053.png

 

3 REPLIES 3
Message 2 of 4

Try to use OpenAndActivateDocument instead of ShowElements to change the active document before you close the active family document

uiApp.OpenAndActivateDocument(doc.PathName);

 

Message 3 of 4

Thank you @mohamedelimam, that solved it

Message 4 of 4
tamas.deri
in reply to: mohamedelimam

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.

Post to forums  

Autodesk Design & Make Report