Accessing toolbars and buttons programatically

Accessing toolbars and buttons programatically

Anonymous
Not applicable
3,485 Views
9 Replies
Message 1 of 10

Accessing toolbars and buttons programatically

Anonymous
Not applicable

Hi all,

 

I'm setting up some custom toolbars and that's no problem, but what I'd like to do is have one of the buttons be a toggle, so the icon changes depending on what state it is in.  I'm just having a bit of trouble figuring out how I find those buttons programatically from within C#. I've included Autodesk.Autocad.Customization which gives me the Toolbar, Toolbarbutton, etc classes, but I don't know how to find the existing toolbars from the running session of AutoCAD, and I can't find any information on this anywhere else on the internet.

 

0 Likes
Accepted solutions (1)
3,486 Views
9 Replies
Replies (9)
Message 2 of 10

chiefbraincloud
Collaborator
Collaborator

I would suggest starting here:

 

http://through-the-interface.typepad.com/through_the_interface/2007/05/creating_a_part.html

 

It doesn't cover all of the types of objects that can be in a CUI, but it should get you started with the right approach.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 10

Balaji_Ram
Alumni
Alumni

Hi,

 

The link provided by chiefbraincloud is a good one.

 

Here is a code snippet that can help you get started with the CUI API to list the toolbars :

 

        using Autodesk.AutoCAD.Customization;

 

        [CommandMethod("ListToolbars")]
        public void ListToolbarsCommand()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME");
            mainCuiFile += ".cuix";
            CustomizationSection cs = new CustomizationSection(mainCuiFile);

            foreach (Workspace wk in cs.Workspaces)
            {
                ed.WriteMessage(string.Format("\n---------- {0} ----------", wk.Name);
                foreach (WorkspaceToolbar wsTb in wk.WorkspaceToolbars)
                {
                    Toolbar tb = cs.getToolbar(wsTb.ToolbarId);
                    if (tb != null)
                    {
                        ed.WriteMessage(string.Format("\n{0}", tb.Name));
                    }
                }
            }
        }



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 10

Anonymous
Not applicable
Accepted solution

I question whether or not this is actually feasible on the fly without some lag time involved. I would be curious if you were able to do it. I would assume that all the alternate icon files would need to be present within the toolbar cuix and you are just changing the file pointer. In the past I have found that you can create something that looks similar to a toolbar by using a windows form where the controls are sized to small buttons and all of the wasted form space is removed. What you loose by it not being a true toolbar you gain with all the other interface and visual options available to use in the form and its controls. Suddenly your fake toolbar can do things a real toolbar cannot. For example you can have the toolbar form morph in context with the tasks it provides in many other ways than the one you are trying to do. If this is a task related need and if you still want a toolbar interface parked somewhere in the session then have a real toolbar launch the faux toolbar, which would then dismiss itself when the task is finished.

0 Likes
Message 5 of 10

Anonymous
Not applicable

So I've been able to find the toolbarbuttons in the customization section,  but I can't figure any way to replace the image programatically, as that doesn't seem to be an exposed property of the button.  I tried used the ToolbarButton.Replace(CustomizationElement item) function, which seemed to replace the item in memory, but the toolbar didn't update.  Here's some code, anyone have any input?

[CommandMethod("BPI-TEST")]
        public void BPITest()
        {
            string strMainCUIfile = Application.GetSystemVariable("MENUNAME") as string;
            strMainCUIfile += ".cuix";
            
            CustomizationSection csMain = new CustomizationSection(strMainCUIfile);

            CustomizationSection csGavin = null;

            foreach (string strCUIFile in csMain.PartialCuiFiles)
            {
                if (strCUIFile.Contains("gavincustom"))
                    csGavin = new CustomizationSection(strCUIFile);
            }

            if (csGavin == null)
            {
                ed.WriteMessage("CUIx file 'gavincustom.cuix' not found'\n");
                return;
            }


            ToolbarButton tbbFlangedOn = csGavin.getToolbarButton("TBBU_0022");
            ToolbarButton tbbFlangedOff = csGavin.getToolbarButton("TBBU_0012");

            ed.WriteMessage(tbbFlangedOff.Name + "\n");

            tbbFlangedOff.Replace(tbbFlangedOn);

            ed.WriteMessage(tbbFlangedOff.Name + "\n");

        }

 And this is the result:

Command: bpi-test
VALVE-FLANGED-TOGGLEOFF
VALVE-FLANGED-TOGGLEON

 

But the toolbar button itself doesn't change.

0 Likes
Message 6 of 10

Alexander.Rivilis
Mentor
Mentor
csGavin.Save();

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 7 of 10

chiefbraincloud
Collaborator
Collaborator

What you have done there is loaded the CUIX file from the disc, modified it in memory, and then 1) didn't save it, and 2) haven't actually changed what is loaded into AutoCAD.

 

The save should be pretty easy, but unfortunately the last time I messed with CUI code, I had the same problem of not being able to load my custom menu into AutoCAD using .NET, and I haven't messed with it recently, so I'm not up to date on the current Customization API.  (As I recall, there may have been a way using com, but I don't reference the COM type libraries, so I didn't want to do that)

 

Looks Like Alexander just posted something, while I was digging around to find the load mechanism.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 8 of 10

Alexander.Rivilis
Mentor
Mentor

As I remember reloading aof cui/cuix posible only with command method such as:

string flName = csGavin.CUIFileBaseName;
Application.SetSystemVariable("FILEDIA",0);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_cuiunload " + flName + " ",false,false,false);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_cuiload " + flName + " filedia 1 ",false,false,false); 

This link can be useful for you: http://through-the-interface.typepad.com/through_the_interface/2007/05/creating_a_part.html

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 9 of 10

Anonymous
Not applicable

Hmm so I actually have to modify the cuix file and then reload it?  There's no way to access the buttons that are loaded directly into memory?  If that's the case I don't think that modifying the CustomizationSection is the way to do what I want to do, there'll be far too much of a time lag between clicking the button and updating.  I think I'll probably use aksaks' method of making a fake toolbar (in my case it'll basically be just a window showing the status of the toggles).

0 Likes
Message 10 of 10

Alexander.Rivilis
Mentor
Mentor

Another way is using COM/ActiveX, but you have to create toolbar every time and can not save it  to cui/cuix file: http://forums.autodesk.com/t5/NET/Create-toolbar/m-p/1687527#M3911

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes