Bumblebee/Classes/ExShape.cs
Flow diagram
- ExShape Class (ExShape类): The main class being described.
- Constructors (构造函数):
- Copy Constructor (复制构造函数)
- ComObj Constructor (ComObj构造函数)
- SmartArt Constructor (SmartArt构造函数)
- Properties (属性):
- ShapeType (形状类型)
- Methods (方法):
- SetFillColor (设置填充颜色)
- SetStrokeColor (设置描边颜色)
- SetStrokeWeight (设置描边宽度)
- SetList (设置列表类型)
- ToString Override (重写ToString方法)
这个流程图展示了ExShape类的主要组成部分和流程:
- ExShape类: 被描述的主要类。
- 构造函数:
- 复制构造函数
- ComObj构造函数
- SmartArt构造函数
- 属性:
- 形状类型
- 方法:
- 设置填充颜色
- 设置描边颜色
- 设置描边宽度
- 设置列表类型
- 重写ToString方法
Description
- SetFillColor 方法
/// <summary>
/// 设置形状的填充颜色
/// </summary>
/// <param name="color">要设置的颜色</param>
public void SetFillColor(Sd.Color color)
{
if (this.shapeType != ShapeTypes.Line)
{
this.ComObj.Fill.ForeColor.RGB = Sd.ColorTranslator.ToOle(color);
this.ComObj.Fill.BackColor.RGB = Sd.ColorTranslator.ToOle(color);
}
}
解释:
这个方法用于设置形状的填充颜色。它首先检查形状是否为线条类型,因为线条没有填充色。如果不是线条,则设置填充的前景色和背景色。
关键点:
ColorTranslator.ToOle(color)
: 这个方法将System.Drawing.Color转换为Excel COM对象可以理解的OLE颜色值。(This method converts System.Drawing.Color to an OLE color value that Excel COM objects can understand.)- 同时设置前景色和背景色确保了完全覆盖和一致的颜色效果。
- SetStrokeColor 方法
/// <summary>
/// 设置形状的描边颜色
/// </summary>
/// <param name="color">要设置的颜色</param>
public void SetStrokeColor(Sd.Color color)
{
this.ComObj.Line.ForeColor.RGB = Sd.ColorTranslator.ToOle(color);
this.ComObj.Line.BackColor.RGB = Sd.ColorTranslator.ToOle(color);
}
解释:
这个方法设置形状轮廓的颜色。它同时设置线条的前景色和背景色,以确保一致的颜色效果。
关键点:
- 使用
ColorTranslator.ToOle
方法将C#的Color对象转换为Excel可用的颜色值。 - 设置前景色和背景色可以确保在不同的形状和线条样式下都能正确显示颜色。
- SetStrokeWeight 方法
/// <summary>
/// 设置形状的描边宽度
/// </summary>
/// <param name="weight">要设置的宽度</param>
public void SetStrokeWeight(double weight)
{
this.ComObj.Line.Weight = (float)weight;
}
解释:
此方法用于设置形状轮廓线的粗细。
关键点:
(float)weight
: 将double类型转换为float类型。(Converts the double type to float type.)
这是因为Excel COM对象的Line.Weight属性期望接收一个float值。
- SetList 方法
/// <summary>
/// 设置SmartArt的布局类型
/// </summary>
/// <param name="type">布局类型的索引</param>
public void SetList(int type)
{
this.ComObj.SmartArt.Layout = this.sheet.ParentApp.ComObj.SmartArtLayouts[type];
}
解释:
这个方法用于设置SmartArt对象的布局类型。它使用一个整数索引来选择特定的布局。
关键点:
this.sheet.ParentApp.ComObj.SmartArtLayouts[type]
: 这行代码访问了Excel应用程序中可用的SmartArt布局集合,并根据提供的索引选择特定布局。(This line accesses the collection of available SmartArt layouts in the Excel application and selects a specific layout based on the provided index.)
- ToString 方法重写
/// <summary>
/// 重写ToString方法,返回形状的类型信息
/// </summary>
/// <returns>包含形状类型的字符串</returns>
public override string ToString()
{
return "Smart | " + ShapeType.ToString();
}
解释:
这个方法重写了Object类的ToString方法,提供了ExShape对象的自定义字符串表示。
关键点:
"Smart | " + ShapeType.ToString()
: 这个字符串组合提供了对象类型的简洁描述,包括它是一个"Smart"对象以及具体的形状类型。
- ShapeType 属性
/// <summary>
/// 获取形状的类型
/// </summary>
public virtual ShapeTypes ShapeType
{
get { return shapeType; }
}
解释:
这是一个只读属性,用于获取形状的类型。
关键点:
virtual
: 这个关键字允许派生类重写此属性,提供了扩展性。(This keyword allows derived classes to override this property, providing extensibility.)
总结:
这些方法和属性共同构成了一个强大的接口,用于在C#中操作Excel形状,特别是SmartArt对象。它们展示了如何与Excel的COM对象交互,修改形状的各种属性,如颜色、线条粗细和布局。这个类的设计考虑到了易用性和灵活性,允许开发者精确控制Excel中的图形元素。
Code
当然,我会为这段代码添加详细的中文注释,以便于您的学习和博客发表。以下是带有详细中文注释的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sd = System.Drawing; // 系统绘图命名空间的别名
using Rg = Rhino.Geometry; // Rhino几何命名空间的别名
using XL = Microsoft.Office.Interop.Excel; // Excel互操作命名空间的别名
using MC = Microsoft.Office.Core; // Office核心命名空间的别名
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
namespace Bumblebee
{
public class ExShape
{
#region 成员变量
public ExWorksheet sheet = null; // 关联的工作表
public XL.Shape ComObj = null; // Excel Shape COM对象
// 形状类型枚举
public enum ShapeTypes { Illustration, Control, SmartArt, Line };
protected ShapeTypes shapeType = ShapeTypes.Illustration; // 默认形状类型
#endregion
#region 构造函数
// 复制构造函数
public ExShape(ExShape exSmart)
{
this.sheet = new ExWorksheet(exSmart.sheet);
this.ComObj = exSmart.ComObj;
this.shapeType = exSmart.shapeType;
}
// 使用现有COM对象的构造函数
public ExShape(XL.Shape comObj, ExWorksheet sheet, ShapeTypes shapeType)
{
this.sheet = new ExWorksheet(sheet);
this.ComObj = comObj;
this.shapeType = shapeType;
}
// 创建新SmartArt的构造函数
public ExShape(ExWorksheet sheet, string name, Rg.Rectangle3d boundary, List<string> values, List<int> levels)
{
this.sheet = sheet;
this.shapeType = ShapeTypes.SmartArt;
int countV = values.Count;
int countL = levels.Count;
// 确保levels列表长度与values相同
for (int i = countL; i < countV; i++)
{
levels.Add(countL - 1);
}
// 删除同名的已存在形状
foreach (XL.Shape obj in sheet.ComObj.Shapes)
{
if (name == obj.Name)
{
obj.Delete();
break;
}
}
// 添加新的SmartArt对象
this.ComObj = sheet.ComObj.Shapes.AddSmartArt(sheet.ParentApp.ComObj.SmartArtLayouts[1],
boundary.Corner(0).Y, boundary.Width, boundary.Height);
// 删除默认节点
int count = this.ComObj.SmartArt.AllNodes.Count;
for (int i = 0; i < count; i++)
{
this.ComObj.SmartArt.AllNodes[1].Delete();
}
// 添加新节点并设置文本
MC.SmartArtNode node = this.ComObj.SmartArt.AllNodes.Add();
node.TextFrame2.TextRange.Text = values[0];
// 添加其余节点并设置层级
for (int i = 1; i < values.Count; i++)
{
MC.SmartArtNode nodeA = this.ComObj.SmartArt.AllNodes.Add();
nodeA.TextFrame2.TextRange.Text = values[i];
for(int j = 1; j < levels[i] - 1; j++)
{
nodeA.Demote();
}
}
// 设置形状名称
this.ComObj.Name = name;
}
#endregion
#region 属性
// 获取形状类型的虚方法
public virtual ShapeTypes ShapeType
{
get { return shapeType; }
}
#endregion
#region 方法
// 设置填充颜色
public void SetFillColor(Sd.Color color)
{
if (this.shapeType != ShapeTypes.Line)
{
this.ComObj.Fill.ForeColor.RGB = Sd.ColorTranslator.ToOle(color);
this.ComObj.Fill.BackColor.RGB = Sd.ColorTranslator.ToOle(color);
}
}
// 设置描边颜色
public void SetStrokeColor(Sd.Color color)
{
this.ComObj.Line.ForeColor.RGB = Sd.ColorTranslator.ToOle(color);
this.ComObj.Line.BackColor.RGB = Sd.ColorTranslator.ToOle(color);
}
// 设置描边宽度
public void SetStrokeWeight(double weight)
{
this.ComObj.Line.Weight = (float)weight;
}
// 设置SmartArt布局
public void SetList(int type)
{
this.ComObj.SmartArt.Layout = this.sheet.ParentApp.ComObj.SmartArtLayouts[type];
}
#endregion
#region 重写方法
// 重写ToString方法
public override string ToString()
{
return "Smart | " + ShapeType.ToString();
}
#endregion
}
}
这些注释详细解释了代码的各个部分,包括:
- 命名空间别名的使用和目的
- 类的成员变量及其用途
- 不同构造函数的功能和参数说明
- 属性的作用和实现
- 各种方法的功能、参数和实现细节
- 重写方法的目的