X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffileio.cpp;h=8058a41d490aba1d0e7b79847e8067326fb61a3c;hb=10cf4c797bed05831e976068b7504908279dc997;hp=02ab2f577dc60af0a95f392a60e069b32b57b631;hpb=acd27aa62a4c78b6aeee523e3145a8aa53f4bcde;p=architektonas diff --git a/src/fileio.cpp b/src/fileio.cpp index 02ab2f5..8058a41 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -19,6 +19,9 @@ #include #include #include +#include "applicationwindow.h" +#include "drawingview.h" +#include "global.h" #include "structs.h" /* @@ -58,7 +61,7 @@ Connect() function. Or, instead of a point, a parameter value? Doing that, with the Line and a parameter "t", if t == 0 we have endpoint 1. if t == 1, then we have endpoint 2. With a Circle, the parameter is a number -between 0 and 1 (scaled to 0 to 2π). With an Arc, the parameter goes from 0 to +between 0 and 1 (scaled to 0 to tau). With an Arc, the parameter goes from 0 to 1, 0 being enpoint 1 and 1 being endpoint 2. How does this work for moving objects that are connected? Again, with the Line @@ -105,18 +108,13 @@ OTHER CONSIDERATIONS: - Need to figure out how to store the Layer list (should layer list be optional?) - Need to figure out how to store the Block list and blocks - */ -//enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFLine, OTFCircle, OTFArc, -// OTFDimension, OTFPolygon, OTFText, OTFImage, OTFBlock, OTFEndOfFile }; enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; - // Instantiate class variables /*static*/ int FileIO::objectFileType = OTFObject; - /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * c) { /* Approach: loop through the container, doing a depth-first traversal. Any @@ -125,28 +123,54 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; virtual Object function that reports the object by itself if it's a non- Container, otherwise it enumerates all objects within itself. */ - fprintf(file, "ARCHITEKTONAS DRAWING V1.1\n"); + fprintf(file, "ARCHITEKTONAS DRAWING V1.2\n"); + fprintf(file, "PROPERTIES 4\n"); + fprintf(file, "BASE_UNIT %i\n", c->baseUnit); + fprintf(file, "UNIT_STYLE %i\n", c->unitStyle); + fprintf(file, "DEC_PREC %i\n", c->decimalPrecision); + fprintf(file, "FRAC_PREC %i\n", c->fractionalPrecision); + + fprintf(file, "LAYERS %i\n", Global::numLayers); + + for(int i=0; i containerStack; Container * currentTopContainer = drawing; + ResetLayerVectors(); while (!feof(file)) { @@ -194,7 +219,6 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; return false; } - /*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing) { // Approach: read each object in the file, one by one. If the object is a @@ -202,6 +226,7 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; // This will require a stack to maintain the current Container. std::vector containerStack; Container * currentTopContainer = drawing; + ResetLayerVectors(); while (!feof(file)) { @@ -227,6 +252,12 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; } else if (objectFileType == OTFContainerEnd) { + // Add the extents of the current container + Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer); + currentTopContainer->p[0] = r.TopLeft(); + currentTopContainer->p[1] = r.BottomRight(); +//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b); + // Container is done, so pop the stack to get back the previous TLC currentTopContainer = containerStack.back(); containerStack.pop_back(); @@ -241,8 +272,86 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; return false; } +/*static*/ bool FileIO::LoadVersion1_2(FILE * file, Container * drawing) +{ + int hidden, locked, props = 0; + char textBuffer[65536]; + + // Load drawing properties, if any, first + fscanf(file, "PROPERTIES %i\n", &props); + + if (props == 4) + { + fscanf(file, "BASE_UNIT %i\n", &(drawing->baseUnit)); + fscanf(file, "UNIT_STYLE %i\n", &(drawing->unitStyle)); + fscanf(file, "DEC_PREC %i\n", &(drawing->decimalPrecision)); + fscanf(file, "FRAC_PREC %i\n", &(drawing->fractionalPrecision)); + } + + // Load layer information next + fscanf(file, "LAYERS %i\n", &Global::numLayers); + + for(int i=0; i containerStack; + Container * currentTopContainer = drawing; + + while (!feof(file)) + { + // Reconstruct the object (extended format!) + Object * obj = GetObjectFromFile(file, true, true); + + // objectFileType is set in GetObjectFromFile()... + if (objectFileType == OTFObject) + { + if (obj == NULL) + return false; + + currentTopContainer->objects.push_back(obj); +//printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size()); + + // If the object is a container, push current TLC on the stack and + // set it as the new TLC + if (obj->type == OTContainer) + { + containerStack.push_back(currentTopContainer); + currentTopContainer = (Container *)obj; + } + } + else if (objectFileType == OTFContainerEnd) + { + // Add the extents of the current container + Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer); + currentTopContainer->p[0] = r.TopLeft(); + currentTopContainer->p[1] = r.BottomRight(); +//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b); + + // Container is done, so pop the stack to get back the previous TLC + currentTopContainer = containerStack.back(); + containerStack.pop_back(); + } + else if (objectFileType == OTFEndOfFile) + { +//printf("Load: container size = %li\n", drawing->objects.size()); + return true; + } + } + + return false; +} -/*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/) +/*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/, bool ext2/*= false*/) { char buffer[256]; char textBuffer[65536]; @@ -280,6 +389,7 @@ if (errno) } // Need to add pen attributes as well... do that for v1.1 :-P + // And fill attributes... do that for v1.3 (1.2 added layers) if (strcmp(buffer, "LINE") == 0) { Point p1, p2; @@ -300,6 +410,26 @@ if (errno) fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2); obj = (Object *)new Arc(p, r, a1, a2); } + else if (strcmp(buffer, "POLYLINE") == 0) + { + long int size; + std::vector pts; + uint32_t color; + float thickness; + int style; + + fscanf(file, "(%li)", &size); + fscanf(file, " (%i, %f, %i)\n", &color, &thickness, &style); + + for(int i=0; ilayer = foundLayer; - if (extended && (obj->type != OTContainer)) + if (extended && (obj->type != OTContainer) && (obj->type != OTPolyline)) { - fscanf(file, " (%i, %f, %i)", &obj->color, &obj->thickness, &obj->style); + fscanf(file, " (%i, %f, %i)\n", &obj->color, &obj->thickness, &obj->style); } } return obj; } - /*static*/ bool FileIO::WriteObjectToFile(FILE * file, Object * obj) { // Sanity check @@ -352,21 +486,30 @@ if (errno) switch (obj->type) { case OTLine: - fprintf(file, "LINE %i (%lf,%lf) (%lf,%lf)", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y); + fprintf(file, "LINE %i (%.16lf,%.16lf) (%.16lf,%.16lf)", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y); break; case OTCircle: - fprintf(file, "CIRCLE %i (%lf,%lf) %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0]); + fprintf(file, "CIRCLE %i (%.16lf,%.16lf) %.16lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0]); break; case OTEllipse: break; case OTArc: - fprintf(file, "ARC %i (%lf,%lf) %lf, %lf, %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0], obj->angle[0], obj->angle[1]); + fprintf(file, "ARC %i (%.16lf,%.16lf) %.16lf, %.16lf, %.16lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0], obj->angle[0], obj->angle[1]); break; - case OTPolygon: + case OTPolyline: + { + Polyline * p = (Polyline *)obj; + fprintf(file, "POLYLINE %i (%li)", p->layer, p->points.size()); + fprintf(file, " (%i, %f, %i)\n", obj->color, obj->thickness, obj->style); + + for(long unsigned int i=0; ipoints.size(); i++) + { + fprintf(file, "(%.16lf,%.16lf,%.16lf)\n", p->points[i].x, p->points[i].y, p->points[i].b); + } + } break; case OTDimension: -// fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType); - fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype); + fprintf(file, "DIMENSION %i (%.16lf,%.16lf) (%.16lf,%.16lf) %i %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype, ((Dimension *)obj)->offset); break; case OTSpline: break; @@ -394,9 +537,8 @@ if (errno) break; } - if (obj->type != OTContainer) + if ((obj->type != OTContainer) && (obj->type != OTPolyline)) fprintf(file, " (%i, %f, %i)\n", obj->color, obj->thickness, obj->style); return true; } -