.NET

Reply
Active Contributor
makepeace1
Posts: 28
Registered: ‎01-31-2014
Message 1 of 4 (149 Views)
Accepted Solution

Monitor for profile change

149 Views, 3 Replies
05-16-2014 02:16 AM

I have got a ribbon that loads using a dll

 

At the moment i have got it monitoring to see if the "WSCURRENT" changes but when the profile changes it doesnt change the "WSCURRENT"

 

as long as its not autocad calssic it will reload the ribbon

 

 If Not Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("WSCURRENT").ToString.ToLower = "autocad classic" Then

 

How can i monitor for a profile change ?

I asked the question to autodesk this is the reponse that i got

 

The RibbonPaletteSet.Loaded event is the best way to ensure your ribbon items get re-created whenever a disrupting action occurs in AutoCAD.

 

Here is a complete example:

 

[CommandMethod("SimpleButton")]
public void SimpleButton()
{
RibbonServices.RibbonPaletteSet.Loaded +=
new EventHandler(RibbonPaletteSet_Loaded);
// Makes sure Ribbon is visible (crash otherwise)
// If not make Ribbon visible
if (Autodesk.Windows.ComponentManager.Ribbon != null)
{
CreateSimpleButton();
}
else
{
Autodesk.AutoCAD.ApplicationServices.Application.Idle +=
new EventHandler(Application_Idle);
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.SendStringToExecute("RIBBON ", true, false, false);
}
}
void RibbonPaletteSet_Loaded(object sender, EventArgs e)
{
Autodesk.AutoCAD.ApplicationServices.Application.Idle +=
new EventHandler(Application_Idle);
}
void Application_Idle(object sender, EventArgs e)
{
if (Autodesk.Windows.ComponentManager.Ribbon != null)
{
Autodesk.AutoCAD.ApplicationServices.Application.Idle -=
new EventHandler(Application_Idle);
var ribbon = Autodesk.Windows.ComponentManager.Ribbon;
var tab = ribbon.FindTab("ADN_TAB_ID");
if (tab == null || !tab.IsVisible)
{
CreateSimpleButton();
}
}
}
public void CreateSimpleButton()
{
RibbonControl ribbonControl =
ComponentManager.Ribbon;
RibbonTab Tab = new RibbonTab();
Tab.Title = "Test Ribbon";
Tab.Id = "ADN_TAB_ID";
ribbonControl.Tabs.Add(Tab);
RibbonPanelSource srcPanel =
new RibbonPanelSource();
srcPanel.Title = "Panel1";
RibbonPanel Panel = new RibbonPanel();
Panel.Source = srcPanel;
Tab.Panels.Add(Panel);
Autodesk.Windows.RibbonButton button1 =
new RibbonButton();
button1.Text = "Button";
button1.Size = RibbonItemSize.Large;
button1.Orientation = System.Windows.Controls.Orientation.Horizontal;
button1.Image =
getBitmap(Properties.Resources.Apps, 16, 16);
button1.LargeImage =
getBitmap(Properties.Resources.Apps, 32, 32);
button1.ShowText = false;
button1.CommandParameter = "._LINE ";
button1.CommandHandler = new SimpleButtonCmdHandler();
srcPanel.Items.Add(button1);
Tab.IsActive = true;
}
//Use: getBitmap(Properties.Resources.a_large);
BitmapImage getBitmap(Bitmap bitmap, int height, int width)
{
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(stream.ToArray());
bmp.DecodePixelHeight = height;
bmp.DecodePixelWidth = width;
bmp.EndInit();
return bmp;
}
public class SimpleButtonCmdHandler :
System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (parameter is RibbonButton)
{
// builds escape sequence to cancel active commands
string esc = "";
string cmds = (string) Application.
GetSystemVariable("CMDNAMES");
if (cmds.Length > 0)
{
int cmdNum = cmds.Split(new char[] { '\'' }).Length;
for (int i = 0; i < cmdNum; i++)
esc += '\x03';
}
RibbonButton button = parameter as RibbonButton;
Document doc = Application.
DocumentManager.MdiActiveDocument;
doc.SendStringToExecute(esc + button.CommandParameter,
true, false, false);
}
}
}

 Seems to do the job

Distinguished Contributor
quigs
Posts: 143
Registered: ‎09-30-2008
Message 2 of 4 (117 Views)

Re: Monitor for profile change

05-18-2014 03:25 AM in reply to: makepeace1

Hi,

You could look at Event Handlers.  I am not sure what one but I would assume there is some kind of event that could be used for this.

My name is Martin.. :smileyvery-happy:
Active Contributor
makepeace1
Posts: 28
Registered: ‎01-31-2014
Message 3 of 4 (100 Views)

Re: Monitor for profile change

05-19-2014 02:46 AM in reply to: quigs

ok

 

I have worked out how to do it but it seems to unload again once i click ok to close the window where you change the profile

 

AddHandler Autodesk.AutoCAD.ApplicationServices.Application.UserConfigurationManager.CurrentProfileChanged, AddressOf acApp_CurrentProfileChanged

 

Private Sub acApp_CurrentProfileChanged(ByVal sender As Object, ByVal e As ProfileEventArgs)

            LOADMENU()

        End Sub

 Any idea's?

Active Contributor
makepeace1
Posts: 28
Registered: ‎01-31-2014
Message 4 of 4 (88 Views)

Re: Monitor for profile change

05-20-2014 01:48 AM in reply to: makepeace1

I asked the question to autodesk this is the reponse that i got

 

The RibbonPaletteSet.Loaded event is the best way to ensure your ribbon items get re-created whenever a disrupting action occurs in AutoCAD.

 

Here is a complete example:

 

[CommandMethod("SimpleButton")]
public void SimpleButton()
{
    RibbonServices.RibbonPaletteSet.Loaded += 
        new EventHandler(RibbonPaletteSet_Loaded);           

    // Makes sure Ribbon is visible (crash otherwise)
    // If not make Ribbon visible
    if (Autodesk.Windows.ComponentManager.Ribbon != null)
    {
        CreateSimpleButton();
    }
    else
    {
        Autodesk.AutoCAD.ApplicationServices.Application.Idle +=
            new EventHandler(Application_Idle);

        Document doc = Application.DocumentManager.MdiActiveDocument;
        doc.SendStringToExecute("RIBBON ", true, false, false);
    }
}

void RibbonPaletteSet_Loaded(object sender, EventArgs e)
{
    Autodesk.AutoCAD.ApplicationServices.Application.Idle +=
        new EventHandler(Application_Idle);
}

void Application_Idle(object sender, EventArgs e)
{
    if (Autodesk.Windows.ComponentManager.Ribbon != null)
    {
        Autodesk.AutoCAD.ApplicationServices.Application.Idle -= 
            new EventHandler(Application_Idle);

        var ribbon = Autodesk.Windows.ComponentManager.Ribbon;

        var tab = ribbon.FindTab("ADN_TAB_ID");

        if (tab == null || !tab.IsVisible)
        {
            CreateSimpleButton();
        }
    }
}

public void CreateSimpleButton()
{
    RibbonControl ribbonControl = 
        ComponentManager.Ribbon;

    RibbonTab Tab = new RibbonTab();
    Tab.Title = "Test Ribbon";
    Tab.Id = "ADN_TAB_ID";

    ribbonControl.Tabs.Add(Tab);

    RibbonPanelSource srcPanel = 
        new RibbonPanelSource();

    srcPanel.Title = "Panel1";

    RibbonPanel Panel = new RibbonPanel();
    Panel.Source = srcPanel;
    Tab.Panels.Add(Panel);

    Autodesk.Windows.RibbonButton button1 = 
        new RibbonButton();

    button1.Text = "Button";
    button1.Size = RibbonItemSize.Large;
    button1.Orientation = System.Windows.Controls.Orientation.Horizontal;   

    button1.Image = 
        getBitmap(Properties.Resources.Apps, 16, 16);

    button1.LargeImage = 
        getBitmap(Properties.Resources.Apps, 32, 32);

    button1.ShowText = false;
    button1.CommandParameter = "._LINE ";
    button1.CommandHandler = new SimpleButtonCmdHandler();

    srcPanel.Items.Add(button1);
            
    Tab.IsActive = true;
}

//Use: getBitmap(Properties.Resources.a_large);
BitmapImage getBitmap(Bitmap bitmap, int height, int width)
{
    MemoryStream stream = new MemoryStream();
    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

    BitmapImage bmp = new BitmapImage();
    bmp.BeginInit();
    bmp.StreamSource = new MemoryStream(stream.ToArray());
    bmp.DecodePixelHeight = height;
    bmp.DecodePixelWidth = width;
    bmp.EndInit();

    return bmp;
}

public class SimpleButtonCmdHandler : 
    System.Windows.Input.ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (parameter is RibbonButton)
        {
            // builds escape sequence to cancel active commands
            string esc = "";

            string cmds = (string) Application.
                GetSystemVariable("CMDNAMES");

            if (cmds.Length > 0)
            {
                int cmdNum = cmds.Split(new char[] { '\'' }).Length;

                for (int i = 0; i < cmdNum; i++)
                    esc += '\x03';
            }

            RibbonButton button = parameter as RibbonButton;

            Document doc = Application.
                DocumentManager.MdiActiveDocument;

            doc.SendStringToExecute(esc + button.CommandParameter, 
                true, false, false); 
        }
    }
}

 Seems to do the job

Need installation help?

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