- 积分
- 24566
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2010-2-3 23:05: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:- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
-
- namespace ViewportCreation
- {
- public class Commands
- {
- [CommandMethod("NRVPS")]
- static public void CreateNonRectangularViewports()
- {
- Document doc =
- Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
-
- // We're accessing drawing objects, so we need a transaction
-
- Transaction tr = db.TransactionManager.StartTransaction();
- using (tr)
- {
- // Get the primary paperspace from the block table
-
- BlockTable bt =
- (BlockTable)tr.GetObject(
- db.BlockTableId,
- OpenMode.ForRead
- );
- BlockTableRecord ps =
- (BlockTableRecord)tr.GetObject(
- bt[BlockTableRecord.PaperSpace],
- OpenMode.ForWrite
- );
-
- // Create a variety of objects for our clip boundaries
-
- DBObjectCollection objs = new DBObjectCollection();
-
- // An ellipse...
-
- Ellipse el =
- new Ellipse(
- new Point3d(3.5, 4.7, 0),
- Vector3d.ZAxis,
- new Vector3d(1.4, 0.03, 0),
- 0.35, 0, 0
- );
- objs.Add(el);
-
- // A circle...
-
- Circle cir =
- new Circle(
- new Point3d(3.4, 1.9, 0),
- Vector3d.ZAxis,
- 0.9
- );
- objs.Add(cir);
-
- // A closed polyline...
-
- Polyline pl =
- new Polyline(6);
- pl.AddVertexAt(0, new Point2d(4.92, 5.29), 0, 0, 0);
- pl.AddVertexAt(1, new Point2d(5.16, 6.02), 0, 0, 0);
- pl.AddVertexAt(2, new Point2d(6.12, 6.49), 0, 0, 0);
- pl.AddVertexAt(3, new Point2d(7.29, 6.26), -0.27, 0, 0);
- pl.AddVertexAt(4, new Point2d(8.11, 5.53), -0.47, 0, 0);
- pl.AddVertexAt(5, new Point2d(7.75, 5.41), 0, 0, 0);
- pl.Closed = true;
- objs.Add(pl);
-
- // A closed spline...
-
- Point3dCollection pts =
- new Point3dCollection(
- new Point3d[] {
- new Point3d (5.5, 2.06, 0),
- new Point3d (5.26, 2.62, 0),
- new Point3d (5.66, 4.16, 0),
- new Point3d (8.56, 4.21, 0),
- new Point3d (7.2, 0.86, 0),
- new Point3d (6.44, 2.85, 0),
- new Point3d (5.62, 1.8, 0),
- new Point3d (5.5, 2.06, 0)
- }
- );
- Spline sp = new Spline(pts, 2, 0.5);
- objs.Add(sp);
-
- // Add each to the paperspace blocktablerecord
- // and create/add an associated viewport object
-
- foreach (DBObject obj in objs)
- {
- Entity ent = obj as Entity;
- if (ent != null)
- {
- // Add our boundary to paperspace and the
- // transaction
-
- ObjectId id = ps.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(obj, true);
-
- // Create our viewport, adding that also
-
- Viewport vp = new Viewport();
- ps.AppendEntity(vp);
- tr.AddNewlyCreatedDBObject(vp, true);
-
- // Set the boundary entity and turn the
- // viewport/clipping on
-
- vp.NonRectClipEntityId = id;
- vp.NonRectClipOn = true;
- vp.On = true;
- }
- }
- tr.Commit();
- }
-
- // Let's take a look at the results in paperspace
-
- db.TileMode = false;
- }
- }
- }
Here’s what happens when we draw some geometry in modelspace:
And then call the NRVPS command to create our non-rectangular viewports
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|