Zoom Extents in .Net

原文:来自ADN

缩放模型空间到最合适屏幕,即Zoom E命令

public void SetViewportToExtents(Database db, ViewportTableRecord viewportTableRec) 
{ 
    //lets update the database extents first
    //true gives the best fit but will take time
    db.UpdateExt(true);

    //get the screen aspect ratio to calculate the height and width
    double scrRatio = (viewportTableRec.Width / viewportTableRec.Height);

    //prepare Matrix for DCS to WCS transformation
    Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(viewportTableRec.ViewDirection);
    
    //for DCS target point is the origin
    matWCS2DCS = Matrix3d.Displacement(viewportTableRec.Target-Point3d.Origin) * matWCS2DCS; 
    
    //WCS Xaxis is twisted by twist angle
    matWCS2DCS = Matrix3d.Rotation(-viewportTableRec.ViewTwist,
                                    viewportTableRec.ViewDirection,
                                    viewportTableRec.Target) 
                                    * matWCS2DCS;
        
    matWCS2DCS = matWCS2DCS.Inverse();

    //tranform the extents to the DCS defined by the viewdir
    Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
    extents.TransformBy(matWCS2DCS);

    //width of the extents in current view
    double width = (extents.MaxPoint.X - extents.MinPoint.X);

    //height of the extents in current view
    double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

    //get the view center point
    Point2d center = new Point2d((extents.MaxPoint.X  + extents.MinPoint.X)*0.5, 
                                 (extents.MaxPoint.Y  + extents.MinPoint.Y)*0.5);
   
    //check if the width 'fits' in current window
    //if not then get the new height as per the viewports aspect ratio
    if (width > (height * scrRatio)) 
        height = width / scrRatio;

    viewportTableRec.Height = height; 
    viewportTableRec.Width = height * scrRatio;
    viewportTableRec.CenterPoint = center;
}

[CommandMethod("ModelZoomExtents")]
public void ModelZoomExtents()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        ed.UpdateTiledViewportsInDatabase();

        ViewportTableRecord viewportTableRec = Tx.GetObject(ed.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;

        SetViewportToExtents(db, viewportTableRec);

        ed.UpdateTiledViewportsFromDatabase();

        Tx.Commit();
    }
}
[CommandMethod("PaperZoomExtents")]
static public void PaperZoomExtents()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    PromptEntityOptions peo = new PromptEntityOptions("\nSelect a viewport: ");
    peo.SetRejectMessage("\nMust be a viewport...");
    peo.AddAllowedClass(typeof(Viewport), true);

    PromptEntityResult per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK) return;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForWrite) as Viewport;

    
        db.UpdateExt(true);

        double scrRatio = (vp.Width / vp.Height);

        Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
        
        matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget-Point3d.Origin) * matWCS2DCS; 
        
        matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle,
                                        vp.ViewDirection,
                                        vp.ViewTarget) 
                                        * matWCS2DCS;
            
        matWCS2DCS = matWCS2DCS.Inverse();

        Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
        extents.TransformBy(matWCS2DCS);

        double width = (extents.MaxPoint.X - extents.MinPoint.X);

        double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

        Point2d center = new Point2d((extents.MaxPoint.X  + extents.MinPoint.X)*0.5, 
                                     (extents.MaxPoint.Y  + extents.MinPoint.Y)*0.5);
       
        if (width > (height * scrRatio)) 
            height = width / scrRatio;

        vp.ViewHeight = height;
        vp.ViewCenter = center;

        Tx.Commit();
    }
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值