libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00041 #include "vport.h" 00042 00043 00055 DxfVPort * 00056 dxf_vport_new () 00057 { 00058 #if DEBUG 00059 DXF_DEBUG_BEGIN 00060 #endif 00061 DxfVPort *vport = NULL; 00062 size_t size; 00063 00064 size = sizeof (DxfVPort); 00065 /* avoid malloc of 0 bytes */ 00066 if (size == 0) size = 1; 00067 if ((vport = malloc (size)) == NULL) 00068 { 00069 fprintf (stderr, 00070 (_("Error in %s () could not allocate memory for a DxfVPort struct.\n")), 00071 __FUNCTION__); 00072 vport = NULL; 00073 } 00074 else 00075 { 00076 memset (vport, 0, size); 00077 } 00078 #if DEBUG 00079 DXF_DEBUG_END 00080 #endif 00081 return (vport); 00082 } 00083 00084 00098 DxfVPort * 00099 dxf_vport_init 00100 ( 00101 DxfVPort *vport 00103 ) 00104 { 00105 #if DEBUG 00106 DXF_DEBUG_BEGIN 00107 #endif 00108 /* Do some basic checks. */ 00109 if (vport == NULL) 00110 { 00111 fprintf (stderr, 00112 (_("Warning in %s () a NULL pointer was passed.\n")), 00113 __FUNCTION__); 00114 vport = dxf_vport_new (); 00115 } 00116 if (vport == NULL) 00117 { 00118 fprintf (stderr, 00119 (_("Error in %s () could not allocate memory for a DxfVPort struct.\n")), 00120 __FUNCTION__); 00121 return (NULL); 00122 } 00123 vport->id_code = 0; 00124 vport->viewport_name = strdup (""); 00125 vport->x_min = 0.0; 00126 vport->y_min = 0.0; 00127 vport->x_max = 0.0; 00128 vport->y_max = 0.0; 00129 vport->x_center = 0.0; 00130 vport->y_center = 0.0; 00131 vport->x_snap_base = 0.0; 00132 vport->y_snap_base = 0.0; 00133 vport->x_snap_spacing = 0.0; 00134 vport->y_snap_spacing = 0.0; 00135 vport->x_grid_spacing = 0.0; 00136 vport->y_grid_spacing = 0.0; 00137 vport->x_direction = 0.0; 00138 vport->y_direction = 0.0; 00139 vport->z_direction = 0.0; 00140 vport->x_target = 0.0; 00141 vport->y_target = 0.0; 00142 vport->z_target = 0.0; 00143 vport->view_height = 0.0; 00144 vport->viewport_aspect_ratio = 0.0; 00145 vport->lens_length = 0.0; 00146 vport->front_plane_offset = 0.0; 00147 vport->back_plane_offset = 0.0; 00148 vport->snap_rotation_angle = 0.0; 00149 vport->view_twist_angle = 0.0; 00150 vport->status_field = 0; 00151 vport->id = 0; 00152 vport->standard_flag = 0; 00153 vport->view_mode = 0; 00154 vport->circle_zoom_percent = 0; 00155 vport->fast_zoom_setting = 0; 00156 vport->UCSICON_setting = 0; 00157 vport->snap_on = 0; 00158 vport->grid_on = 0; 00159 vport->snap_style = 0; 00160 vport->snap_isopair = 0; 00161 vport->dictionary_owner_soft = strdup (""); 00162 vport->dictionary_owner_hard = strdup (""); 00163 vport->next = NULL; 00164 #if DEBUG 00165 DXF_DEBUG_END 00166 #endif 00167 return (vport); 00168 } 00169 00170 00189 DxfVPort * 00190 dxf_vport_read 00191 ( 00192 DxfFile *fp, 00194 DxfVPort *vport 00196 ) 00197 { 00198 #if DEBUG 00199 DXF_DEBUG_BEGIN 00200 #endif 00201 char *temp_string = NULL; 00202 00203 /* Do some basic checks. */ 00204 if (fp == NULL) 00205 { 00206 fprintf (stderr, 00207 (_("Error in %s () a NULL file pointer was passed.\n")), 00208 __FUNCTION__); 00209 /* Clean up. */ 00210 free (temp_string); 00211 return (NULL); 00212 } 00213 if (vport == NULL) 00214 { 00215 fprintf (stderr, 00216 (_("Warning in %s () a NULL pointer was passed.\n")), 00217 __FUNCTION__); 00218 vport = dxf_vport_new (); 00219 vport = dxf_vport_init (vport); 00220 } 00221 (fp->line_number)++; 00222 fscanf (fp->fp, "%[^\n]", temp_string); 00223 while (strcmp (temp_string, "0") != 0) 00224 { 00225 if (ferror (fp->fp)) 00226 { 00227 fprintf (stderr, 00228 (_("Error in %s () while reading from: %s in line: %d.\n")), 00229 __FUNCTION__, fp->filename, fp->line_number); 00230 fclose (fp->fp); 00231 /* Clean up. */ 00232 free (temp_string); 00233 return (NULL); 00234 } 00235 if (strcmp (temp_string, "5") == 0) 00236 { 00237 /* Now follows a string containing a sequential 00238 * id number. */ 00239 (fp->line_number)++; 00240 fscanf (fp->fp, "%x\n", &vport->id_code); 00241 } 00242 else if (strcmp (temp_string, "2") == 0) 00243 { 00244 /* Now follows a string containing a viewport 00245 * name. */ 00246 (fp->line_number)++; 00247 fscanf (fp->fp, "%s\n", vport->viewport_name); 00248 } 00249 else if (strcmp (temp_string, "10") == 0) 00250 { 00251 /* Now follows a string containing the 00252 * X value of the lower-left corner of viewport. */ 00253 (fp->line_number)++; 00254 fscanf (fp->fp, "%lf\n", &vport->x_min); 00255 } 00256 else if (strcmp (temp_string, "20") == 0) 00257 { 00258 /* Now follows a string containing the 00259 * Y value of the lower-left corner of viewport. */ 00260 (fp->line_number)++; 00261 fscanf (fp->fp, "%lf\n", &vport->y_min); 00262 } 00263 else if (strcmp (temp_string, "11") == 0) 00264 { 00265 /* Now follows a string containing the 00266 * X value of the upper-right corner of viewport. */ 00267 (fp->line_number)++; 00268 fscanf (fp->fp, "%lf\n", &vport->x_max); 00269 } 00270 else if (strcmp (temp_string, "21") == 0) 00271 { 00272 /* Now follows a string containing the 00273 * Y value of the upper-right corner of viewport. */ 00274 (fp->line_number)++; 00275 fscanf (fp->fp, "%lf\n", &vport->y_max); 00276 } 00277 else if (strcmp (temp_string, "12") == 0) 00278 { 00279 /* Now follows a string containing the 00280 * X value of the view center point. */ 00281 (fp->line_number)++; 00282 fscanf (fp->fp, "%lf\n", &vport->x_center); 00283 } 00284 else if (strcmp (temp_string, "22") == 0) 00285 { 00286 /* Now follows a string containing the 00287 * Y value of the view center point. */ 00288 (fp->line_number)++; 00289 fscanf (fp->fp, "%lf\n", &vport->y_center); 00290 } 00291 else if (strcmp (temp_string, "13") == 0) 00292 { 00293 /* Now follows a string containing the 00294 * X value of the snap base point. */ 00295 (fp->line_number)++; 00296 fscanf (fp->fp, "%lf\n", &vport->x_snap_base); 00297 } 00298 else if (strcmp (temp_string, "23") == 0) 00299 { 00300 /* Now follows a string containing the 00301 * Y value of the snap base point. */ 00302 (fp->line_number)++; 00303 fscanf (fp->fp, "%lf\n", &vport->y_snap_base); 00304 } 00305 else if (strcmp (temp_string, "14") == 0) 00306 { 00307 /* Now follows a string containing the 00308 * X value of snap spacing X and Y. */ 00309 (fp->line_number)++; 00310 fscanf (fp->fp, "%lf\n", &vport->x_snap_spacing); 00311 } 00312 else if (strcmp (temp_string, "24") == 0) 00313 { 00314 /* Now follows a string containing the 00315 * Y value of snap spacing X and Y. */ 00316 (fp->line_number)++; 00317 fscanf (fp->fp, "%lf\n", &vport->y_snap_spacing); 00318 } 00319 else if (strcmp (temp_string, "15") == 0) 00320 { 00321 /* Now follows a string containing the 00322 * X value of grid spacing X and Y. */ 00323 (fp->line_number)++; 00324 fscanf (fp->fp, "%lf\n", &vport->x_grid_spacing); 00325 } 00326 else if (strcmp (temp_string, "25") == 0) 00327 { 00328 /* Now follows a string containing the 00329 * Y value of grid spacing X and Y. */ 00330 (fp->line_number)++; 00331 fscanf (fp->fp, "%lf\n", &vport->y_grid_spacing); 00332 } 00333 else if (strcmp (temp_string, "16") == 0) 00334 { 00335 /* Now follows a string containing the 00336 * X value of the view direction from target point. */ 00337 (fp->line_number)++; 00338 fscanf (fp->fp, "%lf\n", &vport->x_direction); 00339 } 00340 else if (strcmp (temp_string, "26") == 0) 00341 { 00342 /* Now follows a string containing the 00343 * Y value of the view direction from target point. */ 00344 (fp->line_number)++; 00345 fscanf (fp->fp, "%lf\n", &vport->y_direction); 00346 } 00347 else if (strcmp (temp_string, "36") == 0) 00348 { 00349 /* Now follows a string containing the 00350 * Z value of the view direction from target point. */ 00351 (fp->line_number)++; 00352 fscanf (fp->fp, "%lf\n", &vport->z_direction); 00353 } 00354 else if (strcmp (temp_string, "17") == 0) 00355 { 00356 /* Now follows a string containing the 00357 * X value of the view target point. */ 00358 (fp->line_number)++; 00359 fscanf (fp->fp, "%lf\n", &vport->x_target); 00360 } 00361 else if (strcmp (temp_string, "27") == 0) 00362 { 00363 /* Now follows a string containing the 00364 * Y value of the view target point. */ 00365 (fp->line_number)++; 00366 fscanf (fp->fp, "%lf\n", &vport->y_target); 00367 } 00368 else if (strcmp (temp_string, "37") == 0) 00369 { 00370 /* Now follows a string containing the 00371 * Z value of the view target point. */ 00372 (fp->line_number)++; 00373 fscanf (fp->fp, "%lf\n", &vport->z_target); 00374 } 00375 else if (strcmp (temp_string, "40") == 0) 00376 { 00377 /* Now follows a string containing the view 00378 * height. */ 00379 (fp->line_number)++; 00380 fscanf (fp->fp, "%lf\n", &vport->view_height); 00381 } 00382 else if (strcmp (temp_string, "41") == 0) 00383 { 00384 /* Now follows a string containing the viewport 00385 * aspect ratio. */ 00386 (fp->line_number)++; 00387 fscanf (fp->fp, "%lf\n", &vport->viewport_aspect_ratio); 00388 } 00389 else if (strcmp (temp_string, "42") == 0) 00390 { 00391 /* Now follows a string containing the lens 00392 * length. */ 00393 (fp->line_number)++; 00394 fscanf (fp->fp, "%lf\n", &vport->viewport_aspect_ratio); 00395 } 00396 else if (strcmp (temp_string, "43") == 0) 00397 { 00398 /* Now follows a string containing the front 00399 * clipping plane - offset from target point. */ 00400 (fp->line_number)++; 00401 fscanf (fp->fp, "%lf\n", &vport->front_plane_offset); 00402 } 00403 else if (strcmp (temp_string, "44") == 0) 00404 { 00405 /* Now follows a string containing the back 00406 * clipping plane - offset from target point. */ 00407 (fp->line_number)++; 00408 fscanf (fp->fp, "%lf\n", &vport->back_plane_offset); 00409 } 00410 else if (strcmp (temp_string, "50") == 0) 00411 { 00412 /* Now follows a string containing the snap 00413 * rotation angle. */ 00414 (fp->line_number)++; 00415 fscanf (fp->fp, "%lf\n", &vport->snap_rotation_angle); 00416 } 00417 else if (strcmp (temp_string, "51") == 0) 00418 { 00419 /* Now follows a string containing the view 00420 * twist angle. */ 00421 (fp->line_number)++; 00422 fscanf (fp->fp, "%lf\n", &vport->view_twist_angle); 00423 } 00424 else if (strcmp (temp_string, "68") == 0) 00425 { 00426 /* Now follows a string containing the 00427 * status field value. */ 00428 (fp->line_number)++; 00429 fscanf (fp->fp, "%d\n", &vport->status_field); 00430 } 00431 else if (strcmp (temp_string, "69") == 0) 00432 { 00433 /* Now follows a string containing the 00434 * ID value. */ 00435 (fp->line_number)++; 00436 fscanf (fp->fp, "%d\n", &vport->id); 00437 } 00438 else if (strcmp (temp_string, "70") == 0) 00439 { 00440 /* Now follows a string containing the 00441 * standard flag value. */ 00442 (fp->line_number)++; 00443 fscanf (fp->fp, "%d\n", &vport->standard_flag); 00444 } 00445 else if (strcmp (temp_string, "71") == 0) 00446 { 00447 /* Now follows a string containing the 00448 * view mode value. */ 00449 (fp->line_number)++; 00450 fscanf (fp->fp, "%d\n", &vport->view_mode); 00451 } 00452 else if (strcmp (temp_string, "72") == 0) 00453 { 00454 /* Now follows a string containing the circle 00455 * zoom percent value. */ 00456 (fp->line_number)++; 00457 fscanf (fp->fp, "%d\n", &vport->circle_zoom_percent); 00458 } 00459 else if (strcmp (temp_string, "73") == 0) 00460 { 00461 /* Now follows a string containing the fast 00462 * zoom setting value. */ 00463 (fp->line_number)++; 00464 fscanf (fp->fp, "%d\n", &vport->fast_zoom_setting); 00465 } 00466 else if (strcmp (temp_string, "74") == 0) 00467 { 00468 /* Now follows a string containing the UCSICON 00469 * setting value. */ 00470 (fp->line_number)++; 00471 fscanf (fp->fp, "%d\n", &vport->UCSICON_setting); 00472 } 00473 else if (strcmp (temp_string, "75") == 0) 00474 { 00475 /* Now follows a string containing the snap 00476 * on/off value. */ 00477 (fp->line_number)++; 00478 fscanf (fp->fp, "%d\n", &vport->snap_on); 00479 } 00480 else if (strcmp (temp_string, "76") == 0) 00481 { 00482 /* Now follows a string containing the grid 00483 * on/off value. */ 00484 (fp->line_number)++; 00485 fscanf (fp->fp, "%d\n", &vport->grid_on); 00486 } 00487 else if (strcmp (temp_string, "77") == 0) 00488 { 00489 /* Now follows a string containing the snap 00490 * style value. */ 00491 (fp->line_number)++; 00492 fscanf (fp->fp, "%d\n", &vport->snap_style); 00493 } 00494 else if (strcmp (temp_string, "78") == 0) 00495 { 00496 /* Now follows a string containing the snap 00497 * isopair value. */ 00498 (fp->line_number)++; 00499 fscanf (fp->fp, "%d\n", &vport->snap_isopair); 00500 } 00501 else if (strcmp (temp_string, "330") == 0) 00502 { 00503 /* Now follows a string containing Soft-pointer 00504 * ID/handle to owner dictionary. */ 00505 (fp->line_number)++; 00506 fscanf (fp->fp, "%s\n", vport->dictionary_owner_soft); 00507 } 00508 else if (strcmp (temp_string, "360") == 0) 00509 { 00510 /* Now follows a string containing Hard owner 00511 * ID/handle to owner dictionary. */ 00512 (fp->line_number)++; 00513 fscanf (fp->fp, "%s\n", vport->dictionary_owner_hard); 00514 } 00515 else if (strcmp (temp_string, "999") == 0) 00516 { 00517 /* Now follows a string containing a comment. */ 00518 (fp->line_number)++; 00519 fscanf (fp->fp, "%s\n", temp_string); 00520 fprintf (stdout, "DXF comment: %s\n", temp_string); 00521 } 00522 00523 } 00524 /* Handle omitted members and/or illegal values. */ 00525 if ((strcmp (vport->viewport_name, "") == 0) 00526 || (vport->viewport_name == NULL)) 00527 { 00528 fprintf (stderr, 00529 (_("Error in %s () empty string in viewport name found while reading from: %s in line: %d.\n")), 00530 __FUNCTION__, fp->filename, fp->line_number); 00531 return (NULL); 00532 } 00533 /* Clean up. */ 00534 free (temp_string); 00535 #if DEBUG 00536 DXF_DEBUG_END 00537 #endif 00538 return (vport); 00539 } 00540 00541 00552 int 00553 dxf_vport_write 00554 ( 00555 DxfFile *fp, 00557 DxfVPort *vport 00559 ) 00560 { 00561 #if DEBUG 00562 DXF_DEBUG_BEGIN 00563 #endif 00564 char *dxf_entity_name = strdup ("VPORT"); 00565 00566 /* Do some basic checks. */ 00567 if (fp == NULL) 00568 { 00569 fprintf (stderr, 00570 (_("Error in %s () a NULL file pointer was passed.\n")), 00571 __FUNCTION__); 00572 /* Clean up. */ 00573 free (dxf_entity_name); 00574 return (EXIT_FAILURE); 00575 } 00576 if (vport == NULL) 00577 { 00578 fprintf (stderr, 00579 (_("Error in %s () a NULL pointer was passed.\n")), 00580 __FUNCTION__); 00581 /* Clean up. */ 00582 free (dxf_entity_name); 00583 return (EXIT_FAILURE); 00584 } 00585 if ((vport->viewport_name == NULL) 00586 || (strcmp (vport->viewport_name, "") == 0)) 00587 { 00588 fprintf (stderr, 00589 (_("Error: empty viewport name string for the %s entity with id-code: %x\n")), 00590 dxf_entity_name, vport->id_code); 00591 fprintf (stderr, 00592 (_("\t%s entity is discarded from output.\n")), 00593 dxf_entity_name); 00594 /* Clean up. */ 00595 free (dxf_entity_name); 00596 return (EXIT_FAILURE); 00597 } 00598 /* Start writing output. */ 00599 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00600 if (vport->id_code != -1) 00601 { 00602 fprintf (fp->fp, " 5\n%x\n", vport->id_code); 00603 } 00614 if ((strcmp (vport->dictionary_owner_soft, "") != 0) 00615 && (fp->acad_version_number >= AutoCAD_14)) 00616 { 00617 fprintf (fp->fp, "102\n{ACAD_REACTORS\n"); 00618 fprintf (fp->fp, "330\n%s\n", vport->dictionary_owner_soft); 00619 fprintf (fp->fp, "102\n}\n"); 00620 } 00621 if ((strcmp (vport->dictionary_owner_hard, "") != 0) 00622 && (fp->acad_version_number >= AutoCAD_14)) 00623 { 00624 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n"); 00625 fprintf (fp->fp, "360\n%s\n", vport->dictionary_owner_hard); 00626 fprintf (fp->fp, "102\n}\n"); 00627 } 00628 if (fp->acad_version_number >= AutoCAD_13) 00629 { 00630 fprintf (fp->fp, "100\nAcDbSymbolTableRecord\n"); 00631 fprintf (fp->fp, "100\nAcDbViewportTableRecord\n"); 00632 } 00633 fprintf (fp->fp, " 2\n%s\n", vport->viewport_name); 00634 fprintf (fp->fp, " 70\n%d\n", vport->standard_flag); 00635 fprintf (fp->fp, " 10\n%f\n", vport->x_min); 00636 fprintf (fp->fp, " 20\n%f\n", vport->y_min); 00637 fprintf (fp->fp, " 11\n%f\n", vport->x_max); 00638 fprintf (fp->fp, " 21\n%f\n", vport->y_max); 00639 fprintf (fp->fp, " 12\n%f\n", vport->x_center); 00640 fprintf (fp->fp, " 22\n%f\n", vport->y_center); 00641 fprintf (fp->fp, " 13\n%f\n", vport->x_snap_base); 00642 fprintf (fp->fp, " 23\n%f\n", vport->y_snap_base); 00643 fprintf (fp->fp, " 14\n%f\n", vport->x_snap_spacing); 00644 fprintf (fp->fp, " 24\n%f\n", vport->y_snap_spacing); 00645 fprintf (fp->fp, " 15\n%f\n", vport->x_grid_spacing); 00646 fprintf (fp->fp, " 25\n%f\n", vport->y_grid_spacing); 00647 fprintf (fp->fp, " 16\n%f\n", vport->x_direction); 00648 fprintf (fp->fp, " 26\n%f\n", vport->y_direction); 00649 fprintf (fp->fp, " 36\n%f\n", vport->z_direction); 00650 fprintf (fp->fp, " 17\n%f\n", vport->x_target); 00651 fprintf (fp->fp, " 27\n%f\n", vport->y_target); 00652 fprintf (fp->fp, " 37\n%f\n", vport->z_target); 00653 fprintf (fp->fp, " 40\n%f\n", vport->view_height); 00654 fprintf (fp->fp, " 41\n%f\n", vport->viewport_aspect_ratio); 00655 fprintf (fp->fp, " 42\n%f\n", vport->lens_length); 00656 fprintf (fp->fp, " 43\n%f\n", vport->front_plane_offset); 00657 fprintf (fp->fp, " 44\n%f\n", vport->back_plane_offset); 00658 fprintf (fp->fp, " 50\n%f\n", vport->snap_rotation_angle); 00659 fprintf (fp->fp, " 51\n%f\n", vport->view_twist_angle); 00660 // fprintf (fp->fp, " 68\n%d\n", vport->status_field); 00661 // fprintf (fp->fp, " 69\n%d\n", vport->id); 00662 fprintf (fp->fp, " 71\n%d\n", vport->view_mode); 00663 fprintf (fp->fp, " 72\n%d\n", vport->circle_zoom_percent); 00664 fprintf (fp->fp, " 73\n%d\n", vport->fast_zoom_setting); 00665 fprintf (fp->fp, " 74\n%d\n", vport->UCSICON_setting); 00666 fprintf (fp->fp, " 75\n%d\n", vport->snap_on); 00667 fprintf (fp->fp, " 76\n%d\n", vport->grid_on); 00668 fprintf (fp->fp, " 77\n%d\n", vport->snap_style); 00669 fprintf (fp->fp, " 78\n%d\n", vport->snap_isopair); 00670 /* Clean up. */ 00671 free (dxf_entity_name); 00672 #if DEBUG 00673 DXF_DEBUG_END 00674 #endif 00675 return (EXIT_SUCCESS); 00676 } 00677 00678 00692 int 00693 dxf_vport_free 00694 ( 00695 DxfVPort *vport 00698 ) 00699 { 00700 #if DEBUG 00701 DXF_DEBUG_BEGIN 00702 #endif 00703 /* Do some basic checks. */ 00704 if (vport == NULL) 00705 { 00706 fprintf (stderr, 00707 (_("Error in %s () a NULL pointer was passed.\n")), 00708 __FUNCTION__); 00709 return (EXIT_FAILURE); 00710 } 00711 if (vport->next != NULL) 00712 { 00713 fprintf (stderr, 00714 (_("Error in %s () pointer to next was not NULL.\n")), 00715 __FUNCTION__); 00716 return (EXIT_FAILURE); 00717 } 00718 free (vport->viewport_name); 00719 free (vport->dictionary_owner_soft); 00720 free (vport->dictionary_owner_hard); 00721 free (vport); 00722 vport = NULL; 00723 #if DEBUG 00724 DXF_DEBUG_END 00725 #endif 00726 return (EXIT_SUCCESS); 00727 } 00728 00729 00740 void 00741 dxf_vport_free_chain 00742 ( 00743 DxfVPort *vports 00746 ) 00747 { 00748 #ifdef DEBUG 00749 DXF_DEBUG_BEGIN 00750 #endif 00751 if (vports == NULL) 00752 { 00753 fprintf (stderr, 00754 (_("Warning in %s () a NULL pointer was passed.\n")), 00755 __FUNCTION__); 00756 } 00757 while (vports != NULL) 00758 { 00759 struct DxfVPort *iter = vports->next; 00760 dxf_vport_free (vports); 00761 vports = (DxfVPort *) iter; 00762 } 00763 #if DEBUG 00764 DXF_DEBUG_END 00765 #endif 00766 } 00767 00768 00769 /* EOF*/