.NET

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

image attach

216 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()
            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

 

Moderator
Alexander.Rivilis
Posts: 1,452
Registered: ‎04-09-2008
Message 2 of 2 (194 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.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

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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.