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

view.c

Go to the documentation of this file.
00001 
00035 #include "view.h"
00036 
00037 
00052 DxfView *
00053 dxf_view_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfView *view = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfView);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((view = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfView struct.\n")),
00068                   __FUNCTION__);
00069                 view = NULL;
00070         }
00071         else
00072         {
00073                 memset (view, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (view);
00079 }
00080 
00081 
00095 DxfView *
00096 dxf_view_init
00097 (
00098         DxfView *view
00100 )
00101 {
00102 #if DEBUG
00103         DXF_DEBUG_BEGIN
00104 #endif
00105         /* Do some basic checks. */
00106         if (view == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 view = dxf_view_new ();
00112         }
00113         if (view == NULL)
00114         {
00115                 fprintf (stderr,
00116                   (_("Error in %s () could not allocate memory for a DxfView struct.\n")),
00117                   __FUNCTION__);
00118                 return (NULL);
00119         }
00120         view->id_code = 0;
00121         view->view_name = strdup ("");
00122         view->x_view = 0.0;
00123         view->y_view = 0.0;
00124         view->x_direction = 0.0;
00125         view->y_direction = 0.0;
00126         view->z_direction = 0.0;
00127         view->x_target = 0.0;
00128         view->y_target = 0.0;
00129         view->z_target = 0.0;
00130         view->view_height = 0.0;
00131         view->view_width = 0.0;
00132         view->lens_length = 0.0;
00133         view->front_plane_offset = 0.0;
00134         view->back_plane_offset = 0.0;
00135         view->view_twist_angle = 0.0;
00136         view->flag = 0;
00137         view->view_mode = 0;
00138         view->dictionary_owner_soft = strdup ("");
00139         view->dictionary_owner_hard = strdup ("");
00140         view->next = NULL;
00141 #if DEBUG
00142         DXF_DEBUG_END
00143 #endif
00144         return (view);
00145 }
00146 
00147 
00165 DxfView *
00166 dxf_view_read
00167 (
00168         DxfFile *fp,
00170         DxfView *view
00172 )
00173 {
00174 #if DEBUG
00175         DXF_DEBUG_BEGIN
00176 #endif
00177         char *temp_string = NULL;
00178 
00179         /* Do some basic checks. */
00180         if (fp == NULL)
00181         {
00182                 fprintf (stderr,
00183                   (_("Error in %s () a NULL file pointer was passed.\n")),
00184                   __FUNCTION__);
00185                 /* Clean up. */
00186                 free (temp_string);
00187                 return (NULL);
00188         }
00189         if (view == NULL)
00190         {
00191                 fprintf (stderr,
00192                   (_("Warning in %s () a NULL pointer was passed.\n")),
00193                   __FUNCTION__);
00194                 view = dxf_view_new ();
00195                 view = dxf_view_init (view);
00196         }
00197         (fp->line_number)++;
00198         fscanf (fp->fp, "%[^\n]", temp_string);
00199         while (strcmp (temp_string, "0") != 0)
00200         {
00201                 if (ferror (fp->fp))
00202                 {
00203                         fprintf (stderr,
00204                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00205                           __FUNCTION__, fp->filename, fp->line_number);
00206                         fclose (fp->fp);
00207                         /* Clean up. */
00208                         free (temp_string);
00209                         return (NULL);
00210                 }
00211                 if (strcmp (temp_string, "5") == 0)
00212                 {
00213                         /* Now follows a string containing a sequential
00214                          * id number. */
00215                         (fp->line_number)++;
00216                         fscanf (fp->fp, "%x\n", &view->id_code);
00217                 }
00218                 else if (strcmp (temp_string, "2") == 0)
00219                 {
00220                         /* Now follows a string containing a view
00221                          * name. */
00222                         (fp->line_number)++;
00223                         fscanf (fp->fp, "%s\n", view->view_name);
00224                 }
00225                 else if (strcmp (temp_string, "10") == 0)
00226                 {
00227                         /* Now follows a string containing the
00228                          * X-coordinate of the View center point. */
00229                         (fp->line_number)++;
00230                         fscanf (fp->fp, "%lf\n", &view->x_view);
00231                 }
00232                 else if (strcmp (temp_string, "20") == 0)
00233                 {
00234                         /* Now follows a string containing the
00235                          * Y-coordinate of the View center point. */
00236                         (fp->line_number)++;
00237                         fscanf (fp->fp, "%lf\n", &view->y_view);
00238                 }
00239                 else if (strcmp (temp_string, "11") == 0)
00240                 {
00241                         /* Now follows a string containing the
00242                          * X-coordinate of the View direction from
00243                          * target. */
00244                         (fp->line_number)++;
00245                         fscanf (fp->fp, "%lf\n", &view->x_direction);
00246                 }
00247                 else if (strcmp (temp_string, "21") == 0)
00248                 {
00249                         /* Now follows a string containing the
00250                          * Y-coordinate of the View direction from
00251                          * target. */
00252                         (fp->line_number)++;
00253                         fscanf (fp->fp, "%lf\n", &view->y_direction);
00254                 }
00255                 else if (strcmp (temp_string, "31") == 0)
00256                 {
00257                         /* Now follows a string containing the
00258                          * Z-coordinate of the View direction from
00259                          * target. */
00260                         (fp->line_number)++;
00261                         fscanf (fp->fp, "%lf\n", &view->z_direction);
00262                 }
00263                 else if (strcmp (temp_string, "12") == 0)
00264                 {
00265                         /* Now follows a string containing the
00266                          * X-coordinate of the Target point. */
00267                         (fp->line_number)++;
00268                         fscanf (fp->fp, "%lf\n", &view->x_target);
00269                 }
00270                 else if (strcmp (temp_string, "22") == 0)
00271                 {
00272                         /* Now follows a string containing the
00273                          * Y-coordinate of the Target point. */
00274                         (fp->line_number)++;
00275                         fscanf (fp->fp, "%lf\n", &view->y_target);
00276                 }
00277                 else if (strcmp (temp_string, "32") == 0)
00278                 {
00279                         /* Now follows a string containing the
00280                          * Z-coordinate of the Target point. */
00281                         (fp->line_number)++;
00282                         fscanf (fp->fp, "%lf\n", &view->z_target);
00283                 }
00284                 else if (strcmp (temp_string, "40") == 0)
00285                 {
00286                         /* Now follows a string containing the view
00287                          * height. */
00288                         (fp->line_number)++;
00289                         fscanf (fp->fp, "%lf\n", &view->view_height);
00290                 }
00291                 else if (strcmp (temp_string, "41") == 0)
00292                 {
00293                         /* Now follows a string containing the view
00294                          * width. */
00295                         (fp->line_number)++;
00296                         fscanf (fp->fp, "%lf\n", &view->view_width);
00297                 }
00298                 else if (strcmp (temp_string, "42") == 0)
00299                 {
00300                         /* Now follows a string containing the lens
00301                          * length. */
00302                         (fp->line_number)++;
00303                         fscanf (fp->fp, "%lf\n", &view->lens_length);
00304                 }
00305                 else if (strcmp (temp_string, "43") == 0)
00306                 {
00307                         /* Now follows a string containing the Front
00308                          * clipping plane - offset from target point. */
00309                         (fp->line_number)++;
00310                         fscanf (fp->fp, "%lf\n", &view->front_plane_offset);
00311                 }
00312                 else if (strcmp (temp_string, "44") == 0)
00313                 {
00314                         /* Now follows a string containing the Back
00315                          * clipping plane - offset from target point. */
00316                         (fp->line_number)++;
00317                         fscanf (fp->fp, "%lf\n", &view->back_plane_offset);
00318                 }
00319                 else if (strcmp (temp_string, "50") == 0)
00320                 {
00321                         /* Now follows a string containing the view
00322                          * twist angle. */
00323                         (fp->line_number)++;
00324                         fscanf (fp->fp, "%lf\n", &view->view_twist_angle);
00325                 }
00326                 else if (strcmp (temp_string, "70") == 0)
00327                 {
00328                         /* Now follows a string containing the
00329                          * standard flag value. */
00330                         (fp->line_number)++;
00331                         fscanf (fp->fp, "%d\n", &view->flag);
00332                 }
00333                 else if (strcmp (temp_string, "71") == 0)
00334                 {
00335                         /* Now follows a string containing the view mode
00336                          * value. */
00337                         (fp->line_number)++;
00338                         fscanf (fp->fp, "%d\n", &view->view_mode);
00339                 }
00340                 else if ((fp->acad_version_number >= AutoCAD_13)
00341                         && (strcmp (temp_string, "100") == 0))
00342                 {
00343                         /* Now follows a string containing the
00344                          * subclass marker value. */
00345                         (fp->line_number)++;
00346                         fscanf (fp->fp, "%s\n", temp_string);
00347                         if ((strcmp (temp_string, "AcDbSymbolTableRecord") != 0)
00348                         && ((strcmp (temp_string, "AcDbViewTableRecord") != 0)))
00349                         {
00350                                 fprintf (stderr,
00351                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00352                                   __FUNCTION__, fp->filename, fp->line_number);
00353                         }
00354                 }
00355                 else if (strcmp (temp_string, "330") == 0)
00356                 {
00357                         /* Now follows a string containing Soft-pointer
00358                          * ID/handle to owner dictionary. */
00359                         (fp->line_number)++;
00360                         fscanf (fp->fp, "%s\n", view->dictionary_owner_soft);
00361                 }
00362                 else if (strcmp (temp_string, "360") == 0)
00363                 {
00364                         /* Now follows a string containing Hard owner
00365                          * ID/handle to owner dictionary. */
00366                         (fp->line_number)++;
00367                         fscanf (fp->fp, "%s\n", view->dictionary_owner_hard);
00368                 }
00369                 else if (strcmp (temp_string, "999") == 0)
00370                 {
00371                         /* Now follows a string containing a comment. */
00372                         (fp->line_number)++;
00373                         fscanf (fp->fp, "%s\n", temp_string);
00374                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00375                 }
00376                 else
00377                 {
00378                         fprintf (stderr,
00379                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00380                           __FUNCTION__, fp->filename, fp->line_number);
00381                 }
00382         }
00383         /* Clean up. */
00384         free (temp_string);
00385 #if DEBUG
00386         DXF_DEBUG_END
00387 #endif
00388         return (view);
00389 }
00390 
00391 
00404 int
00405 dxf_view_write
00406 (
00407         DxfFile *fp,
00409         DxfView *view
00411 )
00412 {
00413 #if DEBUG
00414         DXF_DEBUG_BEGIN
00415 #endif
00416         char *dxf_entity_name = strdup ("VIEW");
00417 
00418         /* Do some basic checks. */
00419         if (fp == NULL)
00420         {
00421                 fprintf (stderr,
00422                   (_("Error in %s () a NULL file pointer was passed.\n")),
00423                   __FUNCTION__);
00424                 /* Clean up. */
00425                 free (dxf_entity_name);
00426                 return (EXIT_FAILURE);
00427         }
00428         if (view == NULL)
00429         {
00430                 fprintf (stderr,
00431                   (_("Error in %s () a NULL pointer was passed.\n")),
00432                   __FUNCTION__);
00433                 /* Clean up. */
00434                 free (dxf_entity_name);
00435                 return (EXIT_FAILURE);
00436         }
00437         if ((view->view_name == NULL)
00438           || (strcmp (view->view_name, "") == 0))
00439         {
00440                 fprintf (stderr,
00441                   (_("Error in %s () empty UCS name string for the %s entity with id-code: %x\n")),
00442                   __FUNCTION__, dxf_entity_name, view->id_code);
00443                 fprintf (stderr,
00444                   (_("\t%s entity is discarded from output.\n")),
00445                   dxf_entity_name);
00446                 /* Clean up. */
00447                 free (dxf_entity_name);
00448                 return (EXIT_FAILURE);
00449         }
00450         /* Start writing output. */
00451         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00452         if (view->id_code != -1)
00453         {
00454                 fprintf (fp->fp, "  5\n%x\n", view->id_code);
00455         }
00466         if ((strcmp (view->dictionary_owner_soft, "") != 0)
00467           && (fp->acad_version_number >= AutoCAD_14))
00468         {
00469                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00470                 fprintf (fp->fp, "330\n%s\n", view->dictionary_owner_soft);
00471                 fprintf (fp->fp, "102\n}\n");
00472         }
00473         if ((strcmp (view->dictionary_owner_hard, "") != 0)
00474           && (fp->acad_version_number >= AutoCAD_14))
00475         {
00476                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00477                 fprintf (fp->fp, "360\n%s\n", view->dictionary_owner_hard);
00478                 fprintf (fp->fp, "102\n}\n");
00479         }
00480         if (fp->acad_version_number >= AutoCAD_13)
00481         {
00482                 fprintf (fp->fp, "100\nAcDbSymbolTableRecord\n");
00483                 fprintf (fp->fp, "100\nAcDbViewTableRecord\n");
00484         }
00485         fprintf (fp->fp, "  2\n%s\n", view->view_name);
00486         fprintf (fp->fp, " 40\n%f\n", view->view_height);
00487         fprintf (fp->fp, " 70\n%d\n", view->flag);
00488         fprintf (fp->fp, " 10\n%f\n", view->x_view);
00489         fprintf (fp->fp, " 20\n%f\n", view->y_view);
00490         fprintf (fp->fp, " 41\n%f\n", view->view_width);
00491         fprintf (fp->fp, " 11\n%f\n", view->x_direction);
00492         fprintf (fp->fp, " 21\n%f\n", view->y_direction);
00493         fprintf (fp->fp, " 31\n%f\n", view->z_direction);
00494         fprintf (fp->fp, " 12\n%f\n", view->x_target);
00495         fprintf (fp->fp, " 22\n%f\n", view->y_target);
00496         fprintf (fp->fp, " 32\n%f\n", view->z_target);
00497         fprintf (fp->fp, " 42\n%f\n", view->lens_length);
00498         fprintf (fp->fp, " 43\n%f\n", view->front_plane_offset);
00499         fprintf (fp->fp, " 44\n%f\n", view->back_plane_offset);
00500         fprintf (fp->fp, " 50\n%f\n", view->view_twist_angle);
00501         fprintf (fp->fp, " 71\n%d\n", view->view_mode);
00502         /* Clean up. */
00503         free (dxf_entity_name);
00504 #if DEBUG
00505         DXF_DEBUG_END
00506 #endif
00507         return (EXIT_SUCCESS);
00508 }
00509 
00510 
00524 int
00525 dxf_view_free
00526 (
00527         DxfView *view
00529 )
00530 {
00531 #if DEBUG
00532         DXF_DEBUG_BEGIN
00533 #endif
00534         /* Do some basic checks. */
00535         if (view == NULL)
00536         {
00537                 fprintf (stderr,
00538                   (_("Error in %s () a NULL pointer was passed.\n")),
00539                   __FUNCTION__);
00540                 return (EXIT_FAILURE);
00541         }
00542         if (view->next != NULL)
00543         {
00544                 fprintf (stderr,
00545                   (_("Error in %s () pointer to next was not NULL.\n")),
00546                   __FUNCTION__);
00547                 return (EXIT_FAILURE);
00548         }
00549         free (view->view_name);
00550         free (view->dictionary_owner_soft);
00551         free (view->dictionary_owner_hard);
00552         free (view);
00553         view = NULL;
00554 #if DEBUG
00555         DXF_DEBUG_END
00556 #endif
00557         return (EXIT_SUCCESS);
00558 }
00559 
00560 
00571 void
00572 dxf_view_free_chain
00573 (
00574         DxfView *views
00577 )
00578 {
00579 #ifdef DEBUG
00580         DXF_DEBUG_BEGIN
00581 #endif
00582         if (views == NULL)
00583         {
00584                 fprintf (stderr,
00585                   (_("Warning in %s () a NULL pointer was passed.\n")),
00586                   __FUNCTION__);
00587         }
00588         while (views != NULL)
00589         {
00590                 struct DxfView *iter = views->next;
00591                 dxf_view_free (views);
00592                 views = (DxfView *) iter;
00593         }
00594 #if DEBUG
00595         DXF_DEBUG_END
00596 #endif
00597 }
00598 
00599 
00600 /* EOF */