.NET

Reply
Active Member
GavinLW
Posts: 6
Registered: ‎02-02-2011
Message 1 of 10 (654 Views)
Accepted Solution

Accessing toolbars and buttons programatically

654 Views, 9 Replies
10-06-2011 10:16 AM

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.

 

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.

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 2 of 10 (644 Views)

Re: Accessing toolbars and buttons programatically

10-06-2011 01:32 PM in reply to: GavinLW

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
ADN Support Specialist
Balaji_Ram
Posts: 723
Registered: ‎03-21-2011
Message 3 of 10 (625 Views)

Re: Accessing toolbars and buttons programatically

10-07-2011 03:22 AM in reply to: GavinLW

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

Valued Contributor
aksaks
Posts: 93
Registered: ‎03-23-2009
Message 4 of 10 (601 Views)

Re: Accessing toolbars and buttons programatically

10-07-2011 02:22 PM in reply to: Balaji_Ram

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.

Active Member
GavinLW
Posts: 6
Registered: ‎02-02-2011
Message 5 of 10 (576 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 10:46 AM in reply to: GavinLW

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.

Moderator
Alexander.Rivilis
Posts: 1,434
Registered: ‎04-09-2008
Message 6 of 10 (570 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 11:43 AM in reply to: GavinLW
csGavin.Save();

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 7 of 10 (566 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 11:49 AM in reply to: GavinLW

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
Moderator
Alexander.Rivilis
Posts: 1,434
Registered: ‎04-09-2008
Message 8 of 10 (564 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 12:04 PM in reply to: chiefbraincloud

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


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Member
GavinLW
Posts: 6
Registered: ‎02-02-2011
Message 9 of 10 (559 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 12:12 PM in reply to: GavinLW

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).

Moderator
Alexander.Rivilis
Posts: 1,434
Registered: ‎04-09-2008
Message 10 of 10 (553 Views)

Re: Accessing toolbars and buttons programatically

10-11-2011 12:20 PM in reply to: GavinLW

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


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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