Valued Contributor
Posts: 75
Registered: ‎09-14-2009
Message 1 of 2 (192 Views)

image attach

192 Views, 1 Replies
03-25-2013 02:08 PM

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()
        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)
                    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)
                End If
            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()
                imageDef.SourceFileName = imageFilespec

                Dim name As [String] = RasterImageDef.SuggestName(imageDictionary, imageFilespec)

                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
            End Try
        End Function


Posts: 1,417
Registered: ‎04-09-2008
Message 2 of 2 (170 Views)

Re: image attach

03-25-2013 04:50 PM in reply to: fallright

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 DatabaseByVal trans As TransactionByVal imageFilespec As StringAs 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.Load()
            Return imgDef
          End If
        End If
      Dim imageDef As New RasterImageDef()
        imageDef.SourceFileName = imageFilespec
        Dim name As [String] = RasterImageDef.SuggestName(imageDictionary, imageFilespec)
        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
      End Try
    End Function

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.