Navisworks 2014 Api 简单的使用

初次接触Navisworks Api  .NET 的二次开发.主要是研究了一下。关于NavisWorks 结构树的加载.

     void LoadModel()
        {
            //清空当前的结构树信息
            treeView1.Nodes.Clear();
            //当前加载的模型
            Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            
            //循环现有模型
            foreach (var documentModel in doc.Models)
            {
              
                var modelItemList = documentModel.RootItem.Descendants;
                Model model = documentModel;
                var modelItems = modelItemList.Where(o => o.Parent == model.RootItem);
           
                if (modelItems.Any())
                {
                    TreeNode cNode;
                    foreach (var quItem in modelItems)
                    {
                         cNode = new TreeNode(quItem.DisplayName);
                        cNode.Tag = quItem;
                       // cNode.Text = quItem.DisplayName;//判断名称
                        treeView1.Nodes.Add(cNode);
                        if (quItem.Children.Any())
                        {
                            LoadChild(quItem.Children, quItem, cNode);   
                        }
           
                      
                    }
                }
                
            }
  
        }

        /// <summary>
        /// 递归判断结构树信息
        /// </summary>
        /// <param name="modelItemEnumerableCollection">数据源信息</param>
        /// <param name="parentItem">父级节点信息</param>
        /// <param name="pNode">子节点信息</param>
        private void LoadChild(IEnumerable<ModelItem> modelItemEnumerableCollection, ModelItem parentItem, TreeNode pNode)
        {
            var query = modelItemEnumerableCollection.Where(o => o.Parent == parentItem);
            if (query.Count()>0)
            {
                foreach (var quItem in query)
                {
                    TreeNode chNode = new TreeNode(quItem.DisplayName);
                    chNode.Tag = quItem;
                    pNode.Nodes.Add(chNode);
                    if (quItem.Children.Any())
                    {
                        LoadChild(quItem.Children, quItem, chNode);
                    }
                  
                }
            }
        }

TreeView Node 选中事件

       void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = e.Node;
            if (node != null)
            {
                ModelItem oCurrentNode = (ModelItem)node.Tag;
                propertyGrid1.SelectedObject = oCurrentNode;
                if (oCurrentNode != null)
                {

                    //设置选择集合
                    ModelItemCollection oMC = new ModelItemCollection();
                    oMC.Add(oCurrentNode);
                    Document oDoc = view.ViewControl.DocumentControl.Document;
                    //设置选中
                    oDoc.CurrentSelection.CopyFrom(oMC);
                }
          }
        }

模型加载窗口:

    public partial class FrmModelView : DevExpress.XtraEditors.XtraForm
    {
        public ViewControl ViewControl;
        public FrmModelView()
        {
            InitializeComponent();
            if (this.components == null)
                this.components = new Container();
            //初始化Document控件
            DocumentControl document = new DocumentControl(this.components);
            document.Document.SetGraduatedBackground(Autodesk.Navisworks.Api.Color.FromByteRGB(176,196,222),Autodesk.Navisworks.Api.Color.FromByteRGB(176,196,166));
            //初始化View控件  并添加Document控件
            ViewControl = new ViewControl();
            ViewControl.DocumentControl = document;
            ViewControl.BackColor = System.Drawing.Color.LightSteelBlue;
            ViewControl.Dock = DockStyle.Fill;
            ViewControl.Name = "viewControl";
            this.Controls.Add(ViewControl);
        }
    }

模型选择事件:

        #region 模型选择事件

        private void CurrentSelection_Changed(object sender, EventArgs e)
        {
            try
            {
                Document doc = (Document)sender;
                if (doc != null)
                {
                    var item = doc.CurrentSelection.SelectedItems[0];
                    if (item != null)
                    {

                        TreeNode tnRet = null;
                        foreach (TreeNode tn in treeView1.Nodes)
                        {
                            tnRet = FindNode(tn, item.DisplayName);
                            if (tnRet != null)
                                break;
                        }
                        if (tnRet != null)
                        {
                            if (oldNode != null)
                                oldNode.BackColor = Color.White;
                            tnRet.BackColor = Color.YellowGreen;
                            treeView1.SelectedNode = tnRet;
                            oldNode = tnRet;
                            GetProperty();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        #endregion

模型的属性加载:

  #region 属性信息加载

        void GetProperty()
        {
            //验证模型
            if (Autodesk.Navisworks.Api.Application.ActiveDocument != null &&
                !Autodesk.Navisworks.Api.Application.ActiveDocument.IsClear)
            {
              
                this.vGridControl1.Rows.Clear();
                // 获取选中的相关的模型信息
                foreach (ModelItem item in Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems)
                {
              
                    //获取想的模型属性信息
                    foreach (PropertyCategory category in item.PropertyCategories)
                    {
                        CategoryRow  categoryRow=new CategoryRow(category.DisplayName);
                        foreach (var control in category.Properties)
                        {
                            EditorRow row1 = new EditorRow();
                            row1.Properties.FieldName = "Value";
                            row1.Properties.Caption = control.DisplayName;
                            var itemValue = control.Value;
                            string valueInfo;
                            switch (itemValue.DataType)
                            {
                                case VariantDataType.Boolean:
                                    valueInfo = itemValue.ToBoolean().ToString();
                                    break;
                                case VariantDataType.DateTime:
                                    valueInfo = itemValue.ToDateTime().ToString(CultureInfo.InvariantCulture);
                                    break;
                                case VariantDataType.DisplayString:
                                    valueInfo = itemValue.ToDisplayString();
                                    break;
                                case VariantDataType.Double:
                                    valueInfo = itemValue.ToDouble().ToString();
                                    break;
                                case VariantDataType.DoubleAngle:
                                    valueInfo = itemValue.ToDoubleAngle().ToString();
                                    break;
                                case VariantDataType.DoubleArea:
                                    valueInfo = itemValue.ToDoubleArea().ToString();
                                    break;
                                case VariantDataType.DoubleLength:
                                    valueInfo = itemValue.ToDoubleLength().ToString();
                                    break;
                                case VariantDataType.DoubleVolume:
                                    valueInfo = itemValue.ToDoubleVolume().ToString();
                                    break;
                                case VariantDataType.IdentifierString:
                                    valueInfo = itemValue.ToIdentifierString();
                                    break;
                                case VariantDataType.Int32:
                                    valueInfo = itemValue.ToInt32().ToString();
                                    break;
                                default:
                                    valueInfo = itemValue.ToString();
                                    break;
                            }
                            row1.Properties.Value = valueInfo;
                            
                            categoryRow.ChildRows.Add(row1);
                        }
                        this.vGridControl1.Rows.Add(categoryRow);
                    }
                }
            }
        }
        #endregion

最终效果:

主要是刚接触这个.不懂 只是自己在这写的。如果那位网友有更好的解决方案。请告诉我.谢谢哈。

源代码 下载。

posted @ 2014-11-25 22:11  在 水 一 方  阅读(7354)  评论(71编辑  收藏  举报