Assembly reference

Assembly reference

karl.sch1983
Advocate Advocate
1,105 Views
4 Replies
Message 1 of 5

Assembly reference

karl.sch1983
Advocate
Advocate

Hi friends, in this post, what assembly reference do I need to get rid of the missing assembly reference issue. I am getting red squiggly lines for Properties and BitmapImage. I tried all the assemblies I could think of, including System.Drawing and System.Windows. Thanks in advance for your help. Following is an image of the issue.

References.png

0 Likes
Accepted solutions (1)
1,106 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

Did you copy-pasted the code from outside of the project? it looks like you are doing ribbon stuff, which required references to WPF related system assemblies:

 

PresentationFramework

PresentationCore

System.Xaml

WindowsBase

 

Of course you also need

acWindows.dll

adWindows.dll

 

Once you have these referenced, simply right click the squiggly "BitmapImage" Visual Stuido should suggests adding 

"using System.Windows.Media.Imageing;" namespace.

As for "Properties.Resources...", it would only be available if you created Resources in your project: go to "Project->Properties->Resources tab" and create needed image resources, in your case, they are a bunch of image files to be read into the resources.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

karl.sch1983
Advocate
Advocate

Thanks @norman.yuan, this did the trick. I was able to load the ribbon. A new problem arises. The ribbon keeps loading a new instance every time I call simplebutton. Based on the post that is not the behavior I expected. Following is my code which is exactly from the previously posted link. Can you please check if it does the same for you.

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Windows;
using System.Media;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: ExtensionApplication(typeof(ManagedApplication.Initialization))]
[assembly: CommandClass(typeof(ManagedApplication.Commands))]


namespace ManagedApplication
{
    public class Initialization : Autodesk.AutoCAD.Runtime.IExtensionApplication
    {
        public void Initialize()
        {
            Active.Ed.WriteMessage("Initializing - do something useful.");
        }

        public void Terminate()
        {
            Debug.WriteLine("Cleaning up...");
        }
    }

    public class Commands
    {
        [CommandMethod("SimpleButton")]
        public void SimpleButton()
        {
            Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged += new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(Application_SystemVariableChanged);
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.CommandEnded += new CommandEventHandler(MdiActiveDocument_CommandEnded);

            CreateSimpleButton();
        }

        void MdiActiveDocument_CommandEnded(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "QUICKCUI" || e.GlobalCommandName == "CUI")
                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);
                CreateSimpleButton();
            }
        }

        void Application_SystemVariableChanged(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
        {
            if (e.Name == "WSCURRENT")
            {
                string cmdNames = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES");
                if (cmdNames.ToUpper().IndexOf("QUICKCUI") >= 0 || cmdNames.ToUpper().IndexOf("CUI") >= 0)
                    return;

                CreateSimpleButton();
            }
        }

        public void CreateSimpleButton()
        {
            Autodesk.Windows.RibbonControl ribbonControl = Autodesk.Windows.ComponentManager.Ribbon;
            RibbonTab Tab = new RibbonTab();
            Tab.Title = "Test Ribbon";
            Tab.Id = "TESTRIBBON_TAB_ID";

            ribbonControl.Tabs.Add(Tab);

            Autodesk.Windows.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.Image = getBitmap(EnsureRibbon.Properties.Resources.Apps, 32, 32);
            button1.LargeImage = getBitmap(EnsureRibbon.Properties.Resources.Apps, 32, 32);

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

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

        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)
                {
                    string esc = "";
                    string cmds = (string)Autodesk.AutoCAD.ApplicationServices.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 = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                    doc.SendStringToExecute(esc + button.CommandParameter,true, false, false);
                }
            }
        }
    }
}

 

 

I can probably move the code to a startup reactor or something and eliminate the multiple command issue. But I am not sure why calling cui loads a new ribbon each time. I will be grateful for any insight. The following screencast shows the issue.

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor

When your ribbon tab building code CreateSimpleButton() method starts run, it should first check if there is a ribbon tab with the same Title and/or Id exist. If yes, the code either no need to go further, or remove it and rebuild a new one.  Although each tab in ribbon has a property "Id", but there is no measure in AutoCAD to prevent tabs have duplicated Id. So, your code would look like this:

 

public void CreateSimpleButton()

{

    var ribbonControl = Autodesk.AutoCAD.Componentanager.Ribbon;

 

    // loop through all tabs to compare its name, if found, stop ribbon tab building

    If (FindExistingTab(ribbonControl, "Test Ribbon") return;  

 

    // start building new tab here

    var tab= new RibbonTab();

    ... ...

    ... ... 

}

 

private bool FindExistingTab(RibbonControl control, string tabName)

{

    ... ....

}

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5

karl.sch1983
Advocate
Advocate

Thanks! I will give that a shot.

0 Likes