dxflib android

  1. 首先下載dxflib:https://github.com/clothbot/dxflib

  2. 然后將其中src文件導入到android 的cpp文件夾下
    如圖:

cpp.png
  1. 更改Cmakelist添加

src/main/cpp/dl_dxf.cpp
src/main/cpp/dl_writer_ascii.cpp

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html```

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s)

              src/main/cpp/dl_dxf.cpp
              src/main/cpp/dl_writer_ascii.cpp
              src/main/cpp/native-lib.cpp )


# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
  1. 編輯nativelib

這里主要是需要注意dxf的初始化 考抄,以保證生成文件滿足cad要求调榄。
還有更改dxf.cpp文件里的函數(shù) void DL_Dxf::writeStyle(DL_WriterA& dw) 中的 dw.dxfString( 3, "txt");為dw.dxfString( 3, "ARIAL.TTF");

#include <jni.h>
#include <string>
#include "dl_writer_ascii.h"
#include "dl_dxf.h"
#include "Android/log.h"
#include "dl_codes.h"

#define TAG    "myhello-jni-test" //
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)

#define LC_NAME_zh_CN_GBK        LC_NAME_zh_CN "." CSET_GBK
#define LC_NAME_zh_CN_UTF8        LC_NAME_zh_CN "." CSET_UTF8
#define LC_NAME_zh_CN_DEFAULT    LC_NAME_zh_CN_GBK
static DL_Codes::version sversion;
static DL_Attributes attributes = DL_Attributes("mainlayer", 256, -1, "BYLAYER");
DL_Dxf *dxf;
DL_WriterA *dw;

JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_addPoint(JNIEnv *env, jobject instance, jdouble x, jdouble y, jdouble z) {
    DL_PointData data = DL_PointData(x, y, z);
    dxf->writePoint(*dw, data, attributes);
    // TODO

}

extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_addText(JNIEnv *env, jobject instance, jdouble x, jdouble y, jdouble z,
                                  jstring text_) {
    const char *chars = env->GetStringUTFChars(text_, 0);
    // TODO
    DL_TextData data = DL_TextData(x, y, 0, 0, 1, 1, 0, 0, 0, 0, 0, chars, "", 0);
    dxf->writeText(*dw, data, attributes);
    env->ReleaseStringUTFChars(text_, chars);
}
extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_addLine(JNIEnv *env, jobject instance, jdouble x1, jdouble y1, jdouble z1,
                                  jdouble x2, jdouble y2, jdouble z3) {

    // TODO

}
extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_addCircle(JNIEnv *env, jobject instance, jdouble radius, jdouble x1,
                                    jdouble y1, jdouble y2) {

    // TODO

}



extern "C"
JNIEXPORT jint JNICALL
Java_adr_ycx_com_c_WriteC_init(JNIEnv *env, jobject instance, jint verison, jstring filepath_) {
    const char *filepath = env->GetStringUTFChars(filepath_, 0);
    // TODO const char *filepath_ = env->GetStringUTFChars(filepath, 0);
    switch (verison) {
        case 1:
            sversion = DL_Codes::AC1009;
            break;
        case 2:
            sversion = DL_Codes::AC1012;
            break;
        case 3:
            sversion = DL_Codes::AC1014;
            break;
        case 4:
            sversion = DL_Codes::AC1015;
            break;
        default:
            break;
    }
    dxf = new DL_Dxf();
    dw = dxf->out(filepath, sversion);
    if (dw == NULL) {
        return -1;
    } else {
        dxf->writeHeader(*dw);
        // int variable:
        dw->dxfString(9, "$INSUNITS");
        dw->dxfInt(70, 4);
        // real (double, float) variable:
        dw->dxfString(9, "$DIMEXE");
        dw->dxfReal(40, 1.25);
        // string variable:
        dw->dxfString(9, "$TEXTSTYLE");
        dw->dxfString(7, "Standard");
        // vector variable:
        dw->dxfString(9, "$LIMMIN");
        dw->dxfReal(10, 0.0);
        dw->dxfReal(20, 0.0);
        dw->sectionEnd();
        dw->sectionTables();
        dxf->writeVPort(*dw);
        dw->tableLineTypes(25);
        dxf->writeLineType(*dw, DL_LineTypeData("BYBLOCK", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("BYLAYER", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("CONTINUOUS", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("ACAD_ISO02W100", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("ACAD_ISO03W100", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("ACAD_ISO04W100", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("ACAD_ISO05W100", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("BORDER", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("BORDER2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("BORDERX2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("CENTER", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("CENTER2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("CENTERX2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DASHDOT", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DASHDOT2", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("DASHDOTX2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DASHED", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DASHED2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DASHEDX2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DIVIDE", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DIVIDE2", 0));
        dxf->writeLineType(*dw,
                           DL_LineTypeData("DIVIDEX2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DOT", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DOT2", 0));
        dxf->writeLineType(*dw, DL_LineTypeData("DOTX2", 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"));       // default line style

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

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

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

        dw->tableAppid(1);
        dw->tableAppidEntry(0x12);
        dw->dxfString(2, "ACAD");
        dw->dxfInt(70, 0);
        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();
    }
    env->ReleaseStringUTFChars(filepath_, filepath);
    return 1;
}
extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_close(JNIEnv *env, jobject instance) {

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

}
extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_startEnity(JNIEnv *env, jobject instance) {
    dw->sectionEntities();
    // TODO

}
extern "C"
JNIEXPORT void JNICALL
Java_adr_ycx_com_c_WriteC_endEnity(JNIEnv *env, jobject instance) {
    dw->sectionEnd();
    dxf->writeObjects(*dw);
    dxf->writeObjectsEnd(*dw);
    // TODO

}
  1. 最后需要將生產(chǎn)的文件進行轉(zhuǎn)碼幌墓,因為該方法導出文件是UTF_8的街夭,為了中文顯示正確赐劣,需要將其轉(zhuǎn)為CAD支持的GBK編碼,建議使用java轉(zhuǎn)換
 public static void convert(File file) throws IOException {
        // 如果是文件則進行編碼轉(zhuǎn)換竖幔,寫入覆蓋原文件
        int fileCount = 0;
        Charset sourceCharset = Charset.forName("utf-8");
        Charset targetCharset = Charset.forName("gbk");
        if (file.isFile()) {
            // 只處理.java結(jié)尾的代碼文件
            InputStreamReader isr = new InputStreamReader(new FileInputStream(
                    file), sourceCharset);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                // 注意寫入換行符
                sb.append(line + "\n");
            }
            br.close();
            isr.close();

            OutputStreamWriter osw = new OutputStreamWriter(
                    new FileOutputStream(file), targetCharset);
            BufferedWriter bw = new BufferedWriter(osw);
            // 以字符串的形式一次性寫入
            bw.write(sb.toString());
            bw.close();
            osw.close();

            System.out.println("Deal:" + file.getPath());
        
        } 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市食侮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌目胡,老刑警劉巖锯七,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異誉己,居然都是意外死亡眉尸,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門巨双,熙熙樓的掌柜王于貴愁眉苦臉地迎上來噪猾,“玉大人,你說我怎么就攤上這事筑累「だ” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵慢宗,是天一觀的道長坪蚁。 經(jīng)常有香客問我,道長镜沽,這世上最難降的妖魔是什么敏晤? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮缅茉,結(jié)果婚禮上嘴脾,老公的妹妹穿的比我還像新娘。我一直安慰自己蔬墩,他們只是感情好译打,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拇颅,像睡著了一般奏司。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蔬蕊,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天结澄,我揣著相機與錄音哥谷,去河邊找鬼岸夯。 笑死,一個胖子當著我的面吹牛们妥,可吹牛的內(nèi)容都是我干的猜扮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼监婶,長吁一口氣:“原來是場噩夢啊……” “哼旅赢!你這毒婦竟也來了齿桃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤煮盼,失蹤者是張志新(化名)和其女友劉穎短纵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體僵控,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡香到,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了报破。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悠就。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖充易,靈堂內(nèi)的尸體忽然破棺而出梗脾,到底是詐尸還是另有隱情,我是刑警寧澤盹靴,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布炸茧,位于F島的核電站,受9級特大地震影響稿静,放射性物質(zhì)發(fā)生泄漏宇立。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一自赔、第九天 我趴在偏房一處隱蔽的房頂上張望妈嘹。 院中可真熱鬧,春花似錦绍妨、人聲如沸润脸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽毙驯。三九已至,卻和暖如春灾测,著一層夾襖步出監(jiān)牢的瞬間爆价,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工媳搪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铭段,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓秦爆,卻偏偏與公主長得像序愚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子等限,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容