#include "Main.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 1;
}
if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
printf("%s",AICMD_MSG_EXPORT_HELP_E);
return 0;
}
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);
}
ImportData import;
ProcessStandardArguments(import,params+1,num-1);
std::string outf = "";
for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num;++i) {
if (!params[i]) {
continue;
}
if (!strncmp( params[i], "-f",2)) {
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(),::tolower);
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());
}
outfi = GetMatchingFormat(outf,true);
if (outfi == SIZE_MAX) {
outfi = GetMatchingFormat(outext,true);
if (outfi == SIZE_MAX) {
printf("assimp export: no output format specified and I failed to guess it\n");
return -23;
}
}
else {
outext = outf;
}
}
if (out[0] == '-') {
std::string::size_type s = in.find_last_of('.');
if (s == std::string::npos) {
s = in.length();
}
out = in.substr(0,s);
}
const aiExportFormatDesc* const e = globalExporter->GetExportFormatDescription(outfi);
printf("assimp export: select file format: \'%s\' (%s)\n",e->id,e->description);
const aiScene* scene = ImportModel(import,in);
if (!scene) {
return -39;
}
out += "."+outext;
if(!ExportModel(scene, import, out,e->id)) {
return -25;
}
printf("assimp export: wrote output file: %s\n",out.c_str());
return 0;
}
#endif