minor code readability fixes

git-svn-id: http://svn.clifford.at/openscad/trunk@264 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
kintel 2010-01-11 19:19:30 +00:00
parent a44e586611
commit f0833fd560
2 changed files with 50 additions and 40 deletions

View File

@ -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) DxfData::DxfData(double fn, double fs, double fa, QString filename, QString layername, double xorigin, double yorigin, double scale)
{ {
handle_dep(filename); handle_dep(filename); // Register ourselves as a dependency
QFile f(filename);
QFile f(filename);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { 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; return;
} }
QTextStream stream(&f); QTextStream stream(&f);
QList<Line> lines;
Grid2d< QVector<int> > grid; 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_entities_section = false;
bool in_blocks_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(_p1x, _p1y).append(lines.count()); \
grid.data(_p2x, _p2y).append(lines.count()); \ grid.data(_p2x, _p2y).append(lines.count()); \
if (in_entities_section) \ 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()) \ if (in_blocks_section && !current_block.isNull()) \
blockdata[current_block].append( \ blockdata[current_block].append( \
Line(p(_p1x, _p1y), p(_p2x, _p2y))); \ Line(addPoint(_p1x, _p1y), addPoint(_p2x, _p2y))); \
} while (0) } while (0)
QString mode, layer, name, iddata; 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; QHash<QString, int> unsupported_entities_list;
//
// Parse DXF file. Will populate this->points, this->dims, lines and blockdata
//
while (!stream.atEnd()) while (!stream.atEnd())
{ {
QString id_str = stream.readLine(); 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" && else if (mode == "DIMENSION" &&
(layername.isNull() || layername == layer)) { (layername.isNull() || layername == layer)) {
dims.append(Dim()); this->dims.append(Dim());
dims.last().type = dimtype; this->dims.last().type = dimtype;
for (int i = 0; i < 7; i++) for (int i = 0; i < 7; i++)
for (int j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
dims.last().coords[i][j] = coords[i][j]; this->dims.last().coords[i][j] = coords[i][j];
dims.last().angle = start_angle; this->dims.last().angle = start_angle;
dims.last().name = name; this->dims.last().name = name;
} }
else if (mode == "BLOCK") { else if (mode == "BLOCK") {
current_block = iddata; 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; QHash<int, int> enabled_lines;
for (int i = 0; i < lines.count(); i++) { for (int i = 0; i < lines.count(); i++) {
enabled_lines[i] = i; enabled_lines[i] = i;
@ -338,8 +348,8 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye
break; break;
create_open_path: create_open_path:
paths.append(Path()); this->paths.append(Path());
Path *this_path = &paths.last(); Path *this_path = &this->paths.last();
this_path->points.append(lines[current_line].p[current_point]); this_path->points.append(lines[current_line].p[current_point]);
while (1) { 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; int current_line = enabled_lines.begin().value(), current_point = 0;
paths.append(Path()); this->paths.append(Path());
Path *this_path = &paths.last(); Path *this_path = &this->paths.last();
this_path->is_closed = true; this_path->is_closed = true;
this_path->points.append(lines[current_line].p[current_point]); 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 #if 0
printf("----- DXF Data -----\n"); printf("----- DXF Data -----\n");
for (int i = 0; i < paths.count(); i++) { for (int i = 0; i < this->paths.count(); i++) {
printf("Path %d (%s):\n", i, paths[i].is_closed ? "closed" : "open"); printf("Path %d (%s):\n", i, this->paths[i].is_closed ? "closed" : "open");
for (int j = 0; j < paths[i].points.count(); j++) for (int j = 0; j < this->paths[i].points.count(); j++)
printf(" %f %f\n", paths[i].points[j]->x, paths[i].points[j]->y); printf(" %f %f\n", this->paths[i].points[j]->x, this->paths[i].points[j]->y);
} }
printf("--------------------\n"); printf("--------------------\n");
fflush(stdout); fflush(stdout);
@ -420,27 +430,27 @@ DxfData::DxfData(double fn, double fs, double fa, QString filename, QString laye
void DxfData::fixup_path_direction() void DxfData::fixup_path_direction()
{ {
if (paths.count() > 0) { if (this->paths.count() > 0) {
for (int i = 0; i < paths.count(); i++) { for (int i = 0; i < this->paths.count(); i++) {
if (!paths[i].is_closed) if (!this->paths[i].is_closed)
break; break;
paths[i].is_inner = true; this->paths[i].is_inner = true;
double min_x = paths[i].points[0]->x; double min_x = this->paths[i].points[0]->x;
int min_x_point = 0; int min_x_point = 0;
for (int j = 0; j < paths[i].points.count(); j++) { for (int j = 0; j < this->paths[i].points.count(); j++) {
if (paths[i].points[j]->x < min_x) { if (this->paths[i].points[j]->x < min_x) {
min_x = paths[i].points[j]->x; min_x = this->paths[i].points[j]->x;
min_x_point = j; min_x_point = j;
} }
} }
// rotate points if the path is in non-standard rotation // rotate points if the path is in non-standard rotation
int b = min_x_point; int b = min_x_point;
int a = b == 0 ? paths[i].points.count() - 2 : b - 1; int a = b == 0 ? this->paths[i].points.count() - 2 : b - 1;
int c = b == paths[i].points.count() - 1 ? 1 : b + 1; int c = b == this->paths[i].points.count() - 1 ? 1 : b + 1;
double ax = paths[i].points[a]->x - paths[i].points[b]->x; double ax = this->paths[i].points[a]->x - this->paths[i].points[b]->x;
double ay = paths[i].points[a]->y - paths[i].points[b]->y; double ay = this->paths[i].points[a]->y - this->paths[i].points[b]->y;
double cx = paths[i].points[c]->x - paths[i].points[b]->x; double cx = this->paths[i].points[c]->x - this->paths[i].points[b]->x;
double cy = paths[i].points[c]->y - paths[i].points[b]->y; double cy = this->paths[i].points[c]->y - this->paths[i].points[b]->y;
#if 0 #if 0
printf("Rotate check:\n"); printf("Rotate check:\n");
printf(" a/b/c indices = %d %d %d\n", a, b, c); 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)); printf(" b->c vector = %f %f (%f)\n", cx, cy, atan2(cx, cy));
#endif #endif
if (atan2(ax, ay) < atan2(cx, cy)) { if (atan2(ax, ay) < atan2(cx, cy)) {
for (int j = 0; j < paths[i].points.count()/2; j++) for (int j = 0; j < this->paths[i].points.count()/2; j++)
paths[i].points.swap(j, paths[i].points.count()-1-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)); this->points.append(Point(x, y));
return &points.last(); return &this->points.last();
} }

View File

@ -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(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); DxfData(const struct CGAL_Nef_polyhedron &N);
Point *p(double x, double y); Point *addPoint(double x, double y);
private: private:
void fixup_path_direction(); void fixup_path_direction();