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

xline.c

Go to the documentation of this file.
00001 
00035 #include "xline.h"
00036 
00037 
00052 DxfXLine *
00053 dxf_xline_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfXLine *xline = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfXLine);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((xline = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfXLine struct.\n")),
00068                   __FUNCTION__);
00069                 xline = NULL;
00070         }
00071         else
00072         {
00073                 memset (xline, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (xline);
00079 }
00080 
00081 
00094 DxfXLine *
00095 dxf_xline_init
00096 (
00097         DxfXLine *xline
00099 )
00100 {
00101 #if DEBUG
00102         DXF_DEBUG_BEGIN
00103 #endif
00104         /* Do some basic checks. */
00105         if (xline == NULL)
00106         {
00107                 fprintf (stderr,
00108                   (_("Warning in %s () a NULL pointer was passed.\n")),
00109                   __FUNCTION__);
00110                 xline = dxf_xline_new ();
00111         }
00112         if (xline == NULL)
00113         {
00114               fprintf (stderr,
00115                 (_("Error in %s () could not allocate memory for a DxfXLine struct.\n")),
00116                 __FUNCTION__);
00117               return (NULL);
00118         }
00119         xline->id_code = 0;
00120         xline->linetype = strdup (DXF_DEFAULT_LINETYPE);
00121         xline->layer = strdup (DXF_DEFAULT_LAYER);
00122         xline->x0 = 0.0;
00123         xline->y0 = 0.0;
00124         xline->z0 = 0.0;
00125         xline->x1 = 0.0;
00126         xline->y1 = 0.0;
00127         xline->z1 = 0.0;
00128         xline->elevation = 0.0;
00129         xline->thickness = 0.0;
00130         xline->linetype_scale = DXF_DEFAULT_LINETYPE_SCALE;
00131         xline->visibility = DXF_DEFAULT_VISIBILITY;
00132         xline->color = DXF_COLOR_BYLAYER;
00133         xline->paperspace = DXF_MODELSPACE;
00134         xline->dictionary_owner_soft = strdup ("");
00135         xline->dictionary_owner_hard = strdup ("");
00136         xline->next = NULL;
00137 #if DEBUG
00138         DXF_DEBUG_END
00139 #endif
00140         return (xline);
00141 }
00142 
00143 
00161 DxfXLine *
00162 dxf_xline_read
00163 (
00164         DxfFile *fp,
00166         DxfXLine *xline
00168 )
00169 {
00170 #if DEBUG
00171         DXF_DEBUG_BEGIN
00172 #endif
00173         char *temp_string = NULL;
00174 
00175         /* Do some basic checks. */
00176         if (fp == NULL)
00177         {
00178                 fprintf (stderr,
00179                   (_("Error in %s () a NULL file pointer was passed.\n")),
00180                   __FUNCTION__);
00181                 /* Clean up. */
00182                 free (temp_string);
00183                 return (NULL);
00184         }
00185         if (xline == NULL)
00186         {
00187                 fprintf (stderr,
00188                   (_("Warning in %s () a NULL pointer was passed.\n")),
00189                   __FUNCTION__);
00190                 xline = dxf_xline_new ();
00191                 xline = dxf_xline_init (xline);
00192         }
00193         (fp->line_number)++;
00194         fscanf (fp->fp, "%[^\n]", temp_string);
00195         while (strcmp (temp_string, "0") != 0)
00196         {
00197                 if (ferror (fp->fp))
00198                 {
00199                         fprintf (stderr,
00200                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00201                           __FUNCTION__, fp->filename, fp->line_number);
00202                         fclose (fp->fp);
00203                         /* Clean up. */
00204                         free (temp_string);
00205                         return (NULL);
00206                 }
00207                 if (strcmp (temp_string, "5") == 0)
00208                 {
00209                         /* Now follows a string containing a sequential
00210                          * id number. */
00211                         (fp->line_number)++;
00212                         fscanf (fp->fp, "%x\n", &xline->id_code);
00213                 }
00214                 else if (strcmp (temp_string, "6") == 0)
00215                 {
00216                         /* Now follows a string containing a linetype
00217                          * name. */
00218                         (fp->line_number)++;
00219                         fscanf (fp->fp, "%s\n", xline->linetype);
00220                 }
00221                 else if (strcmp (temp_string, "8") == 0)
00222                 {
00223                         /* Now follows a string containing a layer name. */
00224                         (fp->line_number)++;
00225                         fscanf (fp->fp, "%s\n", xline->layer);
00226                 }
00227                 else if (strcmp (temp_string, "10") == 0)
00228                 {
00229                         /* Now follows a string containing the
00230                          * X-coordinate of the center point. */
00231                         (fp->line_number)++;
00232                         fscanf (fp->fp, "%lf\n", &xline->x0);
00233                 }
00234                 else if (strcmp (temp_string, "20") == 0)
00235                 {
00236                         /* Now follows a string containing the
00237                          * Y-coordinate of the center point. */
00238                         (fp->line_number)++;
00239                         fscanf (fp->fp, "%lf\n", &xline->y0);
00240                 }
00241                 else if (strcmp (temp_string, "30") == 0)
00242                 {
00243                         /* Now follows a string containing the
00244                          * Z-coordinate of the center point. */
00245                         (fp->line_number)++;
00246                         fscanf (fp->fp, "%lf\n", &xline->z0);
00247                 }
00248                 else if (strcmp (temp_string, "11") == 0)
00249                 {
00250                         /* Now follows a string containing the
00251                          * X-coordinate of the center point. */
00252                         (fp->line_number)++;
00253                         fscanf (fp->fp, "%lf\n", &xline->x1);
00254                 }
00255                 else if (strcmp (temp_string, "21") == 0)
00256                 {
00257                         /* Now follows a string containing the
00258                          * Y-coordinate of the center point. */
00259                         (fp->line_number)++;
00260                         fscanf (fp->fp, "%lf\n", &xline->y1);
00261                 }
00262                 else if (strcmp (temp_string, "31") == 0)
00263                 {
00264                         /* Now follows a string containing the
00265                          * Z-coordinate of the center point. */
00266                         (fp->line_number)++;
00267                         fscanf (fp->fp, "%lf\n", &xline->z1);
00268                 }
00269                 else if ((fp->acad_version_number <= AutoCAD_11)
00270                         && (strcmp (temp_string, "38") == 0))
00271                 {
00272                         /* Now follows a string containing the
00273                          * elevation. */
00274                         (fp->line_number)++;
00275                         fscanf (fp->fp, "%lf\n", &xline->elevation);
00276                 }
00277                 else if (strcmp (temp_string, "39") == 0)
00278                 {
00279                         /* Now follows a string containing the
00280                          * thickness. */
00281                         (fp->line_number)++;
00282                         fscanf (fp->fp, "%lf\n", &xline->thickness);
00283                 }
00284                 else if (strcmp (temp_string, "48") == 0)
00285                 {
00286                         /* Now follows a string containing the linetype
00287                          * scale. */
00288                         (fp->line_number)++;
00289                         fscanf (fp->fp, "%lf\n", &xline->linetype_scale);
00290                 }
00291                 else if (strcmp (temp_string, "60") == 0)
00292                 {
00293                         /* Now follows a string containing the
00294                          * visibility value. */
00295                         (fp->line_number)++;
00296                         fscanf (fp->fp, "%hd\n", &xline->visibility);
00297                 }
00298                 else if (strcmp (temp_string, "62") == 0)
00299                 {
00300                         /* Now follows a string containing the
00301                          * color value. */
00302                         (fp->line_number)++;
00303                         fscanf (fp->fp, "%d\n", &xline->color);
00304                 }
00305                 else if (strcmp (temp_string, "67") == 0)
00306                 {
00307                         /* Now follows a string containing the
00308                          * paperspace value. */
00309                         (fp->line_number)++;
00310                         fscanf (fp->fp, "%d\n", &xline->paperspace);
00311                 }
00312                 else if ((fp->acad_version_number >= AutoCAD_13)
00313                         && (strcmp (temp_string, "100") == 0))
00314                 {
00315                         /* Now follows a string containing the
00316                          * subclass marker value. */
00317                         (fp->line_number)++;
00318                         fscanf (fp->fp, "%s\n", temp_string);
00319                         if ((strcmp (temp_string, "AcDbEntity") != 0)
00320                         && ((strcmp (temp_string, "AcDbXline") != 0)))
00321                         {
00322                                 fprintf (stderr,
00323                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00324                                   __FUNCTION__, fp->filename, fp->line_number);
00325                         }
00326                 }
00327                 else if (strcmp (temp_string, "330") == 0)
00328                 {
00329                         /* Now follows a string containing Soft-pointer
00330                          * ID/handle to owner dictionary. */
00331                         (fp->line_number)++;
00332                         fscanf (fp->fp, "%s\n", xline->dictionary_owner_soft);
00333                 }
00334                 else if (strcmp (temp_string, "360") == 0)
00335                 {
00336                         /* Now follows a string containing Hard owner
00337                          * ID/handle to owner dictionary. */
00338                         (fp->line_number)++;
00339                         fscanf (fp->fp, "%s\n", xline->dictionary_owner_hard);
00340                 }
00341                 else if (strcmp (temp_string, "999") == 0)
00342                 {
00343                         /* Now follows a string containing a comment. */
00344                         (fp->line_number)++;
00345                         fscanf (fp->fp, "%s\n", temp_string);
00346                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00347                 }
00348                 else
00349                 {
00350                         fprintf (stderr,
00351                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00352                           __FUNCTION__, fp->filename, fp->line_number);
00353                 }
00354         }
00355         /* Handle omitted members and/or illegal values. */
00356         if (strcmp (xline->linetype, "") == 0)
00357         {
00358                 xline->linetype = strdup (DXF_DEFAULT_LINETYPE);
00359         }
00360         if (strcmp (xline->layer, "") == 0)
00361         {
00362                 xline->layer = strdup (DXF_DEFAULT_LAYER);
00363         }
00364         /* Clean up. */
00365         free (temp_string);
00366 #if DEBUG
00367         DXF_DEBUG_END
00368 #endif
00369         return (xline);
00370 }
00371 
00372 
00385 int
00386 dxf_xline_write
00387 (
00388         DxfFile *fp,
00390         DxfXLine *xline
00392 )
00393 {
00394 #if DEBUG
00395         DXF_DEBUG_BEGIN
00396 #endif
00397         char *dxf_entity_name = strdup ("XLINE");
00398 
00399         /* Do some basic checks. */
00400         if (fp == NULL)
00401         {
00402                 fprintf (stderr,
00403                   (_("Error in %s () a NULL file pointer was passed.\n")),
00404                   __FUNCTION__);
00405                 /* Clean up. */
00406                 free (dxf_entity_name);
00407                 return (EXIT_FAILURE);
00408         }
00409         if (xline == NULL)
00410         {
00411                 fprintf (stderr,
00412                   (_("Error in %s () a NULL pointer was passed.\n")),
00413                   __FUNCTION__);
00414                 /* Clean up. */
00415                 free (dxf_entity_name);
00416                 return (EXIT_FAILURE);
00417         }
00418         if ((xline->x0 == xline->x1)
00419                 && (xline->y0 == xline->y1)
00420                 && (xline->z0 == xline->z1))
00421         {
00422                 fprintf (stderr,
00423                   (_("Error in %s () start point and end point are identical for the %s entity with id-code: %x\n")),
00424                   __FUNCTION__, dxf_entity_name, xline->id_code);
00425                 dxf_entity_skip (dxf_entity_name);
00426                 /* Clean up. */
00427                 free (dxf_entity_name);
00428                 return (EXIT_FAILURE);
00429         }
00430         if (strcmp (xline->layer, "") == 0)
00431         {
00432                 fprintf (stderr,
00433                   (_("Warning in %s () empty layer string for the %s entity with id-code: %x\n")),
00434                   __FUNCTION__, dxf_entity_name, xline->id_code);
00435                 fprintf (stderr,
00436                   (_("    %s entity is relocated to layer 0\n")),
00437                   dxf_entity_name);
00438                 xline->layer = strdup (DXF_DEFAULT_LAYER);
00439         }
00440         /* Start writing output. */
00441         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00442         if (xline->id_code != -1)
00443         {
00444                 fprintf (fp->fp, "  5\n%x\n", xline->id_code);
00445         }
00456         if ((strcmp (xline->dictionary_owner_soft, "") != 0)
00457           && (fp->acad_version_number >= AutoCAD_14))
00458         {
00459                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00460                 fprintf (fp->fp, "330\n%s\n", xline->dictionary_owner_soft);
00461                 fprintf (fp->fp, "102\n}\n");
00462         }
00463         if ((strcmp (xline->dictionary_owner_hard, "") != 0)
00464           && (fp->acad_version_number >= AutoCAD_14))
00465         {
00466                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00467                 fprintf (fp->fp, "360\n%s\n", xline->dictionary_owner_hard);
00468                 fprintf (fp->fp, "102\n}\n");
00469         }
00470         if (fp->acad_version_number >= AutoCAD_13)
00471         {
00472                 fprintf (fp->fp, "100\nAcDbEntity\n");
00473         }
00474         if (xline->paperspace == DXF_PAPERSPACE)
00475         {
00476                 fprintf (fp->fp, " 67\n%d\n", DXF_PAPERSPACE);
00477         }
00478         fprintf (fp->fp, "  8\n%s\n", xline->layer);
00479         if (strcmp (xline->linetype, DXF_DEFAULT_LINETYPE) != 0)
00480         {
00481                 fprintf (fp->fp, "  6\n%s\n", xline->linetype);
00482         }
00483         if ((fp->acad_version_number <= AutoCAD_11)
00484           && DXF_FLATLAND
00485           && (xline->elevation != 0.0))
00486         {
00487                 fprintf (fp->fp, " 38\n%f\n", xline->elevation);
00488         }
00489         if (xline->color != DXF_COLOR_BYLAYER)
00490         {
00491                 fprintf (fp->fp, " 62\n%d\n", xline->color);
00492         }
00493         if (xline->linetype_scale != 1.0)
00494         {
00495                 fprintf (fp->fp, " 48\n%f\n", xline->linetype_scale);
00496         }
00497         if (xline->visibility != 0)
00498         {
00499                 fprintf (fp->fp, " 60\n%d\n", xline->visibility);
00500         }
00501         if (fp->acad_version_number >= AutoCAD_13)
00502         {
00503                 fprintf (fp->fp, "100\nAcDbXline\n");
00504         }
00505         if (xline->thickness != 0.0)
00506         {
00507                 fprintf (fp->fp, " 39\n%f\n", xline->thickness);
00508         }
00509         fprintf (fp->fp, " 10\n%f\n", xline->x0);
00510         fprintf (fp->fp, " 20\n%f\n", xline->y0);
00511         fprintf (fp->fp, " 30\n%f\n", xline->z0);
00512         fprintf (fp->fp, " 11\n%f\n", xline->x1);
00513         fprintf (fp->fp, " 21\n%f\n", xline->y1);
00514         fprintf (fp->fp, " 31\n%f\n", xline->z1);
00515         /* Clean up. */
00516         free (dxf_entity_name);
00517 #if DEBUG
00518         DXF_DEBUG_END
00519 #endif
00520         return (EXIT_SUCCESS);
00521 }
00522 
00523 
00537 int
00538 dxf_xline_free
00539 (
00540         DxfXLine *xline
00543 )
00544 {
00545 #if DEBUG
00546         DXF_DEBUG_BEGIN
00547 #endif
00548         /* Do some basic checks. */
00549         if (xline == NULL)
00550         {
00551                 fprintf (stderr,
00552                   (_("Error in %s () a NULL pointer was passed.\n")),
00553                   __FUNCTION__);
00554                 return (EXIT_FAILURE);
00555         }
00556         if (xline->next != NULL)
00557         {
00558               fprintf (stderr,
00559                 (_("Error in %s () pointer to next was not NULL.\n")),
00560                 __FUNCTION__);
00561               return (EXIT_FAILURE);
00562         }
00563         free (xline->linetype);
00564         free (xline->layer);
00565         free (xline->dictionary_owner_soft);
00566         free (xline->dictionary_owner_hard);
00567         free (xline);
00568         xline = NULL;
00569 #if DEBUG
00570         DXF_DEBUG_END
00571 #endif
00572         return (EXIT_SUCCESS);
00573 }
00574 
00575 
00586 void
00587 dxf_xline_free_chain
00588 (
00589         DxfXLine *xlines
00591 )
00592 {
00593 #ifdef DEBUG
00594         DXF_DEBUG_BEGIN
00595 #endif
00596         if (xlines == NULL)
00597         {
00598                 fprintf (stderr,
00599                   (_("Warning in %s () a NULL pointer was passed.\n")),
00600                   __FUNCTION__);
00601         }
00602         while (xlines != NULL)
00603         {
00604                 struct DxfXLine *iter = xlines->next;
00605                 dxf_xline_free (xlines);
00606                 xlines = (DxfXLine *) iter;
00607         }
00608 #if DEBUG
00609         DXF_DEBUG_END
00610 #endif
00611 }
00612 
00613 
00614 /* EOF */