heitzmann / gdstk

Gdstk (GDSII Tool Kit) is a C++/Python library for creation and manipulation of GDSII and OASIS files.
https://heitzmann.github.io/gdstk/
Boost Software License 1.0
324 stars 78 forks source link

Write a gds file containing text objects or labels using gdstk #257

Closed SouthChinaHuihuisauce closed 3 weeks ago

SouthChinaHuihuisauce commented 3 weeks ago

Dear author, I would like to ask you how to add a text object or label to a gds file in lib using write_gds in gdstk.A polygon that is not a text.The following is the code I created in the gdstk document to create a gds file. `

include "mainwindow.h"

include

include

include

include

include

include "ui_mainwindow.h"

include <gdstk/gdstk.hpp>

include

using namespace gdstk; // 使用GDSTK库的命名空间

// 定义一个函数,生成一个包含一系列矩形的单元(Cell) Cell grating(double period, double fill_frac, double length, double width, Tag tag, const char name) { double x = width / 2; // 计算矩形的x坐标 double w = period * fill_frac; // 计算矩形的宽度 int64_t num = (int64_t) (length / period); // 计算需要生成的矩形的数量

Cell *result = (Cell *) allocate_clear(sizeof(Cell)); // 分配内存并初始化一个新的单元
result->name = copy_string(name, NULL);               // 设置单元的名称
result->polygon_array.ensure_slots(num);              // 确保多边形数组有足够的空间
for (int64_t i = 0; i < num; i++) {                   // 对于每一个矩形
    double y = i * period;                            // 计算矩形的y坐标
    Polygon *rect = (Polygon *) allocate(sizeof(Polygon)); // 分配内存并初始化一个新的矩形
    *rect = rectangle(Vec2{-x, y}, Vec2{x, y + w}, tag); // 设置矩形的位置和大小
    result->polygon_array.append(rect);                  // 将矩形添加到单元中
}

return result; // 返回生成的单元

}

// 主窗口的构造函数 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) // 调用基类的构造函数 , ui(new Ui::MainWindow) // 创建用户界面 { ui->setupUi(this); // 设置用户界面

Library lib = {};                // 创建一个新的GDS库
lib.init("library", 1e-6, 1e-9); // 初始化库,设置名称和单位

double length = 20; // 定义光栅的长度

Cell *grat1 = grating(3.5, 0.5, length, 25, make_tag(1, 0), "Grating 1"); // 创建第一个光栅单元
lib.cell_array.append(grat1); // 将光栅单元添加到库中

// 创建grat1的子cell
// 创建一个新的单元
Cell *grat1_sub = grating(3.5, 0.5, length, 25, make_tag(2, 0), "grat1_sub");
lib.cell_array.append(grat1_sub);

Cell *grat2 = grating(3.0, 0.5, length, 25, make_tag(1, 0), "Grating 2"); // 创建第二个光栅单元
lib.cell_array.append(grat2); // 将光栅单元添加到库中

Cell *main_cell = (Cell *) allocate_clear(sizeof(Cell)); // 创建一个新的主单元
main_cell->name = copy_string("Main", NULL);             // 设置主单元的名称
lib.cell_array.append(main_cell);                        // 将主单元添加到库中

Polygon *rect = (Polygon *) allocate(sizeof(Polygon)); // 创建一个新的矩形
*rect = rectangle(Vec2{0, -10}, Vec2{150, 10}, 0);     // 设置矩形的位置和大小
main_cell->polygon_array.append(rect);                 // 将矩形添加到主单元中
// 构成lib库

// 创建引用关系
Reference *ref1 = (Reference *) allocate_clear(sizeof(Reference)); // 创建一个新的引用
qDebug() << "初始化grat1" << grat1->name;
ref1->init(grat1);                       // 设置引用的目标为第一个光栅单元
ref1->origin = Vec2{length, 0};          // 设置引用的原点
ref1->rotation = M_PI / 2;               // 设置引用的旋转角度
main_cell->reference_array.append(ref1); // 将引用添加到主单元中

// 创建新的引用关系
Reference *ref1_sub = (Reference *) allocate_clear(sizeof(Reference));
qDebug() << "初始化grat1_sub" << grat1_sub->name;
ref1_sub->init(grat1_sub);
ref1_sub->origin = Vec2{0, 0}; // 引用点
ref1_sub->rotation = M_PI;     // 不旋转
grat1->reference_array.append(ref1_sub);

Reference *ref2 = (Reference *) allocate_clear(sizeof(Reference)); // 创建一个新的引用
ref2->init(grat2);
//    ref2->type = ReferenceType::Cell, ref2->cell = grat2,
ref2->origin = Vec2{150 - length, 0}; // 设置引用的类型、目标、原点
ref2->rotation = -M_PI / 2;
main_cell->reference_array.append(ref2); // 设置引用的旋转角度、放大倍数,并将引用添加到主单元中

lib.write_gds("E:\\workspace\\test\\123\\pcell.gds", 0, NULL); // 将库写入GDS文件

lib.free_all(); // 释放库占用的所有资源

}

// 主窗口的析构函数 MainWindow::~MainWindow() { delete ui; // 删除用户界面 } ` I want to write a label or text object in the GDS file, like this 1