输出特定颜色清单

2019-06-05  本文已影响0人  饭桶2018

比如我们想输出模型中红色部分部件清单

我们需要先知道红色的代号是什么 ,获取方法 点此前往

然后修改下面代码中color值即可。

Color myColor = new Color(1.0, 0.0, 0.0); // construct color you want to find
image.png

我们可以使用如下脚本

image.png
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Color = Autodesk.Navisworks.Api.Color;

namespace CScript
{
  public class CScript
  {
     static public void Main()
     {

        // export name with override color https://forums.autodesk.com/t5/navisworks-api/select-elements-by-color-override/td-p/7084412
        Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;

        Color myColor = new Color(1.0, 1.0, 0.0); // construct color you want to find

        ModelItemCollection hidden = new ModelItemCollection();

        ModelItemCollection oModelColl= oDoc.CurrentSelection.SelectedItems;
        int n = oModelColl.Count();
        if (n != 1) {
            Console.WriteLine("Please Select In Selection Tree!!!");
            return;
        }
       
        ModelItem  ce = oModelColl[0];
        var descendants = ce.Descendants;

        Color itemColor;
        Console.WriteLine("Start Search Color {0} from {1} ", myColor, ce.DisplayName );
        foreach(ModelItem oItem in descendants) {
          try{
            itemColor = oItem.Geometry.PermanentColor;
            if( itemColor.ToString() == myColor.ToString()){
                Console.WriteLine("{3}\t{2}\t{1}",oItem.Geometry.PermanentColor, oItem.DisplayName, oItem.Parent.DisplayName, oItem.Parent.Parent.DisplayName  );
                hidden.Add(oItem); // 如果不需要隐藏,请把我注释掉
            }
          }
          catch(Exception)
          {
            continue;
          }
        }
     Autodesk.Navisworks.Api.Application.ActiveDocument.Models.SetHidden(hidden, true);
     }
  }
}
上一篇下一篇

猜你喜欢

热点阅读