.NET

.NET

Reply
Contributor
ohioDeveloper
Posts: 13
Registered: ‎05-03-2013
Message 1 of 6 (384 Views)

How to create ribbon tabs that persist through workspace changes?

384 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
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 2 of 6 (349 Views)

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
ohioDeveloper
Posts: 13
Registered: ‎05-03-2013
Message 3 of 6 (340 Views)

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
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 4 of 6 (324 Views)

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
ohioDeveloper
Posts: 13
Registered: ‎05-03-2013
Message 5 of 6 (307 Views)

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
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 6 of 6 (299 Views)

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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.