.NET

.NET

Reply
Contributor
13 Posts
0 Kudos
Registered: ‎05-03-2013
Post 1 of 6

How to create ribbon tabs that persist through workspace changes?

416 Views, 5 Replies
09-03-2013 08:33 AM

I've followed a few examples and am able to create a new tab on the Ribbon, with panels and buttons, that appears when my DLL is loaded. However, it doesn't persist through workspace changes. I've read that this is due to my use of AdWindows.dll and my current code, as opposed to AcCui.dll, which would modify a CUIx file (that I don't know much about) to make persistant changes.

 

Does anyone know of examples of this? I've searched around and haven't seen them.

 

Here is a sample of my current code.

 

public class Commands : Autodesk.AutoCAD.Runtime.IExtensionApplication
{
	public void Initialize()
	{
		Application.Idle += callback_Idle;
	}

	private void callback_Idle(Object sender, EventArgs e)
	{
		CreateRibbon();
		Application.Idle -= callback_Idle;
	}
	
	private void CreateRibbon()
	{
		RibbonControl mainAcadRibbon = ComponentManager.Ribbon;
		if (mainAcadRibbon != null)
		{
			RibbonTab rTab = mainAcadRibbon.FindTab("Tab Name");
			if (rTab != null)
			{
				mainAcadRibbon.Tabs.Remove(rTab);
			}
			rTab = new RibbonTab();
			rTab.Title = "Title";
			rTab.Id = "ID";
			mainAcadRibbon.Tabs.Add(rTab);
			addPanelsToRibbon(rTab);
		}
	}

	static void addPanelsToRibbon(RibbonTab rtab)
	{
		rtab.Panels.Add(panelExample());
	}

	static RibbonPanel panelExample()
	{
		RibbonButton rb1;
		RibbonButton rb2;
		RibbonPanelSource rps = new RibbonPanelSource();
		rps.Title = "Title";
		RibbonPanel rp = new RibbonPanel();
		rp.Source = rps;

		rb1 = new RibbonButton();
		rb1.Name = "Name1";
		rb1.ShowText = true;
		rb1.Text = "Text1";
		rb1.CommandHandler = new RibbonCommandHandler();
		rps.Items.Add(rb1);

		rb2 = new RibbonButton();
		rb2.Name = "Name2";
		rb2.ShowText = true;
		rb2.Text = "Text2";
		rb2.CommandHandler = new RibbonCommandHandler();
		rps.Items.Add(rb2);

		return rp;
	}

	public class RibbonCommandHandler : System.Windows.Input.ICommand
	{
		public bool CanExecute(object parameter)
		{
			return true;
		}
		// CanExecuteChanged must be implemented, even if not used.
		public event EventHandler CanExecuteChanged;
		public void Execute(object parameter)
		{
			Document doc = Application.DocumentManager.MdiActiveDocument;
			if (parameter is RibbonButton)
			{
				try
				{
					switch (button.Name.ToString())
					{
					case "Name1":
						break;
					case "Name2":
						break;
					default:
						break;
					}
				}
				catch (Autodesk.AutoCAD.Runtime.Exception ex)
				{
					Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
				}
				catch (System.NullReferenceException ex)
				{
					Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
				}
			}
		}
	}
}

 

Valued Contributor
91 Posts
4 Kudos
Registered: ‎12-05-2011
Post 2 of 6

Re: How to create ribbon tabs that persist through workspace changes?

09-04-2013 06:18 AM in reply to: ohioDeveloper

Can you please say wheere you are getting those ribbon controls from?  I included the accui.dll in the project and got rid of some of the errors but am having a problem finding where these are coming from...

Autodesk.AutoCAD.???...ComponentManager.Ribbon

Learning as I go
Contributor
13 Posts
0 Kudos
Registered: ‎05-03-2013
Post 3 of 6

Re: How to create ribbon tabs that persist through workspace changes?

09-04-2013 06:41 AM in reply to: jaboone
I included AdWindows.dll as a reference, and then used the Autodesk.Windows namespace.
Valued Contributor
91 Posts
4 Kudos
Registered: ‎12-05-2011
Post 4 of 6

Re: How to create ribbon tabs that persist through workspace changes?

09-04-2013 08:04 AM in reply to: ohioDeveloper

Thanks for that help.  I got stuck on one more item if you wouldn't helping with that too.  Am compiling to dll.  Impossible to debug.

Learning as I go
Contributor
13 Posts
0 Kudos
Registered: ‎05-03-2013
Post 5 of 6

Re: How to create ribbon tabs that persist through workspace changes?

09-04-2013 12:59 PM in reply to: jaboone
I forgot a line in my code, before the try { } on the switch case.

RibbonButton button = parameter as RibbonButton;
Valued Contributor
91 Posts
4 Kudos
Registered: ‎12-05-2011
Post 6 of 6

Re: How to create ribbon tabs that persist through workspace changes?

09-04-2013 02:44 PM in reply to: ohioDeveloper

OK that did the trick.  Thanks for the code.  I converted it to VB but have not tested yet.  Here it is.

 

include acwindows.dll & accui.dll


Imports Autodesk.AutoCAD.Customization
Imports Autodesk.Windows




      Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
         AddHandler Application.Idle, AddressOf callback_Idle
      End Sub

      Private Sub callback_Idle(ByVal sender As [Object], ByVal e As EventArgs)
         CreateRibbon()
         RemoveHandler Application.Idle, AddressOf callback_Idle
      End Sub

      Private Sub CreateRibbon()
         Dim mainAcadRibbon As Autodesk.Windows.RibbonControl = Autodesk.Windows.ComponentManager.Ribbon '   ComponentManager.Ribbon
         If mainAcadRibbon IsNot Nothing Then
            Dim rTab As RibbonTab = mainAcadRibbon.FindTab("Tab Name")
            If rTab IsNot Nothing Then
               mainAcadRibbon.Tabs.Remove(rTab)
            End If
            rTab = New RibbonTab()
            rTab.Title = "Title"
            rTab.Id = "ID"
            mainAcadRibbon.Tabs.Add(rTab)
            addPanelsToRibbon(rTab)
         End If
      End Sub

      Private Shared Sub addPanelsToRibbon(ByVal rtab As RibbonTab)
         rtab.Panels.Add(panelExample())
      End Sub

      Private Shared Function panelExample() As RibbonPanel
         Dim rb1 As Autodesk.Windows.RibbonButton
         Dim rb2 As Autodesk.Windows.RibbonButton
         Dim rps As New Autodesk.Windows.RibbonPanelSource()
         rps.Title = "Title"
         Dim rp As New RibbonPanel()
         rp.Source = rps

         rb1 = New Autodesk.Windows.RibbonButton()
         rb1.Name = "Name1"
         rb1.ShowText = True
         rb1.Text = "Text1"
         rb1.CommandHandler = New RibbonCommandHandler()
         rps.Items.Add(rb1)

         rb2 = New Autodesk.Windows.RibbonButton()
         rb2.Name = "Name2"
         rb2.ShowText = True
         rb2.Text = "Text2"
         rb2.CommandHandler = New RibbonCommandHandler()
         rps.Items.Add(rb2)

         Return rp
      End Function

      Public Class RibbonCommandHandler
         'Implements System.Windows.Input.ICommand

         Public Function CanExecute(ByVal parameter As Object) As Boolean
            Return True
         End Function

         ' CanExecuteChanged must be implemented, even if not used.
         Public Event CanExecuteChanged As EventHandler

         Public Sub Execute(ByVal parameter As Object)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            If TypeOf parameter Is Autodesk.Windows.RibbonButton Then
               Dim button As Autodesk.Windows.RibbonButton = TryCast(parameter, Autodesk.Windows.RibbonButton)
               Try
                  Select Case button.Name.ToString()
                     Case "Name1"
                        Exit Select
                     Case "Name2"
                        Exit Select
                     Case Else
                        Exit Select
                  End Select
               Catch ex As Autodesk.AutoCAD.Runtime.Exception
                  Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString())
               Catch ex As System.NullReferenceException
                  Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString())
               End Try
            End If
         End Sub
      End Class

 

Learning as I go
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!