CAD创建不规则形状视口

时间:2021-12-25 21:27:14

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

         /// <summary>
/// 创建视口
/// </summary>
/// <param name="roundLine">模型空间多段线</param>
/// <param name="curentLayout">当前布局名称</param>
/// <param name="insertPoint">布局空间视口插入点</param>
/// <param name="scale">缩放比例</param>
private void MakeViewport(Polyline roundLine, string curentLayout, Point3d insertPoint, double scale)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord; Polyline bound = roundLine.Clone() as Polyline;
bound.Closed = true;
Point3d maxPoint = bound.GeometricExtents.MaxPoint;
Point3d minPoint = bound.GeometricExtents.MinPoint;
Point3d centerPoint = new Point3d((maxPoint.X + minPoint.X) / , (maxPoint.Y + minPoint.Y) / , );
bound.TransformBy(Matrix3d.Scaling(scale, centerPoint));
bound.TransformBy(Matrix3d.Displacement(centerPoint.GetVectorTo(insertPoint)));
btr.AppendEntity(bound);
tran.AddNewlyCreatedDBObject(bound, true); Viewport vp = new Viewport();
btr.AppendEntity(vp);
tran.AddNewlyCreatedDBObject(vp, true);
vp.NonRectClipEntityId = bound.Id; vp.CenterPoint = insertPoint; //视口插入位置
vp.ViewCenter = new Point2d(centerPoint.X, centerPoint.Y); //模型空间中心位置 vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
vp.Height = scale * vp.ViewHeight; //视口高度
vp.NonRectClipOn = true;
vp.Locked = true;
vp.On = true;
tran.Commit();
}
}