DXF文件格式转成图片(dxflib和opencv) 您所在的位置:网站首页 dxf格式图纸 DXF文件格式转成图片(dxflib和opencv)

DXF文件格式转成图片(dxflib和opencv)

2023-07-31 01:26| 来源: 网络整理| 查看: 265

参考博客:https://blog.csdn.net/weixinhum/article/details/53860176 该博主的文件工程下下来直接能用 很良心(比这个博主多做的一点是把文字实体填上了但是有点问题没法解决就是opencv不能将旋转文字填上 要是能解决的希望可以留言) 参考博客:http://www.ribbonsoft.com/doc/dxflib/2.5/classref/struct_d_l___m_text_data.html 各个函数用法

/* * @file main.cpp */ /***************************************************************************** ** $Id: main.cpp 3591 2006-10-18 21:23:25Z andrew $ ** ** This is part of the dxflib library ** Copyright (C) 2000-2001 Andrew Mustun ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU Library General Public License as ** published by the Free Software Foundation. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU Library General Public License for more details. ** ** You should have received a copy of the GNU Library General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ //2016/12/24 //by huangwx //点,直线,圆,圆弧,多边形,样条曲线(包括用控制点,和顶点两种),以及单行文本。 #include #include #include #include "dl_dxf.h" #include "dl_creationadapter.h" #include "test_creationclass.h" float zoom =10;//放大倍数 CvPoint coordinatemove;//坐标偏移 //图像边界变量 int xleftborder = 0, xrightborder = 0; int ytopborder = 0, ybuttomborder = 0; CvPoint setWorldCoordinate(int rows,vector ::iterator myblock, CvPoint2D32f mypoint)//世界坐标系转换,得到dxf文件边界位置 { CvPoint worldcoordinatepoint; worldcoordinatepoint.x = coordinatemove.x + (mypoint.x*myblock->sx + myblock->ipx) * zoom; worldcoordinatepoint.y = rows-((mypoint.y*myblock->sy + myblock->ipy) * zoom+ coordinatemove.y); return worldcoordinatepoint; } void drawdxfimg(Mat* img, Test_CreationClass *creationClass) { vector ::iterator blockItor;//块容器迭代器 blockItor = creationClass->myblock.begin(); while (blockItor != creationClass->myblock.end()) { if (blockItor->drawflag == false) { blockItor++; continue; } //绘制直线 vector ::iterator lineItor;//块容器迭代器 lineItor = blockItor->line.begin(); while (lineItor != blockItor->line.end()) { CvPoint bpoint = setWorldCoordinate((*img).rows, blockItor, lineItor->beginpoint); CvPoint epoint = setWorldCoordinate((*img).rows, blockItor, lineItor->endpoint); line(*img, bpoint, epoint, Scalar(255,255,255),3); lineItor++; } //绘制圆 vector ::iterator circleItor;//块容器迭代器 circleItor = blockItor->circle.begin(); while (circleItor != blockItor->circle.end()) { CvPoint cpoint = setWorldCoordinate((*img).rows, blockItor, circleItor->centerpoint); int radius = circleItor->radius*blockItor->sx * zoom; circle(*img, cpoint, radius, Scalar(255,255, 255),3); circleItor++; } //绘制圆弧 vector ::iterator arcItor;//块容器迭代器 arcItor = blockItor->arc.begin(); while (arcItor != blockItor->arc.end()) { CvPoint cpoint = setWorldCoordinate((*img).rows, blockItor, arcItor->centerpoint); int radius = arcItor->radius*blockItor->sx * zoom; if (arcItor->bangleeangle)//逆时针圆弧 { ellipse(*img, cpoint, cvSize(radius, radius), 0, (360 - arcItor->eangle), (360 - arcItor->bangle), cvScalar(255,255, 255),3); } else//顺时针画圆弧 { double bangle, eangle; bangle = 360 - arcItor->bangle; eangle = -arcItor->eangle; ellipse(*img, cpoint, cvSize(radius, radius), 0, bangle, eangle, cvScalar(255,255, 255),3); } arcItor++; } //绘制字体 vector ::iterator textItor;//块容器迭代器 textItor= blockItor->text.begin(); while (textItor != blockItor->text.end()) { CvPoint tpoint = setWorldCoordinate((*img).rows, blockItor, textItor->insertionpoint); string text = textItor->m_text; //double tangle = textItor->angle; putText(*img,text,tpoint,FONT_HERSHEY_COMPLEX,1,cv::Scalar(0, 255, 255),3,8,0); textItor++; } //绘制多线实体 vector ::iterator polylineentitiesItor;//多线实体容器迭代器 polylineentitiesItor = blockItor->polylineentities.begin(); while (polylineentitiesItor != blockItor->polylineentities.end()) { for (size_t i = 0; i < polylineentitiesItor->vertex.size(); i++) { int nextvertex = i + 1; if (nextvertex == polylineentitiesItor->vertex.size()) { if (polylineentitiesItor->isclose == true)//闭合则画终点到起点的线 { nextvertex = 0; } else { break; } } CvPoint bpoint = setWorldCoordinate((*img).rows, blockItor, polylineentitiesItor->vertex[i]); CvPoint epoint = setWorldCoordinate((*img).rows, blockItor, polylineentitiesItor->vertex[nextvertex]); line(*img, bpoint, epoint, Scalar(255,255, 255),3); } polylineentitiesItor++; } blockItor++; } } void getWorldCoordinateborder(vector ::iterator myblock, CvPoint2D32f mypoint)//世界坐标系转换,得到dxf文件边界位置 { CvPoint worldcoordinatepoint; worldcoordinatepoint.x = (mypoint.x*myblock->sx + myblock->ipx) * zoom; worldcoordinatepoint.y = (mypoint.y*myblock->sy + myblock->ipy) * zoom; if (xleftborder>worldcoordinatepoint.x) { xleftborder = worldcoordinatepoint.x; } if (xrightborderworldcoordinatepoint.y) { ytopborder = worldcoordinatepoint.y; } if (ybuttombordermyblock.begin(); while (blockItor != creationClass->myblock.end()) { if (blockItor->drawflag==false) { blockItor++; continue; } //直线 vector ::iterator lineItor;//块容器迭代器 lineItor = blockItor->line.begin(); while (lineItor != blockItor->line.end()) { getWorldCoordinateborder(blockItor, lineItor->beginpoint); getWorldCoordinateborder(blockItor, lineItor->endpoint); lineItor++; } //圆 vector ::iterator circleItor;//块容器迭代器 circleItor = blockItor->circle.begin(); while (circleItor != blockItor->circle.end()) { getWorldCoordinateborder(blockItor, circleItor->centerpoint); int radius = circleItor->radius*blockItor->sx * zoom; circleItor++; } //圆弧 vector ::iterator arcItor;//块容器迭代器 arcItor = blockItor->arc.begin(); while (arcItor != blockItor->arc.end()) { getWorldCoordinateborder(blockItor, arcItor->centerpoint); int radius = arcItor->radius*blockItor->sx * zoom; arcItor++; } //文字 vector::iterator testItor;//块容器迭代器 testItor = blockItor->text.begin(); while (testItor != blockItor->text.end()) { getWorldCoordinateborder(blockItor, testItor->insertionpoint); string text = testItor->m_text; testItor++; } //绘制多线实体 vector ::iterator polylineentitiesItor;//多线实体容器迭代器 polylineentitiesItor = blockItor->polylineentities.begin(); while (polylineentitiesItor != blockItor->polylineentities.end()) { for (size_t i = 0; i < polylineentitiesItor->vertex.size(); i++) { int nextvertex = i + 1; if (nextvertex == polylineentitiesItor->vertex.size()) { if (polylineentitiesItor->isclose == true)//闭合则画终点到起点的线 { nextvertex = 0; } else { break; } } getWorldCoordinateborder(blockItor, polylineentitiesItor->vertex[i]); getWorldCoordinateborder(blockItor, polylineentitiesItor->vertex[nextvertex]); } polylineentitiesItor++; } blockItor++; } } int main() { char* file = "C:/temp/cleandxf3.dxf"; //char* file = "demo.dxf"; std::cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有