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

最終效果:

主要是剛接觸這個.不懂 只是自己在這寫的。如果那位網友有更好的解決方案。請告訴我.謝謝哈。

源代碼 下載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM