.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

.NET C# Ribbon tab

20 REPLIES 20
SOLVED
Reply
Message 1 of 21
Sined99
19862 Views, 20 Replies

.NET C# Ribbon tab

Hello, I am new in the autodesk customization, I have autocad 2013 and visual studio 2010

 

I am interested in make a custom ribbon tab, where I can put a button.

 

I have found some (incomplete) samples to implement a custom ribbon tab, but I dont know how to "insert" my tab into autocad 2013, and how must I load my tab? with a command?  or it can load automatically?

 

Can someone show me an example?

 

Thank you very much.

 

20 REPLIES 20
Message 2 of 21
Jeff_M
in reply to: Sined99

Here's a simple example of adding a new tab, panel, & button. YOu can have it load automatically by utilizing your Intialize event.

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;

namespace AutoCADTest
{
    public class Class1
    {
        [CommandMethod("testmyRibbon", CommandFlags.Transparent)]
        public void Testme()
        {
            RibbonControl ribbon = ComponentManager.Ribbon;
            if (ribbon != null)
            {
                RibbonTab rtab = ribbon.FindTab("TESTME");
                if (rtab != null)
                {
                    ribbon.Tabs.Remove(rtab);
                }
                rtab = new RibbonTab();
                rtab.Title = "TEST  ME";
                rtab.Id = "Testing";
                //Add the Tab
                ribbon.Tabs.Add(rtab);
                addContent(rtab);                
            }
        }

        static void addContent(RibbonTab rtab)
        {
            rtab.Panels.Add(AddOnePanel());
        }

        static RibbonPanel AddOnePanel()
        {
            RibbonButton rb;
            RibbonPanelSource rps = new RibbonPanelSource();
            rps.Title = "Test One";
            RibbonPanel rp = new RibbonPanel();
            rp.Source = rps;

            //Create a Command Item that the Dialog Launcher can use,
            // for this test it is just a place holder.
            RibbonButton rci = new RibbonButton();
            rci.Name = "TestCommand";

            //assign the Command Item to the DialgLauncher which auto-enables
            // the little button at the lower right of a Panel
            rps.DialogLauncher = rci;
            
            rb = new RibbonButton();
            rb.Name = "Test Button";
            rb.ShowText = true;
            rb.Text = "Test Button";
            //Add the Button to the Tab
            rps.Items.Add(rb);
            return rp;
        }
    }
}

 

Jeff_M, also a frequent Swamper
EESignature
Message 3 of 21
Sined99
in reply to: Sined99

Hello,

 

Thank you for the sample!!, but I have a problem, I am working with autocad 2013 and visual studio 2010, I can't find the reference(.dll) for the Ribbon Controls, I have attached a picture of the errors, I dont know if the Ribbon control can be include in autocad 2013...

 

-Which is the reference for the ribbon controls??

-Autocad 2013 accept the ribbon controls?

 

Thank you so much!

Message 4 of 21
norman.yuan
in reply to: Sined99

You not only need to set references to AcWindows.dll and AdWindows.dll coming from AutoCAD, you also need to set reference to PresentationFramework.dll, PresentationCore.dll, System.Xaml.dll and Windows.Base.dll from .NET framework, because acwindows/adwindows.dll depend on them.

Message 5 of 21
Sined99
in reply to: norman.yuan

Hello,

 

Thank you for the answer! it works!, thank you very much!

Message 6 of 21
Stemdriller
in reply to: Sined99

Hi

 

I have managed to fumble my way through the C# code and converted it into vb.net, and indeed have managed to create a custom tab.

 

I have placed a button within the panel, but can I get my head around how to fire it when clicked. All i need is to get it to show a form when the user clicks the button.

 

Think it is to do with event handling, which at the moment is a bit of a blurr to say the least.

 

Could someone point me in the right direction?

 

Thanks

 

Gareth 

 

Message 7 of 21
norman.yuan
in reply to: Stemdriller

To respond to user action (clicking, in your case) to a RibbonCommandItem (RibbonButton is derived from RibbonCommandItem), you need to have a class that implements System.Windows.Input.ICommand as a command handler, then assign this command handler to the RibbonCommandItem. Something like:

 

In the code of adding RibbonButton to a RibbonTab,

 

...

RibbonButton btn=new RibbonButton()

btn.Text="Click me";

btn...

btn.CommandHandler=new MyRibbonCommandHandler();

btn.CommandParameter="MyCommand";

btn.ToolTip=....

 

public class MyRibbonCommandHandler : System.Windows.Input.ICommand

{

    ....

 

    public void Execute(object parameter)

    {

        RibbonCommandItem btn = parameter as RibbonCommandItem

        if (btn!=null)

        {

            //execute an AutoCAD command, or your custom command defined by [CommandMethod]

            Document dwg=Application.DocimentManager.MdiActiveDocument;

            dwg.SendStringToExecute((string)btn.CommandParameter + " ", true, false, true);

        }

    }

}

 

HTH.

Message 8 of 21
Stemdriller
in reply to: norman.yuan

Thanks, i nailed it now.

 

GW

Message 9 of 21
f.rampf
in reply to: Jeff_M

Hi Jeff,

I want to create my own AutoCad Plugin, but I have no experience with c# at this point. Anyway I want to learn and see how far I can get.

To begin with I thought it would be cool, to have my own Ribbon, like in the example-code you gave.

Therefore I copied your code into Visual Studio Express (2015) with all the AutoDesk templates installed (using the C# Autocad template)

I've also added all the references you posted. Building worked just fine and didn't give me any error-messages.

However, when I start Autocad (2018) and use the 'netload'-command to load the .dll-file, nothing happens. I don't get an error-message either, but no additional ribbon appears.

Do you have any idea, what the issue could be?

 

Regards,

Felix

Message 10 of 21
banna.kbet
in reply to: f.rampf

You should call command.

example: if, [CommandMethod("KBET")]

 

then type KBET in AutoCAD command line

Message 11 of 21
ActivistInvestor
in reply to: f.rampf


@f.rampf wrote:

Hi Jeff,

I want to create my own AutoCad Plugin, but I have no experience with c# at this point. Anyway I want to learn and see how far I can get.

To begin with I thought it would be cool, to have my own Ribbon, like in the example-code you gave.

Therefore I copied your code into Visual Studio Express (2015) with all the AutoDesk templates installed (using the C# Autocad template)

I've also added all the references you posted. Building worked just fine and didn't give me any error-messages.

However, when I start Autocad (2018) and use the 'netload'-command to load the .dll-file, nothing happens. I don't get an error-message either, but no additional ribbon appears.

Do you have any idea, what the issue could be?

 

Regards,

Felix


You have to make calls to APIs in order to add your content to the Ribbon. If you read what it says at the top of the example, you'll see the Initialize() method mentioned. What the poster was actually referring to is the IExtensionApplication interface's Initialize() method, which you must provide.

 

You can search here for IExtensionApplication and you should find examples of how its used to execute your code when your .DLL is loaded.

 

Message 12 of 21
f.rampf
in reply to: Sined99

Thanks for your answer! I was trying to load it adding the command into the acad2018doc.lsp file but that doesn't work for some reason, so I'll now look further into the method you explained.
Message 13 of 21
f.rampf
in reply to: Sined99

Using netload command and then the command used in my code worked for me so far, but I want it to load upon startup of AutoCAD without any user interaction. But I'm sure I'll figure that out too. Thanks!
Message 14 of 21
tdivittis
in reply to: norman.yuan

Norman and Jeff_M,

 

I just wanted to post a thank you to you both for these (reasonably old) examples.  I've looked into this topic a few times over the years, wanting to do this, but never having it be a pressing need, so always gave up on it.  The two of you, with a few paragraphs, have saved me countless hours of wading through documentation and such trying to figure out how to do this.

 

Thank you both for all of your efforts, here, and providing such useful, simple, on point, practical samples.

Message 15 of 21
dgkhoa89
in reply to: norman.yuan

Hello @norman.yuan ,

 

I try to find these file PresentationFramework.dll, PresentationCore.dll, System.Xaml.dll but could not see them. Where should i find it ? 

 

Thanks you,

Message 16 of 21
norman.yuan
in reply to: dgkhoa89

Those are part of .NET Framework, which is, obviously, available with your computer, because you have AutoCAD and/or VIsual Studio installed to do AutoCAD .NET programming.

 

In VS, open open "Add References..." dialog box, select "Asseblies" tab on the left, then you can find all of them, as shown in pictures below.

 

ReferencesToWPF_01.pngReferencesToWPF_02.png

 

 

Message 17 of 21
saifumk8
in reply to: Jeff_M

how can I add a new button in Add-ins Ribbon with a custom icon? 

Also, need to load .dll file when the user clicks on the button.

Thanks in Advance.

 

Message 18 of 21
norman.yuan
in reply to: saifumk8

When you say "Add-ins Ribbon", do you mean a Ribbon tab, created by your code/add-in, or an existing ribbon tab, created by other add-in (or, simply an existing ribbon tab, which may already loaded at the moment you want to add a button to)? Let's assume this is the possible scenario: you want to add a button to the ribbon menu, preferably, an existing/loaded ribbon tab; however, if the existing ribbon tab does not exists, your code would decide either to add a new ribbon tab (and a ribbon panel on the tab), then the ribbon button; or to not add the button at all.

 

If you have read Jeff's code (message 2/17) and mine (7/17) carefully enough, you would have gotten the idea already:

 

1. Obtain RibbonControl object, as Jeff's code shows;

2. Use FindTab() method to decide if the target ribbon tab exists or not;

3. If the tab is found, you will need to decide which ribbon panel the button would be added into, or add a new ribbon panel for the button;

4. If the tab is not found, you need to create one, if the button must be added. Of course you also need to add a ribbon panel to the new tab.

5. Once the button added (as Jeff's code demos), you simply set the button's CommandParameter to a string of command, something like

"_NETLOAD \"[Path]\myApp.dll\""

 

Message 19 of 21
saifumk8
in reply to: norman.yuan

@norman.yuan  Thank you for your detailed reply.

I mean an existing Ribbon which is called "Add-Ins" as below screenshot. However, I'll go through the codes from you and Jeff.

Add-ins.png

Message 20 of 21
norman.yuan
in reply to: saifumk8

Firstly, changing an existing ribbon menu, which is not created by your own code/customization, might not be a good idea; secondly, changing/updating CUI might be better/easier solution than dynamic change made by code (meaning you have to make sure your code must run to guarantee your ribbon button's availability.

 

If you have reach the conclusion that you need to use code to generate the said ribbon button, again, Jeff's code HAS ALREADY provide enough clue of how to do it. Let me reiterate again:

 

1. Use RibbonControl.FindTab() to make sure "Add-ins" tab does exists; You would need to decide what if it does not exists (say, the current AutoCAD profile does not load AutoCAD default ribbon, but user defined custom ribbon): you would either add the tab, or choose not adding your ribbon button.

2. Add a new panel into the "Add-ins" tab, as Jeff's code demos (addContent() and addOnePanel() methods in Jeff's code, you only need minor and obvious change, such as panel/button text...)

3. Set button's CommandParameter property to "_.NETLOAD ..." string.

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost