明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4141|回复: 0

[Kean专集] Kean专题(18)—对象模型

 关闭 [复制链接]
发表于 2010-2-3 23:17:00 | 显示全部楼层 |阅读模式
一、创建非矩形的视口
December 14, 2009
Creating non-rectangular paperspace viewports in AutoCAD using .NET
Thanks to Philippe Leefsma, from DevTech in Europe, for the ObjectARX code that inspired this post.
In AutoCAD it’s possible to create non-rectangular viewports in paperspace using a variety of closed curve objects: circles, polylines (2D, 3D and lightweight), ellipses, regions, splines and faces. In this post we’re going to see some code that creates four new viewports in the paperspace of the active drawing using a subset of these objects: an Ellipse, a Circle, a closed Spline and a closed Polyline.
Here’s the C# code:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. namespace ViewportCreation
  7. {
  8.   public class Commands
  9.   {
  10.     [CommandMethod("NRVPS")]
  11.     static public void CreateNonRectangularViewports()
  12.     {
  13.       Document doc =
  14.         Application.DocumentManager.MdiActiveDocument;
  15.       Database db = doc.Database;
  16.       Editor ed = doc.Editor;
  17.       // We're accessing drawing objects, so we need a transaction
  18.       Transaction tr = db.TransactionManager.StartTransaction();
  19.       using (tr)
  20.       {
  21.         // Get the primary paperspace from the block table
  22.         BlockTable bt =
  23.           (BlockTable)tr.GetObject(
  24.             db.BlockTableId,
  25.             OpenMode.ForRead
  26.           );
  27.         BlockTableRecord ps =
  28.           (BlockTableRecord)tr.GetObject(
  29.             bt[BlockTableRecord.PaperSpace],
  30.             OpenMode.ForWrite
  31.           );
  32.         // Create a variety of objects for our clip boundaries
  33.         DBObjectCollection objs = new DBObjectCollection();
  34.         // An ellipse...
  35.         Ellipse el =
  36.           new Ellipse(
  37.             new Point3d(3.5, 4.7, 0),
  38.             Vector3d.ZAxis,
  39.             new Vector3d(1.4, 0.03, 0),
  40.             0.35, 0, 0
  41.           );
  42.         objs.Add(el);
  43.         // A circle...
  44.         Circle cir =
  45.           new Circle(
  46.             new Point3d(3.4, 1.9, 0),
  47.             Vector3d.ZAxis,
  48.             0.9
  49.           );
  50.         objs.Add(cir);
  51.         // A closed polyline...
  52.         Polyline pl =
  53.           new Polyline(6);
  54.         pl.AddVertexAt(0, new Point2d(4.92, 5.29), 0, 0, 0);
  55.         pl.AddVertexAt(1, new Point2d(5.16, 6.02), 0, 0, 0);
  56.         pl.AddVertexAt(2, new Point2d(6.12, 6.49), 0, 0, 0);
  57.         pl.AddVertexAt(3, new Point2d(7.29, 6.26), -0.27, 0, 0);
  58.         pl.AddVertexAt(4, new Point2d(8.11, 5.53), -0.47, 0, 0);
  59.         pl.AddVertexAt(5, new Point2d(7.75, 5.41), 0, 0, 0);
  60.         pl.Closed = true;
  61.         objs.Add(pl);
  62.         // A closed spline...
  63.         Point3dCollection pts =
  64.           new Point3dCollection(
  65.             new Point3d[] {
  66.               new Point3d (5.5, 2.06, 0),
  67.               new Point3d (5.26, 2.62, 0),
  68.               new Point3d (5.66, 4.16, 0),
  69.               new Point3d (8.56, 4.21, 0),
  70.               new Point3d (7.2, 0.86, 0),
  71.               new Point3d (6.44, 2.85, 0),
  72.               new Point3d (5.62, 1.8, 0),
  73.               new Point3d (5.5, 2.06, 0)
  74.             }
  75.           );
  76.         Spline sp = new Spline(pts, 2, 0.5);
  77.         objs.Add(sp);
  78.         // Add each to the paperspace blocktablerecord
  79.         // and create/add an associated viewport object
  80.         foreach (DBObject obj in objs)
  81.         {
  82.           Entity ent = obj as Entity;
  83.           if (ent != null)
  84.           {
  85.             // Add our boundary to paperspace and the
  86.             // transaction
  87.             ObjectId id = ps.AppendEntity(ent);
  88.             tr.AddNewlyCreatedDBObject(obj, true);
  89.             // Create our viewport, adding that also
  90.             Viewport vp = new Viewport();
  91.             ps.AppendEntity(vp);
  92.             tr.AddNewlyCreatedDBObject(vp, true);
  93.             // Set the boundary entity and turn the
  94.             // viewport/clipping on
  95.             vp.NonRectClipEntityId = id;
  96.             vp.NonRectClipOn = true;
  97.             vp.On = true;
  98.           }
  99.         }
  100.         tr.Commit();
  101.       }
  102.       // Let's take a look at the results in paperspace
  103.       db.TileMode = false;
  104.     }
  105.   }
  106. }
Here’s what happens when we draw some geometry in modelspace:

And then call the NRVPS command to create our non-rectangular viewports

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 20:26 , Processed in 0.170608 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表