-1

The idea of the program is to concatenate different parameters and put it all inside another parameter. How to let the end user decide if he wants to concatenate 2 or 3 parameters. Right now it is like if you don't put in 3 parameters it won't work. Nothing I came up with works.

namespace CombineParametersWinForm
    {
        public partial class Form1 : System.Windows.Forms.Form
       
        {
                //Class variable 
                Document revitDoc { get; set; }
    
                public Form1(Document doc)
                {
                    InitializeComponent();
    
                    this.revitDoc = doc;
                    //Create a list of the parameters you want your user to choose from 
                    List<string> stringParameters = new List<string>
                {
                    "Weight",
                    "Angle",
                    "Manufacturer"
                };
                    //Add list to comboboxes on form 
                    foreach (string parameterName in stringParameters)
                    {
                        comboBox1.Items.Insert(0, parameterName);
                        comboBox2.Items.Insert(0, parameterName);
                        comboBox3.Items.Insert(0, parameterName);
                    }
    
                }
    
                private void button1_Click(object sender, EventArgs e)
                {
                    FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
                    ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_PipeFitting);
                    //Applying Filter
                    IList<Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
                    foreach (Element duct in ducts)
                    {
                        //Get Parameter values
                        string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
                        string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
                        string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();
    
                        string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
                    
                        //do not need .ToString() when setting parameter
    
                        using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
                        {
                            t.Start();
                            duct.LookupParameter("New").Set(newValue);
                            t.Commit();
                        }
                    

Ideas. First is pretty long one.

                string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
                string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
                string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();


                if (parameterValue1 != "" || parameterValue1 != null)

                {
                    parameterValue1 = parameterValue11;
                }

                if (parameterValue2 != "" || parameterValue2 != null)

                {
                    parameterValue1 = parameterValue22;
                }
                if (parameterValue3 != "" || parameterValue3 != null)

                {
                    parameterValue3 = parameterValue33;
                }


                if (parameterValue1 == "" || parameterValue1 == null)

                {
                    parameterValue11 = "";
                }

                if (parameterValue2 == "" || parameterValue2 == null)

                {
                    parameterValue22 = "";
                }
                if (parameterValue3 == "" || parameterValue3 == null)

                {
                    parameterValue33 = "";
                }

                string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;

                using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
                {
                    t.Start();
                    duct.LookupParameter("New").Set(newValue);
                    t.Commit();
                }
            }
            
        }
    }
}

Second is a short one but still doesn't work

                string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
                string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
                string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();


                List<string> listParam = new List<string> { parameterValue1, parameterValue2, parameterValue3 };

                foreach (string s in listParam)

                {

                    if (s != "" /*&& s != null*/)
                    {
                        List<string> listParamNotNull = new List<string> { s };
                        string newValue = String.Join(" ,", listParamNotNull);


                        using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
                        {
                            t.Start();
                            duct.LookupParameter("New").Set(newValue);
                            t.Commit();
                        }

                    
fysharp
  • 101
  • 7

1 Answers1

0

You are creating three separate Lists and wanting to do something against all of them combined, but actually doing against each of them individually, overwriting as you go.

Please take a closer look at your nested logic.

Something like this is likely more appropriate:

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        string parameterValue1 = "Value1";
        string parameterValue2 = ""; // purposefully providing an empty value
        string parameterValue3 = "Value3";

        var listParamIn = new List<string> { parameterValue1, parameterValue2, parameterValue3 };
        var listParamOut = new List<string>();

        foreach (string s in listParamIn){
            if (string.IsNullOrEmpty(s))
                continue;
            
            listParamOut.Add(s);
        }
        
        string newValue = String.Join(", ", listParamOut);
        Console.WriteLine(newValue);

        /* continue with your transaction
        using (Transaction t = new Transaction(revitDoc, "Set Parameter name")){
            t.Start();
            duct.LookupParameter("New").Set(newValue);
            t.Commit();
        }
        */
    }
}

Output:

Value1, Value3

See: https://dotnetfiddle.net/mXdL5F

Jonathan
  • 4,916
  • 2
  • 20
  • 37