diff options
-rw-r--r-- | dxfdata.cc | 88 | ||||
-rw-r--r-- | openscad.h | 2 |
2 files changed, 50 insertions, 40 deletions
@@ -36,20 +36,23 @@ DxfData::DxfData() { } +/*! + Reads a layer from the given file, or all layers if layename.isNull() + */ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString layername, double xorigin, double yorigin, double scale) { - handle_dep(filename); - QFile f(filename); + handle_dep(filename); // Register ourselves as a dependency + QFile f(filename); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - PRINTF("WARNING: Can't open DXF file `%s'.", filename.toAscii().data()); + PRINTF("WARNING: Can't open DXF file `%s'.", filename.toUtf8().data()); return; } QTextStream stream(&f); - QList<Line> lines; Grid2d< QVector<int> > grid; - QHash< QString, QList<Line> > blockdata; + QList<Line> lines; // Global lines + QHash< QString, QList<Line> > blockdata; // Lines in blocks bool in_entities_section = false; bool in_blocks_section = false; @@ -67,10 +70,11 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye grid.data(_p1x, _p1y).append(lines.count()); \ grid.data(_p2x, _p2y).append(lines.count()); \ if (in_entities_section) \ - lines.append(Line(p(_p1x, _p1y), p(_p2x, _p2y))); \ + lines.append( \ + Line(addPoint(_p1x, _p1y), addPoint(_p2x, _p2y))); \ if (in_blocks_section && !current_block.isNull()) \ blockdata[current_block].append( \ - Line(p(_p1x, _p1y), p(_p2x, _p2y))); \ + Line(addPoint(_p1x, _p1y), addPoint(_p2x, _p2y))); \ } while (0) QString mode, layer, name, iddata; @@ -86,6 +90,10 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye QHash<QString, int> unsupported_entities_list; + + // + // Parse DXF file. Will populate this->points, this->dims, lines and blockdata + // while (!stream.atEnd()) { QString id_str = stream.readLine(); @@ -223,13 +231,13 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye } else if (mode == "DIMENSION" && (layername.isNull() || layername == layer)) { - dims.append(Dim()); - dims.last().type = dimtype; + this->dims.append(Dim()); + this->dims.last().type = dimtype; for (int i = 0; i < 7; i++) for (int j = 0; j < 2; j++) - dims.last().coords[i][j] = coords[i][j]; - dims.last().angle = start_angle; - dims.last().name = name; + this->dims.last().coords[i][j] = coords[i][j]; + this->dims.last().angle = start_angle; + this->dims.last().name = name; } else if (mode == "BLOCK") { current_block = iddata; @@ -309,6 +317,8 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye } } + // Extract paths from parsed data + QHash<int, int> enabled_lines; for (int i = 0; i < lines.count(); i++) { enabled_lines[i] = i; @@ -338,8 +348,8 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye break; create_open_path: - paths.append(Path()); - Path *this_path = &paths.last(); + this->paths.append(Path()); + Path *this_path = &this->paths.last(); this_path->points.append(lines[current_line].p[current_point]); while (1) { @@ -373,8 +383,8 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye { int current_line = enabled_lines.begin().value(), current_point = 0; - paths.append(Path()); - Path *this_path = &paths.last(); + this->paths.append(Path()); + Path *this_path = &this->paths.last(); this_path->is_closed = true; this_path->points.append(lines[current_line].p[current_point]); @@ -408,10 +418,10 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye #if 0 printf("----- DXF Data -----\n"); - for (int i = 0; i < paths.count(); i++) { - printf("Path %d (%s):\n", i, paths[i].is_closed ? "closed" : "open"); - for (int j = 0; j < paths[i].points.count(); j++) - printf(" %f %f\n", paths[i].points[j]->x, paths[i].points[j]->y); + for (int i = 0; i < this->paths.count(); i++) { + printf("Path %d (%s):\n", i, this->paths[i].is_closed ? "closed" : "open"); + for (int j = 0; j < this->paths[i].points.count(); j++) + printf(" %f %f\n", this->paths[i].points[j]->x, this->paths[i].points[j]->y); } printf("--------------------\n"); fflush(stdout); @@ -420,27 +430,27 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye void DxfData::fixup_path_direction() { - if (paths.count() > 0) { - for (int i = 0; i < paths.count(); i++) { - if (!paths[i].is_closed) + if (this->paths.count() > 0) { + for (int i = 0; i < this->paths.count(); i++) { + if (!this->paths[i].is_closed) break; - paths[i].is_inner = true; - double min_x = paths[i].points[0]->x; + this->paths[i].is_inner = true; + double min_x = this->paths[i].points[0]->x; int min_x_point = 0; - for (int j = 0; j < paths[i].points.count(); j++) { - if (paths[i].points[j]->x < min_x) { - min_x = paths[i].points[j]->x; + for (int j = 0; j < this->paths[i].points.count(); j++) { + if (this->paths[i].points[j]->x < min_x) { + min_x = this->paths[i].points[j]->x; min_x_point = j; } } // rotate points if the path is in non-standard rotation int b = min_x_point; - int a = b == 0 ? paths[i].points.count() - 2 : b - 1; - int c = b == paths[i].points.count() - 1 ? 1 : b + 1; - double ax = paths[i].points[a]->x - paths[i].points[b]->x; - double ay = paths[i].points[a]->y - paths[i].points[b]->y; - double cx = paths[i].points[c]->x - paths[i].points[b]->x; - double cy = paths[i].points[c]->y - paths[i].points[b]->y; + int a = b == 0 ? this->paths[i].points.count() - 2 : b - 1; + int c = b == this->paths[i].points.count() - 1 ? 1 : b + 1; + double ax = this->paths[i].points[a]->x - this->paths[i].points[b]->x; + double ay = this->paths[i].points[a]->y - this->paths[i].points[b]->y; + double cx = this->paths[i].points[c]->x - this->paths[i].points[b]->x; + double cy = this->paths[i].points[c]->y - this->paths[i].points[b]->y; #if 0 printf("Rotate check:\n"); printf(" a/b/c indices = %d %d %d\n", a, b, c); @@ -448,16 +458,16 @@ void DxfData::fixup_path_direction() printf(" b->c vector = %f %f (%f)\n", cx, cy, atan2(cx, cy)); #endif if (atan2(ax, ay) < atan2(cx, cy)) { - for (int j = 0; j < paths[i].points.count()/2; j++) - paths[i].points.swap(j, paths[i].points.count()-1-j); + for (int j = 0; j < this->paths[i].points.count()/2; j++) + this->paths[i].points.swap(j, this->paths[i].points.count()-1-j); } } } } -DxfData::Point *DxfData::p(double x, double y) +DxfData::Point *DxfData::addPoint(double x, double y) { - points.append(Point(x, y)); - return &points.last(); + this->points.append(Point(x, y)); + return &this->points.last(); } @@ -445,7 +445,7 @@ public: DxfData(double fn, double fs, double fa, QString filename, QString layername = QString(), double xorigin = 0.0, double yorigin = 0.0, double scale = 1.0); DxfData(const struct CGAL_Nef_polyhedron &N); - Point *p(double x, double y); + Point *addPoint(double x, double y); private: void fixup_path_direction(); |