Write bitmaps to archive

Hello fellows,

i want to write bitmaps into 3dm files using write3dmbitmap( ) function. Is there any example ?

My problem is that i can’t cast CrhinoBitmap or create a functional ON_Bitmap to use the above function. ON_Bitmap has to be overridden but i can’t follow Rhino SDK help to myself.

C++ SDK
Rhino 5

Thanks everyone in advance.

Hi Mike,

If you need an ON_Bitmap, use CRhinoBitmap::Bitmap().

Does this help?

– Dale

Hi Dale,

Searching in docs i found that CRhinoBitmap class can’t be instantiated because has it’s constructor in the private section and i must use bitmaptables through CRhinoDoc class. Right ?

I’m using CRhinoDoc::m_bitmaptable to store some custom bitmaps. Can i have separate CRhinoBitmap classes in order to be used anywhere ?

  1. Does CRhinoDoc stores bitmap table to 3dm file automatically or i have to force it ?

  2. If i store bitmap to 3dm (using write3dmbitmap) then how can i read it back ? read3dmbitmap needs an ON_Bitmap pointer pointer and It’s ok so far. Does this pointer has to point to an already reserved bitmap or not ? Can you provide me a simple example ? Is CRhinoBitmapTable::AddBitmap (ON_Bitmap*, bool) enough ?

  3. I’m using

    const CRhinoBitmap *r = doc.m_bitmap_table.Bitmap(L"C:\Users\Mike\Desktop\index.jpg", true);
    const ON_Bitmap *b = r->Bitmap(); //Not safe for now but it’s not NULL for tests

    if(b==NULL || r==NULL)
    RhinoApp().Print(L"Failed accuire bitmap");
    else
    RhinoApp().Print(b->m_bitmap_filename); //Goes here, means that none of the above pointers are NULL

    if(archive.Write3dmBitmap( *b ) == false)
    RhinoApp().Print(L"Failed to write bitmap"); //Finally archive doesn’t write bitmap to 3dm

but it fails to write bitmap to file.

Thanks Dale, appreciated !

Correct.

Being that you cannot create them, no. You might want to consider some other class to handle your bitmaps. A CRhinoDib might be the way to go.

This process is automatic.

Yep. I have not tested this sample, but it should work. Note, “m_dib” is a CRhinoDib…

BOOL CSampleBitmapPlugIn::WriteDocument( CRhinoDoc& doc, ON_BinaryArchive& archive, const CRhinoFileWriteOptions& options )
{
  // Description:
  //   Called when Rhino is saving a .3dm file to allow the plug-in
  //   to save document user data.

  ON_WindowsBitmapEx bitmap;
  bitmap.Create( m_dib.BitmapInfo(), (const LPBYTE)m_dib.FindDIBBits(), true );
  if( !bitmap.IsValid() )
    return FALSE;

  if( !archive.Write3dmChunkVersion(m_major_version, m_minor_version) )
    return FALSE;

  if( !bitmap.Write(archive) )
    return FALSE;

  return TRUE;
}

BOOL CSamplePlugIn::ReadDocument( CRhinoDoc& doc, ON_BinaryArchive& archive, const CRhinoFileReadOptions& options )
{
  // Description:
  //   Called whenever a Rhino document is being loaded and plug-in user data was
  //   encountered written by a plug-in with this plug-in's GUID.

  BOOL bImportMode = options.Mode(CRhinoFileReadOptions::ImportMode) || options.Mode(CRhinoFileReadOptions::ImportReferenceMode);

  int major = 0, minor = 0;
  if( !archive.Read3dmChunkVersion(&major, &minor) )
    return FALSE;

  if( major != m_major_version || minor != m_minor_version )
    return FALSE;

  ON_WindowsBitmapEx bitmap;
  if( !bitmap.Read(archive) || !bitmap.IsValid() )
    return FALSE;

  if( !bImportMode )
  {
    m_dib.DestroyDib();
    m_dib.SetDib( bitmap.m_bmi, false );
  }

  return TRUE;
}

Dale, a beer is on way.

Thanks. I used bitmaptable.