【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的设置及其重要性

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/878219.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

FFmpeg源码:read_packet_wrapper、fill_buffer函数分析

AVIOContext结构体和其相关的函数分析&#xff1a; FFmpeg源码&#xff1a;avio_r8、avio_rl16、avio_rl24、avio_rl32、avio_rl64函数分析 FFmpeg源码&#xff1a;read_packet_wrapper、fill_buffer函数分析 FFmpeg源码&#xff1a;avio_read函数分析 FFmpeg源码&#xff…

scrapy--图片管道-ImagesPipeline

免责声明:本文仅做演示与分享~ 目录 介绍 ImagesPipeline pipelines.py items.py zz.py settings.py 介绍 scrapy 还提供了处理图片、视频、音频等媒体文件的插件&#xff0c;如&#xff1a; - scrapy-images&#xff1a;用于下载和处理图片 - scrapy-video&#xff1…

责任链设计模式详解

责任链设计模式详解 一、定义 责任链设计模式&#xff08;Chain of Responsibility Pattern&#xff09;是一种行为设计模式&#xff0c;它允许多个对象有机会处理请求&#xff0c;从而避免请求的发送者和接收者之间的耦合。这种模式将这些对象连接成一条链&#xff0c;并沿着…

提前还房贷结果失败了该怎么办?需要注意哪些?怎么做更顺利?

提前还房贷结果失败了&#xff0c;该怎么办&#xff1f; 1. 满足条件再申请&#xff1a;部分银行对提前还款设有一定的条件和限制&#xff0c;例如需要提前预约&#xff0c;对已还款时间和还款金额也有具体的要求。如果借款人未能满足这些条件&#xff0c;提前还款的申请可能会…

【精选】计算机毕业设计之:基于springboot超市进销存系统

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…

Stable Diffusion AI绘画工具的安装与配置(MAC用户)

AI绘画的热潮席卷了整个创意行业&#xff0c;Stable Diffusion作为其中的翘楚&#xff0c;让艺术创作变得前所未有的简单。然而&#xff0c;对于使用Mac电脑用户来说&#xff0c;安装和配置Stable Diffusion可能显得有些棘手。别担心&#xff0c;这份详细的教程将手把手教你如何…

【Material-UI】Select 组件中的 `Auto width`、`Small Size` 和 `Other Props` 详解

文章目录 一、Select 组件概述1. 组件介绍2. Select 组件的基本结构 二、Auto width 属性详解1. Auto width 的作用2. Auto width 属性的基本用法3. Auto width 的实际应用场景 三、Small Size 属性详解1. Small Size 的作用2. Small Size 属性的基本用法3. Small Size 的实际应…

pytorch 数据处理

torch工具类Dataset和DataLoader 对于NN模型训练来说&#xff0c;需要将数据转换成torch识别的数据类型&#xff0c;才能喂给模型。pytorch中&#xff0c;通常使用Dataset和DataLoader这两个工具类来构建数据管道。 Dataset定义了数据集的内容&#xff0c;类似一个列表的数据…

Windows怎么让防火墙开放端口

开放端口的方法 先从控制面板,进入到Windows Defender防火墙 点击高级设置,点击入站规则 点击右边的新建规则,点击端口,点击下一步 选择协议类型和端口号点击下一步即可 查看是否开放端口成功的方法: 进入任务管

【rk3588】环境搭建及系统编译

开发板&#xff1a;ROC-RK3588S-PC 官方链接&#xff1a;Welcome to ROC-RK3588S-PC Manual — Firefly Wiki (t-firefly.com) 串口调试配置 一、产品介绍 — Firefly Wiki (t-firefly.com)&#xff0c;可以按照官方链接的说明在个人PC上使用串口。这个串口会输出rk3588的日…

【Python机器学习】NLP词频背后的含义——从词频到主题得分

目录 TF-IDF向量及词形归并 主题向量 一个思想实验 一个主题评分算法 一个LDA分类器 LDiA TF-IDF向量&#xff08;词项频率—逆文档频率向量&#xff09;可以帮助我们估算词在文本块中的重要度&#xff0c;我们使用TF-IDF向量和矩阵可以表明每个词对于文档集合中的一小段…

WHAT - 通过 react-use 源码学习 React(Side-effects 篇)

目录 一、官方介绍1. Sensors2. UI3. Animations4. Side-Effects5. Lifecycles6. State7. Miscellaneous 二、源码学习示例&#xff1a;n. xx - yySide-effects - useAsync, useAsyncFn, and useAsyncRetryuseAsyncuseAsyncFnuseAsyncRetry 一、官方介绍 Github 地址 react-u…

在vue3中封装WebSocket

下载websocket npm install websocket 或 yarn add websocket 一、新建webSockte.js文件 // webSocket.js // 自定义组合式函数&#xff0c;用于管理 WebSocket 连接 import { ref, onMounted, onBeforeUnmount } from "vue"; const useWebSocket (url, reco…

【日常记录-Linux】unzip指令

Author&#xff1a;赵志乾 Date&#xff1a;2024-08-28 Declaration&#xff1a;All Right Reserved&#xff01;&#xff01;&#xff01; 1. 简介 unzip是一个在类Unix系统(如Linux、macOS)上广泛使用的命令行工具&#xff0c;用于解压缩.zip格式的文件。.zip是一种广泛支持…

离线环境玩转 Tauri

离线环境玩转 Tauri 1. Tauri 是什么 Tauri 是一个用于构建跨平台桌面应用程序的框架&#xff0c;它允许开发者使用前端技术&#xff08;如 React、Vue、Svelte 等&#xff09;来构建桌面应用程序&#xff0c;同时提供高性能和低资源消耗的特性。 Tauri 的核心思想是使用前端…

令牌和签名详细介绍+开发使用教程

令牌和签名简介 1. 令牌&#xff08;Token&#xff09; 概念 令牌&#xff08;Token&#xff09;是一个用于身份验证的小段数据&#xff0c;通常在用户登录时由服务器生成&#xff0c;并返回给客户端。客户端在后续的请求中将令牌附加到请求头中&#xff0c;服务器通过验证令…

鸿蒙 装饰器 @State、@Prop、@Link 等说明

首先要明白什么是“状态变量”?即被状态装饰器(@State、@Prop、@Link、@Provide、@Consume)修饰的变量,比如 @State str : string=; str就是状态变量。状态变量值的改变会引起UI界面重新渲染。 @State @State装饰的变量,是私有的,只能被组件内部访问,在声明时必须指定…

计算机视觉编程 1(图片处理)

目录 灰色度 缩略图 拷贝粘贴区域 调整图像尺寸 旋转图像45 画图线、描点 灰色度 灰度是指图像中每个像素的亮度值&#xff0c;用来描述图像中各个像素的明暗程度。在计算机视觉中&#xff0c;灰度可以通过以下方式来计算&#xff1a; 1. 平均值法&#xff1a;将图像中每…

【Linux】深入探讨Linux进程等待:`waitpid`与`wait`

文章目录 深入探讨Linux进程等待&#xff1a;waitpid与wait API一、waitpid与wait简介1. wait2. waitpid 二、waitpid与wait的实际应用1. 基本用法示例2. 使用 waitpid 处理多个子进程3. 非阻塞等待 三、使用场景 深入探讨Linux进程等待&#xff1a;waitpid与wait API 在Linux…

Java基础——自学习使用(泛型)

一、泛型的定义 泛型的本质是参数化类型&#xff0c;也就是所操作的数据类型被指定为一个参数。 泛型泛指一切类型&#xff0c;能够代表一切类型&#xff0c;是一种在编程中广泛使用的概念&#xff0c;特别是在面向对象编程中。它允许在编写代码时使用类型参数&#xff0c;这些…