基于开源库dxflib解析dxf文件: qt绘制多段线

基于开源库dxflib 解析dxf文件,qt绘制线段

和上文一样
继承DL_CreationAdapter接口实现 addPolyline和addVertex

class analyzeDXF : public DL_CreationAdapter
{
public:
	analyzeDXF() {};
	~analyzeDXF() {};
	//多线段
	virtual void addPolyline(const DL_PolylineData& data);
	virtual void addVertex(const DL_VertexData& data);
private:
	//画多线段 LWPOLYLINE
	void drawPolyline();
	std::vector < std::pair<DL_PolylineData, DL_Attributes>>  m_vecDxfPolylines;
	std::vector< std::pair<DL_VertexData, DL_Attributes>>  m_vecDxfVertexs;
}

把数据保存到 成员变量m_vecDxfPolylines和 m_vecDxfVertexs

我们查资料得到

在dxflib库中多段线图元和优化多段线图元均是通过这两者读出,首先在addPolyline(const DL_PolylineData& data)中获得点数(data.number)和标志位(data.flags),其中点数表示了addVertex中的点数,而标志位则表示了该多段线的闭合。需要注意的是在读写LWPOLYLINE时所构成的点需要考虑点的凸度,如果凸度不为0,则需要进行弧的绘制。
————————————————
版权声明:本文为CSDN博主「S3Z10」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/syhzju/article/details/80110633

猜测多线段每个线段的信息保存在 m_vecDxfPolylines,而每个点保存到m_vecDxfVertexs
验证一下:

void analyzeDXF::drawPolyline()
{
	std::vector< std::pair<DL_PolylineData, DL_Attributes>>::iterator iter;
	int num = 0;
	int sum = 0;
	for (iter = m_vecDxfPolylines.begin(); iter < m_vecDxfPolylines.end(); iter++)
	{
		num++;
		DL_PolylineData &data = (*iter).first;
		sum += data.number;
		//线段是否闭合flags
		qDebug() << "Polyline: " << "number :" << data.number <<" , flags: "<<data.flags<< " , m: " << data.m << " , n: " << data.n << " ,elevation " << data.elevation;
	}
	qDebug() << "Polyline number is  " << num << ". the sum of Vertexs is " <<sum;
	std::vector< std::pair<DL_VertexData, DL_Attributes>>::iterator iter2;
	num = 0;
	for (iter2 = m_vecDxfVertexs.begin(); iter2 < m_vecDxfVertexs.end(); iter2++)
	{
		DL_VertexData &data = (*iter2).first;
		//qDebug() << "Vertex: (" << data.x<<" , " << data.y << ") ,Bulge: " << data.bulge;
		num++;
	}
	qDebug() << "Vertex number is  " << num;
}

其中 DL_PolylineData 结构体 的成员 unsigned int number; 表示有多少个顶点。 int flags;表示多线段是否闭合

struct DXFLIB_EXPORT DL_PolylineData {
    /**
     * Constructor.
     * Parameters: see member variables.
     */
    DL_PolylineData(int pNumber, int pMVerteces, int pNVerteces, int pFlags, double pElevation = 0.0) {
        number = pNumber;
        m = pMVerteces;
        n = pNVerteces;
        elevation = pElevation;
        flags = pFlags;
    }

    /*! Number of vertices in this polyline. */
    unsigned int number;

    /*! Number of vertices in m direction if polyline is a polygon mesh. */
    unsigned int m;

    /*! Number of vertices in n direction if polyline is a polygon mesh. */
    unsigned int n;

    /*! elevation of the polyline. */
    double elevation;

    /*! Flags */
    int flags;
};

在这里插入图片描述
如图所示 , 所有线段顶点的和 与 定点数对上了!
所以我们需要遍历每个DL_PolylineData ,得到该多线段有几个顶点,是否闭合,然后从m_vecDxfVertexs把顶点取出来

首先存储点集和线段信息

void analyzeDXF::addPolyline(const DL_PolylineData& data)
{
	qDebug() << "addPolyline:  ";
	m_vecDxfPolylines.push_back(std::pair<DL_PolylineData, DL_Attributes>(data, attributes));
}

void analyzeDXF::addVertex(const DL_VertexData& data)
{
	double x = data.x;
	double y = data.y;
	OCStoWCS(*extrusion, x, y);
	DL_VertexData newData = data;
	newData.x = x;
	newData.y = y;
	m_vecDxfVertexs.push_back(std::pair<DL_VertexData, DL_Attributes>(newData, attributes));
}

然后遍历m_vecDxfPolylines 找到每一段线段对应的点,绘图

void analyzeDXF::drawPolyline()
{
	std::vector< std::pair<DL_PolylineData, DL_Attributes>>::iterator iter;
	int index = 0;//当前线段的第一个点在m_vecDxfVertexs中的索引
	for (iter = m_vecDxfPolylines.begin(); iter < m_vecDxfPolylines.end(); iter++)
	{
		//获取颜色
		QColor arcColor = getColor((*iter).second);
		//获取宽度
		int lineWidth = getWidth((*iter).second);
		QPen pen(arcColor, lineWidth, Qt::SolidLine);
		m_painter->setPen(pen);

		DL_PolylineData &data = (*iter).first;
		int subIndex = 0;
		//每个折线取出第一个点
		DL_VertexData preVertex = m_vecDxfVertexs[index].first;
		DL_VertexData curVertex;
		subIndex++;
		int countVertex = data.number;//该线段含有几个点
		for (subIndex; subIndex < countVertex; subIndex++)
		{
			//取折线的第二个点
			curVertex = m_vecDxfVertexs[index + subIndex].first;
			if (preVertex.bulge == 0)//线段
			{
				m_painter->drawLine(preVertex.x, preVertex.y, curVertex.x, curVertex.y);

			}
			else//圆弧
			{
				QRectF  rectangle;
				double  startAngle, spanAngle;
				calArc(preVertex.x , preVertex.y, curVertex.x , curVertex.y, preVertex.bulge , rectangle , startAngle, spanAngle);
				m_painter->drawArc(rectangle, startAngle, spanAngle);
			}
			preVertex = curVertex;
		}
		if (data.flags == 1) //闭合
		{
			preVertex = m_vecDxfVertexs[index].first;
			curVertex = m_vecDxfVertexs[index + data.number-1].first;
			m_painter->drawLine(preVertex.x, preVertex.y, curVertex.x, curVertex.y);
		}
		index = index + data.number;
	}
}
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值