CAD创建不规则形状视口

时间:2024-04-17 14:19:09

选择CAD模型空间中多段线,在指定的布局中创建视口,方法如下:

 1         /// <summary>
 2         /// 创建视口
 3         /// </summary>
 4         /// <param name="roundLine">模型空间多段线</param>
 5         /// <param name="curentLayout">当前布局名称</param>
 6         /// <param name="insertPoint">布局空间视口插入点</param>
 7         /// <param name="scale">缩放比例</param>
 8         private void MakeViewport(Polyline roundLine, string curentLayout, Point3d insertPoint, double scale)
 9         {
10             Database db = HostApplicationServices.WorkingDatabase;
11             using (Transaction tran = db.TransactionManager.StartTransaction())
12             {
13                 BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
14                 BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
15                
16                 Polyline bound =  roundLine.Clone() as Polyline;
17                 bound.Closed = true;
18                 Point3d maxPoint = bound.GeometricExtents.MaxPoint;
19                 Point3d minPoint = bound.GeometricExtents.MinPoint;
20                 Point3d centerPoint = new Point3d((maxPoint.X + minPoint.X) / 2, (maxPoint.Y + minPoint.Y) / 2, 0);
21                 bound.TransformBy(Matrix3d.Scaling(scale, centerPoint));
22                 bound.TransformBy(Matrix3d.Displacement(centerPoint.GetVectorTo(insertPoint)));
23                 btr.AppendEntity(bound);
24                 tran.AddNewlyCreatedDBObject(bound, true);
25 
26                 Viewport vp = new Viewport();
27                 btr.AppendEntity(vp);
28                 tran.AddNewlyCreatedDBObject(vp, true);
29                 vp.NonRectClipEntityId = bound.Id;
30 
31                 vp.CenterPoint = insertPoint;  //视口插入位置
32                 vp.ViewCenter = new Point2d(centerPoint.X, centerPoint.Y); //模型空间中心位置
33 
34                 vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
35                 vp.Height = scale * vp.ViewHeight;  //视口高度
36                 vp.NonRectClipOn = true;
37                 vp.Locked = true;
38                 vp.On = true;
39                 tran.Commit();
40             }
41         }