Use Database.GetViewports
布局自己本身就是一个视口。
Use Database.GetViewports(true)
依次返回的是,模型空间中的视口、布局一本身、布局一中的视口、布局二本身、布局二中的视口。
Use Database.GetViewports(false)
返回的是布局中的视口。排除了模型空间中的视口和布局本身。一般这个才是我们所最需要的。
通过遍历Layout中的视口依次返回的是:布局一本身、布局一中的视口、布局二本身、布局二中的视口。
GridBoundToLimits可以区分视口是布局还是布局中的视口。True为布局中的视口。
下面是测试代码。
[CommandMethod("Test")] public void Test() { Document doc = AcadApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; ed.WriteMessage("use my Func:\n"); ObjectIdCollection myViewports = new ObjectIdCollection(); using (Transaction trans = db.TransactionManager.StartTransaction()) { DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary; foreach (var layoutId in layoutDict) { Layout lout = trans.GetObject(layoutId.Value, OpenMode.ForRead) as Layout; if (lout == null) { continue; } var vports = lout.GetViewports(); foreach (var item in vports) { myViewports.Add((ObjectId)item); } } ed.WriteMessage(myViewports.Count.ToString() + "\n"); foreach (ObjectId id in myViewports) { ed.WriteMessage(string.Format("{0} IsErased = '{1}'\n", id.Handle, id.IsErased)); } } ed.WriteMessage("\nUse Database.GetViewports(true):\n"); var viewports = db.GetViewports(true); ed.WriteMessage(viewports.Count.ToString() + "\n"); foreach (ObjectId id in viewports) { ed.WriteMessage(string.Format("{0} IsErased = '{1}'\n", id.Handle, id.IsErased)); } ed.WriteMessage("\nUse Database.GetViewports(false):\n"); var viewportsfalse = db.GetViewports(false); ed.WriteMessage(viewportsfalse.Count.ToString() + "\n"); foreach (ObjectId id in viewportsfalse) { ed.WriteMessage(string.Format("{0} IsErased = '{1}'\n", id.Handle, id.IsErased)); } }