基于开源库dxflib 解析dxf文件,qt绘制线段
·
开源库基本用法
根据上文
我们把dxf文件中所有线段的数据都保存到
//analyzeDXF.h
std::vector < std::pair<DL_LineData, DL_Attributes>> m_vecDxfLines;
//analyzeDXF.cpp
void analyzeDXF::addLine(const DL_LineData& data)
{
m_vecDxfLines.push_back(std::pair<DL_LineData, DL_Attributes>(data, attributes));
}
然后遍历该成员变量绘图
其中 line的颜色和宽度保存在DL_Attributes,点的信息保存在DL_LineData中
根据上文我们知道
if (attributes.getColor() == 256) {
printf("BYLAYER");
}
else if (attributes.getColor() == 0) {
printf("BYBLOCK");
}
else {
printf("%d", attributes.getColor());
}
color的值有时候是根据layer的属性来的,所以首先要解析layer的数据
我们使用一个map来存贮 每一层对应的颜色。在cad中颜色是不rgb的形式,而是一个索引(int)对应一个rgb值
QMap<QString, int> m_layerColorMap; //每一层默认的颜色index
然后我们遍历层的数据
void analyzeDXF::drawLayers( )
{
m_layerColorMap.clear();
std::vector< std::pair<DL_LayerData, DL_Attributes>>::iterator iter;
int num = 1;
for (iter = m_vecDxfLayers.begin(); iter < m_vecDxfLayers.end(); iter++)
{
DL_LayerData &data = (*iter).first;
DL_Attributes &attributes = (*iter).second;
//获取该层的名称
QString layerName = data.name.c_str();
int colorIndex;
if (attributes.getColor() > 0 && attributes.getColor() < 256)
{
colorIndex = attributes.getColor();
}
else
{
//{7,QColor(0,0,0) } ,
colorIndex = 7; //设置默认颜色为黑色
}
m_layerColorMap.insert(layerName, colorIndex);
}
}
本文把rgb对应的颜色索引存储到colorMap 中
QMap<int, QColor> colorMap =
{
{1,QColor(255,0,0) } ,
{2,QColor(255,255,0) } ,
...
}
从线段的数据中获取颜色索引 int nColor =attributes.getColor();
如果索引nColor == 256 || nColor == 0,则从层的信息里面寻找
代码如下
void analyzeDXF::drawLine( )
{
std::vector< std::pair<DL_LineData, DL_Attributes>>::iterator iter;
int num = 1;
for (iter = m_vecDxfLines.begin(); iter < m_vecDxfLines.end(); iter++)
{
//获取颜色
int nColor =attributes.getColor();
if (nColor == 256 || nColor == 0) //BYLAYER
{
QString layerName = attributes.getLayer().c_str();
nColor = m_layerColorMap[layerName];
}
QColor lineColor = colorMap[nColor];
//获取宽度
int lineWidth = attributes.getWidth();
if (lineWidth <1)
{
lineWidth = 1;
}
QPen pen(lineColor, lineWidth ,Qt::SolidLine);
m_painter->setPen(pen);
DL_LineData &data = (*iter).first;
//只画二图图像 data.z1不要
checkPixmapSize({ data.x1, data.y1, data.x2, data.y2 });
m_painter->drawLine(data.x1, data.y1, data.x2, data.y2);
num++;
}
}
如何绘图呢
m_PixMap = new QPixmap( width , height );
m_PixMap ->fill(Qt::transparent);//背景透明
m_painter->begin(m_PixMap);
m_painter->setWindow(-pix.width() / 2, pix.height() / 2, pix.width(), -pix.height()); //将中心点设为原点 x正方向朝右 y轴正方向朝上
drawLayers();
drawLine();
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)