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();
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/shangdishijiao/p/15165231.html