libDXF 0.0.1
A library with DXF related functions written in C.

tolerance.c

Go to the documentation of this file.
00001 
00035 #include "tolerance.h"
00036 
00037 
00052 DxfTolerance *
00053 dxf_tolerance_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfTolerance *tolerance = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfTolerance);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((tolerance = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfTolerance struct.\n")),
00068                   __FUNCTION__);
00069                 tolerance = NULL;
00070         }
00071         else
00072         {
00073                 memset (tolerance, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (tolerance);
00079 }
00080 
00081 
00095 DxfTolerance *
00096 dxf_tolerance_init
00097 (
00098         DxfTolerance *tolerance
00100 )
00101 {
00102 #if DEBUG
00103         DXF_DEBUG_BEGIN
00104 #endif
00105         /* Do some basic checks. */
00106         if (tolerance == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 tolerance = dxf_tolerance_new ();
00112         }
00113         if (tolerance == NULL)
00114         {
00115                 fprintf (stderr,
00116                   (_("Error in %s () could not allocate memory for a DxfTolerance struct.\n")),
00117                   __FUNCTION__);
00118                 return (NULL);
00119         }
00120         tolerance->dimstyle_name = strdup ("");
00121         tolerance->id_code = 0;
00122         tolerance->linetype = strdup (DXF_DEFAULT_LINETYPE);
00123         tolerance->layer = strdup (DXF_DEFAULT_LAYER);
00124         tolerance->x0 = 0.0;
00125         tolerance->y0 = 0.0;
00126         tolerance->z0 = 0.0;
00127         tolerance->x1 = 0.0;
00128         tolerance->y1 = 0.0;
00129         tolerance->z1 = 0.0;
00130         tolerance->extr_x0 = 0.0;
00131         tolerance->extr_y0 = 0.0;
00132         tolerance->extr_z0 = 0.0;
00133         tolerance->elevation = 0.0;
00134         tolerance->thickness = 0.0;
00135         tolerance->linetype_scale = DXF_DEFAULT_LINETYPE_SCALE;
00136         tolerance->visibility = DXF_DEFAULT_VISIBILITY;
00137         tolerance->color = DXF_COLOR_BYLAYER;
00138         tolerance->paperspace = DXF_MODELSPACE;
00139         tolerance->dictionary_owner_soft = strdup ("");
00140         tolerance->dictionary_owner_hard = strdup ("");
00141         tolerance->next = NULL;
00142 #if DEBUG
00143         DXF_DEBUG_END
00144 #endif
00145         return (tolerance);
00146 }
00147 
00148 
00166 DxfTolerance *
00167 dxf_tolerance_read
00168 (
00169         DxfFile *fp,
00171         DxfTolerance *tolerance
00173 )
00174 {
00175 #if DEBUG
00176         DXF_DEBUG_BEGIN
00177 #endif
00178         char *temp_string = NULL;
00179 
00180         /* Do some basic checks. */
00181         if (fp == NULL)
00182         {
00183                 fprintf (stderr,
00184                   (_("Error in %s () a NULL file pointer was passed.\n")),
00185                   __FUNCTION__);
00186                 /* Clean up. */
00187                 free (temp_string);
00188                 return (NULL);
00189         }
00190         if (tolerance == NULL)
00191         {
00192                 fprintf (stderr,
00193                   (_("Warning in %s () a NULL pointer was passed.\n")),
00194                   __FUNCTION__);
00195                 tolerance = dxf_tolerance_new ();
00196                 tolerance = dxf_tolerance_init (tolerance);
00197         }
00198         (fp->line_number)++;
00199         fscanf (fp->fp, "%[^\n]", temp_string);
00200         while (strcmp (temp_string, "0") != 0)
00201         {
00202                 if (ferror (fp->fp))
00203                 {
00204                         fprintf (stderr,
00205                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00206                           __FUNCTION__, fp->filename, fp->line_number);
00207                         fclose (fp->fp);
00208                         /* Clean up. */
00209                         free (temp_string);
00210                         return (NULL);
00211                 }
00212                 if (strcmp (temp_string, "3") == 0)
00213                 {
00214                         /* Now follows a string containing the dimension
00215                          * style name. */
00216                         (fp->line_number)++;
00217                         fscanf (fp->fp, "%s\n", tolerance->dimstyle_name);
00218                 }
00219                 if (strcmp (temp_string, "5") == 0)
00220                 {
00221                         /* Now follows a string containing a sequential
00222                          * id number. */
00223                         (fp->line_number)++;
00224                         fscanf (fp->fp, "%x\n", &tolerance->id_code);
00225                 }
00226                 else if (strcmp (temp_string, "6") == 0)
00227                 {
00228                         /* Now follows a string containing a linetype
00229                          * name. */
00230                         (fp->line_number)++;
00231                         fscanf (fp->fp, "%s\n", tolerance->linetype);
00232                 }
00233                 else if (strcmp (temp_string, "8") == 0)
00234                 {
00235                         /* Now follows a string containing a layer name. */
00236                         (fp->line_number)++;
00237                         fscanf (fp->fp, "%s\n", tolerance->layer);
00238                 }
00239                 else if (strcmp (temp_string, "10") == 0)
00240                 {
00241                         /* Now follows a string containing the
00242                          * X-coordinate of the insertion point. */
00243                         (fp->line_number)++;
00244                         fscanf (fp->fp, "%lf\n", &tolerance->x0);
00245                 }
00246                 else if (strcmp (temp_string, "20") == 0)
00247                 {
00248                         /* Now follows a string containing the
00249                          * Y-coordinate of the insertion point. */
00250                         (fp->line_number)++;
00251                         fscanf (fp->fp, "%lf\n", &tolerance->y0);
00252                 }
00253                 else if (strcmp (temp_string, "30") == 0)
00254                 {
00255                         /* Now follows a string containing the
00256                          * Z-coordinate of the insertion point. */
00257                         (fp->line_number)++;
00258                         fscanf (fp->fp, "%lf\n", &tolerance->z0);
00259                 }
00260                 else if (strcmp (temp_string, "11") == 0)
00261                 {
00262                         /* Now follows a string containing the
00263                          * X-coordinate of the direction vector. */
00264                         (fp->line_number)++;
00265                         fscanf (fp->fp, "%lf\n", &tolerance->x1);
00266                 }
00267                 else if (strcmp (temp_string, "21") == 0)
00268                 {
00269                         /* Now follows a string containing the
00270                          * Y-coordinate of the direction vector. */
00271                         (fp->line_number)++;
00272                         fscanf (fp->fp, "%lf\n", &tolerance->y1);
00273                 }
00274                 else if (strcmp (temp_string, "31") == 0)
00275                 {
00276                         /* Now follows a string containing the
00277                          * Z-coordinate of the direction vector. */
00278                         (fp->line_number)++;
00279                         fscanf (fp->fp, "%lf\n", &tolerance->z1);
00280                 }
00281                 else if ((fp->acad_version_number <= AutoCAD_11)
00282                         && (strcmp (temp_string, "38") == 0))
00283                 {
00284                         /* Now follows a string containing the
00285                          * elevation. */
00286                         (fp->line_number)++;
00287                         fscanf (fp->fp, "%lf\n", &tolerance->elevation);
00288                 }
00289                 else if (strcmp (temp_string, "39") == 0)
00290                 {
00291                         /* Now follows a string containing the
00292                          * thickness. */
00293                         (fp->line_number)++;
00294                         fscanf (fp->fp, "%lf\n", &tolerance->thickness);
00295                 }
00296                 else if (strcmp (temp_string, "48") == 0)
00297                 {
00298                         /* Now follows a string containing the linetype
00299                          * scale. */
00300                         (fp->line_number)++;
00301                         fscanf (fp->fp, "%lf\n", &tolerance->linetype_scale);
00302                 }
00303                 else if (strcmp (temp_string, "60") == 0)
00304                 {
00305                         /* Now follows a string containing the
00306                          * visibility value. */
00307                         (fp->line_number)++;
00308                         fscanf (fp->fp, "%hd\n", &tolerance->visibility);
00309                 }
00310                 else if (strcmp (temp_string, "62") == 0)
00311                 {
00312                         /* Now follows a string containing the
00313                          * color value. */
00314                         (fp->line_number)++;
00315                         fscanf (fp->fp, "%d\n", &tolerance->color);
00316                 }
00317                 else if (strcmp (temp_string, "67") == 0)
00318                 {
00319                         /* Now follows a string containing the
00320                          * paperspace value. */
00321                         (fp->line_number)++;
00322                         fscanf (fp->fp, "%d\n", &tolerance->paperspace);
00323                 }
00324                 else if ((fp->acad_version_number >= AutoCAD_13)
00325                         && (strcmp (temp_string, "100") == 0))
00326                 {
00327                         /* Now follows a string containing the
00328                          * subclass marker value. */
00329                         (fp->line_number)++;
00330                         fscanf (fp->fp, "%s\n", temp_string);
00331                         if ((strcmp (temp_string, "AcDbEntity") != 0)
00332                         && ((strcmp (temp_string, "AcDbFcf") != 0)))
00333                         {
00334                                 fprintf (stderr,
00335                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00336                                   __FUNCTION__, fp->filename, fp->line_number);
00337                         }
00338                 }
00339                 else if (strcmp (temp_string, "210") == 0)
00340                 {
00341                         /* Now follows a string containing the
00342                          * X-value of the extrusion vector. */
00343                         (fp->line_number)++;
00344                         fscanf (fp->fp, "%lf\n", &tolerance->extr_x0);
00345                 }
00346                 else if (strcmp (temp_string, "220") == 0)
00347                 {
00348                         /* Now follows a string containing the
00349                          * Y-value of the extrusion vector. */
00350                         (fp->line_number)++;
00351                         fscanf (fp->fp, "%lf\n", &tolerance->extr_y0);
00352                 }
00353                 else if (strcmp (temp_string, "230") == 0)
00354                 {
00355                         /* Now follows a string containing the
00356                          * Z-value of the extrusion vector. */
00357                         (fp->line_number)++;
00358                         fscanf (fp->fp, "%lf\n", &tolerance->extr_z0);
00359                 }
00360                 else if (strcmp (temp_string, "330") == 0)
00361                 {
00362                         /* Now follows a string containing Soft-pointer
00363                          * ID/handle to owner dictionary. */
00364                         (fp->line_number)++;
00365                         fscanf (fp->fp, "%s\n", tolerance->dictionary_owner_soft);
00366                 }
00367                 else if (strcmp (temp_string, "360") == 0)
00368                 {
00369                         /* Now follows a string containing Hard owner
00370                          * ID/handle to owner dictionary. */
00371                         (fp->line_number)++;
00372                         fscanf (fp->fp, "%s\n", tolerance->dictionary_owner_hard);
00373                 }
00374                 else if (strcmp (temp_string, "999") == 0)
00375                 {
00376                         /* Now follows a string containing a comment. */
00377                         (fp->line_number)++;
00378                         fscanf (fp->fp, "%s\n", temp_string);
00379                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00380                 }
00381                 else
00382                 {
00383                         fprintf (stderr,
00384                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00385                           __FUNCTION__, fp->filename, fp->line_number);
00386                 }
00387         }
00388         /* Handle omitted members and/or illegal values. */
00389         if (strcmp (tolerance->dimstyle_name, "") == 0)
00390         {
00391                 return (NULL);
00392         }
00393         if (strcmp (tolerance->linetype, "") == 0)
00394         {
00395                 tolerance->linetype = strdup (DXF_DEFAULT_LINETYPE);
00396         }
00397         if (strcmp (tolerance->layer, "") == 0)
00398         {
00399                 tolerance->layer = strdup (DXF_DEFAULT_LAYER);
00400         }
00401         /* Clean up. */
00402         free (temp_string);
00403 #if DEBUG
00404         DXF_DEBUG_END
00405 #endif
00406         return (tolerance);
00407 }
00408 
00409 
00422 int
00423 dxf_tolerance_write
00424 (
00425         DxfFile *fp,
00427         DxfTolerance *tolerance
00429 )
00430 {
00431 #if DEBUG
00432         DXF_DEBUG_BEGIN
00433 #endif
00434         char *dxf_entity_name = strdup ("TOLERANCE");
00435 
00436         /* Do some basic checks. */
00437         if (fp == NULL)
00438         {
00439                 fprintf (stderr,
00440                   (_("Error in %s () a NULL file pointer was passed.\n")),
00441                   __FUNCTION__);
00442                 /* Clean up. */
00443                 free (dxf_entity_name);
00444                 return (EXIT_FAILURE);
00445         }
00446         if (tolerance == NULL)
00447         {
00448                 fprintf (stderr,
00449                   (_("Error in %s () a NULL pointer was passed.\n")),
00450                   __FUNCTION__);
00451                 /* Clean up. */
00452                 free (dxf_entity_name);
00453                 return (EXIT_FAILURE);
00454         }
00455         if (strcmp (tolerance->dimstyle_name, "") == 0)
00456         {
00457                 fprintf (stderr, "Error in dxf_tolerance_write () empty dimstyle name for the %s entity with id-code: %x.\n",
00458                         dxf_entity_name, tolerance->id_code);
00459                 fprintf (stderr, "\tskipping %s entity.\n",
00460                         dxf_entity_name);
00461                 /* Clean up. */
00462                 free (dxf_entity_name);
00463                 return (EXIT_FAILURE);
00464         }
00465         if (strcmp (tolerance->linetype, "") == 0)
00466         {
00467                 fprintf (stderr,
00468                   (_("Warning in %s () empty linetype string for the %s entity with id-code: %x\n")),
00469                   __FUNCTION__, dxf_entity_name, tolerance->id_code);
00470                 fprintf (stderr,
00471                   (_("\t%s entity is reset to default linetype")),
00472                   dxf_entity_name);
00473                 tolerance->linetype = strdup (DXF_DEFAULT_LINETYPE);
00474         }
00475         if (strcmp (tolerance->layer, "") == 0)
00476         {
00477                 fprintf (stderr,
00478                   (_("Warning in %s () empty layer string for the %s entity with id-code: %x\n")),
00479                   __FUNCTION__, dxf_entity_name, tolerance->id_code);
00480                 fprintf (stderr,
00481                   (_("\t%s entity is relocated to layer 0")),
00482                   dxf_entity_name);
00483                 tolerance->layer = DXF_DEFAULT_LAYER;
00484         }
00485         /* Start writing output. */
00486         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00487         if (tolerance->id_code != -1)
00488         {
00489                 fprintf (fp->fp, "  5\n%x\n", tolerance->id_code);
00490         }
00501         if ((strcmp (tolerance->dictionary_owner_soft, "") != 0)
00502           && (fp->acad_version_number >= AutoCAD_14))
00503         {
00504                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00505                 fprintf (fp->fp, "330\n%s\n", tolerance->dictionary_owner_soft);
00506                 fprintf (fp->fp, "102\n}\n");
00507         }
00508         if ((strcmp (tolerance->dictionary_owner_hard, "") != 0)
00509           && (fp->acad_version_number >= AutoCAD_14))
00510         {
00511                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00512                 fprintf (fp->fp, "360\n%s\n", tolerance->dictionary_owner_hard);
00513                 fprintf (fp->fp, "102\n}\n");
00514         }
00515         if (fp->acad_version_number >= AutoCAD_13)
00516         {
00517                 fprintf (fp->fp, "100\nAcDbEntity\n");
00518         }
00519         if (tolerance->paperspace == DXF_PAPERSPACE)
00520         {
00521                 fprintf (fp->fp, " 67\n%d\n", DXF_PAPERSPACE);
00522         }
00523         fprintf (fp->fp, "  8\n%s\n", tolerance->layer);
00524         if (strcmp (tolerance->linetype, DXF_DEFAULT_LINETYPE) != 0)
00525         {
00526                 fprintf (fp->fp, "  6\n%s\n", tolerance->linetype);
00527         }
00528         if ((fp->acad_version_number <= AutoCAD_11)
00529           && DXF_FLATLAND
00530           && (tolerance->elevation != 0.0))
00531         {
00532                 fprintf (fp->fp, " 38\n%f\n", tolerance->elevation);
00533         }
00534         if (tolerance->color != DXF_COLOR_BYLAYER)
00535         {
00536                 fprintf (fp->fp, " 62\n%d\n", tolerance->color);
00537         }
00538         if (tolerance->linetype_scale != 1.0)
00539         {
00540                 fprintf (fp->fp, " 48\n%f\n", tolerance->linetype_scale);
00541         }
00542         if (tolerance->visibility != 0)
00543         {
00544                 fprintf (fp->fp, " 60\n%d\n", tolerance->visibility);
00545         }
00546         if (fp->acad_version_number >= AutoCAD_13)
00547         {
00548                 fprintf (fp->fp, "100\nAcDbFcf\n");
00549         }
00550         if (tolerance->thickness != 0.0)
00551         {
00552                 fprintf (fp->fp, " 39\n%f\n", tolerance->thickness);
00553         }
00554         fprintf (fp->fp, "  3\n%s\n", tolerance->dimstyle_name);
00555         fprintf (fp->fp, " 10\n%f\n", tolerance->x0);
00556         fprintf (fp->fp, " 20\n%f\n", tolerance->y0);
00557         fprintf (fp->fp, " 30\n%f\n", tolerance->z0);
00558         if ((fp->acad_version_number >= AutoCAD_12)
00559                 && (tolerance->extr_x0 != 0.0)
00560                 && (tolerance->extr_y0 != 0.0)
00561                 && (tolerance->extr_z0 != 1.0))
00562         {
00563                 fprintf (fp->fp, "210\n%f\n", tolerance->extr_x0);
00564                 fprintf (fp->fp, "220\n%f\n", tolerance->extr_y0);
00565                 fprintf (fp->fp, "230\n%f\n", tolerance->extr_z0);
00566         }
00567         fprintf (fp->fp, " 11\n%f\n", tolerance->x1);
00568         fprintf (fp->fp, " 21\n%f\n", tolerance->y1);
00569         fprintf (fp->fp, " 31\n%f\n", tolerance->z1);
00570         /* Clean up. */
00571         free (dxf_entity_name);
00572 #if DEBUG
00573         DXF_DEBUG_END
00574 #endif
00575         return (EXIT_SUCCESS);
00576 }
00577 
00578 
00592 int
00593 dxf_tolerance_free
00594 (
00595         DxfTolerance *tolerance
00598 )
00599 {
00600 #if DEBUG
00601         DXF_DEBUG_BEGIN
00602 #endif
00603         /* Do some basic checks. */
00604         if (tolerance == NULL)
00605         {
00606                 fprintf (stderr,
00607                   (_("Error in %s () a NULL pointer was passed.\n")),
00608                   __FUNCTION__);
00609                 return (EXIT_FAILURE);
00610         }
00611         if (tolerance->next != NULL)
00612         {
00613                 fprintf (stderr,
00614                   (_("Error in %s () pointer to next was not NULL.\n")),
00615                   __FUNCTION__);
00616                 return (EXIT_FAILURE);
00617         }
00618         free (tolerance->dimstyle_name);
00619         free (tolerance->linetype);
00620         free (tolerance->layer);
00621         free (tolerance->dictionary_owner_soft);
00622         free (tolerance->dictionary_owner_hard);
00623         free (tolerance);
00624         tolerance = NULL;
00625 #if DEBUG
00626         DXF_DEBUG_END
00627 #endif
00628         return (EXIT_SUCCESS);
00629 }
00630 
00631 
00642 void
00643 dxf_tolerance_free_chain
00644 (
00645         DxfTolerance *tolerances
00648 )
00649 {
00650 #ifdef DEBUG
00651         DXF_DEBUG_BEGIN
00652 #endif
00653         if (tolerances == NULL)
00654         {
00655                 fprintf (stderr,
00656                   (_("Warning in %s () a NULL pointer was passed.\n")),
00657                   __FUNCTION__);
00658         }
00659         while (tolerances != NULL)
00660         {
00661                 struct DxfTolerance *iter = tolerances->next;
00662                 dxf_tolerance_free (tolerances);
00663                 tolerances = (DxfTolerance *) iter;
00664         }
00665 #if DEBUG
00666         DXF_DEBUG_END
00667 #endif
00668 }
00669 
00670 
00671 /* EOF*/