3DView List in windows form combo box

3DView List in windows form combo box

Anonymous
Not applicable
4,321 Views
13 Replies
Message 1 of 14

3DView List in windows form combo box

Anonymous
Not applicable

Hi,

 

I want to create a combo box within a windows form that will show a list of all 3D views in the project (I am just testing combo boxes to use them in one of my add-ins).

When 

When I run the external command the form shows up but the combo box is empty. I have searched a lot on this forum and other websites and cannot figure out what I am doing wrong.

 

This is the code of the external command:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;


namespace Drop_Down_List_Test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class ViewList : IExternalCommand
    {
        static AddInId appId = new AddInId(new Guid("536B73F0-36BF-4CBC-8CF9-EF0E7E210A90"));
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
 
            using (ViewListForm thisForm = new ViewListForm())
            {
                thisForm.ShowDialog();
                if (thisForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return Result.Cancelled;
                }
            }
            return Result.Succeeded;
        }
    }
}

 

And this is the code of the windows form:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.IO;

namespace Drop_Down_List_Test
{
    public partial class ViewListForm : System.Windows.Forms.Form
    {
        public Document doc;

        public ViewListForm()
        {
            InitializeComponent();
        }
        
        private void ViewListForm_Load(object sender, EventArgs e)
        {
            FilteredElementCollector fec = new FilteredElementCollector(doc).OfClass(typeof(View3D));

            foreach (Element elem in fec)
            {
                View3D v = elem as View3D;
                if (!v.IsTemplate)
                    cbViewList.Items.Add(v.Name);
            }
            cbViewList.DisplayMember = "ViewName";
        }

        private void cbViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}

Below screen grab of the 'empty' form after executing the command in Revit.


Capture.PNG

 

If you could help me that would be great.

 

 

Thanks.

 

Accepted solutions (2)
4,322 Views
13 Replies
Replies (13)
Message 2 of 14

Anonymous
Not applicable

Hi! 

 

After your collector, try adding a List:

 

//for example

List<View3d> threeDlist = new List<View3d>();
foreach (View3d View3d in fec)

{
  threeDlist.Add(View3d);

}

return threeDlist;

Hope this helps!

 

 

0 Likes
Message 3 of 14

Revitalizer
Advisor
Advisor
Accepted solution

Hi,

 

in your ViewListForm class, doc variable isn't initialized.

You can add it as a parameter in the constructor:

 

public Document doc;

public ViewListForm(Document doc)
{

    this.doc=doc;
    InitializeComponent();
}

 

In your test command, handle over the Document to the Form:

 

using (ViewListForm thisForm = new ViewListForm(doc))

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 4 of 14

Anonymous
Not applicable

Thanks both for your quick responses.

 

Revitalizer, your instructions were very clear. I did as you said and the drop down list still does not populate. Any ideas?

Below the windows form code:

namespace Drop_Down_List_Test
{
    public partial class ViewListForm : System.Windows.Forms.Form
    {
        public Document doc;

        public ViewListForm(Document doc)
        {
            this.doc = doc;
            InitializeComponent();
        }
        
        private void ViewListForm_Load(object sender, EventArgs e)
        {
            FilteredElementCollector fec = new FilteredElementCollector(doc).OfClass(typeof(View3D));
            foreach (Element elem in fec)
            {
                View3D v = elem as View3D;
                if (!v.IsTemplate)
                    cbViewList.Items.Add(v.Name);
            }
            cbViewList.DisplayMember = "ViewName";
        }

        private void cbViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}

Below external command code:

 

namespace Drop_Down_List_Test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class ViewList : IExternalCommand
    {
        static AddInId appId = new AddInId(new Guid("536B73F0-36BF-4CBC-8CF9-EF0E7E210A90"));
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
 
            using (ViewListForm thisForm = new ViewListForm(doc))
            {
                thisForm.ShowDialog();
                if (thisForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return Result.Cancelled;
                }
            }
            return Result.Succeeded;
        }
    }
}

 

desantis15, I tried your suggestion but I don't know what to do with that list. Is it to use it in my foreach loop instead of the FilteredElementCollector? If you could reproduce the whole ViewListForm_Load class it'd be great.

 

Thanks again.

0 Likes
Message 5 of 14

Revitalizer
Advisor
Advisor

Hi,

 

since you already fill the list box with strings, you don't need to use the approach using databinding, DisplayMember and ValueMember.

 

Remove

cbViewList.DisplayMember = "ViewName";

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 6 of 14

Anonymous
Not applicable

Thanks again for you quick answer Revitalizer. Unfortunately I have removed that line of code and still does fill the box. This is very strange, any other ideas? Many thanks.

 

Current windows form code:

namespace Drop_Down_List_Test
{
    public partial class ViewListForm : System.Windows.Forms.Form
    {
        public Document doc;

        public ViewListForm(Document doc)
        {
            this.doc = doc;
            InitializeComponent();
        }
        
        private void ViewListForm_Load(object sender, EventArgs e)
        {
            FilteredElementCollector fec = new FilteredElementCollector(doc).OfClass(typeof(View3D));

            foreach (Element elem in fec)
            {
                View3D v = elem as View3D;
                if (!v.IsTemplate)
                    cbViewList.Items.Add(v.Name);
            }
        }

        private void cbViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}
0 Likes
Message 7 of 14

Revitalizer
Advisor
Advisor

Hi,

 

change this:

foreach (Element elem in fec)
to that:
foreach (Element elem in fec.ToElements())

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 8 of 14

Anonymous
Not applicable

Thanks Revitalizer, but still does not work 😞

Current code:

namespace Drop_Down_List_Test
{
    public partial class ViewListForm : System.Windows.Forms.Form
    {
        public Document doc;

        public ViewListForm(Document doc)
        {
            this.doc = doc;
            InitializeComponent();
        }
        
        private void ViewListForm_Load(object sender, EventArgs e)
        {
            FilteredElementCollector fec = new FilteredElementCollector(doc).OfClass(typeof(View3D));
            
            foreach (Element elem in fec.ToElements())
            {
                View3D v = elem as View3D;
                if (!v.IsTemplate)
                    cbViewList.Items.Add(v.Name);
            }
        }

        private void cbViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}
0 Likes
Message 9 of 14

Revitalizer
Advisor
Advisor

Hi,

 

are you sure your ViewListForm_Load is called at all?

Are there View3D elements in your document?

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 10 of 14

Anonymous
Not applicable

Hi,

 

Thank you for your patience.

Sorry for the basic question but how / where do I call the function. The code is the one I posted above.

In regards of the 3DViews in the project I am sure I have a couple of 3D views.

 

 

Capture.JPG

 

 

 

0 Likes
Message 11 of 14

Revitalizer
Advisor
Advisor

Hi,

 

 

right-click on ViewListForm_Load, select "Find all references".

Is there only one entry (the method definition itself), or is the method called anywhere?

 

Or create a breakpoint inside the method, see if it is being hit.

 

You also could fill your combobox after the InitializeComponent() call, in the constructor.

 

 

 

Revitalizer

 

 




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 12 of 14

Anonymous
Not applicable

Hi,

 

That must be the problem. If I click in "Find all references" there is only one entry (image below). How can I solve this? Many thanks again.

Capture.JPG

 

 

0 Likes
Message 13 of 14

Revitalizer
Advisor
Advisor
Accepted solution

Hi,

 

in the form designer view, go to the properties palette, swich from properties to events (the flash symbol), scroll to Behaviour section, go to "Load".

If you click next to Load, there you can select your existing method.

Once connected this way, your method will be invoked when your form has been loaded.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 14 of 14

Anonymous
Not applicable

That was the solution!! Many thanks Revitalizer for your responses and patience. My drop down list below now filled!

 

Capture.JPG

0 Likes