【C#】【EXCEL】Bumblebee/Components/Analysis/GH_Ex_Ana_CondAverage.cs

本文主要是介绍【C#】【EXCEL】Bumblebee/Components/Analysis/GH_Ex_Ana_CondAverage.cs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Bumblebee/Components/Analysis/GH_Ex_Ana_CondAverage.cs

这段代码定义了一个名为 GH_Ex_Ana_CondAverage 的类,它是一个 Grasshopper 组件。这个组件的主要功能是为 Excel 工作表中的一个范围添加基于平均值的’条件格式’。以下是对这个组件的功能和特点的详细介绍:

  1. 组件名称和描述:

    • 名称:Conditional Average(条件平均值)
    • 描述:基于值的平均值为 Excel 范围添加条件格式
  2. 输入参数:

    • Worksheet(工作表)
    • Range(范围)
    • Type(条件类型):定义如何应用条件格式(例如,高于平均值、低于平均值等)
    • Cell Color(单元格颜色):用于高亮显示的颜色
    • Clear(清除):是否清除现有的条件格式
    • Activate(激活):是否应用新的条件格式
  3. 输出:

    • 处理后的 Range 对象
  4. 主要功能:

    • 获取指定的 Excel 工作表和范围
    • 根据用户输入的条件类型和颜色,为指定范围添加基于平均值的条件格式
    • 可以选择性地清除现有的条件格式
    • 只有在 Activate 参数为 true 时才执行操作
  5. 特殊特性:

    • 使用枚举类型 AverageCondition 来定义不同的条件类型
    • 组件的暴露级别设置为次要(secondary)
    • 包含自定义图标
  6. 集成性:

    • 这个组件是一个更大的系统( Bumblebee)的一部分,专门用于在 Grasshopper 中处理 Excel 数据

总的来说,这个组件提供了一种在 Grasshopper 环境中直接操作 Excel 数据的方法,特别是添加基于平均值的条件格式。这对于需要在参数化设计过程中分析和可视化 Excel 数据的用户来说非常有用。它简化了 Excel 数据处理的流程,使用户能够直接在 Grasshopper 中完成通常需要在 Excel 中手动执行的操作。

Yes / 是
Yes / 是
No / 否
No / 否
Start / 开始
Initialize Component / 初始化组件
Register Inputs / 注册输入参数
Get Worksheet / 获取工作表
Get Range / 获取范围
Get Condition Type / 获取条件类型
Get Cell Color / 获取单元格颜色
Get Clear Flag / 获取清除标志
Get Activate Flag / 获取激活标志
Activate? / 是否激活?
Clear? / 是否清除?
ClearConditions / 清除条件
AddConditionalAverage / 添加条件平均值
Set Output / 设置输出
End / 结束

这个流程图对应到代码中的主要步骤如下:

  1. Start / 开始:对应构造函数 GH_Ex_Ana_CondAverage()
  2. Initialize Component / 初始化组件:在构造函数中完成
  3. Register Inputs / 注册输入参数:对应 RegisterInputParams 方法
  4. Get Worksheet / 获取工作表:DA.GetData(0, ref gooS);
  5. Get Range / 获取范围:DA.GetData(1, ref gooR);
  6. Get Condition Type / 获取条件类型:DA.GetData(2, ref type);
  7. Get Cell Color / 获取单元格颜色:DA.GetData(3, ref color);
  8. Get Clear Flag / 获取清除标志:DA.GetData(4, ref clear);
  9. Get Activate Flag / 获取激活标志:DA.GetData(5, ref activate);
  10. Activate? / 是否激活?:if (activate)
  11. Clear? / 是否清除?:if (clear)
  12. ClearConditions / 清除条件:range.ClearConditions();
  13. AddConditionalAverage / 添加条件平均值:range.AddConditionalAverage((AverageCondition)type, color);
  14. Set Output / 设置输出:DA.SetData(0, range);
  15. End / 结束:方法结束

这个流程图展示了组件的主要执行步骤,从初始化到获取输入参数,然后根据条件执行相应的操作,最后设置输出。表示了代码的逻辑流程,特别是条件判断和相应的操作。

Description

  1. 构造函数
public GH_Ex_Ana_CondAverage(): base("Conditional Average", "Average","Add conditional formatting to a Range based on the average of the values",Constants.ShortName, Constants.SubAnalysis)
{
}

这是组件的构造函数。它调用基类的构造函数,设置组件的名称、昵称、描述和分类。
// This is the constructor for the component. It calls the base class constructor to set the component’s name, nickname, description, and category.

  1. Exposure 属性
public override GH_Exposure Exposure
{get { return GH_Exposure.secondary; }
}

这个属性设置组件在Grasshopper界面中的显示级别为次要。这意味着该组件不会在主菜单中直接显示,而是在子菜单中。
// This property sets the exposure level of the component in the Grasshopper interface to secondary. This means the component won’t be directly visible in the main menu, but in a submenu.

  1. RegisterInputParams 方法
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{base.RegisterInputParams(pManager);pManager[1].Optional = true;pManager.AddIntegerParameter("Type", "T", "The condition type", GH_ParamAccess.item, 0);pManager[2].Optional = true;// ... (其他参数注册)
}

这个方法注册组件的输入参数。它首先调用基类的方法,然后添加额外的参数如条件类型、单元格颜色等。每个参数都有一个名称、简称、描述和默认值。
// This method registers the input parameters for the component. It first calls the base class method, then adds additional parameters such as condition type, cell color, etc. Each parameter has a name, nickname, description, and default value.

  1. RegisterOutputParams 方法
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{base.RegisterOutputParams(pManager);
}

这个方法注册组件的输出参数。在这个例子中,它只是调用基类的方法,没有添加额外的输出。
// This method registers the output parameters for the component. In this case, it only calls the base class method without adding any additional outputs.

  1. SolveInstance 方法
protected override void SolveInstance(IGH_DataAccess DA)
{// ... (获取输入数据)if (activate){if (clear) range.ClearConditions();range.AddConditionalAverage((AverageCondition)type, color);}DA.SetData(0, range);
}

这是组件的核心方法,执行主要的逻辑。
// This is the core method of the component, executing the main logic.
它首先获取所有输入数据,然后根据条件执行操作:

  1. 如果激活标志为真,它会:
    a. 如果清除标志为真,清除现有的条件格式
    b. 添加新的基于平均值的条件格式

  2. 最后,它设置输出数据(处理后的范围对象)
    // It first retrieves all input data, then performs operations based on conditions:
    // 1. If the activate flag is true, it will:
    // a. Clear existing conditional formatting if the clear flag is true
    // b. Add new conditional formatting based on average
    // 2. Finally, it sets the output data (the processed range object)

  3. Icon 属性

protected override System.Drawing.Bitmap Icon
{get{return Properties.Resources.BB_Cond_Average_01;}
}

这个属性返回组件的图标。它使用预定义的资源图片。
// This property returns the icon for the component. It uses a predefined resource image.

  1. ComponentGuid 属性
public override Guid ComponentGuid
{get { return new Guid("b2854323-ef9f-470f-9f52-1d80fa90fb2a"); }
}

这个属性返回组件的唯一标识符。这个GUID在组件发布后不应更改,用于在Grasshopper中唯一标识这个组件。
// This property returns the unique identifier for the component. This GUID should not be changed after the component is released, as it’s used to uniquely identify this component in Grasshopper.

总结:
这个组件展示了如何创建一个自定义的Grasshopper组件,特别是用于Excel数据处理。它利用了面向对象编程的继承特性,重写了基类的多个方法来定制组件的行为。通过这种方式,它实现了在Grasshopper环境中直接操作Excel数据的功能,为用户提供了强大而灵活的数据处理工具。
// In summary, this component demonstrates how to create a custom Grasshopper component, especially for Excel data processing. It utilizes object-oriented programming inheritance features, overriding multiple base class methods to customize the component’s behavior. Through this approach, it implements functionality to directly manipulate Excel data within the Grasshopper environment, providing users with a powerful and flexible data processing tool.

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
{// 定义一个条件平均值组件类,继承自基础范围组件public class GH_Ex_Ana_CondAverage : GH_Ex_Rng__Base{/// <summary>/// 初始化 GH_Ex_An_CondAverage 类的新实例。/// </summary>public GH_Ex_Ana_CondAverage(): base("Conditional Average", "Average","Add conditional formatting to a Range based on the average of the values",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);// 设置第二个参数(索引1)为可选pManager[1].Optional = true;// 添加整数参数用于条件类型pManager.AddIntegerParameter("Type", "T", "The condition type", GH_ParamAccess.item, 0);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("Clear", "_X", "If true, the existing conditions will be cleared", GH_ParamAccess.item, false);pManager[4].Optional = true;// 添加布尔参数用于激活条件pManager.AddBooleanParameter("Activate", "_A", "If true, the condition will be applied", GH_ParamAccess.item, false);pManager[5].Optional = true;// 为条件类型参数添加命名值Param_Integer paramA = (Param_Integer)pManager[2];foreach (AverageCondition value in Enum.GetValues(typeof(AverageCondition))){paramA.AddNamedValue(value.ToString(), (int)value);}}/// <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 type = 0;DA.GetData(2, ref type);// 获取单元格颜色Sd.Color color = Sd.Color.LightGray;DA.GetData(3, ref color);// 获取清除标志bool clear = false;DA.GetData(4, ref clear);// 获取激活标志bool activate = false;DA.GetData(5, ref activate);// 如果激活,执行条件格式操作if (activate){// 如果需要清除,先清除现有条件if (clear) range.ClearConditions();// 添加新的条件平均值格式range.AddConditionalAverage((AverageCondition)type, color);}// 设置输出数据DA.SetData(0, range);}/// <summary>/// 提供组件的图标。/// </summary>protected override System.Drawing.Bitmap Icon{get{// 返回组件的图标return Properties.Resources.BB_Cond_Average_01;}}/// <summary>/// 获取此组件的唯一ID。发布后不要更改此ID。/// </summary>public override Guid ComponentGuid{// 返回组件的唯一标识符get { return new Guid("b2854323-ef9f-470f-9f52-1d80fa90fb2a"); }}}
}

这段代码现在包含了详细的中文注释,解释了每个方法和属性的功能。这些注释涵盖了以下几个方面:

  1. 类的整体功能和继承关系
  2. 构造函数的作用
  3. 各个方法(如RegisterInputParams、SolveInstance等)的具体功能
  4. 输入参数的设置和含义
  5. 主要逻辑流程(在SolveInstance方法中)
  6. 图标和GUID的设置及其重要性

这篇关于【C#】【EXCEL】Bumblebee/Components/Analysis/GH_Ex_Ana_CondAverage.cs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1107459

相关文章

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

C#文件复制异常:"未能找到文件"的解决方案与预防措施

《C#文件复制异常:未能找到文件的解决方案与预防措施》在C#开发中,文件操作是基础中的基础,但有时最基础的File.Copy()方法也会抛出令人困惑的异常,当targetFilePath设置为D:2... 目录一个看似简单的文件操作问题问题重现与错误分析错误代码示例错误信息根本原因分析全面解决方案1. 确保

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点