乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 18|回复: 0

Kean专题(18)—对象模型

[复制链接]

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2010-2-3 23:17:00 | 显示全部楼层 |阅读模式
一、创建非矩形的视口
December 14, 2009
Creating non-rectangular paperspace viewports in  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
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-28 23:12 , Processed in 1.100412 second(s), 55 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表