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

vertex.c

Go to the documentation of this file.
00001 
00038 #include "vertex.h"
00039 
00040 
00052 DxfVertex *
00053 dxf_vertex_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfVertex *vertex = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfVertex);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((vertex = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfVertex struct.\n")),
00068                   __FUNCTION__);
00069                 vertex = NULL;
00070         }
00071         else
00072         {
00073                 memset (vertex, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (vertex);
00079 }
00080 
00081 
00094 DxfVertex *
00095 dxf_vertex_init
00096 (
00097         DxfVertex *vertex
00099 )
00100 {
00101 #if DEBUG
00102         DXF_DEBUG_BEGIN
00103 #endif
00104         /* Do some basic checks. */
00105         if (vertex == NULL)
00106         {
00107                 fprintf (stderr,
00108                   (_("Warning in %s () a NULL pointer was passed.\n")),
00109                   __FUNCTION__);
00110                 vertex = dxf_vertex_new ();
00111         }
00112         if (vertex == NULL)
00113         {
00114               fprintf (stderr,
00115                 (_("Error in %s () could not allocate memory for a DxfVertex struct.\n")),
00116                 __FUNCTION__);
00117               return (NULL);
00118         }
00119         vertex->id_code = 0;
00120         vertex->linetype = strdup (DXF_DEFAULT_LINETYPE);
00121         vertex->layer = strdup (DXF_DEFAULT_LAYER);
00122         vertex->x0 = 0.0;
00123         vertex->y0 = 0.0;
00124         vertex->z0 = 0.0;
00125         vertex->elevation = 0.0;
00126         vertex->thickness = 0.0;
00127         vertex->start_width = 0.0;
00128         vertex->end_width = 0.0;
00129         vertex->bulge = 0.0;
00130         vertex->curve_fit_tangent_direction = 0.0;
00131         vertex->linetype_scale = DXF_DEFAULT_LINETYPE_SCALE;
00132         vertex->visibility = DXF_DEFAULT_VISIBILITY;
00133         vertex->color = DXF_COLOR_BYLAYER;
00134         vertex->paperspace = DXF_MODELSPACE;
00135         vertex->flag = 0;
00136         vertex->dictionary_owner_soft = strdup ("");
00137         vertex->dictionary_owner_hard = strdup ("");
00138         vertex->next = NULL;
00139 #if DEBUG
00140         DXF_DEBUG_END
00141 #endif
00142         return (vertex);
00143 }
00144 
00145 
00163 DxfVertex *
00164 dxf_vertex_read
00165 (
00166         DxfFile *fp,
00168         DxfVertex *vertex
00170 )
00171 {
00172 #if DEBUG
00173         DXF_DEBUG_BEGIN
00174 #endif
00175         char *temp_string = NULL;
00176 
00177         /* Do some basic checks. */
00178         if (fp == NULL)
00179         {
00180                 fprintf (stderr,
00181                   (_("Error in %s () a NULL file pointer was passed.\n")),
00182                   __FUNCTION__);
00183                 /* Clean up. */
00184                 free (temp_string);
00185                 return (NULL);
00186         }
00187         if (vertex == NULL)
00188         {
00189                 fprintf (stderr,
00190                   (_("Warning in %s () a NULL pointer was passed.\n")),
00191                   __FUNCTION__);
00192                 vertex = dxf_vertex_new ();
00193                 vertex = dxf_vertex_init (vertex);
00194         }
00195         (fp->line_number)++;
00196         fscanf (fp->fp, "%[^\n]", temp_string);
00197         while (strcmp (temp_string, "0") != 0)
00198         {
00199                 if (ferror (fp->fp))
00200                 {
00201                         fprintf (stderr,
00202                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00203                           __FUNCTION__, fp->filename, fp->line_number);
00204                         fclose (fp->fp);
00205                         /* Clean up. */
00206                         free (temp_string);
00207                         return (NULL);
00208                 }
00209                 if (strcmp (temp_string, "5") == 0)
00210                 {
00211                         /* Now follows a string containing a sequential
00212                          * id number. */
00213                         (fp->line_number)++;
00214                         fscanf (fp->fp, "%x\n", &vertex->id_code);
00215                 }
00216                 else if (strcmp (temp_string, "6") == 0)
00217                 {
00218                         /* Now follows a string containing a linetype
00219                          * name. */
00220                         (fp->line_number)++;
00221                         fscanf (fp->fp, "%s\n", vertex->linetype);
00222                 }
00223                 else if (strcmp (temp_string, "8") == 0)
00224                 {
00225                         /* Now follows a string containing a layer name. */
00226                         (fp->line_number)++;
00227                         fscanf (fp->fp, "%s\n", vertex->layer);
00228                 }
00229                 else if (strcmp (temp_string, "10") == 0)
00230                 {
00231                         /* Now follows a string containing the
00232                          * X-coordinate of the point. */
00233                         (fp->line_number)++;
00234                         fscanf (fp->fp, "%lf\n", &vertex->x0);
00235                 }
00236                 else if (strcmp (temp_string, "20") == 0)
00237                 {
00238                         /* Now follows a string containing the
00239                          * Y-coordinate of the point. */
00240                         (fp->line_number)++;
00241                         fscanf (fp->fp, "%lf\n", &vertex->y0);
00242                 }
00243                 else if (strcmp (temp_string, "30") == 0)
00244                 {
00245                         /* Now follows a string containing the
00246                          * Z-coordinate of the point. */
00247                         (fp->line_number)++;
00248                         fscanf (fp->fp, "%lf\n", &vertex->z0);
00249                 }
00250                 else if ((fp->acad_version_number <= AutoCAD_11)
00251                         && (strcmp (temp_string, "38") == 0)
00252                         && (vertex->elevation != 0.0))
00253                 {
00254                         /* Now follows a string containing the
00255                          * elevation. */
00256                         (fp->line_number)++;
00257                         fscanf (fp->fp, "%lf\n", &vertex->elevation);
00258                 }
00259                 else if (strcmp (temp_string, "39") == 0)
00260                 {
00261                         /* Now follows a string containing the
00262                          * thickness. */
00263                         (fp->line_number)++;
00264                         fscanf (fp->fp, "%lf\n", &vertex->thickness);
00265                 }
00266                 else if (strcmp (temp_string, "40") == 0)
00267                 {
00268                         /* Now follows a string containing the
00269                          * start width. */
00270                         (fp->line_number)++;
00271                         fscanf (fp->fp, "%lf\n", &vertex->start_width);
00272                 }
00273                 else if (strcmp (temp_string, "41") == 0)
00274                 {
00275                         /* Now follows a string containing the
00276                          * end width. */
00277                         (fp->line_number)++;
00278                         fscanf (fp->fp, "%lf\n", &vertex->end_width);
00279                 }
00280                 else if (strcmp (temp_string, "42") == 0)
00281                 {
00282                         /* Now follows a string containing the
00283                          * bulge. */
00284                         (fp->line_number)++;
00285                         fscanf (fp->fp, "%lf\n", &vertex->bulge);
00286                 }
00287                 else if (strcmp (temp_string, "48") == 0)
00288                 {
00289                         /* Now follows a string containing the linetype
00290                          * scale. */
00291                         (fp->line_number)++;
00292                         fscanf (fp->fp, "%lf\n", &vertex->linetype_scale);
00293                 }
00294                 else if (strcmp (temp_string, "50") == 0)
00295                 {
00296                         /* Now follows a string containing the
00297                          * curve fitting tangent. */
00298                         (fp->line_number)++;
00299                         fscanf (fp->fp, "%lf\n", &vertex->curve_fit_tangent_direction);
00300                 }
00301                 else if (strcmp (temp_string, "60") == 0)
00302                 {
00303                         /* Now follows a string containing the
00304                          * visibility value. */
00305                         (fp->line_number)++;
00306                         fscanf (fp->fp, "%hd\n", &vertex->visibility);
00307                 }
00308                 else if (strcmp (temp_string, "62") == 0)
00309                 {
00310                         /* Now follows a string containing the
00311                          * color value. */
00312                         (fp->line_number)++;
00313                         fscanf (fp->fp, "%d\n", &vertex->color);
00314                 }
00315                 else if (strcmp (temp_string, "67") == 0)
00316                 {
00317                         /* Now follows a string containing the
00318                          * paperspace value. */
00319                         (fp->line_number)++;
00320                         fscanf (fp->fp, "%d\n", &vertex->paperspace);
00321                 }
00322                 else if (strcmp (temp_string, "70") == 0)
00323                 {
00324                         /* Now follows a string containing the flag
00325                          * value. */
00326                         (fp->line_number)++;
00327                         fscanf (fp->fp, "%d\n", &vertex->flag);
00328                 }
00329                 else if (strcmp (temp_string, "71") == 0)
00330                 {
00331                         /* Now follows a string containing the Polyface
00332                          * mesh vertex index value. */
00333                         (fp->line_number)++;
00334                         fscanf (fp->fp, "%d\n", &vertex->polyface_mesh_vertex_index_1);
00335                 }
00336                 else if (strcmp (temp_string, "72") == 0)
00337                 {
00338                         /* Now follows a string containing the Polyface
00339                          * mesh vertex index value. */
00340                         (fp->line_number)++;
00341                         fscanf (fp->fp, "%d\n", &vertex->polyface_mesh_vertex_index_2);
00342                 }
00343                 else if (strcmp (temp_string, "73") == 0)
00344                 {
00345                         /* Now follows a string containing the Polyface
00346                          * mesh vertex index value. */
00347                         (fp->line_number)++;
00348                         fscanf (fp->fp, "%d\n", &vertex->polyface_mesh_vertex_index_3);
00349                 }
00350                 else if (strcmp (temp_string, "74") == 0)
00351                 {
00352                         /* Now follows a string containing the Polyface
00353                          * mesh vertex index value. */
00354                         (fp->line_number)++;
00355                         fscanf (fp->fp, "%d\n", &vertex->polyface_mesh_vertex_index_4);
00356                 }
00357                 else if ((fp->acad_version_number >= AutoCAD_13)
00358                         && (strcmp (temp_string, "100") == 0))
00359                 {
00360                         /* Now follows a string containing the
00361                          * subclass marker value. */
00362                         (fp->line_number)++;
00363                         fscanf (fp->fp, "%s\n", temp_string);
00364                         if ((strcmp (temp_string, "AcDbEntity") != 0)
00365                         && (strcmp (temp_string, "AcDbVertex") != 0)
00366                         && (strcmp (temp_string, "AcDb2dVertex") != 0)
00367                         && (strcmp (temp_string, "AcDb3dPolylineVertex") != 0))
00368                         {
00369                                 fprintf (stderr,
00370                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00371                                   __FUNCTION__, fp->filename, fp->line_number);
00372                         }
00373                 }
00374                 else if (strcmp (temp_string, "330") == 0)
00375                 {
00376                         /* Now follows a string containing Soft-pointer
00377                          * ID/handle to owner dictionary. */
00378                         (fp->line_number)++;
00379                         fscanf (fp->fp, "%s\n", vertex->dictionary_owner_soft);
00380                 }
00381                 else if (strcmp (temp_string, "360") == 0)
00382                 {
00383                         /* Now follows a string containing Hard owner
00384                          * ID/handle to owner dictionary. */
00385                         (fp->line_number)++;
00386                         fscanf (fp->fp, "%s\n", vertex->dictionary_owner_hard);
00387                 }
00388                 else if (strcmp (temp_string, "999") == 0)
00389                 {
00390                         /* Now follows a string containing a comment. */
00391                         (fp->line_number)++;
00392                         fscanf (fp->fp, "%s\n", temp_string);
00393                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00394                 }
00395                 else
00396                 {
00397                         fprintf (stderr,
00398                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00399                           __FUNCTION__, fp->filename, fp->line_number);
00400                 }
00401         }
00402         /* Handle omitted members and/or illegal values. */
00403         if (strcmp (vertex->linetype, "") == 0)
00404         {
00405                 vertex->linetype = strdup (DXF_DEFAULT_LINETYPE);
00406         }
00407         if (strcmp (vertex->layer, "") == 0)
00408         {
00409                 vertex->layer = strdup (DXF_DEFAULT_LAYER);
00410         }
00411         /* Clean up. */
00412         free (temp_string);
00413 #if DEBUG
00414         DXF_DEBUG_END
00415 #endif
00416         return (vertex);
00417 }
00418 
00419 
00432 int
00433 dxf_vertex_write
00434 (
00435         DxfFile *fp,
00437         DxfVertex *vertex
00439 )
00440 {
00441 #if DEBUG
00442         DXF_DEBUG_BEGIN
00443 #endif
00444         char *dxf_entity_name = strdup ("VERTEX");
00445 
00446         /* Do some basic checks. */
00447         if (fp == NULL)
00448         {
00449                 fprintf (stderr,
00450                   (_("Error in %s () a NULL file pointer was passed.\n")),
00451                   __FUNCTION__);
00452                 /* Clean up. */
00453                 free (dxf_entity_name);
00454                 return (EXIT_FAILURE);
00455         }
00456         if (vertex == NULL)
00457         {
00458                 fprintf (stderr,
00459                   (_("Error in %s () a NULL pointer was passed.\n")),
00460                   __FUNCTION__);
00461                 /* Clean up. */
00462                 free (dxf_entity_name);
00463                 return (EXIT_FAILURE);
00464         }
00465         if (strcmp (vertex->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, vertex->id_code);
00470                 fprintf (stderr,
00471                   (_("    %s entity is reset to default linetype")),
00472                   dxf_entity_name);
00473                 vertex->linetype = strdup (DXF_DEFAULT_LINETYPE);
00474         }
00475         if (strcmp (vertex->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, vertex->id_code);
00480                 fprintf (stderr,
00481                   (_("    %s entity is relocated to layer 0")),
00482                   dxf_entity_name);
00483                 vertex->layer = strdup (DXF_DEFAULT_LAYER);
00484         }
00485         /* Start writing output. */
00486         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00487         if (vertex->id_code != -1)
00488         {
00489                 fprintf (fp->fp, "  5\n%x\n", vertex->id_code);
00490         }
00501         if ((strcmp (vertex->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", vertex->dictionary_owner_soft);
00506                 fprintf (fp->fp, "102\n}\n");
00507         }
00508         if ((strcmp (vertex->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", vertex->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 (vertex->paperspace == DXF_PAPERSPACE)
00520         {
00521                 fprintf (fp->fp, " 67\n%d\n", DXF_PAPERSPACE);
00522         }
00523         fprintf (fp->fp, "  8\n%s\n", vertex->layer);
00524         if (strcmp (vertex->linetype, DXF_DEFAULT_LINETYPE) != 0)
00525         {
00526                 fprintf (fp->fp, "  6\n%s\n", vertex->linetype);
00527         }
00528         if ((fp->acad_version_number <= AutoCAD_11)
00529           && DXF_FLATLAND
00530           && (vertex->elevation != 0.0))
00531         {
00532                 fprintf (fp->fp, " 38\n%f\n", vertex->elevation);
00533         }
00534         if (vertex->color != DXF_COLOR_BYLAYER)
00535         {
00536                 fprintf (fp->fp, " 62\n%d\n", vertex->color);
00537         }
00538         if (vertex->linetype_scale != 1.0)
00539         {
00540                 fprintf (fp->fp, " 48\n%f\n", vertex->linetype_scale);
00541         }
00542         if (vertex->visibility != 0)
00543         {
00544                 fprintf (fp->fp, " 60\n%d\n", vertex->visibility);
00545         }
00547         if (vertex->thickness != 0.0)
00548         {
00549                 fprintf (fp->fp, " 39\n%f\n", vertex->thickness);
00550         }
00551         fprintf (fp->fp, "100\nAcDbVertex\n");
00556         fprintf (fp->fp, "100\nAcDb3dPolylineVertex\n");
00557         fprintf (fp->fp, " 10\n%f\n", vertex->x0);
00558         fprintf (fp->fp, " 20\n%f\n", vertex->y0);
00559         fprintf (fp->fp, " 30\n%f\n", vertex->z0);
00560         if (vertex->start_width != 0.0)
00561         {
00562                 fprintf (fp->fp, " 40\n%f\n", vertex->start_width);
00563         }
00564         if (vertex->end_width != 0.0)
00565         {
00566                 fprintf (fp->fp, " 41\n%f\n", vertex->end_width);
00567         }
00568         if (vertex->bulge != 0.0)
00569         {
00570                 fprintf (fp->fp, " 42\n%f\n", vertex->bulge);
00571         }
00572         fprintf (fp->fp, " 70\n%d\n", vertex->flag);
00573         if (vertex->curve_fit_tangent_direction != 0.0)
00574         {
00575                 fprintf (fp->fp, " 50\n%f\n", vertex->curve_fit_tangent_direction);
00576         }
00577         if (vertex->polyface_mesh_vertex_index_1 != 0)
00578         {
00579                 fprintf (fp->fp, " 71\n%d\n", vertex->polyface_mesh_vertex_index_1);
00580         }
00581         if (vertex->polyface_mesh_vertex_index_2 != 0)
00582         {
00583                 fprintf (fp->fp, " 72\n%d\n", vertex->polyface_mesh_vertex_index_2);
00584         }
00585         if (vertex->polyface_mesh_vertex_index_3 != 0)
00586         {
00587                 fprintf (fp->fp, " 73\n%d\n", vertex->polyface_mesh_vertex_index_3);
00588         }
00589         if (vertex->polyface_mesh_vertex_index_4 != 0)
00590         {
00591                 fprintf (fp->fp, " 74\n%d\n", vertex->polyface_mesh_vertex_index_4);
00592         }
00593         /* Clean up. */
00594         free (dxf_entity_name);
00595 #if DEBUG
00596         DXF_DEBUG_END
00597 #endif
00598         return (EXIT_SUCCESS);
00599 }
00600 
00601 
00615 int
00616 dxf_vertex_free
00617 (
00618         DxfVertex *vertex
00620 )
00621 {
00622 #if DEBUG
00623         DXF_DEBUG_BEGIN
00624 #endif
00625         /* Do some basic checks. */
00626         if (vertex == NULL)
00627         {
00628                 fprintf (stderr,
00629                   (_("Error in %s () a NULL pointer was passed.\n")),
00630                   __FUNCTION__);
00631                 return (EXIT_FAILURE);
00632         }
00633         if (vertex->next != NULL)
00634         {
00635                 fprintf (stderr,
00636                   (_("ERROR in %s () pointer to next was not NULL.\n")),
00637                   __FUNCTION__);
00638                 return (EXIT_FAILURE);
00639         }
00640         free (vertex->linetype);
00641         free (vertex->layer);
00642         free (vertex->dictionary_owner_soft);
00643         free (vertex->dictionary_owner_hard);
00644         free (vertex);
00645         vertex = NULL;
00646 #if DEBUG
00647         DXF_DEBUG_END
00648 #endif
00649         return (EXIT_SUCCESS);
00650 }
00651 
00652 
00663 void
00664 dxf_vertex_free_chain
00665 (
00666         DxfVertex *vertices
00668 )
00669 {
00670 #ifdef DEBUG
00671         DXF_DEBUG_BEGIN
00672 #endif
00673         if (vertices == NULL)
00674         {
00675                 fprintf (stderr,
00676                   (_("Warning in %s () a NULL pointer was passed.\n")),
00677                   __FUNCTION__);
00678         }
00679         while (vertices != NULL)
00680         {
00681                 struct DxfVertex *iter = vertices->next;
00682                 dxf_vertex_free (vertices);
00683                 vertices = (DxfVertex *) iter;
00684         }
00685 #if DEBUG
00686         DXF_DEBUG_END
00687 #endif
00688 }
00689 
00690 
00691 /* EOF */