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

这段代码定义了一个名为 GH_Ex_Ana_CondBar 的 Grasshopper 组件,其主要功能是为 Excel 工作表中的特定范围添加条件格式条形。以下是该组件的主要功能和特点:

  1. 功能概述:

    • 为 Excel 中的数据范围添加基于相对值的条件格式条形
    • 允许用户自定义条形的颜色
    • 提供选项来使用渐变色或纯色条形
    • 可以清除现有的条件格式
    • 可以控制是否激活条件格式
  2. 输入参数:

    • 工作表数据
    • 范围数据
    • 单元格颜色
    • 是否使用渐变
    • 是否清除现有条件
    • 是否激活条件格式
  3. 输出:

    • 处理后的 Excel 范围对象
  4. 组件特性:

    • 作为次要组件显示在 Grasshopper 界面中

    • 具有自定义图标

    • 有唯一的组件标识符(GUID)

      组件标识符(GUID)在 Grasshopper 插件开发中有几个重要的目的:

      1. 唯一标识:
        // Unique Identification
        GUID 确保每个组件在 Grasshopper 环境中都有一个唯一的标识符。这对于区分不同的组件至关重要,即使它们可能有相似的名称或功能。

      2. 版本控制:
        // Version Control
        当组件更新时,保持相同的 GUID 允许 Grasshopper 正确地更新现有文档中的组件实例,而不会丢失连接或数据。

      3. 文档兼容性:
        // Document Compatibility
        GUID 帮助确保使用特定组件创建的 Grasshopper 文档可以在不同的机器上正确打开和运行,只要安装了相同的插件版本。

      4. 序列化和反序列化:
        // Serialization and Deserialization
        在保存和加载 Grasshopper 文档时,GUID 用于正确地序列化和反序列化组件,确保文档能够准确地重建。

      5. 插件管理:
        // Plugin Management
        Grasshopper 使用 GUID 来管理插件和组件库。它有助于避免命名冲突,并允许系统正确地加载和识别不同的组件。

      6. 依赖关系处理:
        // Dependency Handling
        当一个组件依赖于另一个组件时,GUID 可以用来明确指定这些依赖关系,确保正确的组件版本被使用。

      7. 向后兼容性:
        // Backward Compatibility
        在更新插件时,保持相同的 GUID 可以确保旧版本的 Grasshopper 文档仍然可以识别和使用更新后的组件。

      8. 防止冲突:
        // Conflict Prevention
        不同开发者创建的组件可能有相似的名称,但 GUID 确保它们不会相互干扰或被错误识别。

      9. 许可和验证:
        // Licensing and Validation
        在某些情况下,GUID 可以用于许可验证,确保只有授权的组件被使用。

      10. 调试和错误报告:
        // Debugging and Error Reporting
        在调试过程中,GUID 可以帮助开发者准确地定位特定组件,使错误报告和问题追踪更加精确。

      总之,GUID 在 Grasshopper 插件开发中扮演着关键角色,确保了组件的唯一性、可靠性和兼容性。它是插件生态系统正常运作的重要基础。

      // In summary, GUIDs play a crucial role in Grasshopper plugin development, ensuring the uniqueness, reliability, and compatibility of components. They are an essential foundation for the proper functioning of the plugin ecosystem.

  5. 使用场景:
    这个组件适用于需要在 Grasshopper 中处理 Excel 数据的情况,特别是当用户想要通过条件格式条形来可视化数据的相对大小或重要性时。它可以帮助用户快速识别数据集中的高值和低值,提高数据分析和展示的效率。

  6. 代码结构:

    • 继承自基础范围组件 (GH_Ex_Rng__Base)
    • 重写了多个方法来自定义组件行为
    • 使用 SolveInstance 方法实现主要逻辑
  7. 扩展性:
    这个组件是 Grasshopper 与 Excel 集成的一个例子,展示了如何扩展 Grasshopper 的功能以处理外部数据源。类似的方法可以用于创建其他类型的 Excel 数据处理组件。

总的来说,这个组件为 Grasshopper 用户提供了一种简单而强大的方式来增强 Excel 数据的可视化效果,使数据分析和展示更加直观和有效。它体现了将复杂的 Excel 操作集成到 Grasshopper 工作流中的可能性,为建筑、工程等领域的专业人士提供了更丰富的数据处理工具。

Flow diagram

这个流程图将描述 GH_Ex_Ana_CondBar 类中 SolveInstance 方法的主要逻辑流程。

是/Yes
是/Yes
否/No
否/No
开始 / Start
获取工作表数据 / Get Worksheet Data
获取范围数据 / Get Range Data
获取颜色,梯度,清除和激活参数
/ Get Color, Gradient, Clear, and Activate parameters
激活? / Activate?
清除? / Clear?
清除现有条件 / Clear existing conditions
添加条件格式条形 / Add conditional formatting bar
设置输出数据 / Set output data
结束 / End

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

  1. 开始 (Start)
    对应代码: protected override void SolveInstance(IGH_DataAccess DA)

  2. 获取工作表数据 (Get Worksheet Data)
    对应代码:

    IGH_Goo gooS = null;
    DA.GetData(0, ref gooS);
    ExWorksheet worksheet = new ExWorksheet();
    bool hasWs = gooS.TryGetWorksheet(ref worksheet);
    
  3. 获取范围数据 (Get Range Data)
    对应代码:

    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;
    
  4. 获取颜色、梯度、清除和激活参数 (Get Color, Gradient, Clear, and Activate parameters)
    对应代码:

    Sd.Color color = Sd.Color.LightGray;
    DA.GetData(2, ref color);
    bool gradient = false;
    DA.GetData(3, ref gradient);
    bool clear = false;
    DA.GetData(4, ref clear);
    bool activate = false;
    DA.GetData(5, ref activate);
    
  5. 激活? (Activate?)
    对应代码: if (activate)

  6. 清除? (Clear?)
    对应代码: if (clear) range.ClearConditions();

  7. 清除现有条件 (Clear existing conditions)
    对应代码: range.ClearConditions();

  8. 添加条件格式条形 (Add conditional formatting bar)
    对应代码: range.AddConditionalBar(color,gradient);

  9. 设置输出数据 (Set output data)
    对应代码: DA.SetData(0, range);

  10. 结束 (End)

这个流程图清晰地展示了 SolveInstance 方法的主要逻辑流程,包括数据获取、条件判断和操作执行的顺序。

Description

  1. 构造函数
public GH_Ex_Ana_CondBar()
  : base("Conditional Bar", "Bar",
      "Add conditional formatting bar to a Range based on relative 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.AddColourParameter("Cell Color", "C", "The cell highlight color", GH_ParamAccess.item, Sd.Color.LightGray);
    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 cell color, gradient option, 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.AddConditionalBar(color, gradient);
    }
    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 bar based on relative values
    // 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_Bar_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("536de2b8-625f-41ba-930e-ccea54614952"); }
}

这个属性返回组件的唯一标识符。这个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数据处理中的条件格式条形。它继承自基础范围组件,并通过重写多个方法来定制其行为。这个组件允许用户根据相对值为Excel范围添加条件格式条形,提供了灵活的数据可视化选项。通过这种方式,它扩展了Grasshopper在Excel数据处理方面的功能,为用户提供了强大而直观的数据分析工具。

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_CondBar : GH_Ex_Rng__Base
    {
        /// <summary>
        /// 初始化 GH_Ex_Ana_CondBar 类的新实例。
        /// </summary>
        public GH_Ex_Ana_CondBar()
          : base("Conditional Bar", "Bar",
              "根据相对值为范围添加条件格式条形图",
              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.AddColourParameter("Cell Color", "C", "单元格高亮颜色", GH_ParamAccess.item, Sd.Color.LightGray);
            pManager[2].Optional = true;
            // 添加布尔参数,用于设置是否使用渐变
            pManager.AddBooleanParameter("Gradient", "G", "如果为真,条形图将使用渐变色", GH_ParamAccess.item, false);
            pManager[3].Optional = true;
            // 添加布尔参数,用于设置是否清除现有条件
            pManager.AddBooleanParameter("Clear", "_X", "如果为真,将清除现有条件", GH_ParamAccess.item, false);
            pManager[4].Optional = true;
            // 添加布尔参数,用于设置是否激活条件
            pManager.AddBooleanParameter("Activate", "_A", "如果为真,将应用条件", GH_ParamAccess.item, false);
            pManager[5].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;

            // 获取颜色设置
            Sd.Color color = Sd.Color.LightGray;
            DA.GetData(2, ref color);

            // 获取是否使用渐变的设置
            bool gradient = false;
            DA.GetData(3, ref gradient);

            // 获取是否清除现有条件的设置
            bool clear = false;
            DA.GetData(4, ref clear);

            // 获取是否激活条件的设置
            bool activate = false;
            DA.GetData(5, ref activate);

            // 如果激活条件
            if (activate)
            {
                // 如果需要清除,则清除现有条件
                if (clear) range.ClearConditions();
                // 添加条件格式条形图
                range.AddConditionalBar(color, gradient);
            }

            // 设置输出数据
            DA.SetData(0, range);
        }

        /// <summary>
        /// 提供组件的图标。
        /// </summary>
        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                // 返回组件的图标
                return Properties.Resources.BB_Cond_Bar_01;
            }
        }

        /// <summary>
        /// 获取此组件的唯一标识符。发布后请勿更改此ID。
        /// </summary>
        public override Guid ComponentGuid
        {
            // 返回组件的唯一GUID
            get { return new Guid("536de2b8-625f-41ba-930e-ccea54614952"); }
        }
    }
}

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

  1. 类的定义和继承关系
  2. 构造函数的作用
  3. 各种方法和属性的功能
  4. 输入参数的注册和含义
  5. SolveInstance 方法中的逻辑流程
  6. 图标和GUID的设置及其重要性
  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值