The following sample code demonstrates how to open a toolbar collection and display a toolbar in Rhino 4.0. This code assumes that the toolbar collection file is located in the same folder as the plug-in (executable) and has the same file name as the plug-in.
bool LoadPlugInToolbarCollection()
{
bool rc = false;
// Get our plug-in object.
CTestPlugIn& plugin = TestPlugIn();
// Get the plug-in filename.
ON_wString file_name;
plugin.GetPlugInFileName( file_name );
// Split the filename into components.
wchar_t drive[_MAX_DRIVE];
wchar_t dir[_MAX_DIR];
wchar_t fname[_MAX_FNAME];
_wsplitpath( file_name, drive, dir, fname, 0 );
// Build the collection filename.
ON_wString collection_name;
collection_name.Format( L"%s%s%s.tb", drive, dir, fname );
// Verify the collection file exists.
if( !CRhinoFileUtilities::FileExists(collection_name) )
return rc;
// Get Rhino's toolbar manager.
CRhinoAppUiToolBarManager& manager = ::RhinoApp().RhinoUiToolBarManager();
// See if our collection is already loaded.
const CRhinoUiToolBarCollection* collection =
manager.Collection( collection_name, false );
if( 0 == collection )
{
// Open and read our collection file.
if( manager.ReadFile(collection_name, true, true, true) )
// Again, see if our collection is already loaded.
collection = manager.Collection( collection_name, false );
}
// If we get here and we still do not have a collection object, bail.
if( 0 == collection )
return rc;
// Find our "default" toolbar in our collection.
int toolbar_index = collection->FindToolBar( L"Default" );
if( toolbar_index >= 0 )
{
// Get the "default" toolbar object.
const CRhinoUiToolBar* toolbar = collection->ToolBar( toolbar_index );
if( toolbar )
{
// If the toolbar is not visible, show it.
if( !toolbar->IsToolBarVisible() )
rc = manager.ShowToolBar( toolbar, true, false );
}
}
return rc;
}
Private Function LoadPlugInToolbarCollection() As Boolean
Dim rc As Boolean = False
' Get the plug-in filename.
Dim file_name As String = PlugIn().OwnerAssembly.Location
' Build the collection filename.
Dim dir As String = System.IO.Path.GetDirectoryName(file_name)
Dim fname As String = System.IO.Path.GetFileNameWithoutExtension(file_name)
Dim collection_name As String = System.IO.Path.Combine(dir, fname + ".tb")
' Verify the collection file exists.
If (Not System.IO.File.Exists(collection_name)) Then Return rc
' See if our collection is already loaded.
Dim collection As RMA.UI.IRhinoUiToolBarCollection = _
RMA.UI.MRhinoUiToolBarManager.Collection(collection_name, False)
If (collection Is Nothing) Then
' Open and read our collection file.
If (RMA.UI.MRhinoUiToolBarManager.ReadFile(collection_name, True, True, True)) Then
' Again, see if our collection is already loaded.
collection = RMA.UI.MRhinoUiToolBarManager.Collection(collection_name, False)
End If
End If
' If we get here and we still do not have a collection object, bail.
If (collection Is Nothing) Then Return rc
' Find our "default" toolbar in our collection.
Dim toolbar_index As Integer = collection.FindToolBar("Default")
If (toolbar_index >= 0) Then
' Get the "default" toolbar object.
Dim toolbar As RMA.UI.IRhinoUiToolBar = collection.ToolBar(toolbar_index)
If (toolbar IsNot Nothing) Then
' If the toolbar is not visible, show it.
If (Not toolbar.IsToolBarVisible()) Then
rc = RMA.UI.MRhinoUiToolBarManager.ShowToolBar(toolbar, True, False)
End If
End If
End If
Return rc
End Function
private bool LoadPlugInToolbarCollection()
{
bool rc = false;
// Get the plug-in filename.
string file_name = PlugIn().OwnerAssembly.Location;
// Build the collection filename.
string dir = System.IO.Path.GetDirectoryName(file_name);
string fname = System.IO.Path.GetFileNameWithoutExtension(file_name);
string collection_name = System.IO.Path.Combine(dir, fname + ".tb");
// Verify the collection file exists.
if( !System.IO.File.Exists(collection_name) )
return rc;
// See if our collection is already loaded.
RMA.UI.IRhinoUiToolBarCollection collection =
RMA.UI.MRhinoUiToolBarManager.Collection( collection_name, false );
if( collection == null )
{
// Open and read our collection file.
if( RMA.UI.MRhinoUiToolBarManager.ReadFile( collection_name,
true,
true,
true ) )
// Again, see if our collection is already loaded.
collection = RMA.UI.MRhinoUiToolBarManager.Collection( collection_name, false );
}
// If we get here and we still do not have a collection object, bail.
if( collection == null )
return rc;
// Find our "default" toolbar in our collection.
int toolbar_index = collection.FindToolBar( "Default" );
if( toolbar_index >= 0 )
{
// Get the "default" toolbar object.
RMA.UI.IRhinoUiToolBar toolbar = collection.ToolBar(toolbar_index);
if( toolbar != null )
{
// If the toolbar is not visible, show it.
if( !toolbar.IsToolBarVisible() )
rc = RMA.UI.MRhinoUiToolBarManager.ShowToolBar( toolbar, true, false );
}
}
return rc;
}