setView的实现

来源:互联网 发布:淘宝怎么拆分订单付款 编辑:程序博客网 时间:2024/06/05 20:25

    昨天文章中提到的setView的函数,采用了如下代码实现。其中,先获取当前DwgView的尺寸,然后在保证pt1/pt2的显示范围的前提下,以指定矩形中心点为中心,计算出符合当前DwgView比例的范围,然后再外扩,从而得到相关的显示范围。以下代码权作备份吧,如果有开发类似的也可以以此作参考。

//设置当前显示范围
void setView(AcGePoint2d Pt1, AcGePoint2d Pt2, double ex_ratio)
{
 AcGePoint2d CenterPt;

 //若X坐标或Y坐标重合,判为意外,不进行SetView操作
 if ((fabs(Pt1.x-Pt2.x)<1e-6)||(fabs(Pt1.y-Pt2.y)<1e-6))
  return;

 //确保两个坐标点分别为左上角和右下角
 if (Pt1.x>Pt2.x) {
  double tmp;
  tmp = Pt1.x;
  Pt1.x = Pt2.x;
  Pt2.x = tmp;
 }
 if (Pt2.y>Pt1.y) {
  double tmp;
  tmp = Pt1.y;
  Pt1.y = Pt2.y;
  Pt2.y = tmp;
 }
 
 //获取当前DwgView的尺寸
 CRect CADrect;
 acedGetAcadDwgView()->GetClientRect(&CADrect);

 double width,height,ratio;

 ratio = (double)(CADrect.right-CADrect.left)/(double)(CADrect.bottom-CADrect.top);

 if (fabs(ratio)<1e-6)
  return;

 if ((Pt2.x-Pt1.x)/(Pt1.y-Pt2.y) > ratio) {
  width = Pt2.x-Pt1.x;
  height = width/ratio;
 }else{
  height = Pt1.y-Pt2.y;
  width = height * ratio;
 }

 //设置当前视图中心点
 CenterPt.x = (Pt1.x+Pt2.x)/2;
 CenterPt.y = (Pt1.y+Pt2.y)/2;
 
 //改变当前视图
 AcDbViewTableRecord pVwRec;
 pVwRec.setCenterPoint(CenterPt);
 pVwRec.setWidth(width * ex_ratio);
 pVwRec.setHeight(height * ex_ratio);
 acedSetCurrentView( &pVwRec, NULL );
}

 

原创粉丝点击