How to get all Family in Project Document

How to get all Family in Project Document

s04049
Enthusiast Enthusiast
12,412 Views
4 Replies
Message 1 of 5

How to get all Family in Project Document

s04049
Enthusiast
Enthusiast

Hi 

 

I am trying to Get all Editable Family on Project Document but it not working with the this code 

 

 

collector = new FilteredElementCollector(doc).OfClass(typeof(Family));

 

 

I can use FamilySymbol to Find the Family but it will have a duplicate family on the list. 

 

 

 IList<Element> FamilySybol = new FilteredElementCollector(doc).OfClass(typeof (FamilySymbol)).ToElements();
List<Element> EditableFamily = new List<Element>();
foreach (FamilySymbol ele in FamilySybol)
{
if (ele.Category != null && ele.Family.IsEditable)
{
EditableFamily.Add(ele);
}
}

So how can I remove the duplicate Family on the EditableFamily List?

or find the All Editable Family on the Project Document?

 

Thanks

 

0 Likes
Accepted solutions (1)
12,413 Views
4 Replies
Replies (4)
Message 2 of 5

matthew_taylor
Advisor
Advisor

Hi,

The quick answer is by importing system.linq and using .distinct():

EditableFamily.distinct()

 

Cheers,

 

-Matt


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
Message 3 of 5

s04049
Enthusiast
Enthusiast

Hi Matt

 

I try this method before and I still have the duplicate Family on the list.

 

Thanks

0 Likes
Message 4 of 5

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Here is the cure for your pain 🙂

The following code lists all the loadable families names in your project and populates it in a Task dialog.

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RvtTest
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;
        
        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }
        
        #endregion

        #region IExternalCommand Members         

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                //TODO: add your code below.
                FilteredElementCollector collector = new FilteredElementCollector(CachedDoc);
                ICollection<Element> elements = collector.OfClass(typeof(Family)).ToElements();

                StringBuilder sb = new StringBuilder();

                foreach (Element el in elements)
                {
                    sb.AppendLine(el.Name);
                }

                TaskDialog.Show("reavit", sb.ToString());

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

Try the code and if it satisfied your needs just mark this reply as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 5 of 5

s04049
Enthusiast
Enthusiast

Ok thanks for that, this code is working fine.

I just found the mistake after I catch the Family list.

 

 

collector = new FilteredElementCollector(doc).OfClass(typeof(Family));