Get thumbnail from BOM or File using Apprentice from background worker og thread

Get thumbnail from BOM or File using Apprentice from background worker og thread

Norgaard87
Explorer Explorer
433 Views
1 Reply
Message 1 of 2

Get thumbnail from BOM or File using Apprentice from background worker og thread

Norgaard87
Explorer
Explorer

Hi

 

We are trying to access Inventor file information such as iproperties and thumbnail.

in our old softawre we godt the thumbnail from iProperties using aprentice running in main thread, and that worked just fine.

In the new software we are using a background worker and threading to speed up the process of getting and analyzing data.

 

this works just find for all iProperties except for the thumbnail. we have tried to get the Thumbnail from iProperties and directly from Doc.Thumbnail but both methods fires an exception.

 

we have made a method to get the thumbnail using ShellFile as seen below. but this method is a lot slower than using apprentice.

 

    Private Function GetThumbnailBytes(filename As String, prop As InvPropertyDef, desiredHeight As Integer) As InvProperty
        Dim result As New InvProperty With {.Id_InvPropertyDef = prop.Id, .Name = prop.Name}
        Dim thumbnailBytes As Byte()

        Using sfile As ShellFile = ShellFile.FromFilePath(filename)
            Dim thumbBmp As Bitmap = Nothing
            thumbBmp = If(desiredHeight > 256, sfile.Thumbnail.ExtraLargeBitmap, sfile.Thumbnail.LargeBitmap) '.ExtraLargeBitmap
            'Dim thumbBmp2 As Bitmap = sfile.Thumbnail.LargeBitmap
            'Dim thumbBmp3 As Bitmap = sfile.Thumbnail.SmallBitmap

            'compute new width
            Dim Ratio As Double = CDbl(thumbBmp.Width / thumbBmp.Height)
            Dim height As Integer = desiredHeight
            Dim width As Integer = CInt(height / Ratio)

            'resize
            Using resizedBmp As Bitmap = New Bitmap(thumbBmp, width, height)
                Using ms As MemoryStream = New MemoryStream()
                    resizedBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                    thumbnailBytes = ms.ToArray()
                End Using
            End Using
        End Using
        result.ImageValue = thumbnailBytes
        Return result
    End Function

  

Have anyone else tried to get the thumbnail using Apprentice or Inventor from a Background workers and/or Threading?

0 Likes
434 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor

I'm not sure it is possible. When I try to create simple application for extracting Thumbnail from ApprenticeDocument it doesn't work until I set [STAThread] attribute on the Main() method. Perhaps it prevents the application to use multithreading on COM objects.

 

Another thing is when I try to implement another application for Apprentice few years ago, I had a problems with concurrent access to Inventor files. I got errors when I opened assembly in single Apprentice instance and try to open one of its referenced document in another instance. Be careful for it.

 

If you want to extract the Thumbnail from your application and you want to use multithreading, you can split your workflow to several separate applications. Than it can looks like:

  • From the Main.exe start new process GetThumbnail.exe with the path to your interested file(s) as argument.
  • You can wait for the result using async if you use some wrapper. For example this NuGet package https://www.nuget.org/packages/RunProcessAsTask/
  • You are not able to obtain the Thumbail as Image as a result of process, but you can create file thumbnail.jpg somewhere in known location and from Main.exe you can read the file and use them for your needs.

 

I hope it helps, but it is not easy job.

 

0 Likes