How to create a comboBox to show all beams symbol when DocumentOpened

How to create a comboBox to show all beams symbol when DocumentOpened

12060926
Participant Participant
1,736 Views
1 Reply
Message 1 of 2

How to create a comboBox to show all beams symbol when DocumentOpened

12060926
Participant
Participant

I try to use DocumentOpened Event to create a beamlist. And put beamlist to ComboBoxMemberData.

But it's fail. when I open the Document combobox is null.

So I try to check beamlist is null or not. I use MessageBox to show the list. It's OK.

why combobox can't read beamlist. And how to fix this problem.

thank you for answer this question.

 

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text; 
using System.Windows.Forms;
using Autodesk.Revit.UI.Events;

namespace EventTest
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class DocumentCreating : IExternalApplication
    {
        private static string AddInPath = typeof(DocumentCreating).Assembly.Location;
        public static IList<Element> BeamFamilySymbolList = new List<Element>();
        public void DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e)
        {
            Document doc = e.Document;            
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            BeamFamilySymbolList = collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming).ToElements();
            
            StringBuilder st = new StringBuilder();
            foreach (Element BeamName in BeamFamilySymbolList)
            {
                FamilySymbol fs = BeamName as FamilySymbol;
                string name = fs.Family.Name + fs.Name;
                st.AppendLine(name);
            }
            MessageBox.Show(st.ToString());
        }
       
        public Result OnShutdown(UIControlledApplication application)
        {
            // nothing to clean up in this simple case
            return Result.Succeeded;
        }

        public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
        {
            application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocumentOpened);
            RibbonPanel ribbonSamplePanel = application.CreateRibbonPanel("Creat Beam");
            PushButtonData pushButtonData = new PushButtonData("Creat", "creat", AddInPath, "Revit.SDK.Samples.Ribbon.CS.ResetSetting");
            ComboBoxData comboBoxDataBeam = new ComboBoxData("BeamsSelector");
            IList<RibbonItem> ribbonItemsStacked = ribbonSamplePanel.AddStackedItems(pushButtonData, comboBoxDataBeam);
            Autodesk.Revit.UI.ComboBox comboboxBeam = (Autodesk.Revit.UI.ComboBox)(ribbonItemsStacked[1]);
           
            foreach (Element elem in BeamFamilySymbolList)
            {
                FamilySymbol fs = elem as FamilySymbol;
                ComboBoxMemberData comboBoxMemberData = new ComboBoxMemberData(fs.Name, fs.Name);
                comboBoxMemberData.GroupName = fs.Family.Name;
                ComboBoxMember comboboxMember = comboboxBeam.AddItem(comboBoxMemberData);
            }            
            return Result.Succeeded;
        }
    }
}

 

0 Likes
Accepted solutions (1)
1,737 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

I think I know why.  99.99% of the time the computer is always right. The ComboBox is empty. Therefore it was either filled and then emptied or it was never filled. I think it was never filled because there never were any beams to put into it. How could that be?

 

The code to fill the combobox is in the add-in's application startup. That "startup" happens when Revit is first loading the Add-in. This loading happens as part of Revit's session startup long before you are given a chance to open a Revit file. Therefore there never were any beams to place into the combobox when the code executed.  You can confirm this by simply placing a MessageBox.Show("I am here.") just before the combobox filling foreach section in the startup.

 

The combobox filling needs to happen after the Revit file is loaded. You might check to see how long it takes to do that operation because if it takes a split second then you might have the filling happen just as the user first touches the combobox instead of as part of the document open event. That way the combobox contents will always be current. I don't know if a Revit combobox responds to that event or if you can make it do so. Otherwise you'll need to add a refresh control or come up with a different UI concept or some sort of idling watching semaphore mechanism to keep the combobox current if having that is necessary.