librecad 是一个非常优秀的二维cad开源软件,是一个可控的图纸绘制方案。
使用简单,支持命令行绘制。

唯一诟病的 librecad 的中文支持比较差, wqy-unicode 对中文进行了支持, 但是是空心的。
个人对这个有两种想法。
一种是修改文字,重新对几千上万个文字进行绘制肯定不太好, c++opencv中线条细化算法 - 爱码网提供了一种思路。
//四周细化算法
void Refine(Mat& image) {
int p[8];
int top=1, down=1, right=1, left=1;
vector<Point> del;
int grayvalue = 0;
int height = image.rows; //获取图像高度
int width = image.cols; //获取图像宽度
Mat *im = reinterpret_cast<Mat*>((void*)&image); //获取像素点信息
//上下收缩
for (int i = 1; i < height-1; i++) {
for (int j = 1; j < width-1; j++) {
grayvalue = Get_gray(im, j, i); //获取指定点灰度值
if (grayvalue != 0) { //判断中心点是否为前景
p[0] = (Get_gray(im, j + 1, i) == 0) ? 0 : 1;
p[1] = (Get_gray(im, j + 1, i - 1) == 0) ? 0 : 1;
p[2] = (Get_gray(im, j, i - 1) == 0) ? 0 : 1;
p[3] = (Get_gray(im, j - 1, i - 1) == 0) ? 0 : 1;
p[4] = (Get_gray(im, j - 1, i) == 0) ? 0 : 1;
p[5] = (Get_gray(im, j - 1, i + 1) == 0) ? 0 : 1;
p[6] = (Get_gray(im, j, i + 1) == 0) ? 0 : 1;
p[7] = (Get_gray(im, j + 1, i + 1) == 0) ? 0 : 1;
if (i < height - 2)
down = (Get_gray(im, j, i + 2) == 0) ? 0 : 1;
else
down = 1;
// 横向直线
if (p[6] && (p[5] || p[7] || p[0] || p[4]) \
&& !(p[1] || p[3]) && p[2] == 0 && down) {
del.push_back(Point(j, i));
}
if (p[2] && (p[1] || p[3] || p[0] || p[4]) \
&& !( p[5] || p[7]) && p[6] == 0) {
del.push_back(Point(j, i));
}
}
}
}
for (int i = 1; i < height - 2; i++) {
grayvalue = Get_gray(im, 0, i);
if (grayvalue != 0) {
if ( Get_gray(im, 0, i - 1) \
&& Get_gray(im, 1, i - 1) \
&& Get_gray(im, 0, i + 1)==0 \
&& Get_gray(im, 1, i)==0) {//上2,上1,右上1,下1=0,右1=0
del.push_back(Point(0, i));
}
if (Get_gray(im, 0, i - 1) == 0 \
&& Get_gray(im, 1, i + 1) \
&& Get_gray(im, 1, i) == 0 \
&& Get_gray(im, 0, i+2)){//上1=0,下1,右下1,右1=0,下2
del.push_back(Point(0, i));
}
}
if (grayvalue != 0) {
if (Get_gray(im, width - 1, i - 1) \
&& Get_gray(im, width - 2, i - 1) \
&& Get_gray(im, width - 1, i + 1) == 0 \
&& Get_gray(im, width - 2, i) == 0){ //上2,上1,左上1,下1=0,左1=0
del.push_back(Point(width - 1, i));
}
if (Get_gray(im, width - 1, i - 1) == 0 \
&& Get_gray(im, width - 2, i + 1) \
&& Get_gray(im, width - 2, i) == 0 \
&& Get_gray(im, width - 1, i + 2)){//上1=0,下1,左下1,左1=0,下2
del.push_back(Point(width - 1, i));
}
}
}
for (int i = 0; i < del.size();i++) {
uchar* data = image.ptr<uchar>(del[i].y);
data[del[i].x]=0;
}
//左右收缩
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
grayvalue = Get_gray(im, j, i); //获取指定点灰度值
if (grayvalue != 0) { //判断中心点是否为前景
p[0] = (Get_gray(im, j + 1, i) == 0) ? 0 : 1;
p[1] = (Get_gray(im, j + 1, i - 1) == 0) ? 0 : 1;
p[2] = (Get_gray(im, j, i - 1) == 0) ? 0 : 1;
p[3] = (Get_gray(im, j - 1, i - 1) == 0) ? 0 : 1;
p[4] = (Get_gray(im, j - 1, i) == 0) ? 0 : 1;
p[5] = (Get_gray(im, j - 1, i + 1) == 0) ? 0 : 1;
p[6] = (Get_gray(im, j, i + 1) == 0) ? 0 : 1;
p[7] = (Get_gray(im, j + 1, i + 1) == 0) ? 0 : 1;
if (j < width - 2)
right = (Get_gray(im, j + 2, i) == 0) ? 0 : 1;
else
right = 1;
//竖直线
if (p[0] && (p[1] || p[7] || p[2] || p[6]) && !(p[3] || p[5]) && p[4] == 0 && right) {
del.push_back(Point(j, i));
}
if (p[4] && (p[3] || p[5] || p[2] || p[6]) && !(p[1] || p[7]) && p[0] == 0) {
del.push_back(Point(j, i));
}
}
}
}
for (int j = 1; j < width - 2; j++) {
grayvalue = Get_gray(im, j, 0);
if (grayvalue != 0) {
if (Get_gray(im, j - 1, 0) == 0 \
&& Get_gray(im, j + 1, 0) \
&& Get_gray(im, j + 2, 0) \
&& Get_gray(im, j, 1) == 0 \
&& Get_gray(im, j+1, 1)) {//左1=0,右1,右2,下1=0,右下1
del.push_back(Point(j, 0));
}
if (Get_gray(im, j - 1, 0) \
&& Get_gray(im, j+1, 0)==0 \
&& Get_gray(im, j, 1) == 0 \
&& Get_gray(im, j-1, 1)){//左1,右1=0,下1=0,左下1
del.push_back(Point(j, 0));
}
}
}
for (int j = 1; j < width - 2; j++) {
grayvalue = Get_gray(im, j, height-1);
if (grayvalue != 0) {
if (Get_gray(im, j - 1, height - 1) == 0 \
&& Get_gray(im, j + 1, height - 1) \
&& Get_gray(im, j + 2, height - 1) \
&& Get_gray(im, j, height - 2) == 0 \
&& Get_gray(im, j + 1, height - 2)) {//左1=0,右1,右2,下1=0,右下1
del.push_back(Point(j, height - 1));
}
if (Get_gray(im, j - 1, height - 1) \
&& Get_gray(im, j + 1, height - 1) == 0 \
&& Get_gray(im, j, height - 2) == 0 \
&& Get_gray(im, j - 1, height - 2)){//左1,右1=0,下1=0,左下1
del.push_back(Point(j, height - 1));
}
}
}
for (int i = 0; i < del.size(); i++) {
uchar* data = image.ptr<uchar>(del[i].y);
data[del[i].x] = 0;
}
}
另外一种就是直接修改软件, 思路也很简单, 框的数据看了wqy-unicode文件, 里面的数据都是闭环的。
只需要判断以下, 第一个点是否和最后一个点相同,如果相同就调用QT的API填充内容即可。
不过遇到点问题, 我的电脑目前没有装编译环境, 虽然看了librecad的源码,但是还没有动手修改, 等librecad的编译环境搭建好了在说, 这牵扯到另外一个项目。
不过一步一步来, 大家有什么思路,也可以帮忙优化下, 建立一个良好的开源生态。