Revit crashes when trying to get dependent family

Revit crashes when trying to get dependent family

Anonymous
Not applicable
899 Views
5 Replies
Message 1 of 6

Revit crashes when trying to get dependent family

Anonymous
Not applicable

Hi team,

 

I instantiated a family symbol (a door) using the following code:

FamilyInstance instance = doc.Create.NewFamilyInstance(
	new XYZ(),
	familySymbol,
	doc.GetElement(familySymbol.LevelId) as Level,
	Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming
);

However, the catch is this FamilySymbol (the door) is dependent on another FamilySymbol (a doorknob). 

So when I try to hide the door:

view.HideElements(new List<ElementId> { instance.id });

The door is hidden, but the doorknob is left there. 

How do I hide both the door and doorknob? 

 

I tried to get the dependent element of the door, like so:

ElementFilter ef = new ElementClassFilter(typeof(FamilyInstance));
IList<ElementId> dependentElements = instance.GetDependentElements(ef);

foreach (ElementId id in dependentElements)
{
	Debug.WriteLine(doc.GetElement(id).Name);			
}

but Revit just crashes unexpectedly., so no stack trace/ error message is available. 

 

Important Note:

The doorSymbol and its doorknobSymbol is copied over from the original document and spawned in a different document. 

// copy over the symbol to spawn into the new document 
Family fam = uiapp.ActiveUIDocument.Document.EditFamily(doorSymbol.Family).LoadFamily(doc);

 

May I know how to resolve this issue? I need to hide both the door and its dependent family.

 

Attached is the revit family. 

 

0 Likes
Accepted solutions (1)
900 Views
5 Replies
Replies (5)
Message 2 of 6

joshua.lumley
Advocate
Advocate

The first step is to stop revit from crashing by handling and recording errors. Use this code.

 

 

int eL = -1;

            try
            {
//stick your code in here

            }

            #region catch and finally
            catch (Exception ex)
            {
                writeDebug("WindowLoaded, error line:" + eL +  Environment.NewLine + ex.Message + Environment.NewLine + ex.InnerException, true);
            }
            finally
            {
            }
            #endregion


        public static void writeDebug(string x, bool AndShow)
        {

            string path = (System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\` aa Error messages");
            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);

            string FILE_NAME = (path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");

            System.IO.File.Create(FILE_NAME).Dispose();

            System.IO.StreamWriter objWriter = new System.IO.StreamWriter(FILE_NAME, true);
            objWriter.WriteLine(x);
            objWriter.Close();

            if(AndShow)System.Diagnostics.Process.Start(FILE_NAME);
        }

 

0 Likes
Message 3 of 6

Anonymous
Not applicable

Hi @joshua.lumley ,

 

I have followed what you said, but Revit still crashes and I don't see any debug files created in the file path. 

 
 
 

Capture2.JPG

 

0 Likes
Message 4 of 6

joshua.lumley
Advocate
Advocate

 

Make sure ALL the code is inside the try statement, not just the part you think is causing the crashing. 

 

0 Likes
Message 5 of 6

RPTHOMAS108
Mentor
Mentor
Accepted solution

No issue with below, it also hides the door handle ok, feedback within code comments. Testing with 2021 not 2020.

 

My main comment is that you need to break some of the statements down into more parts so you can investigate with stepping where issues occur better.

 

 

 

public Result TObj131(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
		{

			UIApplication UIApp = commandData.Application;
			if (UIApp.ActiveUIDocument == null)
				return Result.Cancelled;
			Document Doc = UIApp.ActiveUIDocument.Document;

			string FPath = "C:\\temp\\Steelcase%20PolyVision%20-%20Sans%20Light%20-%20Board (1).rfa";
			//Consider renaming above to take out %20 parts (parts associated with spaces)

			Document FDoc = UIApp.Application.OpenDocumentFile(FPath);
			//Not sure why not just load family directly into project from file instead of below?
			Family F = FDoc.LoadFamily(Doc);

			ElementId FS_ID = F.GetFamilySymbolIds().FirstOrDefault();
			FamilySymbol FS = Doc.GetElement(FS_ID) as FamilySymbol;

			//Best place to get a level is directly from the document not level property of FamilySymbol just inserted
			ElementClassFilter ECF_LV = new ElementClassFilter(typeof(Level));
			FilteredElementCollector FEC = new FilteredElementCollector(Doc);
			Level Ls = FEC.WherePasses(ECF_LV).Cast<Level>().OrderBy(x => x.Elevation).FirstOrDefault();
			if (ECF_LV == null)
				return Result.Succeeded;

			FamilyInstance Fi = null;
			using (Transaction tx = new Transaction(Doc, "New instance"))
			{

				if (tx.Start() == TransactionStatus.Started)
				{
					if (FS.IsActive == false)
					{
						FS.Activate();
					}
					//Furniture is not structural therefore use StructuralType.NonStructural
					Fi = Doc.Create.NewFamilyInstance(XYZ.Zero, FS, Ls, StructuralType.NonStructural);
					tx.Commit();
				}
			}
			if (Fi == null)
			{
				return Result.Succeeded;
			}

			ElementClassFilter ECF_FI = new ElementClassFilter(typeof(FamilyInstance));
			IList<ElementId> EIDs = Fi.GetDependentElements(ECF_FI);

			//Could use this for shared families within family instances
			ICollection<ElementId> Si = Fi.GetSubComponentIds();

			for (int i = 0; i <= EIDs.Count - 1; i++)
			{
				Element El = Doc.GetElement(EIDs[i]);

				Debug.WriteLine(El.Name + " " + Convert.ToString(El.Id.IntegerValue));

				if (El.Name == "Toolbar - Collaborative")
				{
					if (El.CanBeHidden(Doc.ActiveView))
					{
						using (Transaction Tx = new Transaction(Doc, "Hide door knob"))
						{
							if (Tx.Start() == TransactionStatus.Started)
							{
								Doc.ActiveView.HideElements(new ElementId[] { EIDs[i] }.ToList());
								Tx.Commit();
							}
						}
					}
				}

			}
			return Result.Succeeded;
		}

 

 

 

 

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi @RPTHOMAS108,

 

Thanks for such a detailed explanation, it solved my issue!

 

I had to use:

Level Ls = FEC.WherePasses(ECF_LV).Cast<Level>().OrderBy(x => x.Elevation).FirstOrDefault();

to get the level instead of: 

doc.GetElement(familySymbol.LevelId) as Level

 

Apparently that's what's causing the crash. Thanks 🙂

0 Likes