revit二次开发中combox控件的应用

revit 二次开发断断续续的在学习,在看完了Revit 2012 API Developer Guide.pdf 之后,多看看sdk里面的例子是不错了,跟着叶总走就是正确的了!http://blog.csdn.net/joexiongjin/article/details/6175505


为了交互,必不可少的得自己新建窗口,程序例子中是采用的构造函数传值的形式。在新窗体里面去实现要做的功能。

主窗体中code:

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


namespace _3NewFormLevel
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                using (LF lf = new LF(commandData)) //通过新窗体的构造函数将文档信息全传递给了新窗体。任由新窗体进行其他操作。
                {
                    if (lf.ShowDialog()==DialogResult.OK )
                    {
                        return Result.Succeeded;
                    }
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
            return Result.Cancelled;
        }
    }
}

新窗体中code:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit;

namespace _3NewFormLevel
{
    public partial class LF : System.Windows.Forms.Form
    {
        private ExternalCommandData m_commandData;
        public LF()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 构造函数传值
        /// </summary>
        /// <param name="cd"></param>
        public LF(ExternalCommandData cd)
        {
            m_commandData = cd; 
            InitializeComponent();//初始化组件,窗体上的内容才可以在要的窗体中有体现
            run();//方法封装操作
        }

        private void run()
        {
            Document doc = m_commandData.Application.ActiveUIDocument.Document;
            FilteredElementCollector fec = new FilteredElementCollector(doc);
            ElementClassFilter ecf = new ElementClassFilter(typeof(Level));
            IList<Element> levels = fec.WherePasses(ecf).ToElements();
            foreach (Element  level in levels )
            {
                if (level !=null )
                {
                    comboBox1.Items.Add(level);
                }
            }
            if (this.comboBox1.Items.Count >0)
            {
                this.comboBox1.DisplayMember = "ID";//下拉框中显示的文字。字符串中的内容为对象的属性名你,Name ID等等,可以通过lookup查看
                this.comboBox1.SelectedIndex = 0;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Level level = this.comboBox1.SelectedItem as Level;//SelectedItem包含了装进去的对象的属性
            TaskDialog.Show("inf", level.Name);
        }
    }
}



猜你喜欢

转载自blog.csdn.net/jerryzfc/article/details/50780749
今日推荐