/// <summary> /// 确保布局中的图纸显示在布局中的中间,而不需要使用缩放命令来显示 /// </summary> /// <param name="db">数据库对象</param> /// <param name="layoutName">布局的名称</param> public static void CenterLayoutViewport(this Database db, string layoutName) { Extents3d ext = db.GetAllEntsExtent(); using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in bt) { BlockTableRecord btr = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord; if (btr.IsLayout) { Layout layout = btr.LayoutId.GetObject(OpenMode.ForRead) as Layout; if (layout.LayoutName.CompareTo(layoutName) == 0) { int vpIndex = 0; ObjectId firstViewportId = new ObjectId(); ObjectId secondViewportId = new ObjectId(); foreach (ObjectId entId in btr) { Entity ent = entId.GetObject(OpenMode.ForWrite) as Entity; if (ent is Viewport) { if (vpIndex == 0) { firstViewportId = entId; vpIndex++; } else if (vpIndex == 1) { secondViewportId = entId; } } } // 布局复制过来之后得到了两个视口,第一个视口与模型空间关联,第二个视口则是在正确的位置上 if (firstViewportId.IsValid && secondViewportId.IsValid) { Viewport secondVP = secondViewportId.GetObject(OpenMode.ForWrite) as Viewport; secondVP.ColorIndex = 1; secondVP.Erase(); Viewport firstVP = firstViewportId.GetObject(OpenMode.ForWrite) as Viewport; firstVP.CenterPoint = secondVP.CenterPoint; firstVP.Height = secondVP.Height; firstVP.Width = secondVP.Width; firstVP.ColorIndex = 5; Point3d midPt = GeTools.MidPoint(ext.MinPoint, ext.MaxPoint); firstVP.ViewCenter = new Point2d(midPt.X, midPt.Y);; double xScale = secondVP.Width / ((ext.MaxPoint.X - ext.MinPoint.X) * 1.1); double yScale = secondVP.Height / ((ext.MaxPoint.Y - ext.MinPoint.Y) * 1.1); firstVP.CustomScale = Math.Min(xScale, yScale); } } } } trans.Commit(); } }
/// <summary> /// 根据外接矩形创建椭圆 /// </summary> /// <param name="ellipse">椭圆对象</param> /// <param name="pt1">外接矩形的角点</param> /// <param name="pt2">外接矩形的角点</param> public static void CreateEllipse(this Ellipse ellipse, Point3d pt1, Point3d pt2) { //椭圆的中心点 Point3d center = GeTools.MidPoint(pt1, pt2); Vector3d normal = Vector3d.ZAxis;//法向量 //椭圆1/2长轴的矢量,矢量的起点为椭圆的中心,终点为椭圆长轴的一个端点 Vector3d majorAxis = new Vector3d(Math.Abs(pt1.X - pt2.X) / 2, 0, 0); //椭圆短轴与长轴的长度比例 double ratio = Math.Abs((pt1.Y - pt2.Y) / (pt1.X - pt2.X)); //设置椭圆的参数 ellipse.Set(center, normal, majorAxis, ratio, 0, 2 * Math.PI); }
/// <summary> /// 两点创建圆 /// </summary> /// <param name="circle">圆对象</param> /// <param name="startPoint">起点</param> /// <param name="endPoint">终点</param> public static void CreateCircle(this Circle circle, Point3d startPoint, Point3d endPoint) { circle.Center = GeTools.MidPoint(startPoint, endPoint); circle.Radius = startPoint.DistanceTo(endPoint) / 2; }