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); }
public ObjectId DefineRasterImage(Document doc, string url, Point3d basePoint, Vector3d v1, Vector3d v2, string suggestedName, byte transparency) { ObjectId result; try { System.Drawing.Image.FromStream(new WebClient().OpenRead(url)); } catch { result = ObjectId.Null; return(result); } Database database = doc.Database; Editor editor = doc.Editor; ObjectId objectId = ObjectId.Null; ObjectId arg_39_0 = 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()) { string.IsNullOrEmpty(suggestedName); ObjectId objectId2 = RasterImageDef.GetImageDictionary(database); if (objectId2.IsNull) { objectId2 = RasterImageDef.CreateImageDictionary(database); } RasterImageDef rasterImageDef = new RasterImageDef(); rasterImageDef.SourceFileName = (url); rasterImageDef.Load(); bool arg_A4_0 = rasterImageDef.IsLoaded; DBDictionary dBDictionary = (DBDictionary)transaction.GetObject(objectId2, OpenMode.ForWrite); string text = RasterImageDef.SuggestName(dBDictionary, url); if (!string.IsNullOrEmpty(suggestedName)) { text = suggestedName; int num = 0; while (dBDictionary.Contains(text)) { num++; text = suggestedName + num; } } ObjectId arg_F8_0 = ObjectId.Null; if (dBDictionary.Contains(text)) { //editor.WriteMessage(AfaStrings.ImageAlreadyExits); result = ObjectId.Null; return(result); } dBDictionary.SetAt(text, rasterImageDef); transaction.AddNewlyCreatedDBObject(rasterImageDef, true); dBDictionary.Contains(text); ObjectId layer = GetLayer(database, transaction, ref text); 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 Transparency(b); rasterImage.Transparency = transparency2; ; rasterImage.Orientation = new CoordinateSystem3d(basePoint, v1, v2); BlockTable blockTable = (BlockTable)transactionManager.GetObject(database.BlockTableId, 0, false); BlockTableRecord blockTableRecord = (BlockTableRecord)transactionManager.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); int num2 = 0; try { num2 = blockTableRecord.Cast <object>().Count <object>(); } catch { } rasterImage.ColorIndex = (256); objectId = blockTableRecord.AppendEntity(rasterImage); transactionManager.AddNewlyCreatedDBObject(rasterImage, true); rasterImage.AssociateRasterDef(rasterImageDef); RasterImage.EnableReactors(true); rasterImageDef.UpdateEntities(); DrawOrderTable drawOrderTable = (DrawOrderTable)transaction.GetObject(blockTableRecord.DrawOrderTableId, OpenMode.ForWrite); ObjectIdCollection objectIdCollection = new ObjectIdCollection(); objectIdCollection.Add(objectId); drawOrderTable.MoveToBottom(objectIdCollection); try { rasterImageDef.UpdateEntities(); if (num2 == 0) { ZoomExtents(rasterImage.GeometricExtents.MinPoint, rasterImage.GeometricExtents.MaxPoint); editor.WriteMessage(rasterImage.Orientation.Origin.ToString()); } } catch { } transaction.Commit(); } } result = objectId; } catch (System.Exception ex) { result = ObjectId.Null; } catch { //rrorReport.ShowErrorMessage(AfaStrings.UnexpectedErrorInAddingRasterImage); result = ObjectId.Null; } return(result); }