The following example code demonstrates how to add a background bitmap to a viewport using the SDK function.
CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
// Allow the user to select a bitmap file
ON_wString bitmap_filename;
CRhinoGetFileDialog gf;
gf.SetScriptMode( context.IsInteractive() ? FALSE : TRUE );
BOOL rc = gf.DisplayFileDialog(
CRhinoGetFileDialog::open_bitmap_dialog,
bitmap_filename,
CWnd::FromHandle( RhinoApp().MainWnd() )
);
if( !rc )
return nothing;
// Verify the file that was selected
bitmap_filename = gf.FileName();
bitmap_filename.TrimLeftAndRight();
if( bitmap_filename.IsEmpty() )
return nothing;
// Verify that the bitmap file is valid
CRhinoDib dib;
if( !dib.ReadFromFile(bitmap_filename) )
{
RhinoApp().Print( L"The specified file cannot be identifed as a supported type.\n" );
return nothing;
}
// Allow the user to pick the bitmap origin
CRhinoGetPoint gp;
gp.SetCommandPrompt( L"Bitmap origin" );
gp.ConstrainToConstructionPlane();
gp.GetPoint();
if( gp.CommandResult() != success )
return gp.CommandResult();
// Get the view that the point was picked in.
// This will be the view that the bitmap appears in.
CRhinoView* view = gp.View();
if( !view )
{
view = RhinoApp().ActiveView();
if( !view )
return failure;
}
// Allow the user to specify the bitmap with in model units
CRhinoGetNumber gn;
gn.SetCommandPrompt( L"Bitmap width" );
gn.SetLowerLimit( 1.0 );
gn.GetNumber();
if( gn.CommandResult() != success )
return gn.CommandResult();
// Cook up some scale factors
double w = gn.Number();
double dib_width = (double)dib.Width();
double dib_height = (double)dib.Height();
double h = w * ( dib_height / dib_width );
// Calculate the 3-D points that bound the bitmap
ON_3dPoint rect[4];
rect[0] = gp.Point();
rect[1] = ON_3dPoint(rect[0].x+w, rect[0].y, rect[0].z);
rect[2] = ON_3dPoint(rect[0].x+w, rect[0].y+h, rect[0].z);
rect[3] = ON_3dPoint(rect[0].x, rect[0].y+h, rect[0].z);
// Get Rhino's bitmap table
CRhinoBitmapTable& bitmap_table = context.m_doc.m_bitmap_table;
// Search the bitmap table to see if the bitmap has already
// been used. If so, delete the bitmap so Rhino will not just
// reuse the one it already has (if we want to re-read the bitmap
// file
const CRhinoBitmap* rhino_bitmap = bitmap_table.Bitmap( bitmap_filename );
if( rhino_bitmap )
{
view->ActiveViewport().SetTraceImage( ON_3dmViewTraceImage() );
bitmap_table.DeleteBitmap( bitmap_filename );
}
rhino_bitmap = 0;
// Create the trace image (background bitmap) object
ON_3dmViewTraceImage trace_image;
trace_image.m_bitmap_filename = bitmap_filename;
trace_image.m_bGrayScale = false;
trace_image.m_plane = view->ActiveViewport().ConstructionPlane().m_plane;
trace_image.m_plane.SetOrigin( rect[0] );
trace_image.m_width = fabs( (rect[0] - rect[1]).Length() );
trace_image.m_height = fabs( (rect[0] - rect[3]).Length() );
trace_image.m_bHidden = false;
// Add the trace image to the viewport
view->ActiveViewport().SetTraceImage( trace_image );
view->Redraw();
return success;
}
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
// Allow the user to select a bitmap file
MRhinoGetFileDialog gf = new MRhinoGetFileDialog();
gf.SetScriptMode( context.IsInteractive() );
bool rc = gf.DisplayFileDialog(IRhinoGetFileDialog.file_dialog_type.open_bitmap_dialog,
"", RhUtil.RhinoApp().MainWnd());
if( !rc )
return IRhinoCommand.result.nothing;
// Verify the file that was selected
string bitmap_filename = gf.FileName().Trim();
if( string.IsNullOrEmpty(bitmap_filename))
return IRhinoCommand.result.nothing;
// Verify that the bitmap file is valid
MRhinoDib dib = new MRhinoDib();
if( !dib.ReadFromFile(bitmap_filename) )
{
RhUtil.RhinoApp().Print("The specified file cannot be identifed as a supported type.\n");
return IRhinoCommand.result.nothing;
}
// Allow the user to pick the bitmap origin
MRhinoGetPoint gp = new MRhinoGetPoint();
gp.SetCommandPrompt( "Bitmap origin" );
gp.ConstrainToConstructionPlane();
gp.GetPoint();
if( gp.CommandResult() != IRhinoCommand.result.success )
return gp.CommandResult();
// Get the view that the point was picked in.
// This will be the view that the bitmap appears in.
MRhinoView view = gp.View();
if( view == null )
{
view = RhUtil.RhinoApp().ActiveView();
if( view == null)
return IRhinoCommand.result.failure;
}
// Allow the user to specify the bitmap with in model units
MRhinoGetNumber gn = new MRhinoGetNumber();
gn.SetCommandPrompt( "Bitmap width" );
gn.SetLowerLimit( 1.0 );
gn.GetNumber();
if( gn.CommandResult() != IRhinoCommand.result.success )
return gn.CommandResult();
// Cook up some scale factors
double w = gn.Number();
double dib_width = (double)dib.Width();
double dib_height = (double)dib.Height();
double h = w * ( dib_height / dib_width );
// Calculate the 3-D points that bound the bitmap
On3dPoint[] rect = new On3dPoint[4];
rect[0] = gp.Point();
rect[1] = new On3dPoint(rect[0].x+w, rect[0].y, rect[0].z);
rect[2] = new On3dPoint(rect[0].x + w, rect[0].y + h, rect[0].z);
rect[3] = new On3dPoint(rect[0].x, rect[0].y + h, rect[0].z);
// Get Rhino's bitmap table
MRhinoBitmapTable bitmap_table = context.m_doc.m_bitmap_table;
// Search the bitmap table to see if the bitmap has already
// been used. If so, delete the bitmap so Rhino will not just
// reuse the one it already has (if we want to re-read the bitmap
// file
IRhinoBitmap rhino_bitmap = bitmap_table.Bitmap(bitmap_filename);
if (rhino_bitmap != null)
{
view.ActiveViewport().SetTraceImage(new On3dmViewTraceImage());
bitmap_table.DeleteBitmap(bitmap_filename);
}
rhino_bitmap = null;
// Create the trace image (background bitmap) object
On3dmViewTraceImage trace_image = new On3dmViewTraceImage();
trace_image.m_bitmap_filename = bitmap_filename;
trace_image.m_bGrayScale = false;
trace_image.m_plane = new OnPlane(view.ActiveViewport().ConstructionPlane().m_plane);
trace_image.m_plane.SetOrigin( rect[0] );
trace_image.m_width = System.Math.Abs( (rect[0] - rect[1]).Length() );
trace_image.m_height = System.Math.Abs((rect[0] - rect[3]).Length());
trace_image.m_bHidden = false;
// Add the trace image to the viewport
view.ActiveViewport().SetTraceImage( trace_image );
view.Redraw();
return IRhinoCommand.result.success;
}
Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) _
As RMA.Rhino.IRhinoCommand.result
' Allow the user to select a bitmap file
Dim gf As New MRhinoGetFileDialog()
gf.SetScriptMode(context.IsInteractive())
Dim rc As Boolean = gf.DisplayFileDialog(IRhinoGetFileDialog.file_dialog_type.open_bitmap_dialog, _
"", RhUtil.RhinoApp().MainWnd())
If (rc = False) Then
Return IRhinoCommand.result.nothing
End If
' Verify the file that was selected
Dim bitmap_filename As String = gf.FileName().Trim()
If (String.IsNullOrEmpty(bitmap_filename)) Then
Return IRhinoCommand.result.nothing
End If
' Verify that the bitmap file is valid
Dim dib As New MRhinoDib()
If (Not dib.ReadFromFile(bitmap_filename)) Then
RhUtil.RhinoApp().Print("The specified file cannot be identifed as a supported type." + vbCrLf)
Return IRhinoCommand.result.nothing
End If
' Allow the user to pick the bitmap origin
Dim gp As New MRhinoGetPoint()
gp.SetCommandPrompt("Bitmap origin")
gp.ConstrainToConstructionPlane()
gp.GetPoint()
If (gp.CommandResult() <> IRhinoCommand.result.success) Then
Return gp.CommandResult()
End If
' Get the view that the point was picked in.
' This will be the view that the bitmap appears in.
Dim view As MRhinoView = gp.View()
If (view Is Nothing) Then
view = RhUtil.RhinoApp().ActiveView()
If (view Is Nothing) Then
Return IRhinoCommand.result.failure
End If
End If
' Allow the user to specify the bitmap with in model units
Dim gn As New MRhinoGetNumber()
gn.SetCommandPrompt("Bitmap width")
gn.SetLowerLimit(1.0)
gn.GetNumber()
If (gn.CommandResult() <> IRhinoCommand.result.success) Then
Return gn.CommandResult()
End If
' Cook up some scale factors
Dim w As Double = gn.Number()
Dim dib_width As Double = dib.Width()
Dim dib_height As Double = dib.Height()
Dim h As Double = w * (dib_height / dib_width)
' Calculate the 3-D points that bound the bitmap
Dim rect(3) As On3dPoint
rect(0) = gp.Point()
rect(1) = New On3dPoint(rect(0).x + w, rect(0).y, rect(0).z)
rect(2) = New On3dPoint(rect(0).x + w, rect(0).y + h, rect(0).z)
rect(3) = New On3dPoint(rect(0).x, rect(0).y + h, rect(0).z)
' Get Rhino's bitmap table
Dim bitmap_table As MRhinoBitmapTable = context.m_doc.m_bitmap_table
' Search the bitmap table to see if the bitmap has already
' been used. If so, delete the bitmap so Rhino will not just
' reuse the one it already has (if we want to re-read the bitmap
' file
Dim rhino_bitmap As IRhinoBitmap = bitmap_table.Bitmap(bitmap_filename)
If (rhino_bitmap IsNot Nothing) Then
view.ActiveViewport().SetTraceImage(New On3dmViewTraceImage())
bitmap_table.DeleteBitmap(bitmap_filename)
End If
rhino_bitmap = Nothing
' Create the trace image (background bitmap) object
Dim trace_image As New On3dmViewTraceImage()
trace_image.m_bitmap_filename = bitmap_filename
trace_image.m_bGrayScale = False
trace_image.m_plane = New OnPlane(view.ActiveViewport().ConstructionPlane().m_plane)
trace_image.m_plane.SetOrigin(rect(0))
trace_image.m_width = System.Math.Abs((rect(0) - rect(1)).Length())
trace_image.m_height = System.Math.Abs((rect(0) - rect(3)).Length())
trace_image.m_bHidden = False
' Add the trace image to the viewport
view.ActiveViewport().SetTraceImage(trace_image)
view.Redraw()
Return IRhinoCommand.result.success
End Function