C++ 读取的dxf文件并用OpenCV绘制出来
2016-12-07 16:15
417 查看
先拷贝一小段别人写的开头
注意,以下方法都不用,
“dxflib实现了dxf文件的解析。所有的图元解析完后,都会通过DL_CreationAdapter的虚函数接口回调,所以我们要继承这个类,重写那些想绘制的图元的方法,比如直线对应的就是virtual void addLine(const DL_LineData&); 这个DL_LineData结构体数据保存的就是我们要绘制直线的数据。
绘制我用的是MFC的CDC,双缓冲机制。先把根据数据把所有的图元绘制到内存缓冲中,再一次拷到窗口DC中。值得注意是平移和放大以及坐标系转换。因为应用程序的左上角是原点(0,0),面我们需要显示给人看的是Y轴正方向是屏幕由下往上。(屏幕实际Y方向是由上往下)。只实现了点,直线,圆,圆弧,多边形,样条曲线(包括用控制点,和顶点两种),以及单行文本。 像线型,线条颜色,多行文本,椭圆这些都是没有实现。但是DXFLIB这些信息有解析出来,只需要按照规则理论上都是可以实现的。”(上面两段是拷贝)
正文开始,C++注释很详细,只写了识别块的单层嵌套,只要了解一些DXF文件格式,就可以根据这个例子修改,可以读出线、圆、等等,直接贴源码
稍后会把整个工程上传,里面包含测试文件等等
1、读线,包括块中的线
2、读块的插入信息
转载请声明出处
作者:shaynerain
出处:http://blog.csdn.net/shaynerain/article/details/52670536
工程下载:http://download.csdn.net/detail/shaynerain/9704664
欢迎大神吐槽
注意,以下方法都不用,
“dxflib实现了dxf文件的解析。所有的图元解析完后,都会通过DL_CreationAdapter的虚函数接口回调,所以我们要继承这个类,重写那些想绘制的图元的方法,比如直线对应的就是virtual void addLine(const DL_LineData&); 这个DL_LineData结构体数据保存的就是我们要绘制直线的数据。
绘制我用的是MFC的CDC,双缓冲机制。先把根据数据把所有的图元绘制到内存缓冲中,再一次拷到窗口DC中。值得注意是平移和放大以及坐标系转换。因为应用程序的左上角是原点(0,0),面我们需要显示给人看的是Y轴正方向是屏幕由下往上。(屏幕实际Y方向是由上往下)。只实现了点,直线,圆,圆弧,多边形,样条曲线(包括用控制点,和顶点两种),以及单行文本。 像线型,线条颜色,多行文本,椭圆这些都是没有实现。但是DXFLIB这些信息有解析出来,只需要按照规则理论上都是可以实现的。”(上面两段是拷贝)
正文开始,C++注释很详细,只写了识别块的单层嵌套,只要了解一些DXF文件格式,就可以根据这个例子修改,可以读出线、圆、等等,直接贴源码
稍后会把整个工程上传,里面包含测试文件等等
1、读线,包括块中的线
void DXFRead::ReadLine(CADDATA &data) { FILE *fp = fopen(FILENAME, "r"); if (!fp) { cout << "读取" << FILENAME << "失败,任意键退出" << endl; getchar(); exit(0); } else { cout << "打开" << FILENAME << "成功,正在读取LINE..." << endl; } while (!feof(fp) && !ferror(fp)) { fscanf(fp, "%s", &str); //扫描块中线 if (strcmp(str, "BLOCK") == 0) { fscanf(fp, "%s", &str); while (strcmp(str, "2") != 0) { fscanf(fp, "%s", &str); } //获得块名字 fscanf(fp, "%s", &name); //如果不是这两个块,做记录 if (strcmp(name,"$MODEL_SPACE") != 0 && strcmp(name, "$PAPER_SPACE") != 0) { while (strcmp(str, "ENDBLK") != 0) { fscanf(fp, "%s", &str); //扫描块中线 if (strcmp(str, "LINE") == 0) { //归属块 strcpy(tempLine.name, name); //x1 while (strcmp(str, "10") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.x1 = atof(str); //y1 while (strcmp(str, "20") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.y1 = atof(str); //z1 while (strcmp(str, "30") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.z1 = atof(str); //x2 while (strcmp(str, "11") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.x2 = atof(str); //y2 while (strcmp(str, "21") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.y2 = atof(str); //z2 while (strcmp(str, "31") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tem d3b3 pLine.z2 = atof(str); data.LINE.Add(new LINEPoint(tempLine)); } } } } //扫描主坐标系线 else if (strcmp(str, "LINE") == 0) { //归属块 strcpy(tempLine.name, "ReferenceBlock"); //x1 while (strcmp(str, "10") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.x1 = atof(str); //y1 while (strcmp(str, "20") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.y1 = atof(str); //z1 while (strcmp(str, "30") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.z1 = atof(str); //x2 while (strcmp(str, "11") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.x2 = atof(str); //y2 while (strcmp(str, "21") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.y2 = atof(str); //z2 while (strcmp(str, "31") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempLine.z2 = atof(str); data.LINE.Add(new LINEPoint(tempLine)); } } cout << "LINE读取完毕..." << endl; fclose(fp); }
2、读块的插入信息
void DXFRead::ReadInsert(CADDATA &data) { FILE *fp = fopen(FILENAME, "r"); fp = fopen(FILENAME, "r"); if (!fp) { cout << "读取" << FILENAME << "失败,任意键退出" << endl; getchar(); exit(0); } else { cout << "打开" << FILENAME << "成功,正在读取BLOCK插入信息..." << endl; } while (!feof(fp) && !ferror(fp)) { fscanf(fp, "%s", &str); if (strcmp(str, "INSERT") == 0) { //读取插入块名字 while (strcmp(str, "2") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); strcpy(tempBlock.name, str); //基点坐标 //rx while (strcmp(str, "10") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.rx = atof(str); //ry while (strcmp(str, "20") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.ry = atof(str); //rz while (strcmp(str, "30") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.rz = atof(str); //读取比例因子 //cx while (strcmp(str, "41") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.cx = atof(str); //cy while (strcmp(str, "42") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.cy = atof(str); //cz while (strcmp(str, "43") != 0) { fscanf(fp, "%s", &str); } fscanf(fp, "%s", &str); tempBlock.cz = atof(str); data.BLOCK.Add(new BLOCKPoint(tempBlock)); } } cout << "BLOCK插入信息读取完毕..." << endl; fclose(fp); }3、绘图
void DXFRead::DrawImage(CADDATA &data) { LINEPoint *linep = NULL; BLOCKPoint *blockp = NULL; uchar* Img = NULL; cout << "开始绘图..." << endl; //创建图像,深度为8,3通道RGB图像 cvImg = cvCreateImage(cvSize(15000, 15000), 8, 3); Img = (uchar *)cvImg->imageData; int step = cvImg->widthStep / sizeof(uchar); int channels = cvImg->nChannels; //清空图像 for (int i = 0; i<cvImg->height; i++) for (int j = 0; j<cvImg->width; j++) { Img[i*step + j*channels + 0] = 0; Img[i*step + j*channels + 1] = 0; Img[i*step + j*channels + 2] = 0; } for (int i = 0; i < data.LINE.GetCount(); i++) { linep = (LINEPoint*)data.LINE.GetAt(i); //如果是正常线 if (strcmp(linep->m_line.name, "ReferenceBlock") == 0 ) { cvLine(cvImg, cvPoint(20 * ((linep->m_line.x1) + 300), 20 * ((linep->m_line.y1) + 300)), cvPoint(20 * ((linep->m_line.x2) + 300), 20 * ((linep->m_line.y2) + 300)), CV_RGB(0, 255, 0), 4); } else { for (int j = 0; j < data.BLOCK.GetCount(); j++) { //获取线归属块信息 blockp = (BLOCKPoint*)data.BLOCK.GetAt(j); if (strcmp(linep->m_line.name, blockp->m_block.name) == 0) { cvLine(cvImg, cvPoint(20 * ( (blockp->m_block.cx) *(linep->m_line.x1) + blockp->m_block.rx + 300), 20 * ((blockp->m_block.cy) *(linep->m_line.y1) + blockp->m_block.ry + 300 ) ), cvPoint(20 * ((blockp->m_block.cx) *(linep->m_line.x2) + blockp->m_block.rx + 300), 20 * ( (blockp->m_block.cy) *(linep->m_line.y2) + blockp->m_block.ry + 300 ) ), CV_RGB(0, 255, 0), 4); } } } } cvSaveImage(SAVENAME, cvImg); //释放图像所占的内存 cvReleaseImage(&cvImg); }
转载请声明出处
作者:shaynerain
出处:http://blog.csdn.net/shaynerain/article/details/52670536
工程下载:http://download.csdn.net/detail/shaynerain/9704664
欢迎大神吐槽
相关文章推荐
- 【持续更新】iOS开发之C语言学习笔记(二)
- 语法错误 : 缺少“;”(在“类型”的前面)”—— C语言标准
- iOS开发学习笔记-C语言学习(一)
- C#与C/C++的交互
- c++与c#的类型转换
- C程序判断你主机的数据存储方式(大端和小端)
- ~是什么意思 在C语言中,~0代表什么
- 【C语言】单向循环链表
- DLL的生命期
- C语言程序例题(四)
- C程序设计 例题 判读某一年是否为闰年(4)
- 【C语言】单向链表 头插法
- 针对PCL配置过程出现的问题的解决办法
- vs2008下用c++代码实现ACCESS的访问(DAO方式)
- 百度笔试题:malloc/free与new/delete的区别
- C程序设计 例题 判读某一年是否为闰年(3)
- C++内存池的极简版实现
- 浅谈C++多态性
- C语言基础题
- C++之类与构造函数