Bentley二次开发教程28-交互窗口-案例实战2-EC属性赋值工具

发布于:2024-04-29 ⋅ 阅读:(24) ⋅ 点赞:(0)

EC属性赋值工具

该工具会根据用户输入的路径获取Schema文件的路径,然后通过路径读取对应的Schema文件并在树状图中显示EC类,当用户选择树状图上的节点时,此时会触发事件,根据EC属性添加用户自定义控件用于显示EC属性并可对其进行赋值操作。完成后选取元素,点击界面上的应用,此时元素会被赋予指定EC类,同时修改的EC属性值也会被同步赋予。
该工具涉及到的WinForm控件有:TextBox,Button,TreeView,Panel,UserControl(用户自定义控件)。
在这里插入图片描述
在这里插入图片描述

using Bentley.DgnPlatformNET;
                }                 
                instance.WriteChanges();//更新文件上的EC实例
            }                                                 
        }

        private void AddECPropItems(TreeNodeMouseClickEventArgs e)
        {
            m_proppanel_ECProp.Controls.Clear();//清空面板中的用户控件

            IECClass eCs = m_ecschema.GetClass(e.Node.Name);//使用树状图中的节点名称从ECSchema中获取EC类
            IEnumerable<IECProperty> props = eCs.Properties(true);//获得EC类中的EC属性
            int j = 0;//声明int值用于控制用户控件的间距
            foreach (IECProperty prop in props)//遍历EC类中的EC属性
            {
                ECItem item = new ECItem();//声明用于显示EC属性及值的用户控件
                item.label1.Text = prop.Name;//设置用户控件中的标签名称为EC属性名称
                item.textBox1.Tag = prop.Type.Name;//设置用户控件中的文本框的标签为EC属性类型
                switch (prop.Type.Name)//选择EC属性值
                {
                    case "int"://若EC属性为intbreak;//跳出
                    case "string"://若EC属性为stringbreak;//跳出
                    default://其余情况
                        item.textBox1.Enabled = false;//锁定文本框,不允许用户输入数据
                        break;//跳出
                }
                item.Top = 28 * j;//设置用户控件位置
                item.Parent = m_proppanel_ECProp;//确定控件依赖的面板
                j++;//增加int值
            }
            m_proppanel_ECProp.AutoScroll = true;//启动面板自动滚动
            m_proppanel_ECProp.Show();//显示面板
        }

        private void CreateTreeView()
        {
            IECClass[]classes= m_ecschema.GetClasses();//获得ECSchema中的所有EC类            
            for(int i=0;i<classes.Count();i++)//遍历EC类
            {
                TreeNode ti = new TreeNode();//创建树状图节点
                ti.Name = classes[i].Name;//设置树状图节点的名称的EC类名
                ti.Text = classes[i].Name;//设置树状图节点的文字的EC类名
                m_treeView_ECClass.Nodes.Add(ti);//在树状图中添加节点                
            }
            m_treeView_ECClass.ExpandAll();//展开树状图
        }

        private void SelectSchemaFilePath()
        {
            OpenFileDialog dialog = new OpenFileDialog();//声明文件对话框
            dialog.Filter = "*.ecschema.xml|*.ecschema.XML";//设置文件选取过滤器为识别后缀.ecschema.xml的文件
            dialog.FilterIndex = 1;//设置过滤器索引
            if (dialog.ShowDialog() == DialogResult.OK)//判断是否成功输入
            {
                this.m_textBox_import.Text = dialog.FileName;//设置文本框中的文字为文件路径
            }
        }

        private void LoadSchemaData()
        {
            XmlDocument xmldoc = new XmlDocument();//声明XML文件浏览器
            xmldoc.Load(m_textBox_import.Text);//读取指定路径的xml文件
            string xmlStr = xmldoc.InnerXml;//获得读取到的文字内容

            ECSchemaXmlStringReader xmlReader = new ECSchemaXmlStringReader(xmlStr);//声明ECSchema阅读器
            m_ecschema = xmlReader.Deserialize();//将EC Schema阅读器读取到的信息反序列化

            if (null == m_ecschema)//判断是否成功获得ECSchema
            {
                MessageBox.Show("Schema not found, please check");//对话框输出未获得指定ECSchema
                return;//返回
            }
            ImportSchemaOptions options = new ImportSchemaOptions();//声明导入Schema选项              
            DgnECManager.Manager.ImportSchema(m_ecschema, m_dgnFile, options);//导入ECSchema
            return;//返回
        }       
    }
}
namespace WinformUIIntroduction.UI
            // 
            this.m_proppanel_ECProp.Location = new System.Drawing.Point(187, 42);
            this.m_proppanel_ECProp.Name = "m_proppanel_ECProp";
            this.m_proppanel_ECProp.Size = new System.Drawing.Size(336, 206);
            this.m_proppanel_ECProp.TabIndex = 4;
            // 
            // ECAttachMenu
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(538, 289);
            this.Controls.Add(this.m_proppanel_ECProp);
            this.Controls.Add(this.m_textBox_import);
            this.Controls.Add(this.m_button_apply);
            this.Controls.Add(this.m_button_import);
            this.Controls.Add(this.m_treeView_ECClass);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "ECAttachMenu";
            this.ShowIcon = false;
            this.Text = "ECAttachTool";
            this.TopMost = true;
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TreeView m_treeView_ECClass;
        private System.Windows.Forms.Button m_button_import;
        private System.Windows.Forms.Button m_button_apply;
        private System.Windows.Forms.TextBox m_textBox_import;
        private System.Windows.Forms.Panel m_proppanel_ECProp;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformUIIntroduction.UI
{
    public partial class ECItem : UserControl
    {
        public ECItem()
        {
            InitializeComponent();       
        }        

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//在文本框中点击按键时触发
        {
            switch (textBox1.Tag)//根据文本框的标签进行设置
            {
                case "int"://当文本框的标签设置为"int"if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))//只允许在文本框中输入数组与回退
                    {
                        e.Handled = true;//返回真
                    }
                    break;//跳出
                case "string"://当文本框的标签设置为"string"break;//跳出
            }            
        }       
    }
}

在这里插入图片描述