autocad.net 利用linq获取矩形框内的块参照

利用linq获取在两点确定的矩形内的块参照 

Query syntax:

private ObjectId[] FindBlockReferencesInRectangle(Point3d pt1, Point3d pt2, Database db)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            using (Line line = new Line(pt1, pt2))
            {
                Extents3d bound = line.GeometricExtents;

                BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
                  SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                Func<ObjectId, bool> isBlockReference = (id =>
                    id.ObjectClass == RXClass.GetClass(typeof(BlockReference)));

                Func<ObjectId, bool> isInside = (id =>
                {
                    BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    Extents3d exts = new Extents3d(bound.MinPoint, bound.MaxPoint);
                    exts.AddExtents(br.GeometricExtents);
                    return bound.IsEqualTo(exts);
                }); ;

                var blocks =
                    from ObjectId id in modelSpace
                    where isBlockReference(id) && isInside(id)
                    select id;

                return blocks.ToArray();
            }
        }
View Code

Extension methods syntax:

        private ObjectId[] FindBlockReferencesInRectangle(Point3d pt1, Point3d pt2, Database db)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            using (Line line = new Line(pt1, pt2))
            {
                Extents3d bound = line.GeometricExtents;

                BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                Func<ObjectId, bool> isBlockReference = (id =>
                    id.ObjectClass == RXClass.GetClass(typeof(BlockReference)));

                Func<ObjectId, bool> isInside = (id =>
                {
                    BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    Extents3d exts = new Extents3d(bound.MinPoint, bound.MaxPoint);
                    exts.AddExtents(br.GeometricExtents);
                    return bound.IsEqualTo(exts);
                });

                return modelSpace
                    .Cast<ObjectId>()
                    .Where(isBlockReference)
                    .Where(isInside)
                    .ToArray();
            }
        }
View Code

autocad.net 利用linq获取矩形框内的块参照,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。