AutoCAD.NET API2018二次开发第七章

显示模型空间范围

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//运行时
using Autodesk.AutoCAD.Runtime;
//数据库
using Autodesk.AutoCAD.DatabaseServices;
//应用程序服务
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
namespace AutoDemo9
{
    public class Class1
    {
        [CommandMethod("ZoomExtents")]
        public static void ZoomExtents()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;

            Database acDb = acDoc.Database;

            acDb.UpdateExt(false);
            using (Transaction acTran = acDb.TransactionManager.StartTransaction())
            {
                
                //视图记录表
                ViewTableRecord acView;
                //获取当前视图
                acView = acDoc.Editor.GetCurrentView();
                //获取3d
                Point3d pMin,pMax;

                //当前DB库显示范围最大点
                pMax = acDb.Extmax;
                //当前DB库显示范围最小点
                pMin = acDb.Extmin;

                //定义高度宽度
                double dWidth, dHeight;

                dWidth = pMax.X - pMin.X;
                dHeight = pMax.Y - pMin.Y;

                //当前视图的宽度和高度 比值 
                double dViewRation = acView.Width / acView.Height;

                if (dWidth>dHeight*dViewRation)dHeight = dWidth / dViewRation;

                acView.Width = dWidth;
                acView.Height = dHeight;

                Point2d pCenter = new Point2d((pMax.X + pMin.X) / 2, (pMax.Y + pMin.Y) / 2);

                //视图中新店 = 模型空间点
                acView.CenterPoint = pCenter;
                //更新视图
                acDoc.Editor.SetCurrentView(acView);
                //提交事务
                acTran.Commit();


            }

        }

    }
}

空间界限,当前模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//运行时
using Autodesk.AutoCAD.Runtime;
//数据库
using Autodesk.AutoCAD.DatabaseServices;
//应用程序服务
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;

namespace AutoDemo10
{
    public class Class1
    {
        [CommandMethod("ZoomLimints")]
        public static void ZoomLimints()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取当前数据库
            Database acDb = acDoc.Database;

            using (Transaction acTran = acDb.TransactionManager.StartTransaction())
            {
                //当前是图表记录
                ViewTableRecord acView = acDoc.Editor.GetCurrentView();

                Point2d pMin, pMax;

                //获取数据库界限的最大点
                pMax = acDb.Limmax;
                pMin = acDb.Limmin;

                //定义高度宽度
                double dWidth, dHeight;

                dWidth = pMax.X - pMin.X;
                dHeight = pMax.Y - pMin.Y;

                //当前比例
                double dViewRation = acView.Width / acView.Height;

                if (dWidth > dHeight * dViewRation) dHeight = dWidth / dViewRation;

                Point2d pCenter;

                double x = (pMin.X + pMax.X) / 2;

                double y = (pMin.Y + pMax.Y) / 2;

                pCenter = new Point2d(x,y);

                acView.Width = dWidth;
                acView.Height = dHeight;
                acView.CenterPoint = pCenter;

                acDoc.Editor.SetCurrentView(acView);


                acTran.Commit();
            }
        }
    }
}

定义两点范围,缩放到指定区域

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//运行时
using Autodesk.AutoCAD.Runtime;
//数据库
using Autodesk.AutoCAD.DatabaseServices;
//应用程序服务
using Autodesk.AutoCAD.ApplicationServices;
//几何
using Autodesk.AutoCAD.Geometry;

namespace AutoDemo11
{
    public class Class1
    {
        [CommandMethod("ZoomTop")]
        public static void ZoomLimints()
        {
            Point3d p1 = new Point3d(0, 0, 0);
            Point3d p2 = new Point3d(50, 500,0);

            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取当前数据库
            Database acDb = acDoc.Database;

            using (Transaction acTran = acDb.TransactionManager.StartTransaction())
            {
                //当前视图表记录
                ViewTableRecord acView = acDoc.Editor.GetCurrentView();

                Extents3d eExt;

                using (Line line = new Line(p1,p2))
                {
                    //获取边界值得最大小值
                    eExt = new Extents3d(line.Bounds.Value.MinPoint,line.Bounds.Value.MaxPoint);
                }

                //获取比例
                double viewR = acView.Width / acView.Height;

                //定义宽度高度
                double dWidth, dHeight;

                //三维范围 最大点减最小点
                dWidth = eExt.MaxPoint.X - eExt.MinPoint.X;

                dHeight = eExt.MaxPoint.Y - eExt.MinPoint.Y;

                //如果当前视图宽度大于
                if (dWidth > dHeight * viewR) dHeight = dWidth / viewR;

                //声明二维
                Point2d pC;

                double x, y;

                x = (eExt.MinPoint.X + eExt.MaxPoint.X) / 2;
                y = (eExt.MinPoint.Y + eExt.MaxPoint.Y) / 2;

                pC = new Point2d(x, y);

                acView.CenterPoint = pC;

                acView.Width = dWidth;
                acView.Height = dHeight;

                acDoc.Editor.SetCurrentView(acView);

                acTran.Commit();

            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值