Hi, I’m a newbie in scripting in Visual Studio. I’ve done a very basic Centeroid Plugin but I can’t add the plugin icon. I wrote the resource.$(imageName) then i get error CS0029Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type ‘byte[]’ to ‘System.Drawing.Bitmap’ Att1_S (net48) C:\Users\szeng\source\repos\Att1_S\Att1_SComponent.cs 86 Active".
In addition to this, I cannot debug any code with .net 7. I could only debug with .net 4.8 Framework. Every time I debugged the project I always got an error and .gha file dropped in a “net48” folder in my projects debug folder.
I have attached the full list of errors and warnings below
PS: I don’t know what file format or information I need to include to help me, so please write if I need to include anything extra.
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
namespace Att1_S
{
public class Att1_SComponent : 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 Att1_SComponent()
: base("Centerroid", "Cntrd",
"Centerroid",
"Workshop", "Geometry")
{
}
/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddPointParameter("Points", "P", "Points", GH_ParamAccess.list);
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddPointParameter("Centerroid", "C", "Centerroid", GH_ParamAccess.item);
pManager.AddNumberParameter("Distances", "D", "Distances", GH_ParamAccess.list);
}
/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
List<Point3d> points = new List<Point3d>();
DA.GetDataList(0, points);
Point3d centerroid = new Point3d(0, 0, 0);
for (int i = 0; i < points.Count; i++)
{
centerroid += points[i];
}
centerroid /= points.Count;
List<double> distances = new List<double>();
for (int i = 0; i < points.Count; i++)
{
distances.Add(centerroid.DistanceTo(points[i]));
}
DA.SetData(0, centerroid);
DA.SetDataList(1, distances);
}
/// <summary>
/// The Exposure property controls where in the panel a component icon
/// will appear. There are seven possible locations (primary to septenary),
/// each of which can be combined with the GH_Exposure.obscure flag, which
/// ensures the component will only be visible on panel dropdowns.
/// </summary>
public override GH_Exposure Exposure => GH_Exposure.primary;
/// <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 { get { return Properties.Resources.denme; } }
/// <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("1600ff72-ff0d-46c6-8213-ec0b046d08c8");
}
}
what happens when you add the Nuget Package System.Drawing.Common
as described here ?
(a missing package would explain the error message and the different error for Framework 4.8 vs .Net 7)
I’ve discovered that by selecting Rhino 7 (with .Net Framework 4.8) in the Debug section during project creation, I can successfully debug and place my icon without the issues I was facing. Unfortunately, choosing Rhino 8 initially triggers these problems.
In the meantime, I am attaching the zip files I sent to Mr Stevenson. Perhaps there is something I have not explained or overlooked.
Thank you very much, it works when I add the .ico file but I still can’t understand why I can add my image in the rhino 7 debug project but not in the rhino 8 debug project.
@Senih .Net Core handles the storing of images (apparently, I just learned this) as byte[] instead of as Bitmap type. Which you can see if we look at the generated code-behind file of the Resources for each of your two projects (.Net Framework/.Net Core):
.Net Framework returns Bitmap:
.Net Core returns Byte[]
So you will have to handle the image retrieval differently in the component class, here was your code that wasn’t working:
So if you instead modify the code as follows, it should work as expected:
NOTE: I had to modify the build step that copied the .gha so that it would work on my file system. I also recommend you wrap the build output parameter in double quotes, in case you run the solution from a path that contains spaces in the file path.
Here was the result after starting Rhino 8 with your component:
Thank you very much for your detailed response and getting back to me. Since I still cannot understand the logic of Rhino 8 with .Net Core (I am not very experienced in scripting), I will probably continue to improve with Rhino 7 and .Net Framework. I also want to thank you for the additional information in the note.
I remember you can set the default preference somewhere, as all the current projects i’m working on defaults to bitmap, but i don’t rember how i did that
Hi!
Sorry for the late reply, but I’ll definitely take a look at it. However, I have found the solution to the problem I mentioned the other day by moving forward with the .Net Framework. I would be happy to work with .Net Core for Rhino 8 if that would work in your direction.
Hi, I had the same problem and your suggestion solved it but I get this warning: This call site is reachable on all platforms. ‘Bitmap’ is only supported on: ‘windows’. (CODE: CA1416) Do you happen to know how to solve it?
I don’t have experience developing for the Mac platform, but I suspect you could do a couple of things in your code, so that you can return code that functions in each environment:
if (OperatingSystem.IsWindows())
{
Bitmap bmp = new Bitmap(width, height);
// ... your drawing code
}
else
{
// Handle differently for Mac (return null or some alternative)
}
And then maybe using something like SkiaSharp for your Mac specific logic.
If I’m misunderstanding, post a new Topic, with more specifics and tag me and I’ll see if I can better help you.