【C#】【EXCEL】BumblebeeComponentsAnalysisGH_Ex_Ana_CondScale.cs

时间:2025-05-12 07:59:27
using Grasshopper.Kernel; using Grasshopper.Kernel.Parameters; using Grasshopper.Kernel.Types; using Rhino.Geometry; using System; using System.Collections.Generic; using Sd = System.Drawing; namespace Bumblebee.Components { /// <summary> /// GH_Ex_Ana_CondScale 类定义了一个 Grasshopper 组件, /// 用于在 Excel 范围内添加基于相对值的条件格式颜色。 /// </summary> public class GH_Ex_Ana_CondScale : GH_Ex_Rng__Base { /// <summary> /// 初始化 GH_Ex_Ana_CondScale 类的新实例。 /// </summary> public GH_Ex_Ana_CondScale() : base("Conditional Scale", "Scale", "Add conditional formatting colors to a Range based on relative values", Constants.ShortName, Constants.SubAnalysis) { } /// <summary> /// 设置组件的曝光级别。 /// GH_Exposure.secondary 表示这是一个次要的或不太常用的组件。 /// </summary> public override GH_Exposure Exposure { get { return GH_Exposure.secondary; } } /// <summary> /// 注册所有的输入参数。 /// </summary> /// <param name="pManager">用于添加和配置输入参数的参数管理器</param> protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) { // 调用基类方法注册基本参数(通常是工作表和范围) base.RegisterInputParams(pManager); // 设置第二个参数为可选 pManager[1].Optional = true; // 添加中点参数(用于三色渐变) pManager.AddNumberParameter("Parameter", "P", "The parameter of the midpoint of a 3 color gradient", GH_ParamAccess.item, 0.5); pManager[2].Optional = true; // 添加第一个渐变颜色 pManager.AddColourParameter("Gradient Color 1", "C0", "The first color of the gradient", GH_ParamAccess.item, Sd.Color.LightGray); pManager[3].Optional = true; // 添加第二个渐变颜色 pManager.AddColourParameter("Gradient Color 2", "C1", "The second color of the gradient", GH_ParamAccess.item, Sd.Color.Gray); pManager[4].Optional = true; // 添加第三个渐变颜色(可选) pManager.AddColourParameter("Gradient Color 3", "C2", "The third color of the gradient", GH_ParamAccess.item); pManager[5].Optional = true; // 添加清除现有条件的布尔参数 pManager.AddBooleanParameter("Clear", "_X", "If true, the existing conditions will be cleared", GH_ParamAccess.item, false); pManager[6].Optional = true; // 添加激活条件的布尔参数 pManager.AddBooleanParameter("Activate", "_A", "If true, the condition will be applied", GH_ParamAccess.item, false); pManager[7].Optional = true; } /// <summary> /// 注册所有的输出参数。 /// </summary> /// <param name="pManager">用于添加和配置输出参数的参数管理器</param> protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) { // 调用基类方法注册标准输出(通常是修改后的 Excel 范围) base.RegisterOutputParams(pManager); } /// <summary> /// 这是执行实际工作的方法。 /// </summary> /// <param name="DA">用于检索输入和存储输出的 DA 对象</param> protected override void SolveInstance(IGH_DataAccess DA) { // 获取工作表输入 IGH_Goo gooS = null; DA.GetData(0, ref gooS); ExWorksheet worksheet = new ExWorksheet(); bool hasWs = gooS.TryGetWorksheet(ref worksheet); // 获取范围输入 IGH_Goo gooR = null; DA.GetData(1, ref gooR); ExRange range = new ExRange(); if (!gooR.TryGetRange(ref range, worksheet)) return; if (!hasWs) worksheet = range.Worksheet; // 获取中点参数 double mid = 0.5; DA.GetData(2, ref mid); // 获取第一个颜色 Sd.Color a = Sd.Color.LightGray; DA.GetData(3, ref a); // 获取第二个颜色 Sd.Color b = Sd.Color.Gray; DA.GetData(4, ref b); // 获取第三个颜色(如果提供) Sd.Color c = Sd.Color.DarkGray; bool isThree = DA.GetData(5, ref c); // 获取清除标志 bool clear = false; DA.GetData(6, ref clear); // 获取激活标志 bool activate = false; DA.GetData(7, ref activate); // 如果激活,应用条件格式 if (activate) { // 如果需要,清除现有条件 if (clear) range.ClearConditions(); // 根据是否为三色渐变添加相应的条件格式 if (isThree) { range.AddConditionalScale(a,b,c,mid); } else { range.AddConditionalScale(a, b); } } // 设置输出(修改后的范围) DA.SetData(0, range); } /// <summary> /// 提供组件的图标。 /// </summary> protected override System.Drawing.Bitmap Icon { get { // 返回组件的图标 return Properties.Resources.BB_Cond_Scale_01; } } /// <summary> /// 获取此组件的唯一 ID。发布后不要更改此 ID。 /// </summary> public override Guid ComponentGuid { get { return new Guid("7c251907-0e3b-4c57-b948-98ea7c76b68f"); } } } }