The act of adding a resource might be simple (although not in my personal experience), but the deluge of files and bouncing references around that goes on behind the scene is intimidating.
Its a slightly different way but basicly the same. You can add a resource item the same way you add a new class file etc. Then you can just reference it from the code.
Here is the code that finally worked from the .cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
namespace Plugin_1_Day_3
{
public class Averages : GH_Component
{
/// <summary>
/// Each implementation of GH_Component must provide a public
/// constructor without any arguments.
/// Category represents the Tab in which the component will appear,
/// Subcategory the panel. If you use non-existing tab or panel names,
/// new tabs/panels will automatically be created.
/// </summary>
public Averages()
: base("Averages", "Computes Averages",
"Workshop Test Component - Averages",
"Workshop", "Utilities")
{
}
/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddNumberParameter("first num", "first", "the first num", GH_ParamAccess.item, 0.0);
pManager.AddNumberParameter("second num", "second", "the second num", GH_ParamAccess.item, 0.0);
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Average", "Average", "average of inputs", GH_ParamAccess.item);
}
/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object can be used to retrieve data from input parameters and
/// to store data in output parameters.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
var nums = new List<double>();
double a = double.NaN;
double b = double.NaN;
nums.Add(a);
nums.Add(b);
DA.GetData(0, ref a);
DA.GetData(1, ref b);
double average = 0.5 * (a + b);
DA.SetData(0, average);
}
/// <summary>
/// Provides an Icon for every component that will be visible in the User Interface.
/// Icons need to be 24x24 pixels.
/// You can add image files to your project resources and access them like this:
/// return Resources.IconForThisComponent;
/// </summary>
//protected override System.Drawing.Bitmap Icon => null;
protected override System.Drawing.Bitmap Icon
{
get
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
{
var resourceName = assembly.GetManifestResourceNames().Single(n => n.EndsWith("icon.png"));
var stream = assembly.GetManifestResourceStream(resourceName);
if (stream != null) return new Bitmap(stream);
}
return null;
}
}
/// <summary>
/// Each component must have a unique Guid to identify it.
/// It is vital this Guid doesn't change otherwise old ghx files
/// that use the old ID will partially fail during loading.
/// </summary>
public override Guid ComponentGuid => new Guid("6CBE441F-4367-4146-9755-BF7296D4BFE1");
}
}
For those who might benefit from a visual explanation over a written one, I have found this video quite helpful for when I was first learning how to add icons.
Hello, it seems like the way of adding resources has now changed and I cannot get it to work.
When I go to Properties>Resources I no longer get the page to add resources, but a warning that this has changed and a link to the new location to do this.
I typically use a different route for embedding my icons in GHAs. I use the syntax as shown here for telling a project to embed all pngs from a certain directory
I then access these resources from code with the following syntax
thats great thank you! this way worked well, maybe it could be added to the creating a plugin documentation? seeing this before would have saved me a lot of headaches