Export materials file for gcode toolpaths when exported to obj file

This commit is contained in:
Enrico Turri 2019-08-20 14:35:23 +02:00
parent 2e3c71baaf
commit 1b6490af4c

View File

@ -886,14 +886,51 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const
if (!has_toolpaths_to_export()) if (!has_toolpaths_to_export())
return; return;
FILE* fp = boost::nowide::fopen(filename, "w"); // collect color information to generate materials
std::set<std::array<float, 4>> colors;
for (const GLVolume* volume : this->volumes)
{
if (!can_export_to_obj(*volume))
continue;
std::array<float, 4> color;
::memcpy((void*)color.data(), (const void*)volume->color, 4 * sizeof(float));
colors.insert(color);
}
// save materials file
boost::filesystem::path mat_filename(filename);
mat_filename.replace_extension("mtl");
FILE* fp = boost::nowide::fopen(mat_filename.string().c_str(), "w");
if (fp == nullptr) {
BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << mat_filename.string().c_str() << " for writing";
return;
}
fprintf(fp, "# G-Code Toolpaths Materials\n");
fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
unsigned int colors_count = 1;
for (const std::array<float, 4>& color : colors)
{
fprintf(fp, "\nnewmtl material_%d\n", colors_count++);
fprintf(fp, "Ka 1 1 1\n");
fprintf(fp, "Kd %f %f %f\n", color[0], color[1], color[2]);
fprintf(fp, "Ks 0 0 0\n");
}
fclose(fp);
// save geometry file
fp = boost::nowide::fopen(filename, "w");
if (fp == nullptr) { if (fp == nullptr) {
BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << filename << " for writing"; BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << filename << " for writing";
return; return;
} }
fprintf(fp, "# G-Code Toolpaths\n"); fprintf(fp, "# G-Code Toolpaths\n");
fprintf(fp, "# Generated by %s based on Slic3r\n\n", SLIC3R_BUILD_ID); fprintf(fp, "# Generated by %s based on Slic3r\n", SLIC3R_BUILD_ID);
fprintf(fp, "\nmtllib ./%s\n", mat_filename.filename().string().c_str());
unsigned int vertices_count = 0; unsigned int vertices_count = 0;
unsigned int volumes_count = 0; unsigned int volumes_count = 0;
@ -987,6 +1024,12 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const
fprintf(fp, "vn %f %f %f\n", vertices_and_normals_interleaved[i + 0], vertices_and_normals_interleaved[i + 1], vertices_and_normals_interleaved[i + 2]); fprintf(fp, "vn %f %f %f\n", vertices_and_normals_interleaved[i + 0], vertices_and_normals_interleaved[i + 1], vertices_and_normals_interleaved[i + 2]);
} }
std::array<float, 4> color;
::memcpy((void*)color.data(), (const void*)volume->color, 4 * sizeof(float));
colors.insert(color);
fprintf(fp, "\n# material volume %d\n", volumes_count);
fprintf(fp, "usemtl material_%lld\n", 1 + std::distance(colors.begin(), colors.find(color)));
fprintf(fp, "\n# triangular facets volume %d\n", volumes_count); fprintf(fp, "\n# triangular facets volume %d\n", volumes_count);
for (unsigned int i = 0; i < triangle_indices.size(); i += 3) for (unsigned int i = 0; i < triangle_indices.size(); i += 3)
{ {