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

Load & show partial cui file

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
1016 Views, 2 Replies

Load & show partial cui file

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
}
}
}
}
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: Anonymous

Ok, so that approach was completely bass ackwards, being that I wanted to load a partial CUI file.
The following codes works for me ONLY IF there are no spaces in the string passed to "sendstring"
which of course makes it pretty useless. I poked around in the api and found some other methods for
adding partial CUI's, but they dont seem to work. I know its doable because "express tools" gets
loaded and it uses a partial cui in a directory that has spaces in its name.
Does anyone know how to do this? Is there a way to get around total uselessness of sendstring?
Perry

---------------------------------------------------------------------------------------------
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;
CustomizationSection entCs;
CustomizationSection[] partials;

int numPartialFiles;

// True when enterprise CUI file is loaded successfully
bool entCsLoaded;
bool tbCsLoaded;
//string with spaces screws up "sendstring"
//string tbCuiName = DocData.DocVars.sAppPath + "\\tblocker.cui";
string tbCuiName = "c:\\tblocker.cui";
///
/// 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);
string entCuiFile = (string)Application.GetSystemVariable("ENTERPRISEMENU");
if (entCuiFile.Equals("."))
entCsLoaded = false;
else
{
entCs = new CustomizationSection(entCuiFile);
entCsLoaded = true;
}

// Code for loading all partial CUI's listed in the main CUI file
partials = new CustomizationSection[cs.PartialCuiFiles.Count];
int i = 0;
foreach (string fileName in cs.PartialCuiFiles)
{
if (fileName == tbCuiName)
tbCsLoaded = true;
if (File.Exists(fileName))
{
partials = new CustomizationSection(fileName);
i++;
}
}
numPartialFiles = i;
}
///
/// Check menu
///

public void checkMenu()
{
#if (DEBUG)
Utilities.prompt("\nChecking menu.");
#endif
}
///
/// 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 (!tbCsLoaded)
{
if (File.Exists(tbCuiName))
{
#if (DEBUG)
Utilities.prompt("\nAdding TitleBlocker menu.");
#endif
//The following 5 lines dont do squat
//CustomizationSection cs2 = new CustomizationSection(tbCuiName);
//cs.addPartialCUI(cs2);
//cs.CustomizationSection.addPartialCUI(cs2);
//cs.UpdateWorkspaceComplete();
//cs.MakeDirty();

Application.SetSystemVariable("FILEDIA", 0);
//having spaces in the filename hoses "sendstring"
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + tbCuiName + " filedia 1 ", false, false, false);
}
else
Utilities.prompt("\n" + tbCuiName + " does not exist.");
}
else
Utilities.prompt("TBlocker Menu already Exists\n");
}
///
/// This Command saves all open CUI Files that have been modified
///

[CommandMethod("tbsavemenu")]
public void saveMenu()
{
// Save all Changes made to the CUI file in this session.
// If changes were made to the Main CUI file - save it
// If changes were made to the Partial CUI files need to save them too

if (cs.IsModified)
cs.Save();

for (int i = 0; i < numPartialFiles; i++)
{
if (partials.IsModified)
partials.Save();
}

if (entCsLoaded && entCs.IsModified)
entCs.Save();

// Here we unload and reload the main CUI file so the changes to the CUI file could take effect immediately.
string flName = cs.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);
}
///
/// 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
if (!tbCsLoaded)
{
cs.RemovePartialMenu("Tblocker");
cs.PartialCuiFiles.Remove("Tblocker");
saveMenu();
}
}
}
}
Message 3 of 3
ahmed.felix
in reply to: Anonymous

Hi Perry, to be honest I didn't read all your code :). But I guess this is what you are trying to do

Imports Autodesk.AutoCAD.Interop

' Abrir autocad
Dim acadApp As New AcadApplication

' Agregar barras de herramientas y menus
Dim strCuiName As String = "HemekUtilerias"
Dim strCuiFile As String = "C:\temp\HemekUtilerias.cui"

Dim mgMenugroup As AcadMenuGroup

Try

' Obtener el menugroup
mgMenugroup = acadApp.MenuGroups.Item(strCuiName)

Catch ex As System.Exception

' Un error significa que hay que cargar el archivo
acadApp.MenuGroups.Load(strCuiFile)
mgMenugroup = acadApp.MenuGroups.Item(strCuiName)

End Try


This loads a partial cui previously made using Autocad. The name of this particular file has no spaces but I've tried it before with spaces and it works just fine. This lets you build your own menu along with a toolbar if you want and then load it using .NET. Just add a reference to Autocad 200x type library.
Comments are in Spanish but the code is really simple. Hope this helps.

---------------------

One more thing, I use this code inside a setup project which installs my entire application while Autocad is closed. If you run this while Autocad is open you will have to close it and open it again to see the changes or reference acadApp to the existing window insted of creating a new one (I haven´t tried this). Message was edited by: ahmed.felix

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