Announcements

Community notifications may experience intermittent interruptions between 10–12 November during scheduled maintenance. We appreciate your patience.

Error Plugin on loadong Autocad

Error Plugin on loadong Autocad

bahman.jf.68
Advocate Advocate
1,272 Views
8 Replies
Message 1 of 9

Error Plugin on loadong Autocad

bahman.jf.68
Advocate
Advocate

Hello everyone, 
I have written a plugin in C# .Net for Autocad, it works well in systems with Windows 10, but it gives this error in Windows 7 : 

 

                        Screenshot (1204).png

The codes : 

 

 

public class EICommands : IExtensionApplication
    {
     public void createRibbon()
        {
                RibbonControl ribbon = ComponentManager.Ribbon;
                RibbonTab rtab = new RibbonTab();
                rtab = new RibbonTab();
                rtab.Title = "MyPlugin";
                rtab.Id = "Testing";
                //Add the Tab
                ribbon.Tabs.Add(rtab);

                RibbonPanelSource rps = new RibbonPanelSource();
                rps.Title = "Test";
                RibbonPanel rp = new RibbonPanel();
                rp.Source = rps;
                rtab.Panels.Add(rp);

                RibbonButton btn1 = new RibbonButton();
                btn1.Text = "Button1";
                btn1.CommandParameter = "_tspp ";
                btn1.Id = "TOGGLE1";
                btn1.Text = "topo";
                btn1.ShowText = true;
                btn1.ShowImage = true;
                btn1.Image = LoadImage(Properties.Resources.topo);
                btn1.LargeImage = LoadImage(Properties.Resources.topo);
                btn1.Orientation = 
                System.Windows.Controls.Orientation.Vertical;
                btn1.Size = RibbonItemSize.Large;
                btn1.CommandHandler = new RibbonCommandHandler();
                rps.Items.Add(btn1);
                rps.Items.Add(new RibbonSeparator());
      }

      public class RibbonCommandHandler : System.Windows.Input.ICommand
        {

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                RibbonButton btn = parameter as RibbonButton;
                if (btn != null)
                {
                    Document dwg = 
         Autodesk.AutoCAD.ApplicationServices.Application.
                        DocumentManager.MdiActiveDocument;

                    dwg.SendStringToExecute((string)btn.CommandParameter, 
        true, false, true);
                }
            }
        }
       void IExtensionApplication.Initialize()
        {
             Application.Idle += new EventHandler(Application_Idle);
        }

        void IExtensionApplication.Terminate()
        {
            // Do plug-in application clean up here
        }

        void Application_Idle(object sender, EventArgs e)
        {

            Application.Idle -= Application_Idle;

            EICommands cmd = new EICommands();
            cmd.createRibbon();

        }
}

 

 

I have written it in :
Visual studio 2019
Autocad 2017 api
.Net Framework 4.5


Target system :
Windows 7 64 bit
Autocad 2017

0 Likes
Accepted solutions (1)
1,273 Views
8 Replies
Replies (8)
Message 2 of 9

ActivistInvestor
Mentor
Mentor

You need to run your code in the debugger to find out on what line the exception was thrown.

 

0 Likes
Message 3 of 9

bahman.jf.68
Advocate
Advocate

I don't have Winows 7, I'm just runnig code in the debugger with vs 2019 and win 10 without any error.

0 Likes
Message 4 of 9

bahman.jf.68
Advocate
Advocate

Any help !

0 Likes
Message 5 of 9

fieldguy
Advisor
Advisor

There is more info in the exception text.  Maybe a line number?

0 Likes
Message 6 of 9

bahman.jf.68
Advocate
Advocate

The error is to create ribbon function on loading DLL, I suggest that DLL cann't access to RibbonControl so that it gives Null.

0 Likes
Message 7 of 9

norman.yuan
Mentor
Mentor

Since you do not have Window 7 to run debugging, you may have to let your user know that your app does not support Window 7, though it may work in some cases😅.

 

However, if I have to guess, then, it may be possible while the first Idle event fires during AutoCAD startup, the ribbon control may still not possible for whatever reason(s). 

 

So, to make your code bullet-proof, after the first line of code

RibbonControl ribbon = ComponentManager.Ribbon;

you should test if the RibbonControl is null:

 

if (ribbon!=null)

{

  // rest of the code for creating your custom ribbon item

}

 

Or, at least you could/should place the custom ribbon item creation code in try...catch... block.

 

If the ribbonControl is still null in the first Idle event handler, you may try to use a timer to wait a few seconds and test if AutoCAD is idle then try add custom ribbon again.

Norman Yuan

Drive CAD With Code

EESignature

Message 8 of 9

ActivistInvestor
Mentor
Mentor
Accepted solution

Instead of kludgy workarounds like timers, etc., you can try using RibbonEventManager to deal with the problem, as that's what it was created for. If for example, your user has closed the ribbon, the next time AutoCAD starts, there will be no ribbon until the user issues the RIBBON command to show it. Your code is supposed to be prepared to deal with that situation (which RibbonEventManager does).

 

 

Message 9 of 9

ActivistInvestor
Mentor
Mentor

Heads Up:

 

RibbonEventManager was just updated to address an issue related to the use of async/await in AutoCAD (the issue affects any and all use of async/await in AutoCAD). If code following a call to await throws an exception that is not handled locally, it will terminate AutoCAD.

 

static async void SomeAsyncMethod()
{
    await SomeAwaitableMethod();
    throw new NotSupportedException();  // terminates AutoCAD
}

 

The code was rolled-back to use DocumentCollection.ExecuteInApplicationContext(), (which is just a fancy wrapper for a handler of the Application.Idle event).

 

Anyone that may be using an earlier version of RibbonEventManager, should update to the latest commit.

0 Likes