Author Topic: Block preview icon  (Read 207 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 155
Block preview icon
« on: November 02, 2020, 04:45:01 PM »
Hi,

I have a command that extract each block of a drawing into a folder.  I use previewicon to see a preview in Windows Explorer.
It's working fine except for certain block.  It says previewicon null but in Autocad I see the preview for those blocks.
Does it mean that the preview that I am seeing in Autocad is stock elsewhere ?
Thank you

Atook

  • Swamp Rat
  • Posts: 969
Re: Block preview icon
« Reply #1 on: November 02, 2020, 10:00:56 PM »
Hey latour_g, perhaps this will help:
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Windows.Data;
  2.  
  3. public static Bitmap BlockPreview(string blockName)
  4. {
  5.   Bitmap result = new Bitmap(1,1);
  6.   if (Active.Document == null) return result;
  7.   using (Transaction acTr = Active.TransactionManager.StartTransaction())
  8.   {
  9.     BlockTable bt = (BlockTable)acTr.GetObject(Active.Database.BlockTableId, OpenMode.ForRead);
  10.     if (!bt.Has(blockName)) return result;
  11.     BlockTableRecord btr = acTr.GetObject(bt[blockName], OpenMode.ForRead) as BlockTableRecord;
  12.     if (btr == null) return result;
  13.     if (btr.HasPreviewIcon)
  14.     {
  15.       result = btr.PreviewIcon;
  16.     }
  17.     else
  18.     {
  19.       // generate the preview icon
  20.       result = new Bitmap(ImageSourceToGDI(CMLContentSearchPreviews.GetBlockTRThumbnail(btr) as BitmapSource));
  21.     }
  22.     acTr.Commit();
  23.   }
  24.   return result;
  25. }


latour_g

  • Newt
  • Posts: 155
Re: Block preview icon
« Reply #2 on: November 09, 2020, 11:21:20 AM »
Thanks Atook, it look great but I wasn't able to test it so far.  I'm struggling finding ImageSourceToGDI.

Could you tell me in which library to find it ?

I was looking at AcMgd version 21.0 in Autodesk.Windows.Data but it's not there. 

Thank you

Atook

  • Swamp Rat
  • Posts: 969
Re: Block preview icon
« Reply #3 on: November 09, 2020, 04:03:20 PM »
Hey latour, sorry I left that out, it's just a static function in the same class:
Code - C#: [Select]
  1. private static System.Drawing.Image ImageSourceToGDI(System.Windows.Media.Imaging.BitmapSource src)
  2. {
  3.   var ms = new MemoryStream();
  4.   var encoder =
  5.     new System.Windows.Media.Imaging.BmpBitmapEncoder();
  6.   encoder.Frames.Add(
  7.     System.Windows.Media.Imaging.BitmapFrame.Create(src)
  8.   );
  9.   encoder.Save(ms);
  10.   ms.Flush();
  11.   return System.Drawing.Image.FromStream(ms);
  12. }

But the real work horse here is:
Code - C#: [Select]
  1. CMLContentSearchPreviews.GetBlockTRThumbnail(btr);

It's included in Autodesk.AutoCAD.Windows.Data

Kean has more on it here.