Author Topic: how to get preview dwg without autocad ?  (Read 48321 times)

0 Members and 1 Guest are viewing this topic.

bikelink

  • Guest
how to get preview dwg without autocad ?
« on: November 17, 2009, 05:59:31 AM »
I've been searchin for a while  to get a preview without use readdwg --> getthumbnail but I haven't found a solution.
Could  someone give me some suggestion about it ?
thanks in advance.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: how to get preview dwg without autocad ?
« Reply #1 on: November 17, 2009, 02:05:46 PM »
Fire up a session of TrueView and use the drawing file as an argument.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

BillZndl

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #2 on: November 17, 2009, 02:31:02 PM »
You didn't say what versions you're using but here's some more info on the subject:

http://discussion.autodesk.com/forums/thread.jspa?messageID=5405749&#5405749


bikelink

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #3 on: November 18, 2009, 02:09:47 AM »
some as "extractImage" because I need to obtain the preview of a files in a folder.I've tried but it don't works.
« Last Edit: November 18, 2009, 02:25:09 AM by bikelink »

BillZndl

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #4 on: November 18, 2009, 06:37:45 AM »
same as "extractImage" because I need to obtain the preview of a files in a folder.I've tried but it don't works.

Doesn't work? Really?
That's exactly what I use it for.

What doesn't work?

Oh, this is an external app you're using?

I use it inside AutoCAD in a netloadable dll.






bikelink

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #5 on: November 18, 2009, 09:11:30 AM »
yes it is extern code. I'm  trying to write a usercontrol in order to show preview of a  folder drawings.
Inside autocad I haven't trouble , can i use thumbnail of database or some method to create a preview..(i don't remember the exact name but i already wrote it).
well..I can't imagine how an extern program could be retrieve the preview from dwg!  :x
 
the API to retrieve the bitmap ,loads acmgdb.dll and then  it crash if you don't run inside autocad.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: how to get preview dwg without autocad ?
« Reply #6 on: November 18, 2009, 11:35:55 PM »
OK, I modified a bit of code that I used several years ago ... it needed to be updated anyway ... I could have changed the code a bit to add additional error checking, but I decided against it ...

My code was modeled after a VBA utility designed to read the bitmap and write it to a usercontrol pixel by pixel. I don't remember the original source (it has been too many years) so if anyone knows who it is, let me know and I'll be happy to credit them with the idea.

Anyway, I have updated it to read the thumbnail information and put it into a bitmap object that can be assigned to any control where an image is required. i.e. a picturebox, frame, form etc. I have also added the ability to toggle the background color from the default white in the thumbnail to black (and vice versa) and the ability to save the file if desired.

The usage is very simple ...
This will load the bitmap from the file in FileName and will set the backcolor to black.
Code: [Select]
Dim imgMyImage As Image
imgMyImage = acThumbnailReader.GetThumbnail(FileName, False)
Me.PictureBox1.Image = imgMyImage

Additional options:
Code: [Select]
'returns bitmap image exactly as it is in the file
imgMyImage = acThumbnailReader.GetThumbnail(FileName)

'returns bitmap image exactly as it is in the file
imgMyImage = acThumbnailReader.GetThumbnail(FileName, True)

'returns bitmap image with inverted backcolor
imgMyImage = acThumbnailReader.GetThumbnail(FileName, False)

'returns bitmap image exactly as it is in the file and saves it as a temporary file in %temp%
imgMyImage = acThumbnailReader.GetThumbnail(FileName, True, True)

'returns bitmap image with inverted backcolor and saves it as a temporary file in %temp%
imgMyImage = acThumbnailReader.GetThumbnail(FileName, False, True)

'returns bitmap image exactly as it is in the file and saves it to a specified file
imgMyImage = acThumbnailReader.GetThumbnail(FileName, True, True, SaveNameWithPath)

'returns bitmap image with inverted backcolor and saves it to a specified file
imgMyImage = acThumbnailReader.GetThumbnail(FileName, False, True, SaveNameWithPath)

Simply add the attached class to your VB.Net project

I would convert it to C#, but it is late ..maybe tomorrow ;-)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: how to get preview dwg without autocad ?
« Reply #7 on: November 20, 2009, 11:15:21 AM »
ok .. I've rewritten it in C# for those who prefer ...

The same things apply as in the VB.Net version (i.e. GetThumbnail has 4 overloads) ... and the bitmap can be saved to a file and/or set to the image property of any control.

As always, the error checking is limited so use it keeping that in mind.

Enjoy!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

bikelink

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #8 on: December 01, 2009, 05:20:59 AM »
wow! I saw just now... I'm going to try it! :roll:

ognyandim

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #9 on: November 15, 2010, 05:30:40 AM »
It works fine on AutoCAD 2008 64 bit! :) Very nice work!

kaefer

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #10 on: November 15, 2010, 12:47:13 PM »
It works fine on AutoCAD 2008 64 bit! :) Very nice work!

You mean, it works fine on DWG-files created by AutoCAD 64 bit?  Well, we would certainly hope for that kind of compatibility...

A word of caution though: Those structures aren't packed exactly as they would be inside the drawing, since in Keith's code each field is aligned with native word boundaries. I tried a quick and dirty port to F# (can't help myself), and attempted to read the structures directly by pinning the bytes[] returned by BinaryReader.ReadBytes. That may not be the most efficient approach and could introduce issues with endianness. And BTW, the image format looks like it's actually PNG. Here's the code:

Code: [Select]
#nowarn "9"

//Copyright 2009 K.E. Blackie
//This work is based in part from a VBA macro
//initially developed by members of www.vbdesign.net
//it may be used freely and may be modified to suit
//your needs as long as credit is given where it is due

open System.IO
open System.Runtime.InteropServices

[<Struct; StructLayout(LayoutKind.Sequential, Pack = 1)>]
type BITMAPINFOHEADER =
    val biSize: int32
    val biWidth: int32
    val biHeight: int32
    val biPlanes: int16
    val biBitCount: int16
    val biCompression: int32
    val biSizeImage: int32
    val biXPelsperMeter: int32
    val biYPelsPerMeter: int32
    val biClrUsed: int32
    val biClrImportant: int32

[<Struct; StructLayout(LayoutKind.Sequential, Pack = 1)>]
type IMGREC =
    val bytType: byte
    val lngStart: int32
    val lngLen: int32

[<Struct; StructLayout(LayoutKind.Sequential, Pack = 1)>]
type RGBQUAD =
    val rgbBlue: byte
    val rgbGreen: byte
    val rgbRed: byte
    val rgbReserved: byte

let byteToType<'T>(reader: BinaryReader) =
    let bytes = reader.ReadBytes(Marshal.SizeOf(typeof<'T>))

    let handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    let theStructure = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof<'T>) :?> 'T
    handle.Free()

    theStructure

let getThumbnail strFile boolRetainBackColor boolSaveToFile strSaveName =
   
    let mutable bmp =
        new System.Drawing.Bitmap(
            1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed )
    if File.Exists strFile then
        let fs = File.OpenRead strFile
        try
            use br = new BinaryReader(fs)
            fs.Seek(13L, SeekOrigin.Begin) |> ignore
            let lngImgLoc = br.ReadInt32()
            fs.Seek(int64 lngImgLoc + 17L, SeekOrigin.Begin) |> ignore
            let lngCurLoc = lngImgLoc + 17
            fs.Seek(int64 lngCurLoc + 3L, SeekOrigin.Begin) |> ignore
            let bytCnt = br.ReadByte()
            if bytCnt > 1uy then
                let intCnt = ref 0
                while !intCnt < int bytCnt do
                    incr intCnt
                    let udtRec = byteToType<IMGREC> br
                    if udtRec.bytType = 2uy then
                        fs.Seek(int64 udtRec.lngStart, SeekOrigin.Begin) |> ignore
                        let udtHeader = byteToType<BITMAPINFOHEADER> br
                        if udtHeader.biBitCount = 8s then
                            let udtColors =
                                [|  for count = 0 to 255 do
                                        yield byteToType<RGBQUAD> br |]
                            fs.Seek(int64 udtRec.lngStart, SeekOrigin.Begin) |> ignore
                            let bytBMPBuff = br.ReadBytes udtRec.lngLen

                            bmp <- new System.Drawing.Bitmap(udtHeader.biWidth, udtHeader.biHeight)
                            let lngCnt = ref (udtRec.lngLen - 1)
                            for y = 0 to udtHeader.biHeight - 1 do
                                for x = 1 to udtHeader.biWidth do
                                    let udtColor = udtColors.[int bytBMPBuff.[!lngCnt]]
                                    let lngColor =
                                        System.Drawing.Color.FromArgb(
                                            int udtColor.rgbRed,
                                            int udtColor.rgbGreen,
                                            int udtColor.rgbBlue )
                                        |> System.Drawing.ColorTranslator.ToOle
                                    let lngColor =
                                        if boolRetainBackColor then lngColor
                                        elif lngColor = System.Drawing.ColorTranslator.ToOle System.Drawing.Color.Black then
                                            System.Drawing.ColorTranslator.ToOle System.Drawing.Color.White
                                        elif lngColor = System.Drawing.ColorTranslator.ToOle System.Drawing.Color.White then
                                            System.Drawing.ColorTranslator.ToOle System.Drawing.Color.Black
                                        else lngColor

                                    bmp.SetPixel(udtHeader.biWidth - x,  y, System.Drawing.ColorTranslator.FromOle lngColor)
                                    decr lngCnt
                            intCnt := int bytCnt
        finally
            fs.Close()
   
    if boolSaveToFile then
        if System.String.IsNullOrEmpty strSaveName then
            let fName =
                System.String.Concat(
                    Path.GetTempPath(),
                    Path.GetFileNameWithoutExtension(Path.GetRandomFileName()),
                    ".bmp" )
            bmp.Save fName
        else
            bmp.Save strSaveName
    bmp

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: how to get preview dwg without autocad ?
« Reply #11 on: November 15, 2010, 01:09:38 PM »
meh ... the code I provided is merely for extracting the bitmap preview and letting the user see it, nothing else, others can use it .. or not .. it really makes no difference.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

kaefer

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #12 on: November 16, 2010, 03:10:43 AM »
meh ... the code I provided is merely for extracting the bitmap preview and letting the user see it, nothing else, others can use it .. or not .. it really makes no difference.

Hi Keith,

don't get me wrong. Your code is great and a helpful example of extracting the preview image. And it would really make a difference (for worse), if you hadn't posted it. Might've been my own inexperience that I was thrown off by the field packing issue, since it was somewhat contrary to my expectations.

Many thanks for the information and inspiration!

Sincerely, Thorsten

BillZndl

  • Guest
Re: how to get preview dwg without autocad ?
« Reply #13 on: June 14, 2011, 03:27:42 PM »
Thanks Keith!
So much simpler then what I used before the upgrade. :)


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: how to get preview dwg without autocad ?
« Reply #14 on: June 15, 2011, 11:36:52 AM »
You are welcome
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie