Hi @norman.yuan
Here is code snippet, I'm using for creating a plugin in AutoCAD and I have highlighted the line where I'm getting null.
namespace TestPlugin1
{
public class Class1
{
[CommandMethod("LoadTestPanel", CommandFlags.NoHistory)]
public void RibbonPanelCreation()
{
var ribbonControl = ComponentManager.Ribbon;// Here I'm getting ribbon Control as NULL
AddRibbonTab(ribbonControl);
}
public void AddRibbonTab(RibbonControl ribbonControl)
{
RibbonTab ribbonTab = new RibbonTab()
{
Title = "Test",
Id = "Tes"
};
//Add the Tab
try
{
ribbonControl.Tabs.Add(ribbonTab);
}
catch (System.Exception)
{
MessageBox.Show("Ribbon Tab Not Added Successfully");
}
AddTabContent(ribbonTab);
ribbonTab.IsActive = true;
}
public void AddTabContent(RibbonTab ribbonTab)
{
ribbonTab.Panels.Add(this.AddImportFilePanel());
}
private Autodesk.Windows.RibbonPanel AddImportFilePanel()
{
var ribbonPanelSource = new RibbonPanelSource()
{
Title = "Test Panel Creation"
};
var ribbonPanel = new Autodesk.Windows.RibbonPanel()
{
Source = ribbonPanelSource
};
var importButton = CreateRibonButton("Import File", "Import\nFile", true, true, RibbonItemSize.Large, System.Windows.Controls.Orientation.Vertical, "import.png");
if (importButton != null)
{
importButton.CommandHandler = new ImportFileHandler();
ribbonPanelSource.Items.Add(importButton);
}
return ribbonPanel;
}
private RibbonButton CreateRibonButton(string buttonName, string buttonText, bool showText, bool showImage, RibbonItemSize size, System.Windows.Controls.Orientation orientation, string icon)
{
var icoUtil = new IconUtility();
var iconImage = icoUtil.GetIcon(icon);
var ribbonButton = new RibbonButton
{
Name = buttonName,
ShowText = showText,
Text = buttonText,
ShowImage = showImage,
Orientation = orientation,
Size = size,
Image = iconImage
};
if (size == RibbonItemSize.Large)
ribbonButton.LargeImage = iconImage;
return ribbonButton;
}
}
class IconUtility
{
/// <summary>
/// Method to return bitmap object of the icon
/// </summary>
/// <param name="icon">icon path</param>
/// <returns>bitmap</returns>
public BitmapImage GetIcon(string icon)
{
try
{
var assembly = Assembly.GetExecutingAssembly();
var resourcePath = assembly.GetManifestResourceNames().Single(str => str.Contains(icon));
var bitmap = new BitmapImage();
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
return bitmap;
}
catch (Exception ex)
{
MessageBox.Show("Exception in getting icon: " + ex.Message.ToString());
return new BitmapImage();
}
}
}
}
Regards,
Veeresh Huvinahalli