当前位置: 首页 > news >正文

【C#】【EXCEL】BumblebeeComponentsAnalysisGH_Ex_Ana_CondTopCount

这段代码定义了一个名为 GH_Ex_Ana_CondTopCount 的 Grasshopper 组件。它的主要功能是为 Excel 中的一个范围添加条件格式,具体是根据数值的大小高亮显示前N个(或后N个)数值。以下是该组件的详细介绍:

  1. 功能概述:

    • 组件名称:Conditional Top Count(条件性顶部计数)
    • 简称:Top Count(顶部计数)
    • 描述:根据前N个(或后N个)数值为Excel范围添加条件格式
  2. 主要参数:

    • Worksheet(工作表)和 Range(范围):指定要应用条件格式的Excel区域
    • Total(总数):要高亮显示的值的数量
    • Cell Color(单元格颜色):高亮显示的颜色
    • Flip(翻转):如果为true,则高亮显示底部的值而不是顶部
    • Clear(清除):如果为true,会先清除现有的条件格式
    • Activate(激活):如果为true,才会应用新的条件格式
  3. 工作流程:

    • 获取输入参数
    • 检查是否需要激活条件格式
    • 如果激活,则检查是否需要清除现有条件
    • 应用新的条件格式,高亮显示指定数量的顶部(或底部)值
    • 输出修改后的范围
  4. 特点:

    • 属于次要(secondary)暴露级别的组件
    • 提供了灵活的参数设置,如可选的颜色、是否翻转、是否清除现有格式等
    • 集成了Excel操作,可以直接在Grasshopper环境中修改Excel文件的格式

这个组件是Excel数据分析和可视化的有力工具,特别适用于需要快速识别数据集中最高或最低值的场景。它将Excel的条件格式功能与Grasshopper的参数化设计能力相结合,为用户提供了一种高效的数据处理方法。

Flow diagram

Yes
Yes
No
No
Start / 开始
Initialize Component / 初始化组件
Register Input Parameters / 注册输入参数
Register Output Parameters / 注册输出参数
Solve Instance / 解决实例
Activate? / 激活?
Clear? / 清除?
Clear Conditions / 清除条件
Add Conditional Top Count / 添加条件性顶部计数
Set Output Data / 设置输出数据
End / 结束

这个流程图对应代码中的主要步骤,下面是详细说明:

  1. Start / 开始: 程序开始执行。

  2. Initialize Component / 初始化组件: 对应构造函数 GH_Ex_Ana_CondTopCount()

  3. Register Input Parameters / 注册输入参数: 对应 RegisterInputParams 方法,注册所有输入参数。

  4. Register Output Parameters / 注册输出参数: 对应 RegisterOutputParams 方法,注册所有输出参数。

  5. Solve Instance / 解决实例: 对应 SolveInstance 方法,执行主要的逻辑。

  6. Activate? / 激活?: 检查是否激活条件格式,对应代码中的 if (activate)

  7. Clear? / 清除?: 如果激活,检查是否需要清除现有条件,对应 if (clear)

  8. Clear Conditions / 清除条件: 如果需要清除,执行 range.ClearConditions()

  9. Add Conditional Top Count / 添加条件性顶部计数: 添加新的条件格式,对应 range.AddConditionalTopCount(count,color, flip)

  10. Set Output Data / 设置输出数据: 设置组件的输出,对应 DA.SetData(0, range)

  11. End / 结束: 程序执行结束。

这个流程图清晰地展示了代码的主要执行路径,包括条件判断和主要操作。

Description

public class GH_Ex_Ana_CondTopCount : GH_Ex_Rng__Base
{// 构造函数public GH_Ex_Ana_CondTopCount(): base("Conditional Top Count", "Top Count","Add conditional formatting to a Range based on the top number of values",Constants.ShortName, Constants.SubAnalysis){}// 设置组件的暴露级别public override GH_Exposure Exposure{get { return GH_Exposure.secondary; }}// 注册输入参数protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager){base.RegisterInputParams(pManager);pManager[1].Optional = true;pManager.AddIntegerParameter("Total", "T", "The total number of values to highlight", GH_ParamAccess.item, 10);pManager[2].Optional = true;pManager.AddColourParameter("Cell Color", "C", "The cell highlight color", GH_ParamAccess.item, Sd.Color.LightGray);pManager[3].Optional = true;pManager.AddBooleanParameter("Flip", "F", "If true, the bottom percent will be highlighted", GH_ParamAccess.item, false);pManager[4].Optional = true;pManager.AddBooleanParameter("Clear", "_X", "If true, the existing conditions will be cleared", GH_ParamAccess.item, false);pManager[5].Optional = true;pManager.AddBooleanParameter("Activate", "_A", "If true, the condition will be applied", GH_ParamAccess.item, false);pManager[6].Optional = true;}// 注册输出参数protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager){base.RegisterOutputParams(pManager);}// 执行主要逻辑的方法protected override void SolveInstance(IGH_DataAccess DA){// ... (代码省略,下面会详细解释)}// 提供组件图标protected override System.Drawing.Bitmap Icon{get{return Properties.Resources.BB_Cond_Count_01;}}// 获取组件的唯一IDpublic override Guid ComponentGuid{get { return new Guid("ac420577-b618-40a8-91e7-5bfc5ae0708a"); }}
}

现在,让我们详细解释每个方法:

  1. 构造函数 (Constructor)
public GH_Ex_Ana_CondTopCount(): base("Conditional Top Count", "Top Count","Add conditional formatting to a Range based on the top number of values",Constants.ShortName, Constants.SubAnalysis)
{
}
  • 说明:这是组件的构造函数。它设置了组件的名称、简称和描述。
  • 初始化"条件性顶部计数"组件,设置其基本信息。
  • Initializes the “Conditional Top Count” component, setting its basic information.
  1. Exposure 属性
public override GH_Exposure Exposure
{get { return GH_Exposure.secondary; }
}
  • 说明:这个属性定义了组件在Grasshopper界面中的暴露级别。
  • 将组件设置为次要暴露级别,这意味着它不会在主菜单中直接显示,但可以通过子菜单访问。
  • Sets the component to secondary exposure level, meaning it won’t be directly visible in the main menu but can be accessed through a submenu.
  1. RegisterInputParams 方法
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{base.RegisterInputParams(pManager);pManager[1].Optional = true;pManager.AddIntegerParameter("Total", "T", "The total number of values to highlight", GH_ParamAccess.item, 10);// ... (其他参数注册)
}
  • 说明:这个方法注册了组件的所有输入参数。
  • 注册输入参数,包括总数、单元格颜色、是否翻转、是否清除现有条件、是否激活等。每个参数都有名称、简称、描述和默认值。
  • Registers input parameters including total count, cell color, flip option, clear option, and activate option. Each parameter has a name, nickname, description, and default value.
  1. RegisterOutputParams 方法
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{base.RegisterOutputParams(pManager);
}
  • 说明:这个方法注册了组件的输出参数。
  • 调用基类方法注册标准输出参数(通常是修改后的Excel范围)。
  • Calls the base class method to register standard output parameters (typically the modified Excel range).
  1. SolveInstance 方法
protected override void SolveInstance(IGH_DataAccess DA)
{// ... (详细代码在下面解释)
}
  • 说明:这是执行组件主要逻辑的方法。
  • 处理输入数据,应用条件格式,并设置输出。
  • Processes input data, applies conditional formatting, and sets the output.
  1. Icon 属性
protected override System.Drawing.Bitmap Icon
{get{return Properties.Resources.BB_Cond_Count_01;}
}
  • 说明:这个属性提供了组件的图标。
  • 返回组件在Grasshopper界面中显示的图标。
  • Returns the icon displayed for the component in the Grasshopper interface.
  1. ComponentGuid 属性
public override Guid ComponentGuid
{get { return new Guid("ac420577-b618-40a8-91e7-5bfc5ae0708a"); }
}
  • 说明:这个属性返回组件的唯一标识符。
  • 为组件提供一个唯一的GUID,用于在Grasshopper中识别此组件。发布后不应更改此GUID。
  • Provides a unique GUID for the component, used to identify it within Grasshopper. This GUID should not be changed after publication.

这些方法和属性共同定义了 GH_Ex_Ana_CondTopCount 组件的行为和外观。在您的博客中,您可以重点讨论每个方法的作用,以及它们如何协同工作来创建一个功能完整的Grasshopper组件。特别是 SolveInstance 方法,它包含了组件的核心逻辑,值得详细解释其工作原理。

Code

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
{// 定义一个Grasshopper组件,用于在Excel中添加条件性顶部计数格式public class GH_Ex_Ana_CondTopCount : GH_Ex_Rng__Base{/// <summary>/// 初始化 GH_Ex_An_CondTopCount 类的新实例。/// </summary>public GH_Ex_Ana_CondTopCount(): base("Conditional Top Count", "Top Count","为Range添加基于顶部数值数量的条件格式",Constants.ShortName, Constants.SubAnalysis){}/// <summary>/// 设置组件的暴露级别。/// </summary>public override GH_Exposure Exposure{// 将组件设置为次要暴露级别,使其在子菜单中显示get { return GH_Exposure.secondary; }}/// <summary>/// 注册此组件的所有输入参数。/// </summary>protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager){// 调用基类方法注册基本参数base.RegisterInputParams(pManager);pManager[1].Optional = true;// 添加整数参数:要高亮显示的值的总数pManager.AddIntegerParameter("Total", "T", "要高亮显示的值的总数", GH_ParamAccess.item, 10);pManager[2].Optional = true;// 添加颜色参数:单元格高亮颜色pManager.AddColourParameter("Cell Color", "C", "单元格高亮颜色", GH_ParamAccess.item, Sd.Color.LightGray);pManager[3].Optional = true;// 添加布尔参数:是否高亮显示底部百分比pManager.AddBooleanParameter("Flip", "F", "如果为真,将高亮显示底部百分比", GH_ParamAccess.item, false);pManager[4].Optional = true;// 添加布尔参数:是否清除现有条件pManager.AddBooleanParameter("Clear", "_X", "如果为真,将清除现有条件", GH_ParamAccess.item, false);pManager[5].Optional = true;// 添加布尔参数:是否应用条件pManager.AddBooleanParameter("Activate", "_A", "如果为真,将应用条件", GH_ParamAccess.item, false);pManager[6].Optional = true;}/// <summary>/// 注册此组件的所有输出参数。/// </summary>protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager){// 调用基类方法注册标准输出参数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;// 获取要高亮显示的值的数量int count = 10;DA.GetData(2, ref count);// 获取高亮颜色Sd.Color color = Sd.Color.LightGray;DA.GetData(3, ref color);// 获取是否翻转(高亮底部值)bool flip = false;DA.GetData(4, ref flip);// 获取是否清除现有条件bool clear = false;DA.GetData(5, ref clear);// 获取是否激活条件bool activate = false;DA.GetData(6, ref activate);// 如果激活,应用条件格式if (activate){if (clear) range.ClearConditions(); // 如果需要,清除现有条件range.AddConditionalTopCount(count, color, flip); // 添加条件性顶部计数格式}// 设置输出数据DA.SetData(0, range);}/// <summary>/// 提供组件的图标。/// </summary>protected override System.Drawing.Bitmap Icon{get{// 返回组件的自定义图标return Properties.Resources.BB_Cond_Count_01;}}/// <summary>/// 获取此组件的唯一ID。发布后请勿更改此ID。/// </summary>public override Guid ComponentGuid{get { return new Guid("ac420577-b618-40a8-91e7-5bfc5ae0708a"); }}}
}

这些注释详细解释了代码的每个部分,包括:

  1. 类的整体功能
  2. 构造函数的作用
  3. 各种属性和方法的用途
  4. 输入参数的含义和用途
  5. SolveInstance 方法中的主要逻辑步骤
  6. 图标和GUID的重要性

这些注释不仅有助于理解代码的结构和功能,还为您的博客文章提供了丰富的解释材料。可以根据这些注释进一步展开,解释每个部分如何contribute到整个组件的功能中,以及如何在实际应用中使用这个组件来处理Excel数据。


http://www.mrgr.cn/news/11972.html

相关文章:

  • string的模拟实现与深浅拷贝
  • JVM入门
  • react面试题八
  • 条件拼接 - 根据入参生成where条件
  • 推荐一个完全自由的目录设计网站
  • 自动化脚本到LabVIEW转换
  • 一起学习LeetCode热题100道(56/100)
  • javascript怎么实现队列?
  • 739. 每日温度
  • 48.x86游戏实战-封包抓取进图call
  • 在NVIDIA Jetson AGX Orin中使用jetson-ffmpeg调用硬件编解码加速处理
  • DataWhale AI夏令营-《李宏毅深度学习教程》笔记
  • [C++番外] 抛异常
  • 【论文阅读】NGD-SLAM: Towards Real-Time SLAM for Dynamic Environments without GPU
  • redis基础与进阶(二)
  • 【Linux C++】log4cpp日志库的安装和使用详解
  • wpf livechart 绘制笛卡尔曲线
  • 【LabVIEW子vi引用或者赋值】
  • 【应用开发】解决正点原子I.MX6ull应用编程zlib移植问题
  • 零基础5分钟上手亚马逊云科技 - AI模型内容安全过滤