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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34510308/article/details/84313307

参考博客: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 <iostream>
#include <stdlib.h>
#include <stdio.h>

#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<BlockObj> ::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<BlockObj> ::iterator blockItor;//块容器迭代器  
	blockItor = creationClass->myblock.begin();
	while (blockItor != creationClass->myblock.end())
	{
		if (blockItor->drawflag == false)
		{
			blockItor++;
			continue;
		}
		//绘制直线
		vector<DXFLine> ::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<DXFCircle> ::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<DXFArc> ::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->bangle<arcItor->eangle)//逆时针圆弧
			{
				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<DXFText> ::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<DXFPolyLineEntities> ::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<BlockObj> ::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 (xrightborder<worldcoordinatepoint.x)
	{
		xrightborder = worldcoordinatepoint.x;
	}
	if (ytopborder>worldcoordinatepoint.y)
	{
		ytopborder = worldcoordinatepoint.y;
	}
	if (ybuttomborder<worldcoordinatepoint.y)
	{
		ybuttomborder = worldcoordinatepoint.y;
	}
}

void getimgdxfborder(Test_CreationClass *creationClass)
{
	vector<BlockObj> ::iterator blockItor;//块容器迭代器  
	blockItor = creationClass->myblock.begin();
	while (blockItor != creationClass->myblock.end())
	{
		if (blockItor->drawflag==false)
		{
			blockItor++;
			continue;
		}
		//直线
		vector<DXFLine> ::iterator lineItor;//块容器迭代器  
		lineItor = blockItor->line.begin();
		while (lineItor != blockItor->line.end())
		{
			getWorldCoordinateborder(blockItor, lineItor->beginpoint);
			getWorldCoordinateborder(blockItor, lineItor->endpoint);
			lineItor++;
		}
		//圆
		vector<DXFCircle> ::iterator circleItor;//块容器迭代器  
		circleItor = blockItor->circle.begin();
		while (circleItor != blockItor->circle.end())
		{
			getWorldCoordinateborder(blockItor, circleItor->centerpoint);
			int radius = circleItor->radius*blockItor->sx * zoom;
			circleItor++;
		}
		//圆弧
		vector<DXFArc> ::iterator arcItor;//块容器迭代器  
		arcItor = blockItor->arc.begin();
		while (arcItor != blockItor->arc.end())
		{
			getWorldCoordinateborder(blockItor, arcItor->centerpoint);
			int radius = arcItor->radius*blockItor->sx * zoom;
			arcItor++;
		}
		//文字
		vector<DXFText>::iterator testItor;//块容器迭代器
		testItor = blockItor->text.begin();
		while (testItor != blockItor->text.end())
		{
			getWorldCoordinateborder(blockItor, testItor->insertionpoint);
			string text = testItor->m_text;
			testItor++;
		}
		//绘制多线实体
		vector<DXFPolyLineEntities> ::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 << "Reading file " << file << "...\n";
	Test_CreationClass* creationClass = new Test_CreationClass();
	DL_Dxf* dxf = new DL_Dxf();
	if (!dxf->in(file,creationClass))// if file open failed
	{
		std::cerr << file << " could not be opened.\n";
		return 0;
	}
	getimgdxfborder(creationClass);//获取dxf文件描述图像宽高,以便建立相应图像
	Mat img((ybuttomborder - ytopborder)*1.2, (xrightborder - xleftborder)*1.2, CV_8UC3, Scalar(0, 0, 0));//1.2中的0.2为图像边缘的留白区域
	coordinatemove.x = (xrightborder - xleftborder)*0.1- xleftborder;
	coordinatemove.y = (ybuttomborder - ytopborder)*0.1- ytopborder;
	drawdxfimg(&img, creationClass);//绘制图像
	imwrite("save.tif",~img);
	system("save.png");
	waitKey(0);
	delete dxf;
	delete creationClass;
    return 0;
}


工程下载
https://download.csdn.net/download/qq_34510308/10797499

猜你喜欢

转载自blog.csdn.net/qq_34510308/article/details/84313307