I've create a partial CUI file for an application and am trying to load/show it programatically.
I pretty much just scammed on some Adesk sample code, but I sometimes wonder about their "samples".
The code below will load a cui file, as long as theres no spaces in the name/path, but it wont display
it. The "pm" variable, noted below is always null. The remove menu function also fails for that reason.
Can someone tell me what I'm doing wrong here?
Also, once a menu is loaded, how do I control menu item selectability. In arx I used acedmenucmd
but I dont believe such a beast exists in .net
Thanks, Perry
--------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
// Please add the References AcCui.dll and AcCustomize.dll before trying to
// build this project.
using Autodesk.AutoCAD.Customization;
namespace Tblocker2007
{
///
/// A class for installing the Menu file and checking its whether its options are
/// appropriate in the current context.
///
public class Menu
{
// All Cui files (main/partial/enterprise) have to be loaded into an object of class
// CustomizationSection
// cs - main AutoCAD CUI file
CustomizationSection cs;
///
/// Default constructor
///
public Menu()
{
// retrieve the location of, and open the ACAD Main CUI File
string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME");
mainCuiFile += ".cui";
cs = new CustomizationSection(mainCuiFile);
}
///
/// Check menu
///
public void checkMenu()
{
#if (DEBUG)
Utilities.prompt("\nChecking menu.");
#endif
// Command: addmenu
}
///
/// This Command adds a new menu to all workspaces called TitleBlocker.
/// The Menu is first added to the Main CUI File and then added to all it's workspaces.
///
[CommandMethod("tbaddmenu")]
public void addMenu()
{
#if (DEBUG)
Utilities.prompt("\nAdding TitleBlocker menu.");
#endif
string fileName = DocData.DocVars.sAppPath + "\\Tblocker.cui";
//for testing, acad doesnt like spaces in name
fileName = fileName.Replace("\\", "/");
fileName = "c:\\tblocker.cui";
if (cs.MenuGroup.PopMenus.IsNameFree("TBlocker"))
{
if (File.Exists(fileName))
{
Application.SetSystemVariable("FILEDIA", 0);
//Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + fileName + " filedia 1 ", false, false, false);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + fileName, false, false, false);
Application.SetSystemVariable("FILEDIA", 1);
PopMenu pm = cs.MenuGroup.PopMenus.FindPopWithName("TBlocker");
if (pm != null)// this is always null !
{
foreach (Workspace wk in cs.Workspaces)
{
WorkspacePopMenu wkpm = new WorkspacePopMenu(wk, pm);
wkpm.Display = 1;
Utilities.prompt("\nTest " + wk.Name);
}
}
}
}
else
Utilities.prompt("TBlocker Menu already Exists\n");
}
///
/// Command: remmenu
/// This Command deletes the menu added above from the Main CUI File and any
/// workspaces that it was added to.
///
[CommandMethod("tbremmenu")]
public void remMenu()
{
// Find Index of the desired MenuItem
// Remove it from all Workspaces that it exists in
// Omitting this step leaves nasty left-overs in the Workspace files
// Remove it from the Cui files' Menu List
#if (DEBUG)
Utilities.prompt("\nRemoving TitleBlocker menu.");
#endif
PopMenu pm = cs.MenuGroup.PopMenus.FindPopWithName("TBlocker");
if (pm != null)// this is always null !
{
foreach (Workspace wk in cs.Workspaces)
{
WorkspacePopMenu wkPm = wk.WorkspacePopMenus.FindWorkspacePopMenu(pm.ElementID, pm.Parent.Name);
if (wkPm != null)
wk.WorkspacePopMenus.Remove(wkPm);
Utilities.prompt("\nTest " + wk.Name);
}
cs.MenuGroup.PopMenus.Remove(pm); // Deletes the Menu from ACAD Menu Group
}
}
}
}