[MSCE C#] ScanCriteria 中用 BitMask 过滤元素

代码如下:

BDN.ScanCriteria sc = new BDN.ScanCriteria();
sc.SetModelRef(Session.Instance.GetActiveDgnModel());
sc.SetModelSections(BDN.DgnModelSections.GraphicElements);
BitMask elemTypeMask = new BitMask(false);
elemTypeMask.Capacity = 128;
elemTypeMask.ClearAll();
elemTypeMask.SetBit(1, true);//Tag标签代号?

sc.SetElementTypeTest(elemTypeMask);
sc.Scan((ele, model) => {
//处理元素

return StatusInt.Success;


});

目前出现两个问题,麻烦老师解答一下。
1
 BitMask类的方法SetBit中,第一个参数需要设置一个uint的bitIndex参数,来确定元素的类型,查找了DgnElements.h头文件。用了一个宏来计算类型。
#define ELMBITMSK(elmNum) (1<<((elmNum-1)%16))
这个类型要传入一个eleNum的参数,但是每种元素类型对应的eleNum是啥?这个在哪个文件中去找?
此外,“These following masks must be 'OR'ed with typemask[1]” 这句话是什么意思,有点不理解。比如说,TEXT_ELM元素 的bitIndex怎么算出来。

2
TagElement(VBA中的枚举MsdElementType.msdElementTypeTag )对应的bitIndex是多少,在头文件中没有找到。

  • private static void bitmaskTest()
            {
                ScanCriteria sc = new ScanCriteria();
                //sc.SetModelRef(Session.Instance.GetActiveDgnModel());
    
                DgnModel activeModel = Session.Instance.GetActiveDgnModel();
                foreach (DgnAttachment att in activeModel.GetDgnAttachments())
                {
                    sc.SetModelRef(att);
                    break;
                }
                sc.SetModelSections(DgnModelSections.GraphicElements);
    
                BitMask elemTypeMask = new BitMask(false);
                elemTypeMask.Capacity = 128;
                elemTypeMask.ClearAll();
                // Element Type Number - 1 = BitMask Index
                elemTypeMask.SetBit(2, true); // Element Type = Line, or (int)MSElementType.Line - 1
                elemTypeMask.SetBit(3, true); // Element Type = LineString
                elemTypeMask.SetBit(5, true); // Element Type = Shape
                elemTypeMask.SetBit(16, true); // Element Type = Text
                elemTypeMask.SetBit(18, true); // Element Type = Solid (Not SmartSolid)
                sc.SetElementTypeTest(elemTypeMask);
    
                sc.Scan(new ScanDelegate(Scan));
            }

    以上是一个我以前写的例子,请参考。

    These following masks must be 'OR'ed with typemask[1]” 这句话是什么意思,有点不理解

    这个是针对C/C++的调用来说的,在C# 中可以忽略这句话。



  • 我的这段代码就是照您的代码改的,但是这个代码里只有几个元素类型,我现在无法确定的是 elemTypeMask.SetBit这个方法中,第一次参数该输入的值是多少,比如说,我现在要扫描dgn文件中的所有Tag元素,第一个参数我应该输入的数字是多少。

  • 您试试elemTypeMask.SetBit(37, true); 

    Answer Verified By: Wulix