public DrawImage() { _zoomfactor = int.Parse(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ZOOMFACTOR").ToString()); _zoom_wheel = int.Parse(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ZOOMWHEEL").ToString()); Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged += Application_SystemVariableChanged; Autodesk.AutoCAD.ApplicationServices.Application.PreTranslateMessage += Application_PreTranslateMessage; Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument; MapTiles.darwBounds(); add0Level(); dwg.ViewChanged += dwg_ViewChanged; }
/// <summary> /// 视口变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dwg_ViewChanged(object sender, EventArgs e) { Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument; using (document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, false)) { Editor editor = document.Editor; //Size win_size = document.Window.GetSize(); //Point location = document.Window.GetLocation(); //editor.WriteMessage("\nwin_size:" + win_size.ToString() + "location:" + location.ToString()); if (1 == _level || 0 == _level) { return; } ViewTableRecord currentView = editor.GetCurrentView(); Point2d view_center_point = currentView.CenterPoint; Size view_size = new Size((int)currentView.Width, (int)currentView.Height); //editor.WriteMessage("\ncenter_point:" + view_center_point.ToString() + "view_size:" + view_size.ToString()); Point2d left_down = new Point2d(view_center_point.X - view_size.Width / 2.0, view_center_point.Y - view_size.Height / 2.0); Point2d right_up = new Point2d(view_center_point.X + view_size.Width / 2.0, view_center_point.Y + view_size.Height / 2.0); //Point3d center_point3d = (Point3d)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("VIEWCTR"); //Point2d size_screen = (Point2d)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("SCREENSIZE"); //Size size = new Size((int)size_screen.X, (int)size_screen.Y); //Point2d center_point = new Point2d(center_point3d.X, center_point3d.Y); //MapTiles.webMercatorBounds(center_point, _level, size, ref left_down, ref right_up); //document.Editor.WriteMessage("\n CenterPoint:" + center_point.ToString() + "left_down:" + left_down.ToString() + "right_up:" + right_up.ToString() + "level:" + _level.ToString()); //RectangleF view_image = new RectangleF(); //bool is_contain = MapTiles.getIntersects(left_down, right_up, ref view_image); //if (!is_contain) //{ // return; //} //else //{ // left_down = new Point2d(view_image.X, view_image.Y-view_image.Height); // right_up = new Point2d(view_image.X+view_image.Width, view_image.Y); // MapTiles.darwBounds(left_down,right_up,Autodesk.AutoCAD.Colors.Color.FromRgb(0,255,0),20,20); //} int start_row = 0, end_row = 0, start_col = 0, end_col = 0; MapTiles.webMercatorTilesFromBound(left_down, right_up, _level, out start_row, out start_col, out end_row, out end_col); addImage(document, MapTiles.webMercatorResolution(_level), start_row, end_row, start_col, end_col); } }
/// <summary> /// 加载 首级别的影像 /// </summary> public void add0Level() { Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument; using (document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, false)) { _level = 1; addImage(document, MapTiles.webMercatorResolution(1), 0, 1, 0, 1); } //Point2d point_left_up = MapTiles.webMercatorTilesLeftUpLocation(0,0,1); //string url = String.Format("http://t3.tianditu.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=c&TileMatrix={0}&TileRow={1}&TileCol={2}&style=default&format=tiles", 1, 0, 0); addImage(document, MapTiles.webMercatorResolution(1), 0, 1, 0, 1); //point_left_up = MapTiles.webMercatorTilesLeftUpLocation(1, 0, 1); //string url_01 = String.Format("http://t3.tianditu.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=c&TileMatrix={0}&TileRow={1}&TileCol={2}&style=default&format=tiles", 1, 0, 1); //addImage(document, url_01, point_left_up,MapTiles.webMercatorResolution(1)); ZoomExtents(); }
public ObjectId addImage(Document doc, double resulotion, int start_row, int end_row, int start_col, int end_col) { lockLayerOrNot(_layer_name, false); Database database = doc.Database; Editor editor = doc.Editor; ObjectId result; //try //{ // System.Drawing.Image.FromStream(new WebClient().OpenRead(url)); //} //catch //{ // result = ObjectId.Null; // editor.WriteMessage("Url is invalid!"); // return result; //} ObjectId objectId = ObjectId.Null; try { using (doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, false)) { Autodesk.AutoCAD.ApplicationServices.TransactionManager transactionManager = doc.TransactionManager; doc.TransactionManager.EnableGraphicsFlush(true); using (Transaction transaction = transactionManager.StartTransaction()) { ObjectId objectId2 = RasterImageDef.GetImageDictionary(database); if (objectId2.IsNull) { objectId2 = RasterImageDef.CreateImageDictionary(database); } //rasterImageDef.ResolutionMMPerPixel = new Vector2d(78217.51696, 78217.51696); DBDictionary dBDictionary = (DBDictionary)transaction.GetObject(objectId2, OpenMode.ForWrite); dBDictionary.Erase(); for (int i = start_row; i <= end_row; ++i) { for (int j = start_col; j <= end_col; ++j) { Point2d left_up = MapTiles.webMercatorTilesLeftUpLocation(j, i, _level); string url = string.Format("http://t3.tianditu.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=c&TileMatrix={0}&TileRow={1}&TileCol={2}&style=default&format=tiles", _level, i, j); RasterImageDef rasterImageDef = new RasterImageDef(); rasterImageDef.SourceFileName = (url); rasterImageDef.Load(); bool arg_A4_0 = rasterImageDef.IsLoaded; string text = RasterImageDef.SuggestName(dBDictionary, url); if (dBDictionary.Contains(text)) { result = ObjectId.Null; return(result); } dBDictionary.SetAt(text, rasterImageDef); transaction.AddNewlyCreatedDBObject(rasterImageDef, true); string layer_name = _layer_name; ObjectId layer = GetLayer(database, transaction, ref layer_name); RasterImage rasterImage = new RasterImage(); rasterImage.ImageDefId = (rasterImageDef.ObjectId); rasterImage.SetLayerId(layer, false); //byte b = Convert.ToByte(Math.Floor((100.0 - (double)transparency) / 100.0 * 254.0)); Transparency transparency2 = new Autodesk.AutoCAD.Colors.Transparency(100); rasterImage.Transparency = transparency2; rasterImage.Orientation = new CoordinateSystem3d(new Point3d(left_up.X, left_up.Y, 0), new Vector3d(resulotion * 256, 0, 0), new Vector3d(0, resulotion * 256, 0)); BlockTable blockTable = (BlockTable)transactionManager.GetObject(database.BlockTableId, 0, false); BlockTableRecord blockTableRecord = (BlockTableRecord)transactionManager.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); rasterImage.ColorIndex = (256); objectId = blockTableRecord.AppendEntity(rasterImage); transactionManager.AddNewlyCreatedDBObject(rasterImage, true); rasterImage.AssociateRasterDef(rasterImageDef); RasterImage.EnableReactors(true); rasterImageDef.UpdateEntities(); try { rasterImageDef.UpdateEntities(); } catch (System.Exception ex) { } } } //DrawOrderTable drawOrderTable = (DrawOrderTable)transaction.GetObject(blockTableRecord.DrawOrderTableId, OpenMode.ForWrite); //ObjectIdCollection objectIdCollection = new ObjectIdCollection(); //objectIdCollection.Add(objectId); //drawOrderTable.MoveToBottom(objectIdCollection); transaction.Commit(); } } result = objectId; } catch (System.Exception ex) { string message = ex.Message; ObjectId arg_2FE_0 = ObjectId.Null; result = ObjectId.Null; } catch { //rrorReport.ShowErrorMessage(AfaStrings.UnexpectedErrorInAddingRasterImage); result = ObjectId.Null; } lockLayerOrNot(_layer_name, true); return(result); }