summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/tools/assimp_cmd/Export.cpp
blob: fd9fc7652d3d68cf1e40b7b4c9c7f87d65a2f0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------

Copyright (c) 2006-2022, assimp team

All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/

/** @file  Export.cpp
 *  @brief Implementation of the 'assimp export' utility
 */

#include "Main.h"
#include <assimp/ParsingUtils.h>
#include <assimp/StringUtils.h>

#ifndef ASSIMP_BUILD_NO_EXPORT

const char *AICMD_MSG_EXPORT_HELP_E =
        "assimp export <model> [<out>] [-f<h>] [common parameters]\n"
        "\t -f<h> Specify the file format. If omitted, the output format is \n"
        "\t\tderived from the file extension of the given output file  \n"
        "\t[See the assimp_cmd docs for a full list of all common parameters]  \n";

// -----------------------------------------------------------------------------------
size_t GetMatchingFormat(const std::string &outf, bool byext = false) {
    for (size_t i = 0, end = globalExporter->GetExportFormatCount(); i < end; ++i) {
        const aiExportFormatDesc *const e = globalExporter->GetExportFormatDescription(i);
        if (outf == (byext ? e->fileExtension : e->id)) {
            return i;
        }
    }
    return SIZE_MAX;
}

// -----------------------------------------------------------------------------------
int Assimp_Export(const char *const *params, unsigned int num) {
    const char *const invalid = "assimp export: Invalid number of arguments. See \'assimp export --help\'\n";
    if (num < 1) {
        printf(invalid);
        return AssimpCmdError::InvalidNumberOfArguments;
    }

    // --help
    if (!strcmp(params[0], "-h") || !strcmp(params[0], "--help") || !strcmp(params[0], "-?")) {
        printf("%s", AICMD_MSG_EXPORT_HELP_E);
        return AssimpCmdError::Success;
    }

    std::string in = std::string(params[0]);
    std::string out = (num > 1 ? std::string(params[1]) : "-"), outext;

    //
    const std::string::size_type s = out.find_last_of('.');
    if (s != std::string::npos) {
        outext = out.substr(s + 1);
        out = out.substr(0, s);
    }

    // get import flags
    ImportData import;
    ProcessStandardArguments(import, params + 1, num - 1);

    // process other flags
    std::string outf = "";
    for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num; ++i) {
        if (!params[i]) {
            continue;
        }
        if (!strncmp(params[i], "-f", 2)) {
            if (strncmp(params[i], "-fi", 3))
                outf = std::string(params[i] + 2);
        } else if (!strncmp(params[i], "--format=", 9)) {
            outf = std::string(params[i] + 9);
        }
    }

    std::transform(outf.begin(), outf.end(), outf.begin(), ai_tolower<char>);

    // convert the output format to a format id
    size_t outfi = GetMatchingFormat(outf);
    if (outfi == SIZE_MAX) {
        if (outf.length()) {
            printf("assimp export: warning, format id \'%s\' is unknown\n", outf.c_str());
        }

        // retry to see if we know it as file extension
        outfi = GetMatchingFormat(outf, true);
        if (outfi == SIZE_MAX) {
            // retry to see if we know the file extension of the output file
            outfi = GetMatchingFormat(outext, true);

            if (outfi == SIZE_MAX) {
                // still no match -> failure
                printf("assimp export: no output format specified and I failed to guess it\n");
                return -23;
            }
        } else {
            outext = outf;
        }
    }

    // if no output file is specified, take the file name from input file
    if (out[0] == '-') {
        std::string::size_type pos = in.find_last_of('.');
        if (pos == std::string::npos) {
            pos = in.length();
        }

        out = in.substr(0, pos);
    }

    const aiExportFormatDesc *const e = globalExporter->GetExportFormatDescription(outfi);
    printf("assimp export: select file format: \'%s\' (%s)\n", e->id, e->description);

    // import the  model
    const aiScene *scene = ImportModel(import, in);
    if (!scene) {
        return AssimpCmdExportError::FailedToImportModel;
    }

    // derive the final file name
    out += "." + outext;

    // and call the export routine
    if (!ExportModel(scene, import, out, e->id)) {
        return AssimpCmdExportError::FailedToExportModel;
    }
    printf("assimp export: wrote output file: %s\n", out.c_str());
    return AssimpCmdError::Success;
}

#endif // no export