Allow exporting Three.js either as bare mesh or mesh with viewer.
Most people just want a single self-contained .html file, but more advanced usage will involve embedding in a webpage, where the default viewer would be copied and customized, and fed with bare mesh export.
This commit is contained in:
parent
750842610c
commit
86315b2b1f
@ -575,7 +575,8 @@ void SolveSpaceUI::ExportMeshTo(const std::string &filename) {
|
|||||||
ExportMeshAsStlTo(f, m);
|
ExportMeshAsStlTo(f, m);
|
||||||
} else if(FilenameHasExtension(filename, ".obj")) {
|
} else if(FilenameHasExtension(filename, ".obj")) {
|
||||||
ExportMeshAsObjTo(f, m);
|
ExportMeshAsObjTo(f, m);
|
||||||
} else if(FilenameHasExtension(filename, ".js")) {
|
} else if(FilenameHasExtension(filename, ".js") ||
|
||||||
|
FilenameHasExtension(filename, ".html")) {
|
||||||
SEdgeList *e = &(SK.GetGroup(SS.GW.activeGroup)->displayEdges);
|
SEdgeList *e = &(SK.GetGroup(SS.GW.activeGroup)->displayEdges);
|
||||||
ExportMeshAsThreeJsTo(f, filename, m, e);
|
ExportMeshAsThreeJsTo(f, filename, m, e);
|
||||||
} else {
|
} else {
|
||||||
@ -658,15 +659,14 @@ void SolveSpaceUI::ExportMeshAsObjTo(FILE *f, SMesh *sm) {
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Export the mesh as a JavaScript script, which is compatible with Three.js.
|
// Export the mesh as a JavaScript script, which is compatible with Three.js.
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, SMesh *sm,
|
void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename,
|
||||||
SEdgeList *sel)
|
SMesh *sm, SEdgeList *sel)
|
||||||
{
|
{
|
||||||
SPointList spl = {};
|
SPointList spl = {};
|
||||||
STriangle *tr;
|
STriangle *tr;
|
||||||
SEdge *e;
|
SEdge *e;
|
||||||
Vector bndl, bndh;
|
Vector bndl, bndh;
|
||||||
const char html[] =
|
const char htmlbegin[] =
|
||||||
"/* Autogenerated Three.js viewer for Solvespace Model (copy into another document):\n"
|
|
||||||
"<!DOCTYPE html>\n"
|
"<!DOCTYPE html>\n"
|
||||||
"<html lang=\"en\">\n"
|
"<html lang=\"en\">\n"
|
||||||
" <head>\n"
|
" <head>\n"
|
||||||
@ -674,7 +674,9 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
|||||||
" <title>Three.js Solvespace Mesh</title>\n"
|
" <title>Three.js Solvespace Mesh</title>\n"
|
||||||
" <script src=\"http://threejs.org/build/three.min.js\"></script>\n"
|
" <script src=\"http://threejs.org/build/three.min.js\"></script>\n"
|
||||||
" <script src=\"http://threejs.org/examples/js/controls/OrthographicTrackballControls.js\"></script>\n"
|
" <script src=\"http://threejs.org/examples/js/controls/OrthographicTrackballControls.js\"></script>\n"
|
||||||
" <script src=\"%s.js\"></script>\n"
|
" <style type=\"text/css\">\n"
|
||||||
|
" body { margin: 0; overflow: hidden; }\n"
|
||||||
|
" </style>\n"
|
||||||
" </head>\n"
|
" </head>\n"
|
||||||
" <body>\n"
|
" <body>\n"
|
||||||
" <script>\n"
|
" <script>\n"
|
||||||
@ -848,11 +850,13 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
|||||||
" }\n"
|
" }\n"
|
||||||
" };\n"
|
" };\n"
|
||||||
"\n"
|
"\n"
|
||||||
" document.body.appendChild(solvespace(three_js_%s));\n"
|
" </script>\n"
|
||||||
|
" <script>\n";
|
||||||
|
const char htmlend[] =
|
||||||
|
" document.body.appendChild(solvespace(solvespace_model_%s));\n"
|
||||||
" </script>\n"
|
" </script>\n"
|
||||||
" </body>\n"
|
" </body>\n"
|
||||||
"</html>\n"
|
"</html>\n";
|
||||||
"*/\n\n";
|
|
||||||
|
|
||||||
// A default three.js viewer with OrthographicTrackballControls is
|
// A default three.js viewer with OrthographicTrackballControls is
|
||||||
// generated as a comment preceding the data.
|
// generated as a comment preceding the data.
|
||||||
@ -875,23 +879,27 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
|||||||
double largerBoundXY = max((bndh.x - bndl.x), (bndh.y - bndl.y));
|
double largerBoundXY = max((bndh.x - bndl.x), (bndh.y - bndl.y));
|
||||||
double largerBoundZ = max(largerBoundXY, (bndh.z - bndl.z + 1));
|
double largerBoundZ = max(largerBoundXY, (bndh.z - bndl.z + 1));
|
||||||
|
|
||||||
std::string baseFilename = filename;
|
std::string extension = filename,
|
||||||
|
noExtFilename = filename;
|
||||||
|
size_t dot = noExtFilename.rfind('.');
|
||||||
|
extension.erase(0, dot + 1);
|
||||||
|
noExtFilename.erase(dot);
|
||||||
|
|
||||||
|
std::string baseFilename = noExtFilename;
|
||||||
size_t lastSlash = baseFilename.rfind(PATH_SEP);
|
size_t lastSlash = baseFilename.rfind(PATH_SEP);
|
||||||
if(lastSlash == std::string::npos) oops();
|
if(lastSlash == std::string::npos) oops();
|
||||||
baseFilename.erase(0, lastSlash + 1);
|
baseFilename.erase(0, lastSlash + 1);
|
||||||
|
|
||||||
size_t dot = baseFilename.rfind('.');
|
|
||||||
baseFilename.erase(dot);
|
|
||||||
|
|
||||||
for(int i = 0; i < baseFilename.length(); i++) {
|
for(int i = 0; i < baseFilename.length(); i++) {
|
||||||
if(!isalpha(baseFilename[i]) &&
|
if(!isalpha(baseFilename[i]) &&
|
||||||
/* also permit UTF-8 */ !((unsigned char)baseFilename[i] >= 0x80))
|
/* also permit UTF-8 */ !((unsigned char)baseFilename[i] >= 0x80))
|
||||||
baseFilename[i] = '_';
|
baseFilename[i] = '_';
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(f, html, baseFilename.c_str(), baseFilename.c_str());
|
if(extension == "html")
|
||||||
fprintf(f, "var three_js_%s = {\n"
|
fputs(htmlbegin, f);
|
||||||
|
|
||||||
|
fprintf(f, "var solvespace_model_%s = {\n"
|
||||||
" bounds: {\n"
|
" bounds: {\n"
|
||||||
" x: %f, y: %f, near: %f, far: %f, z: %f, edgeBias: %f\n"
|
" x: %f, y: %f, near: %f, far: %f, z: %f, edgeBias: %f\n"
|
||||||
" },\n",
|
" },\n",
|
||||||
@ -974,6 +982,10 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
|||||||
}
|
}
|
||||||
|
|
||||||
fputs(" ]\n};\n", f);
|
fputs(" ]\n};\n", f);
|
||||||
|
|
||||||
|
if(extension == "html")
|
||||||
|
fprintf(f, htmlend, baseFilename.c_str());
|
||||||
|
|
||||||
spl.Clear();
|
spl.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +188,8 @@ int LoadAutosaveYesNo(void);
|
|||||||
#define MESH_PATTERN \
|
#define MESH_PATTERN \
|
||||||
PAT1("STL Mesh", "stl") \
|
PAT1("STL Mesh", "stl") \
|
||||||
PAT1("Wavefront OBJ Mesh", "obj") \
|
PAT1("Wavefront OBJ Mesh", "obj") \
|
||||||
PAT1("Three.js-compatible JavaScript Mesh", "js") \
|
PAT1("Three.js-compatible Mesh, with viewer", "html") \
|
||||||
|
PAT1("Three.js-compatible Mesh, mesh only", "js") \
|
||||||
ENDPAT
|
ENDPAT
|
||||||
// NURBS surfaces
|
// NURBS surfaces
|
||||||
#define SRF_PATTERN PAT2("STEP File", "step", "stp") ENDPAT
|
#define SRF_PATTERN PAT2("STEP File", "step", "stp") ENDPAT
|
||||||
@ -856,7 +857,8 @@ public:
|
|||||||
void ExportMeshTo(const std::string &filename);
|
void ExportMeshTo(const std::string &filename);
|
||||||
void ExportMeshAsStlTo(FILE *f, SMesh *sm);
|
void ExportMeshAsStlTo(FILE *f, SMesh *sm);
|
||||||
void ExportMeshAsObjTo(FILE *f, SMesh *sm);
|
void ExportMeshAsObjTo(FILE *f, SMesh *sm);
|
||||||
void ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, SMesh *sm, SEdgeList *sel);
|
void ExportMeshAsThreeJsTo(FILE *f, const std::string &filename,
|
||||||
|
SMesh *sm, SEdgeList *sel);
|
||||||
void ExportViewOrWireframeTo(const std::string &filename, bool wireframe);
|
void ExportViewOrWireframeTo(const std::string &filename, bool wireframe);
|
||||||
void ExportSectionTo(const std::string &filename);
|
void ExportSectionTo(const std::string &filename);
|
||||||
void ExportWireframeCurves(SEdgeList *sel, SBezierList *sbl,
|
void ExportWireframeCurves(SEdgeList *sel, SBezierList *sbl,
|
||||||
|
Loading…
Reference in New Issue
Block a user