Getting ComponentManager.Ribbon as null for AutoCAD 2020

Getting ComponentManager.Ribbon as null for AutoCAD 2020

veeresh.huvinahalliACK3L
Enthusiast Enthusiast
1,448 Views
5 Replies
Message 1 of 6

Getting ComponentManager.Ribbon as null for AutoCAD 2020

veeresh.huvinahalliACK3L
Enthusiast
Enthusiast

I was trying to create the custom .net autocad plugin, I'm getting ComponentManager.Ribbon as null currently, previously I tried with same line of code, I was able to get RibbonControl Object but it not working now, Can anybody suggest why these is actually happening?

0 Likes
1,449 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

You should indicate/describe when/how/where your code tries to have access to ComponentManager.Ribbon, ideally by showing your code relevant to the issue.

 

Without knowing the details from you, it is just a wild speculation that your code is loaded and runs at the moment when AutoCAD has not finished its startup, especially the ribbon menu has not been fully loaded. It all depends on how/when your DLL is loaded and how the code runs (as IExtensionApplication, or runs manually...).

 

Again, when you ask question, try provide as much as information/details possible, if you really want to get meaningful responses.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

veeresh.huvinahalliACK3L
Enthusiast
Enthusiast

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

 

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

How do you run the command "LoadTestPanel"? Do you run in manually when AutoCAD fully loaded and there is ribbon menu showing? Or you somehow execute the command automatically when/during AutoCAD startup. I mean, if you run the command manually, do that line of code still give you a null pointer to the ComponentManager.Ribbon?

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 6

veeresh.huvinahalliACK3L
Enthusiast
Enthusiast

Hi @norman.yuan ,

 

Yes, I have tried manually and I tried executing the command automatically using "PackageContents.xml".

 

<?xml version="1.0" encoding="utf-8"?>
<ApplicationPackage SchemaVersion="1.0" AutodeskProduct="AutoCAD" Name="TestPlugin1" Description="TestPlugin1" AppVersion="1.0.0" ProductType="Application" Author="TestPlugin1" ProductCode="{FECE6978-C7A1-4381-BF7E-71AD8E213AC4}" UpgradeCode="{FBB07B46-118D-4B8D-8F76-58C257F95F68}">
<CompanyDetails Name="TestPlugin1" Email="" Url="" />

<Components>
<RuntimeRequirements OS="Win64" Platform="AutoCAD*" SeriesMin="R23.1" SeriesMax="R23.1" />
<ComponentEntry AppName="TestPlugin1" ModuleName=".\Contents\Class1.dll">
<Commands GroupName="LoadTest">
<Command Local="LoadTestPanel" Global="LoadTestPanel" StartupCommand="True" />
</Commands>
</ComponentEntry>
</Components>

</ApplicationPackage>

0 Likes
Message 6 of 6

norman.yuan
Mentor
Mentor

Are you saying that even you run the command manually, that line of code still gives you null for ComponentManager.Ribbon? Sorry, you did not make it very clear. I can see it is problematic when you make this command automatically run as StartupCommand: If you want to create custom ribbon item with code on AutoCAD startup, you MUST make sure the RibbonControl has been created by AutoCAD before your code goes ahead to add custom items. The common practice for this task is to handle AutoCAD's Idle event when your DLL loads (say, add Idle event handler in IExtensionApplication.Initialize()). And then in the event handler, test if RibbonControl is null. This topic has been discussed here quite a few times, such as this:

 

https://forums.autodesk.com/t5/net/custom-menu-and-ribbon-with-code/m-p/10511529#M69850 

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes