]> Shamusworld >> Repos - architektonas/blobdiff - src/fileio.cpp
Preliminary support for Polylines.
[architektonas] / src / fileio.cpp
index ace5bfa247713a3adc4aabee61f0dc3871a4c0aa..ad303f469cdcb0713a9afcb4e67533c1b428c3f3 100644 (file)
@@ -61,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 ). 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
@@ -108,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
@@ -129,6 +124,12 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
           Container, otherwise it enumerates all objects within itself. */
 
        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<Global::numLayers; i++)
@@ -140,7 +141,6 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        return true;
 }
 
-
 /*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing)
 {
        float version;
@@ -159,11 +159,9 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        else if (version == 1.2f)
                return LoadVersion1_2(file, drawing);
 
-//printf("LoadAtnsFile: Could not locate version! (version=%f)\n", version);
        return false;
 }
 
-
 /*static*/ void FileIO::ResetLayerVectors(void)
 {
        // Set up layer vectors
@@ -174,7 +172,6 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        Global::activeLayer = 0;
 }
 
-
 /*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing)
 {
        // Approach: read each object in the file, one by one. If the object is a
@@ -222,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
@@ -276,13 +272,23 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        return false;
 }
 
-
 /*static*/ bool FileIO::LoadVersion1_2(FILE * file, Container * drawing)
 {
-       int hidden, locked;
+       int hidden, locked, props = 0;
        char textBuffer[65536];
 
-       // Load layer information first
+       // 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<Global::numLayers; i++)
@@ -304,7 +310,7 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        while (!feof(file))
        {
                // Reconstruct the object (extended format!)
-               Object * obj = GetObjectFromFile(file, true);
+               Object * obj = GetObjectFromFile(file, true, true);
 
                // objectFileType is set in GetObjectFromFile()...
                if (objectFileType == OTFObject)
@@ -345,8 +351,7 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
        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];
@@ -405,6 +410,20 @@ 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;
+               obj = (Object *)new Polyline();
+               fscanf(file, "(%li)", &size);
+               fscanf(file, " (%i, %f, %i)\n", &obj->color, &obj->thickness, &obj->style);
+
+               for(int i=0; i<size; i++)
+               {
+                       Object * po = new Object();
+                       fscanf(file, "(%lf,%lf,%lf)\n", &po->p[0].x, &po->p[0].y, &po->length);
+                       ((Polyline *)obj)->Add(po);
+               }
+       }
        else if (strcmp(buffer, "TEXT") == 0)
        {
                Point p;
@@ -415,8 +434,13 @@ if (errno)
        {
                Point p1, p2;
                DimensionType type;
+               double offset = 0;
                fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &p1.x, &p1.y, &p2.x, &p2.y, (int *)&type);
-               obj = (Object *)new Dimension(p1, p2, type);
+
+               if (ext2)
+                       fscanf(file, " %lf", &offset);
+
+               obj = (Object *)new Dimension(p1, p2, type, offset);
        }
        else if (strcmp(buffer, "CONTAINER") == 0)
        {
@@ -438,16 +462,15 @@ if (errno)
        {
                obj->layer = 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
@@ -457,21 +480,31 @@ 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(VPVectorIter i=p->points.begin(); i!=p->points.end(); i++)
+               {
+                       Object * po = (Object *)(*i);
+                       fprintf(file, "(%.16lf,%.16lf,%.16lf)\n", po->p[0].x, po->p[0].y, po->length);
+               }
+       }
                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;
@@ -499,9 +532,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;
 }
-