Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Example of pulling enum as string

Nathan.HilsonN67V6
Advocate

Example of pulling enum as string

Nathan.HilsonN67V6
Advocate
Advocate

Can somebody give me an example of how to pull an enum as a string variable? I've been researching and can't find anything specific for revit api enums. Specifically for Fabrication parts/parameters. We use a custom content pack (building data) but I'm assuming the methods/classes will be similar? For example if I wanted to get the service name. 

 

NathanHilsonN67V6_0-1653696391269.png

I'm able to pull the name (which is named default//doesn't help) through a collection but I can't figure out how to dig into the "FabricationPart" field where all the information I want is. Still new to Revit api so may be something obvious I'm missing. This is how I'm pulling the name so maybe I could tweak this to pull the service?

InitializeComponent();
            Doc = doc;
            ICollection<Element> FabricationPipework =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationPipework)
                .ToElements();
            ICollection<Element> FabricationDuctwork =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationDuctwork)
                .ToElements();
            ICollection<Element> FabricationHangers =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationHangers)
                .ToElements();
            {
                foreach (Element element in FabricationPipework)
                    comboBox2.Items.Add(element.Category.Name);
            }
            {
                foreach (Element element in FabricationDuctwork)
                    comboBox2.Items.Add(element.Category.Name);
            }
            {
                foreach (Element element in FabricationHangers)
                    comboBox2.Items.Add(element.Category.Name);
            }

 

0 Likes
Reply
Accepted solutions (1)
584 Views
9 Replies
Replies (9)

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Nathan.HilsonN67V6 ,

 

I don't exactly understand what you are trying to do.

If you want to access the service name parameter you can use the below code.

FabricationPart fabricationPart=null;
                Parameter param=fabricationPart.get_Parameter(BuiltInParameter.FABRICATION_SERVICE_NAME);
                string s = param.AsValueString();

(Or)

FabricationPart fabricationPart;
                string s = fabricationPart.ServiceName;

If this is not what you want, Could you please explain your issue in detail?


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Nathan.HilsonN67V6 ,

 

If you want to get fabrication parts with a specific service name,you can use ElementParameterFilter.

https://www.revitapidocs.com/2023/b0b40351-690c-eb5d-30c2-d4447a42fda1.htm 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Nathan.HilsonN67V6
Advocate
Advocate

My goal is to get a collection of the used services in the active view and assign them to a selectable combobox. I think I my have the right idea but incorrect methods. I've rewritten the code for the direction that I want to go in. 

 

                {
                    FamilySymbol familySymbol = doc.GetElement(new ElementId(Autodesk.Revit.DB.BuiltInParameter.ID_PARAM)) as FamilySymbol;

                    List<Autodesk.Revit.DB.Parameter> myParameters = new List<Autodesk.Revit.DB.Parameter>();
                    System.Collections.IEnumerator symbolParamEnumerator = familySymbol.Parameters.GetEnumerator();
                    while (symbolParamEnumerator.MoveNext())
                    {
                        Autodesk.Revit.DB.Parameter parameter = (Autodesk.Revit.DB.Parameter)symbolParamEnumerator;

                        if (parameter.Definition is InternalDefinition internalDefinition)
                        {
                            if (internalDefinition.BuiltInParameter == Autodesk.Revit.DB.BuiltInParameter.FABRICATION_SERVICE_NAME)
                            {
                                myParameters.Add(parameter);
                                comboBox2.Items.Add(myParameters);
                            }
                        }
                    }
                }

 

It's throwing me an instance error for this specific example. I'm open to all ideas and changes. I found examples databinding but I'm not too familiar with that. 

0 Likes

Nathan.HilsonN67V6
Advocate
Advocate

So to make it a bit clearer getting the parameter "Fabrication Service Name" in a collection or list as a named value not the id.

0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @Nathan.HilsonN67V6 ,

In your screenshot, the service name is "DHWR.."

You want to add this service name to the combo box, is my understanding of your issue correct?

If yes, then please take a look at the below sample code

UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            JLForm form = new JLForm(doc);
            form.ShowDialog();
internal class JLForm : System.Windows.Forms.Form
    {
        public string ReturnValue1 = null;
        System.Windows.Forms.ComboBox mybox = null;
        Document d = null;
        public JLForm(Document doc)
        {
            d = doc;
            InitializeComponent(d);
        }

        private void InitializeComponent(Document doc)
        {
            // Creating and setting the properties of label
            Label l = new Label();
            l.Location = new System.Drawing.Point(22, 10);
            l.Size = new System.Drawing.Size(99, 18);
            l.Text = "Select Service Name";

            // Adding this label to the form
            this.Controls.Add(l);

            // Creating and setting the properties of comboBox
            mybox = new System.Windows.Forms.ComboBox();
            mybox.Location = new System.Drawing.Point(127, 7);
            mybox.Size = new Size(216, 26);
            mybox.MaxLength = 3;
            mybox.DropDownStyle = ComboBoxStyle.DropDown;

            FilteredElementCollector Collector = new FilteredElementCollector(doc).OfClass(typeof(FabricationPart)).WhereElementIsNotElementType();
            foreach (Element e in Collector)
            {
                Parameter p = e.get_Parameter(BuiltInParameter.FABRICATION_SERVICE_NAME);
                if(p!=null)
                {
                    mybox.Items.Add(p.AsValueString());
                }
            }

            // Adding this ComboBox to the form
            this.Controls.Add(mybox);

            Button button = new Button();
            button.Location = new System.Drawing.Point(76, 50);
            button.Size = new Size(100, 18);
            button.Text = "Click me";
            button.Click += new EventHandler(buttonClicked);
            this.Controls.Add(button);
        }

        private void buttonClicked(object sender, EventArgs e)
        {
            string selectedServiceName = mybox.SelectedItem as string;
            /*
             * Do your work here
             */ 
        }
    }

I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Nathan.HilsonN67V6
Advocate
Advocate

This is almost exactly what I was looking for. So I pulled the filter section of the code to mines & I get this error. 

            FilteredElementCollector Collector = new FilteredElementCollector(doc).OfClass(typeof(FabricationPart)).WhereElementIsNotElementType();
            foreach (Element e in Collector)
            {
                Parameter p = e.get_Parameter(BuiltInParameter.FABRICATION_SERVICE_NAME);
                if(p!=null)
                {
                    mybox.Items.Add(p.AsValueString());
                }

NathanHilsonN67V6_0-1654019125634.png

 

I then played around with it a bit and came up with this that will get me using but not the actual name. 

        {      
            InitializeComponent();
            Doc = doc;
            ICollection<Element> FabricationPipework =
               new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationPipework)
                .OfClass(typeof(FabricationPart))
                .OfCategory(BuiltInCategory.OST_FabricationPipework)
                .ToElements();
            foreach (Element element in FabricationPipework)
            {
comboBox2.Items.Add(element.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.FABRICATION_SERVICE_NAME));
            }

NathanHilsonN67V6_1-1654020559567.png

 

In order for me to get it to work I had to add a cast for the enums built in parameters.

 

Nathan.HilsonN67V6
Advocate
Advocate

Update: It worked!!! It was a small error on my end with the enumerations. Thanks a ton. 

naveen.kumar.t
Autodesk Support
Autodesk Support

Great, I am glad that it worked and your issue is now resolved.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Nathan.HilsonN67V6
Advocate
Advocate

Here's how I got mines to work for any others having the same issue. 

        public enum BuiltInParameter
        {
     //****************************** AS VALUESTRINGS ************************************
            FABRICATION_SERVICE_PARAM = -1140339, 
            ELEM_FAMILY_AND_TYPE_PARAM = -1002052, 
            ELEM_CATEGORY_PARAM = -1140362, 
            ELEM_FAMILY_PARAM = -1002051, 
            FABRICATION_INSULATION_SPEC = -1140947, 
            FABRICATION_LEVEL_PARAM = -1140916, 
            FABRICATION_PART_CUT_TYPE = -1140970, 
            FABRICATION_PART_ITEM_NUMBER = -1140975, 
            FABRICATION_PART_LENGTH = -1140944, 
            FABRICATION_PART_MATERIAL = -1140909, 
            FABRICATION_PART_NOTES = -1140977, 
            FABRICATION_PART_WEIGHT = -1140913,
     //****************************** AS STRINGS ************************************
            FABRICATION_SERVICE_NAME = -1140973,
            FABRICATION_PRI_SIZE = -1141010, 
            FABRICATION_PRODUCT_DATA_INSTALL_TYPE = -1140910, 
            FABRICATION_PRODUCT_DATA_LONG_DESCRIPTION = -1140902, 
            FABRICATION_PRODUCT_DATA_MATERIAL_DESCRIPTION = -1140904, 
        }

public //formName(Document doc)

     //****************************** Get Fabrication Service Names ************************************
            try
            {
                FilteredElementCollector Collector = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(FabricationPart)).WhereElementIsNotElementType();
                foreach (Element e in Collector)
                {
                    Autodesk.Revit.DB.Parameter v = e.get_Parameter((Autodesk.Revit.DB.BuiltInParameter)BuiltInParameter.FABRICATION_SERVICE_PARAM);
                    if (v != null)
                    {
                        comboBox2.Items.Add(v.AsValueString());
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 

0 Likes