Map 3D中程序设置地图中心点

你可能需要在Map 3D中通过程序把某块地图拉到屏幕中央来,下面通过程序的方式来实现这个功能。

 

这里有两种情况,如果你在Map 3D中通过Data Connect连接到了一下FDO数据源(SDF、SHP、Oracle Spatial等等),并把他们加入到了地图上,这时你可以使用AcMapMap.ZoomToExtent方法,他接受一个MgEnvelope对象。但如果你么有FDO数据源,那么你就得用AutoCAD API来做了。

 

下面是实现代码:

        [CommandMethod("ZoomCenter")]
        public void ZoomCenter()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Point3d centerPt;
            PromptPointOptions ppo = new PromptPointOptions("Click on map to zoom center:");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status == PromptStatus.OK)
            {
                centerPt = ppr.Value;

                AcMapMap map = AcMapMap.GetCurrentMap();
                MgEnvelope mapExtent = map.GetMapExtent();

                //There are some FDO feature source connected 
                if (!mapExtent.IsNull())
                {
                    double centerX = mapExtent.LowerLeftCoordinate.X + mapExtent.Width / 2;
                    double centerY = mapExtent.LowerLeftCoordinate.Y + mapExtent.Height / 2;

                    ed.WriteMessage("center:" + centerX.ToString() + "," + centerY.ToString() + "\n");

                    MgEnvelope newExtent = new MgEnvelope(centerPt.X - mapExtent.Width / 2,
                                                    centerPt.Y - mapExtent.Height / 2,
                                                    centerPt.X + mapExtent.Width / 2,
                                                    centerPt.Y + mapExtent.Height / 2);

                    map.ZoomToExtent(newExtent);
                }
                else
                {
                    //no FDO data source, use AutoCAD API

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

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

                        viewportTableRec.CenterPoint = new Point2d(centerPt.X, centerPt.Y);

                        ed.UpdateTiledViewportsFromDatabase();

                        Tx.Commit();
                    }

                }


            }

        }

Related Posts Plugin for WordPress, Blogger...