I use the following code to attach an image. But when I look at xref-manager the code doesn't look if the image is alreday attached and if you attach the image twice, you have a second line test_1 that may not be there (see image attach). Can somenone help me to edit my code ?
<CommandMethod("attimage")> _ Public Sub attimage() attim("c:\test.jpg") End Sub Private Sub attim(ByVal filenaam As String) Dim myDWG As ApplicationServices.Document myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument Using trans As Transaction = myDWG.TransactionManager.StartTransaction() Dim idPaper As ObjectId = SymbolUtilityServices.GetBlockPaperSpaceId(myDWG.Database) Dim ms As BlockTableRecord = TryCast(trans.GetObject(idPaper, OpenMode.ForWrite), BlockTableRecord) Dim imageDef As RasterImageDef If filenaam = Nothing Then imageDef = Nothing Else imageDef = AddImageDef(myDWG.Database, trans, filenaam) If imageDef <> Nothing Then Dim image As New RasterImage() image.ImageDefId = imageDef.ObjectId Dim imageId As ObjectId = ms.AppendEntity(image) If imageId.IsNull Then Throw New Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId) End If trans.AddNewlyCreatedDBObject(image, True) RasterImage.EnableReactors(True) image.AssociateRasterDef(imageDef) Dim puntcol As New Geometry.Point3dCollection puntcol.Add(New Geometry.Point3d(0, 100, 0)) puntcol.Add(New Geometry.Point3d(100, 100, 0)) puntcol.Add(New Geometry.Point3d(100, 0, 0)) puntcol.Add(New Geometry.Point3d(0, 0, 0)) puntcol.Add(New Geometry.Point3d(0, 100, 0)) image.Orientation = New CoordinateSystem3d(New Point3d(0, 0, 0), New Vector3d(100, 0, 0), New Vector3d(0, 100, 0)) Dim vers As New Geometry.Point2dCollection Dim mat As Matrix3d mat = image.PixelToModelTransform.Inverse() Dim i As Integer For i = 0 To puntcol.Count - 1 puntcol(i) = puntcol(i).TransformBy(mat) vers.Add(puntcol(i).Convert2d(New Plane())) Next i image.ColorIndex = 253 image.SetClipBoundary(ClipBoundaryType.Poly, vers) Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("IMAGEFRAME", Convert.ToInt16(0)) image.SetClipBoundary(ClipBoundaryType.Poly, vers) image.DisplayOptions = ImageDisplayOptions.ShowUnaligned image.ImageTransparency = False Dim ids As New ObjectIdCollection() Dim dot As DrawOrderTable = DirectCast(trans.GetObject(ms.DrawOrderTableId, OpenMode.ForWrite), DrawOrderTable) ids.Add(imageId) dot.MoveToBottom(ids) End If trans.Commit() End Using End Sub Public Shared Function AddImageDef(ByVal db As Database, ByVal trans As Transaction, ByVal imageFilespec As String) As RasterImageDef If Not File.Exists(imageFilespec) Then Throw New FileNotFoundException(imageFilespec) End If Dim idImageDict As ObjectId = RasterImageDef.GetImageDictionary(db) If idImageDict.IsNull Then idImageDict = RasterImageDef.CreateImageDictionary(db) End If If idImageDict.IsNull Then Throw New InvalidOperationException("failed to get or create image dictionary") End If Dim imageDictionary As DBDictionary = TryCast(trans.GetObject(idImageDict, OpenMode.ForRead), DBDictionary) If imageDictionary = Nothing Then Throw New InvalidOperationException("Failed to open image dictionary") End If Dim imageDef As New RasterImageDef() Try imageDef.SourceFileName = imageFilespec imageDef.Load() Dim name As [String] = RasterImageDef.SuggestName(imageDictionary, imageFilespec) imageDictionary.UpgradeOpen() Dim idEntry As ObjectId = imageDictionary.SetAt(name, imageDef) If idEntry.IsNull Then Throw New InvalidOperationException("Failed to add image definition to image dictionary") End If trans.AddNewlyCreatedDBObject(imageDef, True) Return imageDef Catch imageDef.Dispose() Throw End Try End Function
Do I understand correctly, the question is that there is no duplication of RasterImageDef? If so you can change AddImageDef function such as:
Public Shared Function AddImageDef(ByVal db As Database, ByVal trans As Transaction, ByVal imageFilespec As String) As RasterImageDef If Not File.Exists(imageFilespec) Then Throw New FileNotFoundException(imageFilespec) End If Dim idImageDict As ObjectId = RasterImageDef.GetImageDictionary(db) If idImageDict.IsNull Then idImageDict = RasterImageDef.CreateImageDictionary(db) End If If idImageDict.IsNull Then Throw New InvalidOperationException("failed to get or create image dictionary") End If Dim imageDictionary As DBDictionary = TryCast(trans.GetObject(idImageDict, OpenMode.ForRead), DBDictionary) If imageDictionary = Nothing Then Throw New InvalidOperationException("Failed to open image dictionary") End If For Each en As DBDictionaryEntry In imageDictionary Dim imgDef As RasterImageDef = TryCast(trans.GetObject(en.Value, OpenMode.ForRead), RasterImageDef) If imgDef IsNot Nothing Then If imgDef.ActiveFileName = imageFilespec Or imgDef.SourceFileName = imageFilespec Then imgDef.UpgradeOpen() ' imgDef.Load() Return imgDef End If End If Next Dim imageDef As New RasterImageDef() Try imageDef.SourceFileName = imageFilespec imageDef.Load() Dim name As [String] = RasterImageDef.SuggestName(imageDictionary, imageFilespec) imageDictionary.UpgradeOpen() Dim idEntry As ObjectId = imageDictionary.SetAt(name, imageDef) If idEntry.IsNull Then Throw New InvalidOperationException("Failed to add image definition to image dictionary") End If trans.AddNewlyCreatedDBObject(imageDef, True) Return imageDef Catch imageDef.Dispose() Throw End Try End Function
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn