【问题标题】:How to limit the amount of parameters for concatenatenation?如何限制连接的参数数量?
【发布时间】:2021-09-16 09:16:59
【问题描述】:

该程序的想法是连接不同的参数并将它们全部放在另一个参数中。如何让最终用户决定是否要连接 2 个或 3 个参数。现在,如果您不输入 3 个参数,它将无法正常工作。我想出的任何东西都没有。

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();
                        }
                    

想法。第一个很长。

                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();
                }
            }
            
        }
    }
}

第二个很短,但还是不行

                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();
                        }

                    

【问题讨论】:

    标签: c# winforms parameters combobox revit-api


    【解决方案1】:

    您正在创建三个独立的 Lists,并希望对所有这些组合执行某些操作,但实际上是对它们中的每一个执行单独操作,并在进行时覆盖。

    请仔细查看您的嵌套逻辑。

    这样的东西可能更合适:

    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();
            }
            */
        }
    }
    

    输出:

    值1,值3

    见: https://dotnetfiddle.net/mXdL5F

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多