基于dxflib读取DXF文件

// DXF_test.cpp : 定义控制台应用程序的入口点。


#include "stdafx.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

#include "dl_dxf.h"
#include "dl_creationadapter.h"

#include "test_creationclass.h"

void usage();
void testReading(char* file);
void testWriting();

int main(int argc, char** argv) {

    char *filepath=".\\demo.dxf";
    testReading(filepath);

    testWriting();

    return 0;
}

/*
* @brief Prints error message if file name not specified as command
* line argument.
*/
void usage() {
    std::cout << "\nUsage: test <DXF file>\n\n";
}


void testReading(char* file) {
    // Load DXF file into memory:
    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;
    }

    std::cout << std::endl << "reading finish..." << std::endl;
    delete dxf;
    delete creationClass;
}

void testWriting() {
    DL_Dxf* dxf = new DL_Dxf();
    DL_Codes::version exportVersion = DL_Codes::AC1015;
    DL_WriterA* dw = dxf->out("myfile.dxf", exportVersion);
    if (dw == NULL) {
        printf("Cannot open file 'myfile.dxf' \
               for writing.");
        // abort function e.g. with return
    }

    dxf->writeHeader(*dw);
    dw->sectionEnd();
    dw->sectionTables();
    dxf->writeVPort(*dw);

    dw->tableLinetypes(3);
    dxf->writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
    dxf->writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
    dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0));
    dw->tableEnd();

    int numberOfLayers = 3;
    dw->tableLayers(numberOfLayers);

    dxf->writeLayer(*dw,
        DL_LayerData("0", 0),
        DL_Attributes(
            std::string(""),      // leave empty
            DL_Codes::black,        // default color
            100,                  // default width
            "CONTINUOUS", 1.0));       // default line style

    dxf->writeLayer(*dw,
        DL_LayerData("mainlayer", 0),
        DL_Attributes(
            std::string(""),
            DL_Codes::red,
            100,
            "CONTINUOUS", 1.0));

    dxf->writeLayer(*dw,
        DL_LayerData("anotherlayer", 0),
        DL_Attributes(
            std::string(""),
            DL_Codes::black,
            100,
            "CONTINUOUS", 1.0));

    dw->tableEnd();

    dw->tableStyle(1);
    dxf->writeStyle(*dw, DL_StyleData("standard", 0, 2.5, 1.0, 0.0, 0, 2.5, "txt", ""));
    dw->tableEnd();

    dxf->writeView(*dw);
    dxf->writeUcs(*dw);

    dw->tableAppid(1);
    dxf->writeAppid(*dw, "ACAD");
    dw->tableEnd();

    dxf->writeDimStyle(*dw, 1, 1, 1, 1, 1);

    dxf->writeBlockRecord(*dw);
    dxf->writeBlockRecord(*dw, "myblock1");
    dxf->writeBlockRecord(*dw, "myblock2");
    dw->tableEnd();

    dw->sectionEnd();

    dw->sectionBlocks();
    dxf->writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
    dxf->writeEndBlock(*dw, "*Model_Space");
    dxf->writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
    dxf->writeEndBlock(*dw, "*Paper_Space");
    dxf->writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
    dxf->writeEndBlock(*dw, "*Paper_Space0");

    dxf->writeBlock(*dw, DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0));
    // ...
    // write block entities e.g. with dxf->writeLine(), ..
    // ...
    dxf->writeEndBlock(*dw, "myblock1");

    dxf->writeBlock(*dw, DL_BlockData("myblock2", 0, 0.0, 0.0, 0.0));
    // ...
    // write block entities e.g. with dxf->writeLine(), ..
    // ...
    dxf->writeEndBlock(*dw, "myblock2");

    dw->sectionEnd();
    dw->sectionEntities();

    // write all entities in model space:
    dxf->writePoint(
        *dw,
        DL_PointData(10.0,
            45.0,
            0.0),
        DL_Attributes("mainlayer", 256, -1, "BYLAYER", 1.0));

    dxf->writeLine(
        *dw,
        DL_LineData(25.0,   // start point
            30.0,
            0.0,
            100.0,   // end point
            120.0,
            0.0),
        DL_Attributes("mainlayer", 256, -1, "BYLAYER", 1.0));

    dw->sectionEnd();

    dxf->writeObjects(*dw);
    dxf->writeObjectsEnd(*dw);

    dw->dxfEOF();
    dw->close();

    std::cout << "writting finish..." << std::endl;


    delete dw;
    delete dxf;
}

(1)官网下载dxflib源码文件

(2)src文件夹下的所有文件加入到工程项目下

(3)编译报sprintf错误:

解决方法:右键工程名-->属性-->C/C++-->预处理器-->预处理器定义,编辑右边输入框加入:

_CRT_SECURE_NO_WARNINGS

保存。

(4)编译提示缺少stdafx.h头文件:

解决方法:在每个加入的cpp文件最前面加上#include "stdafx.h"

(5)编译无错误无警告。

(6)添加下载的文件下example下readwrite文件夹下的test_creationclass.cpp和test_creationclass.h文件到项目中。

(7)把工程的main函数所在文件替换成以上代码。上述代码会读取当前目录下的demo.dxf,然后写myfile.dxf文件。

完整工程下载地址:https://download.youkuaiyun.com/download/qq_33628827/11205279

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

究极调参工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值