Get subcomponents from familyinstance

Get subcomponents from familyinstance

oyvind.knappskog.olsen
Explorer Explorer
1,187 Views
3 Replies
Message 1 of 4

Get subcomponents from familyinstance

oyvind.knappskog.olsen
Explorer
Explorer

Hi

 

How can I get the subcomponents from the 3 familyinstances in the file attached?

 

I have tried the GetSubcomponents method but it returns 0.

 

Regards

Øyvind Knappskog Olsen

Norconsult Informasjonssystemer AS

0 Likes
1,188 Views
3 Replies
Replies (3)
Message 2 of 4

JimJia
Alumni
Alumni
Dear Øyvind Knappskog Olsen,
Per our investigation, these three elements are AssemblyInstance but not FamilyInstance, so you can use AssemblyInstance.GetMemeberIds() to retrieve the sub components.

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 3 of 4

Mustafa.Salaheldin
Collaborator
Collaborator

Thanks to @JimJia. I've prepared a sample code to do the job:

 

#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;
using System.Diagnostics;

#endregion

namespace RevitAddinCS2
{
    [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;

            FilteredElementCollector collectorAssemblyInstance = new FilteredElementCollector(CachedDoc);

            collectorAssemblyInstance.OfClass(typeof(AssemblyInstance));

            List<AssemblyInstance> AssemblyInstances = collectorAssemblyInstance.Cast<AssemblyInstance>().ToList();

            StringBuilder sb = new StringBuilder();

            foreach (AssemblyInstance ai in AssemblyInstances)
            {
                sb.AppendLine(string.Format("Assembly {0} contains:", ai.Name));
                sb.AppendLine(string.Format("======================"));

                List<ElementId> members = ai.GetMemberIds().ToList();

                foreach(ElementId id in members)
                {
                    sb.AppendLine(string.Format("\tElement with Id: {0}", id.ToString()));
                }
            }

            TaskDialog.Show("Revit", sb.ToString());
            return Result.Succeeded;
        }

        #endregion
    }
}

If this code satisfies your need pleas mark this reply as an answer.

 


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

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 4 of 4

JimJia
Alumni
Alumni
Dear Øyvind Knappskog Olsen,

Did you get chance to verify this case? do you think we can close this now?

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes