Loading a partial CUI and making its toolbars visible through .NET

A discussion in the comments on this previous entry seemed worthy of turning into a post.

The problem appears to be that when you load a partial CUI file into AutoCAD, by default the various resources (pull-down menus, toolbars) are not displayed.

This snippet of code shows you how to both load a CUI file into AutoCAD and then loop through the toolbars in your menu-group, making them all visible. You could extend it fairly easily to add the pull-down menus contained in the CUI by using mg.Menus.InsertMenuInMenuBar(). I'm choosing to leave that as an exercise for the reader mainly because the choice of where the various menus go can be quite specific to individual applications... toolbars are much simpler - we're just going to turn them all on. 🙂

So here's the code... for convenience I wrote it in VB.NET, but it uses COM Interop to access the menu API in AutoCAD.

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Interop

Public Class ToolbarCmds

  <CommandMethod("LoadTBs")> _

  Public Sub LoadToolbars()

    Const cuiname As String = "mycuiname"

    Const cuifile As String = "c:\mycuifile.cui"

    Dim mg As AcadMenuGroup

    Try

      'Attempt to access our menugroup

      mg = Application.MenuGroups.Item(cuiname)

    Catch ex As System.Exception

      'Failure simply means we need to load the CUI first

      Application.MenuGroups.Load(cuifile)

      mg = Application.MenuGroups.Item(cuiname)

    End Try

    'Cycle through the toobars, setting them to visible

    Dim i As Integer

    For i = 0 To mg.Toolbars.Count - 1

      mg.Toolbars.Item(i).Visible = True

    Next

  End Sub

End Class

  1. Thanks for this article about CUI.
    Can you write some articles about how to add/remove menus/toolbars using .NET?And the usage of the Autodesk.AutoCAD.Customization namespace?

  2. Thanks Kean,
    I have a few issues.
    I am running the following code in C# with no effect to the toolbars visablity:
    #################################
    CustomizationSection swatchdigitalcui = new CustomizationSection("C:\swatchdigital_2.cui");
    //This file has been added to the acad.cui as a partial.cui

    for (int a = 0; a < swatchdigitalcui.MenuGroup.Toolbars.Count - 0; a++)
    {
    acadApp.ShowAlertDialog(swatchdigitalcui.MenuGroup.Toolbars[a].Name);
    if (swatchdigitalcui.MenuGroup.Toolbars[a].Name == "SwatchDigital")
    {
    acadApp.ShowAlertDialog(swatchdigitalcui.MenuGroup.Toolbars[a].ToolbarVisible.ToString());
    swatchdigitalcui.MenuGroup.Toolbars[a].ToolbarVisible = ToolbarVisible.show;
    acadApp.ShowAlertDialog("SwatchDigital Toolbar On");
    acadApp.ShowAlertDialog(swatchdigitalcui.MenuGroup.Toolbars[a].ToolbarVisible.ToString());
    }
    }

    //This code finds the Toolbar then does set the toolbar to show ... but nothing happens in the UI.

    ###############################

    I think this is because it is working on the partial.cui it self but not in the application.

    So I try using the Autodesk.AutoCAD.ApplicationServices.Application.MenuGroups to get the MenuGroup.

    But here in C# MenuGroups does not have a way of accesing the MenuGroup. MenuGroups[1] does not work. MenuGroups.Count does not exist. foreach will not work.

    All in All, I still cannot make toolbars or menus visiable by using the partial.cui.

    Whats up?
    Thanks

  3. By the way in C# I do not have Application.MenuGroups.Load();

    Does this have to do with the Interop?

    If so - how do I access this.

    Thanks

  4. CM - you'll need to add the AutoCAD 200x Type Library as a reference to your project.

    csharpbird - Autodesk.AutoCAD.Customization is about accessing/modifying CUI files - it doesn't help you load them in the AutoCAD Editor, for instance. The way to go right now from .NET is via COM, unless you merely want to modify the CUI file on disk (and not load it).

    Regards,

    Kean

  5. Kean-
    I now have the AutoCAD 2007 Type Library reference in place. I am using Autodesk.AutoCad.Interop. I now can reference an AcadMenuGroup however I still have no way to access a specific MenuGroup. Application.MenuGroups still does not have a .Load() method, nor does it have a .Count or any way to get to the MenuGroup I need (MenuGroups[i] does not get me a MenuGroup)

    (This method is in a Autodesk.AutoCAD.Runtime.IExtensionApplication if that makes a difference)

  6. Kean-
    For you readers I've found the issues:
    1.
    You must use AcadApplication not Application in c#

    AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

    2.
    Unusual VB-like syntax for MenuGroups iteming. MenuGroups[i] will not work. MenuGroups.Item(a) will work

    for (int a = 0; a < app.MenuGroups.Count - 0; a++)
    {
    acadApp.ShowAlertDialog(app.MenuGroups.Item(a).Name);
    if (app.MenuGroups.Item(a).Name == "MyMenuGroup")
    {
    acadApp.ShowAlertDialog("Dis One");
    for (int b = 0; b < app.MenuGroups.Item(a).Toolbars.Count - 0 ; b++)
    {
    if(app.MenuGroups.Item(a).Toolbars.Item(b).Name == "MyToolBar")
    {
    app.MenuGroups.Item(a).Toolbars.Item(b).Visible = true;
    }
    }
    }
    }

  7. Actually - the story gets worse.
    Now I can make the toolbars & menus visible... but not the first time I open autocad.
    I have to restart this app before it will show up.

    The first time it will write the partial.cui into the acad.cui...but it will not find the menus are toolbars.

    We I close & restart acad...it finds the partial.cui & finds the toolbars & menus to open.

    Again defeating the purpose of partial.cui

  8. Hi Kean,
    Now I'm trying your MenuGroups.Load in c#.
    sdname = "MYCUI";
    sdpath = "C:\MYcui.cui";

    try
    {
    themg = app.MenuGroups.Item(sdname);
    acadApp.ShowAlertDialog("SD found");
    }
    catch (System.Exception ex)
    {

    app.MenuGroups.Load(sdpath, app.MenuGroups);
    acadApp.ShowAlertDialog("sd added");
    themg = app.MenuGroups.Item(sdname);
    }

    ________________________________________

    This too fails.
    MenuGroup.Load() needs a 'BaseMenu'?
    What is a base menu?

    Thanks,
    cm

  9. Hi,cm
    You can use the following:
    app.MenuGroups.Load(sdpath, Type.Missing);

  10. Working with AutoCAD 2007 and VS2005. I'm having trouble with Imports Autodesk.AutoCAD.Interop. I can see...
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.Colors
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Geometry
    Imports Autodesk.AutoCAD.GraphicsInterface
    Imports Autodesk.AutoCAD.GraphicsSystem
    Imports Autodesk.AutoCAD.LayerManager
    Imports Autodesk.AutoCAD.PlottingServices
    Imports Autodesk.AutoCAD.Publishing
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.Windows
    but I can't see the Autodesk.AutoCAD.Interop. Do I need to add a Reference? I have acdbmgd.dll, acmgb.dll, and ManagedMapApi.dll in my project.

  11. Yes, you're going to need to add a Reference to the "AutoCAD 2007 Type Library" (which is a COM reference, if course).

    Regards,

    Kean

  12. I don't no about the toolbars, but I had a menu that I had made up and could not get to show up. The solution that I found was to first make sure that the menu is in autocad search path. Either place it in autocad search path and go to tool-options-files then go to support file search path and add it. Then apply it. After this type in cuiload and browse to where your menu is written. Then push load and you should be set to go.It worked for me.(Oh make sure it is cui file and not a mnu file.)

  13. Francisco Lomas Avatar
    Francisco Lomas

    Hi everyone!!
    I don't know if your problem have been solved, but i try this code and it works for me:

    Autodesk.AutoCAD.Interop.AcadApplication acadApp = (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    Autodesk.AutoCAD.Interop.IAcadMenuGroups mngMenus = (Autodesk.AutoCAD.Interop.IAcadMenuGroups)Autodesk.AutoCAD.ApplicationServices.Application.MenuGroups;
    Autodesk.AutoCAD.Interop.IAcadPopupMenu apmMenu = mngMenus.Item(0).Menus.Add("MDMQ");
    mngMenus.Item(0).Menus.Item(0).InsertInMenuBar(0);
    apmMenu.AddMenuItem(0, "Agregar Persona", "ingper");

    The only thing that i got is when you click the menuitem doesn't execute the command, it only writes it on the command line....

    I hope this helps!!!

  14. Francisco Lomas Avatar
    Francisco Lomas

    An update, the command executes if you put an \n at the end of the command name, example:

    apmMenu.AddMenuItem(0, "Agregar Persona", "ingper\n");

    This works great for me!!!

  15. Jürgen Becker Avatar
    Jürgen Becker

    Hi Kean,
    i've got Problems with the Autodesk.AutoCAD.Interop.
    I added the Reference to the Interops and this works fine on my own machine with ADT 2008. But, when I install the program on another machines -also with ADT 2008 -the programm doesnt work.
    Following message appers:

    System.IO.FileNotFoundException: Die Datei oder Assembly Autodesk.AutoCAD.Interop.Common, Version=17.2.0.0, Culture=neutral, PublicKeyToken=eed84259d7cbf30b oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

    Dateiname: Autodesk.AutoCAD.Interop.Common, Version=17.2.0.0, Culture=neutral, PublicKeyToken=eed84259d7cbf30b

    What must I do?

    Regards Jürgen

  16. Kean Walmsley Avatar
    Kean Walmsley

    It seems to be looking for the 2009 COM libraries (17.2).

    Kean

  17. That is what I thought too.
    The Problem is: How can I get the 2008 COM libriaries?

    Jürgen

  18. Kean Walmsley Avatar
    Kean Walmsley

    I don't know the answer to that: it may be that you need to build the app on a machine that does not have AutoCAD 2009 installed.

    Submitting the question via ADN or to one of the discussion groups may get you a better answer, though.

    Kean

  19. Thanks Kean.
    I'll install AutoCAD 2009 on the other machines next week and I hope that would fix the problem.
    Regards Jürgen

  20. Hello Kean,

    I want to an existing Toolbar to insert a ToolbarControl.
    The ToolbarControl, I would like to use for my application config switch between different.

    Inserting a toolbar control with type "ControlType.CustomControl"
    works:

    Dim As acToolbarControl Toolbar Toolbar Control = New Control(ControlType.CustomControl, Me.prtToolbar, 3)
    acToolbarControl.ElementID = "ID_KPLUSS_INISWITCHER"

    But the Toolbar control is always displayed as Layerstruerung.

    How can I enter the Toolbar control their own content (text only) to fill?

    Regards Mario

  21. Hi Mario,

    I don't know - I suggest contacting the ADN team (if you're a member) or otherwise posting to the AutoCAD .NET Discussion Group.

    Regards,

    Kean

  22. Can anybody tell me please where my mistake is.

    When I try to run the code in .NET it always says "MenuGroups is not a member of MyApplication"

    I have this links in my project:
    - AcDbMgd [.net -> c:\Program Files\ObjectARX\inc-win32\AcDbMgd.dll]
    - AcMgd [.net -> c:\Program Files\ObjectARX\inc-win32\AcMgd.dll]
    - AutoCAD 2009 Type Library [COM c:\Windows\assembly\ ...]
    - AutoCAD/ObjectDBX Common 17.0 Type Library [COM c:\Windows\assembly\ ...]

  23. Kean Walmsley Avatar

    It must be something specific to your application... I don't know where "MyApplication" is coming from: it's not in the above code.

    Kean

  24. Application.MenuGroups still does not have a .Load() method, nor does it have a .Count or any way to get to the MenuGroup I need (MenuGroups[i] does not get me a MenuGroup) thank you

  25. how to set toolbar button no use ? (color change to grey) thank you

  26. sorry How to set toolbar button can not be used for the color into a gray thank you

  27. It's not clear which of these questions remains open: I suggest using the online discussion groups in future (if you're not an ADN member) for questions that are off-topic.

    The answer to your MenuGroups questions (both regarding Count and Load) are contained in the previous comments on this post.

    Kean

  28. Hi Kean,

    i use the the code:

    public static string LoadPartialCUI(string CuiPathName)
    {
    // nach keanw.com/2006/11/loading_a_parti.html
    if (string.IsNullOrEmpty(CuiPathName)) return null;
    string CuiName = System.IO.Path.GetFileNameWithoutExtension(CuiPathName);
    try
    {
    AcadMenuGroup group = null;
    AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    if (!HasMenuGroup(CuiName))
    group = app.MenuGroups.Load(CuiPathName, null);
    else
    group = app.MenuGroups.Item(CuiName);
    if (group != null)
    {
    foreach (AcadToolbar acToolbar in group.Toolbars)
    {
    acToolbar.Visible = true;
    }
    }
    return group.Name;
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
    Log.LogException(ex);
    }
    catch (System.Exception ex)
    {
    Log.LogException(ex);
    }
    return null;
    }

    The running good, but the effekt is that the Toolbar is
    visible and the icons unvisible.
    Use i the command "CUI" and make a litle change in the partial cui-file (rename a command and rename to the old
    Value) then press the accept-button, all icons visible.

    Why ????

    Mario

  29. Kean Walmsley Avatar

    Hi Mario,

    I don't have the definition of your HasMenuGroup() function, but if I change your code to use the try-catch approach shown in the VB code it works just fine for me, at least (I'm using AutoCAD 2011).

    Regards,

    Kean

  30. Hi Kean,

    the HasMenuGroup is a little funuction:

    private static bool HasMenuGroup(string MenueGroupName)
    {
    AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    foreach (AcadMenuGroup group in app.MenuGroups)
    {
    if (group.Name == MenueGroupName) return true;
    }
    return false;
    }

    On our AutoCAD 2008 (tested on 5 installations) and AutoCAD Civil 3D 2008 is show the effect.

    Regards,

    Mario

  31. Kean Walmsley Avatar

    Hi Mario,

    I can't really help you from here, I'm afraid, as it works fine on my system.

    I suggest posting your question to the ADN team, if you're a member, or the AutoCAD .NET Discussion Group, otherwise.

    Regards,

    Kean

Leave a Reply to Mario Cancel reply

Your email address will not be published. Required fields are marked *