zoukankan      html  css  js  c++  java
  • C# CAD 参照块或者xref 外部参照块范围 画矩形

     

    C#  CAD 参照块范围 画矩形

    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.DatabaseServices.Filters;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.Runtime;
    using Beaver.Core.Cad;
    
    namespace SpatialFiltering
    {
        public class Commands : CadBase
        {
            private const string filterDictName = "ACAD_FILTER";
    
            private const string spatialName = "SPATIAL";
    
            [CommandMethod("DXC")]
            public void DetectXClip()
            {
                var doc = Application.DocumentManager.MdiActiveDocument;
    
                var ed = doc.Editor;
    
                // Ask for an xclipped xref to be selected
    
                var peo =
    
                  new PromptEntityOptions(
    
                    "
    Select xclipped block or xref"
    
                  );
    
                peo.SetRejectMessage("Must be a block or xref.");
    
                peo.AddAllowedClass(typeof(BlockReference), false);
    
                var per = ed.GetEntity(peo);
    
                if (per.Status != PromptStatus.OK)
    
                    return;
                Point3d maxPoint;
                Point3d minPoint;
                var tr = doc.TransactionManager.StartTransaction();
    
                using (tr)
                {
                    // Open the selected BlockReference
                    var br = tr.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference;
                    // To save multiple codepaths with the same message
                    // ("No clipping information found"), we'll use a flag to
                    // see whether we've found anything
                    bool found = false;
                    // It should always be a block reference, but it might
                    // not have an extension dictionary
    
                    if (
                      br != null && br.ExtensionDictionary != ObjectId.Null)
                    {
                        // The extension dictionary needs to contain a nested
                        // dictionary called ACAD_FILTER
                        var extdict =
                          tr.GetObject(br.ExtensionDictionary, OpenMode.ForRead)
                          as DBDictionary;
    
                        if (extdict != null && extdict.Contains(filterDictName))
                        {
                            var fildict =
                              tr.GetObject(
                                extdict.GetAt(filterDictName), OpenMode.ForRead
                              ) as DBDictionary;
    
                            if (fildict != null)
                            {
                                // The nested dictionary should contain a
    
                                // SpatialFilter object called SPATIAL
    
                                if (fildict.Contains(spatialName))
                                {
                                    var fil =
                                      tr.GetObject(
                                        fildict.GetAt(spatialName), OpenMode.ForRead
                                      ) as SpatialFilter;
    
                                    if (fil != null)
                                    {
                                        // We have a SpatialFilter: print its bounds
                                        var ext = fil.GetQueryBounds();
                                        ed.WriteMessage(
                                          "
    Found clip from {0} to {1}.",
                                          ext.MinPoint, ext.MaxPoint
                                        );
                                        maxPoint = ext.MaxPoint;
                                        minPoint = ext.MinPoint;
                                        var pts = fil.Definition.GetPoints();
                                        foreach (var pt in pts)
                                        {
                                            ed.WriteMessage("
    Boundary point at {0}", pt);
                                        }
    
                                        found = true;
                                    }
                                }
                            }
                        }
                    }
    
                    if (!found)
                    {
                        ed.WriteMessage("
    No clipping information found.");
                    }
                    else
                    {
                       var polyline  =AddEntityEx.Rect(minPoint.ToPoint2d(), maxPoint.ToPoint2d());
                        var objids = Database.AddEntityToModelSpace(new Entity[] { polyline });
                    }
    
                    tr.Commit();
                }
            }
        }
    }
    

      

  • 相关阅读:
    tlb、tlh和tli文件的关系
    String算法
    Reverse A String by STL string
    windows内存管理复习(加深了理解得很!)
    [转载]有关DLL中New和外部Delete以以及跨DLL传递对象的若干问题
    顺势工作时间
    C++箴言:绝不在构造或析构期调用虚函数
    inline函数复习
    从编译器的角度更加深入考虑封装的使用
    复习:constructor和destructor的compiler实现
  • 原文地址:https://www.cnblogs.com/shangdishijiao/p/15165231.html
Copyright © 2011-2022 走看看