Author Topic: check if tab exists  (Read 7022 times)

0 Members and 2 Guests are viewing this topic.

sybold

  • Newt
  • Posts: 62
check if tab exists
« on: July 12, 2012, 02:52:21 AM »
i'm trying to check if a custom tab is in the ribbon

if it is in the ribbon  -> exit
if it is not in the ribbon -> add tab

i'm missing something or doing it completely wrong
Code: [Select]
        <CommandMethod("DOCRIB")> _
        Public Sub DOCRIBCMD()
            Dim tabName As String = "DocumentTab"
            Dim ribCntrl As Autodesk.Windows.RibbonControl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl
            For Each tab As Autodesk.Windows.RibbonTab In ribCntrl.Tabs
                If tab.Title.Contains(tabName) = False Then
                    Try
                        Dim ribbon As New RIBBON()
                        ribbon.Register()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        _ed.WriteMessage(vbCrLf & ex.Message)
                    End Try
                Else
                    _ed.WriteMessage("already there")
                    Exit Sub
                End If
            Next
        End Sub

BlackBox

  • King Gator
  • Posts: 3770
Re: check if tab exists
« Reply #1 on: July 12, 2012, 12:22:27 PM »
There may be a better way of doing this (particularly when testing for an existing tab), but this is the best I can do with the little I know of .NET development.

This code iterates the RibbonTabCollection to check... checks for the defined RibbonTab, and if it exists, sets it active. If the RibbonTab is not found, then the RibbonTab is created and set active.

Pseudo code:

** Edit - Revised code to use the FindTab Method (thanks, Jeff!)

Code - C#: [Select]
  1.  
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.Windows;
  7.  
  8. using System;
  9.  
  10. // This line is not mandatory, but improves loading performances
  11. [assembly: CommandClass(typeof(FOO.MyCommands))]
  12.  
  13. namespace FOO
  14. {
  15.     public class MyCommands
  16.     {
  17.  
  18.         private Editor ed =
  19.             Application.DocumentManager.MdiActiveDocument.Editor;
  20.  
  21.         // Jeff_M is awesome:
  22.         // http://www.theswamp.org/index.php?topic=32869.msg383525#msg383525
  23.  
  24.         [CommandMethod("DOCRIB")]
  25.         public void DOCRIBCMD()
  26.         {
  27.             // Defin ribbon properties
  28.             String myTabId = "DOCTAB";
  29.             String myTabTitle = "Document";
  30.             Boolean myTabIsContextual = true;
  31.  
  32.             try
  33.             {
  34.                 RibbonControl oRibbon = ComponentManager.Ribbon;
  35.                 if (oRibbon != null)
  36.                 {
  37.                     RibbonTab oTab = oRibbon.FindTab(myTabId);
  38.                     if (oTab != null)
  39.                     {
  40.                         oTab.IsActive = true;
  41.                     }
  42.                     else
  43.                     {
  44.                         oTab = new RibbonTab();
  45.                         oTab.Title = myTabTitle;
  46.                         oTab.Id = myTabId;
  47.                         oTab.IsContextualTab = myTabIsContextual;
  48.                         oTab.IsVisible = true;
  49.  
  50.                         oRibbon.Tabs.Add(oTab);
  51.  
  52.                         oTab.IsActive = true;
  53.  
  54.                         // <-- Add your ribbon panel(s) here
  55.  
  56.                     }
  57.                 }
  58.             }
  59.  
  60.             catch (Autodesk.AutoCAD.Runtime.Exception ex)
  61.             {
  62.                 ed.WriteMessage("\n** Error: " + ex.Message + " ** ");
  63.             }
  64.         }
  65.     }
  66. }
  67.  

Also, here are some references that you may find useful:

http://forums.autodesk.com/t5/NET/Create-custom-ribbon-tab-and-buttons-for-Autocad-mechanical-2011/td-p/2834343

http://adndevblog.typepad.com/autocad/2012/04/displaying-a-contextual-tab-upon-entity-selection-using-ribbon-runtime-api.html

http://spiderinnet1.typepad.com/blog/2012/06/autocad-cui-ribbon-net-add-ribbon-panel-to-existing-ribbon-tab.html

HTH
« Last Edit: July 13, 2012, 12:56:30 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

sybold

  • Newt
  • Posts: 62
Re: check if tab exists
« Reply #2 on: July 13, 2012, 07:58:16 AM »
great! thanks for the help, I now realize what i did wrong.

it now works, like i needed it.
Code: [Select]
<CommandMethod("DOCRIB")> _
        Public Sub DOCRIBCMD()
            Dim ribCntrl As Autodesk.Windows.RibbonControl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl
            Dim flag As Boolean = False
                  For Each tab As RibbonTab In ribbonCtrl.Tabs
                If tab.Id.ToUpper() = "DOCTAB" Then
                    tab.IsActive = True
                    flag = True
                    _ed.WriteMessage("Tab already added")
                End If
            Next
            If flag = False Then
                Dim ribbon As New RIBBON()
                ribbon.Register()
                _ed.WriteMessage("Tab added")
            End If
        End Sub

BlackBox

  • King Gator
  • Posts: 3770
Re: check if tab exists
« Reply #3 on: July 13, 2012, 08:08:28 AM »
great! thanks for the help, I now realize what i did wrong.

Happy to help.  :-)

FWIW - I am no expert at Exception handling myself, and I have not researched which Exceptions can be result from iterating the RibbonTabCollection, or even modifying a RibbonTab for that matter, but at minimum, you really should get in the habit of incorporating a Try, Catch [, Finally?] block, methinks.  ;-)
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: check if tab exists
« Reply #4 on: July 13, 2012, 08:34:10 AM »
I've added C# code here, for those that may be interested.
"How we think determines what we do, and what we do determines what we get."

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: check if tab exists
« Reply #5 on: July 13, 2012, 10:15:44 AM »
You might also find the sample code I posted HERE of interest. I used a slightly different approach. (Code is c#)

BlackBox

  • King Gator
  • Posts: 3770
Re: check if tab exists
« Reply #6 on: July 13, 2012, 10:21:57 AM »
You might also find the sample code I posted HERE of interest. I used a slightly different approach. (Code is c#)

Oh... Of course there's a FindTab Method!?!  :ugly:  :lmao:

I kept getting hung up on the Item Method in Object Browser for some reason (which will not work as one might expect, coming from Visual LISP). 

As always, thanks for clarification, Jeff! :beer:
"How we think determines what we do, and what we do determines what we get."

sybold

  • Newt
  • Posts: 62
Re: check if tab exists
« Reply #7 on: July 13, 2012, 11:46:33 AM »
 :? ok, just waisted a few years of my life…
did not see that one comming! a findtab, why so simple

BlackBox

  • King Gator
  • Posts: 3770
Re: check if tab exists
« Reply #8 on: July 13, 2012, 12:59:38 PM »
Building on Jeff's code...Code revised here, per FindTab Method... Hopefully I have not overlooked any potential Exceptions, as nothing was listed in Object Browser (perhaps I was looking in the wrong place?).
"How we think determines what we do, and what we do determines what we get."