QCAD dxf 扩展属性

XDataCAD开放给用户(程序员)的一个功能接口:可以往CAD实体上写入/绑定你的个人信息,CAD不会理会/管理你的个人信息,但会保存到DXF文件中。同时你也可以读取这些个人的信息,方便你编程使用。

QCAD读写dxf文件使用的dxflib。如下所示,利用dxflib写入扩展属性。

dw.dxfString(1001,"Point");
dw.dxfString(1002,"{");
dw.dxfString(1000,"代码");
dw.dxfString(1000,"201365845");
dw.dxfString(1002,"}");

QCAD源码中只需在读入和写入DXF时,加入读写扩展属性(自定义属性)即可。

写入属性

//    dw.dxfString(1001,"Point");
//    dw.dxfString(1000,"代码:201365845");
void RDxfExporter::writeCustomProperties(const REntity& p)
{
    
    
    QMap<QString, QVariantMap>customProperties=p.getCustomProperties();
    QString kv;
    foreach (QString title, customProperties.keys()) {
    
    
        QVariantMap properties=customProperties.value(title);

        dw->dxfString(1001, (const char*)RDxfExporter::escapeUnicode(title));
        foreach (const QString key, properties.keys()) {
    
    
            QVariant value=properties.value(key);
            kv=QString("%1:%2").arg(key,value.toString());
            dw->dxfString(1000,(const char*)RDxfExporter::escapeUnicode(kv));
        }
    }
}

读入属性

void RDxfImporter::importXData(QSharedPointer<REntity> entity)
{
    
    
    QStringList pJson;
    QString str;
    QPair<int,QVariant>p;
    foreach (const QString group, xData.keys()) {
    
    
        QList<QPair<int, QVariant>> properties=xData.value(group);
        for(int i=0;i<properties.size();++i){
    
    
            p=properties.at(i);
            str=p.second.toString();
            if(str.contains(':')){
    
    
                 pJson=str.split(":");
                 if(pJson.length()>1)
                     entity->setProperty(RPropertyTypeId(group,pJson[0]),pJson[1]);
            }
        }

    }
}

这样就可以在QCAD属性编辑面板中添加自定义扩展属性。
在这里插入图片描述

DXF文件中保存的属性:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/mrbaolong/article/details/109752226