TheSwamp

Code Red => .NET => Topic started by: latour_g on November 02, 2020, 04:45:01 PM

Title: Block preview icon
Post by: latour_g 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
Title: Re: Block preview icon
Post by: Atook 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. }

Title: Re: Block preview icon
Post by: latour_g 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
Title: Re: Block preview icon
Post by: Atook 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. (https://www.keanw.com/2013/11/generating-larger-preview-images-for-all-blocks-in-an-autocad-drawing-using-net.html)