The first problem that i found is the extraction of layer and color for all entities and block name for inserts.
Someone know if exist some program.c that use libredwg to extract dwg entities information, as any table format?
I was seeing the Archimedes project the programs load_dwg.c and dwg_ps.c but, how i said i not found the way to modify the program to solve my problem.
someone can help me?I attach some files. one is cad.osd, as a little explanation of the text file format
and the classes that i modify but no completely success. Steps i need to solve 1 - Layer, color, linetype for all entities extraxted2 - Block Name for inserts (i no focus on block drawing, just only name, color, layer, linetype)
3 - Block attibute text data. to be continued... Tanks. Martin Belmonte. http://www.gambas-es.org
cad.ods
Description: application/vnd.oasis.opendocument.spreadsheet
/*****************************************************************************/ /* LibreDWG - free implementation of the DWG file format */ /* */ /* Copyright (C) 2009 Free Software Foundation, Inc. */ /* Copyright (C) 2010 Thien-Thi Nguyen */ /* */ /* This library is free software, licensed under the terms of the GNU */ /* General Public License as published by the Free Software Foundation, */ /* either version 3 of the License, or (at your option) any later version. */ /* You should have received a copy of the GNU General Public License */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /*****************************************************************************/ /* * dwg_ps.c: create a PostScript file from a DWG * written by Felipe Castro * modified by Felipe Corrêa da Silva Sances * modified by Thien-Thi Nguyen */ #include <stdio.h> #include <dwg.h> #include <libps/pslib.h> #include "suffix.c" void create_postscript(Dwg_Data *dwg, char *output) { float dx; float dy; float scale_x; float scale_y; float scale; long unsigned i; FILE *fh; PSDoc *ps; /* Initialization */ PS_boot(); ps = PS_new(); if (PS_open_file(ps, output) < 0) { puts("Cannot write PostScript file"); return; } PS_set_info(ps, "Creator", "dwg_ps"); PS_set_info(ps, "Author", "LibreDWG example"); PS_set_info(ps, "Title", "DWG to Postscript example"); PS_set_info(ps, "Keywords", "dwg, postscript, conversion, CAD, plot"); /* First page: Model Space (?) */ dx = (dwg_model_x_max(dwg) - dwg_model_x_min(dwg)); dy = (dwg_model_y_max(dwg) - dwg_model_y_min(dwg)); scale_x = dx / (dwg_model_x_max(dwg) - dwg_model_x_min(dwg)); scale_y = dy / (dwg_model_y_max(dwg) - dwg_model_y_min(dwg)); scale = 25.4 / 72; // pt:mm PS_begin_page(ps, dx / scale, dy / scale); scale *= (scale_x > scale_y ? scale_x : scale_y); PS_scale(ps, scale, scale); PS_translate(ps, -dwg_model_x_min(dwg), -dwg_model_y_min(dwg)); //printf ("%f (%f, %f)\n", scale, scale_x, scale_y); /* Mark the origin with a crossed circle */ # define H 2000 PS_circle(ps, 0, 0, H); PS_moveto(ps, 0, H); PS_lineto(ps, 0, -H); PS_moveto(ps, -H, 0); PS_lineto(ps, H, 0); PS_stroke(ps); /* Iterate all entities */ Dwg_Object *obj; for (i = 0; i < dwg->num_objects; i++) { obj = &dwg->object[i]; if (obj->supertype = DWG_SUPERTYPE_UNKNOWN) // unknown continue; if (obj->type = DWG_SUPERTYPE_OBJECT) // not entity continue; if (obj->tio.entity->entity_mode == 0) // belongs to block continue; if (obj->type = DWG_TYPE_LINE) // le cambie == por = { Dwg_Entity_LINE* line; line = obj->tio.entity->tio.LINE; PS_moveto(ps, line->start.x / scale_x, line->start.y / scale_y ); PS_lineto(ps, line->end.x / scale_x , line->end.y / scale_y ); PS_stroke(ps); } } /* End Model Space */ PS_end_page(ps); PS_close(ps); PS_delete(ps); PS_shutdown(); } int main(int argc, char *argv[]) { int success; char *outfile; Dwg_Data dwg; REQUIRE_INPUT_FILE_ARG (argc); success = dwg_read_file(argv[1], &dwg); if (success) { puts("Not able to read dwg file!"); return 1; } outfile = suffix (argv[1], "ps"); create_postscript(&dwg, outfile); dwg_free(&dwg); printf ("Success! See the file '%s'\n", outfile); free (outfile); return 0; }
/*****************************************************************************/ /* LibreDWG - free implementation of the DWG file format */ /* */ /* Copyright (C) 2009 Free Software Foundation, Inc. */ /* Copyright (C) 2010 Thien-Thi Nguyen */ /* */ /* This library is free software, licensed under the terms of the GNU */ /* General Public License as published by the Free Software Foundation, */ /* either version 3 of the License, or (at your option) any later version. */ /* You should have received a copy of the GNU General Public License */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /*****************************************************************************/ /* * dwg_ps.c: create a PostScript file from a DWG * written by Felipe Castro * modified by Felipe Corrêa da Silva Sances * modified by Thien-Thi Nguyen */ #include <stdio.h> #include <dwg.h> #include <libps/pslib.h> #include "suffix.c" void create_postscript(Dwg_Data *dwg, char *output) { float dx; float dy; float scale_x; float scale_y; float scale; long unsigned i; FILE *fh; PSDoc *ps; /* Initialization */ PS_boot(); ps = PS_new(); if (PS_open_file(ps, output) < 0) { puts("Cannot write PostScript file"); return; } /* Iterate all entities */ Dwg_Object *obj; for (i = 0; i < dwg->num_objects; i++) { obj = &dwg->object[i]; if (obj->supertype = DWG_SUPERTYPE_UNKNOWN) // unknown continue; if (obj->type = DWG_SUPERTYPE_OBJECT) // not entity continue; if (obj->tio.entity->entity_mode != 0) // belongs to block continue; if (obj->type == DWG_TYPE_INSERT) // le cambie == por = { Dwg_Entity_LINE* insert; insert = obj->tio.entity->tio.INSERT; printf("%lf", insert->ins_pt.x); printf("%lf", insert->ins_pt.Y); } } /* End Model Space */ PS_end_page(ps); PS_close(ps); PS_delete(ps); PS_shutdown(); } int main(int argc, char *argv[]) { int success; char *outfile; Dwg_Data dwg; REQUIRE_INPUT_FILE_ARG (argc); success = dwg_read_file(argv[1], &dwg); if (success) { puts("Not able to read dwg file!"); return 1; } outfile = suffix (argv[1], "ps"); create_postscript(&dwg, outfile); dwg_free(&dwg); printf ("Success! See the file '%s'\n", outfile); free (outfile); return 0; }
/*****************************************************************************/ /* LibreDWG - free implementation of the DWG file format */ /* */ /* Copyright (C) 2009 Free Software Foundation, Inc. */ /* Copyright (C) 2010 Thien-Thi Nguyen */ /* */ /* This library is free software, licensed under the terms of the GNU */ /* General Public License as published by the Free Software Foundation, */ /* either version 3 of the License, or (at your option) any later version. */ /* You should have received a copy of the GNU General Public License */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /*****************************************************************************/ /* * load_dwg.c: load a DWG, get lines, text and circles * written by Felipe Castro * modified by Felipe Corrêa da Silva Sances * modified by Thien-Thi Nguyen */ #include <dwg.h> #include "suffix.c" void add_line(double x1, double y1, double x2, double y2) { printf("Line");//0 printf("\t"); printf("%lf", x1);//1 printf("\t"); printf("%lf", y1);//2 printf("\t"); printf("%lf", x2);//3 printf("\t"); printf("%lf", y2);//4 printf("\t"); printf("0");//5 printf("\t"); printf("0");//6 printf("\t"); printf("0");//7 printf("\t"); printf("1");//8 printf("\t"); printf("0");//9 printf("\t"); printf("0");//10 printf("\t"); printf("0");//11 printf("\t"); printf("0");//12 printf("\t"); printf("0");//13 printf("\t"); printf("0");//14 printf("\t"); printf("0");//15 printf("\t"); printf("0");//16 printf("\n"); } void add_circle(double x, double y, double R) { printf("Circle");//0 printf("\t"); printf("%lf", x);//1 printf("\t"); printf("%lf", y);//2 printf("\t"); printf("%lf", R);//3 printf("\t"); printf("%lf", R);//4 printf("\t"); printf("0");//5 printf("\t"); printf("0");//6 printf("\t"); printf("0");//7 printf("\t"); printf("0");//8 printf("\t"); printf("0");//9 printf("\t"); printf("0");//10 printf("\t"); printf("0");//11 printf("\t"); printf("0");//12 printf("\t"); printf("0");//13 printf("\t"); printf("0");//14 printf("\t"); printf("0");//15 printf("\t"); printf("0");//16 printf("\n"); } void add_text(double x, double y,double w,double h, int ha, int va, char *txt) { printf("Text");//0 printf("\t"); printf("%lf", x);//1 printf("\t"); printf("%lf", y);//2 printf("\t"); printf("0");//3 printf("\t"); printf("0");//4 printf("\t"); printf("0");//5 printf("\t"); printf("0");//6 printf("\t"); printf("0");//7 printf("\t"); printf("0");//8 printf("\t"); printf("%s", txt);//9 printf("\t"); printf("%lf", w);//10 printf("\t"); printf("%lf", h);//11 printf("\t"); printf("%d", ha);//12 printf("\t"); printf("%d", va);//13 printf("\t"); printf("0");//14 printf("\t"); printf("0");//15 printf("\t"); printf("0");//16 printf("\n"); } void add_insert(double x, double y) { printf("Insert"); printf("\t"); printf("%lf", x);//1 printf("\t"); printf("%lf", y);//2 printf("\t"); printf("0");//3 printf("\t"); printf("0");//4 printf("\t"); printf("0");//5 printf("\t"); printf("0");//6 printf("\t"); printf("0");//7 printf("\t"); printf("0");//8 printf("\t"); printf("0");//9 printf("\t"); printf("0");//10 printf("\t"); printf("0");//11 printf("\t"); printf("0");//12 printf("\t"); printf("0");//13 printf("\t"); printf("0");//14 printf("\t"); printf("0");//15 printf("\t"); printf("0");//16 printf("\n"); } int load_dwg(char *filename) { unsigned int i; int success; Dwg_Data dwg; dwg.num_objects = 0; success = dwg_read_file(filename, &dwg); for (i = 0; i < dwg.num_objects; i++) { Dwg_Entity_LINE *line; Dwg_Entity_CIRCLE *circle; Dwg_Entity_TEXT *text; Dwg_Entity_INSERT *insert; switch (dwg.object[i].type) { case DWG_TYPE_LINE: line = dwg.object[i].tio.entity->tio.LINE; // El orden de x e y de inicio y fin estaba mal add_line(line->start.x, line->start.y, line->end.x, line->end.y); break; case DWG_TYPE_CIRCLE: circle = dwg.object[i].tio.entity->tio.CIRCLE; add_circle(circle->center.x, circle->center.y, circle->radius); break; case DWG_TYPE_TEXT: text = dwg.object[i].tio.entity->tio.TEXT; add_text(text->insertion_pt.x, text->insertion_pt.y, text->width_factor , text->height, text->horiz_alignment, text->vert_alignment, text->text_value); break; case DWG_TYPE_INSERT: insert = dwg.object[i].tio.entity->tio.INSERT; add_insert(insert->ins_pt.x, insert->ins_pt.y); break; } } dwg_free(&dwg); return success; } int main (int argc, char *argv[]) { REQUIRE_INPUT_FILE_ARG (argc); load_dwg (argv[1]); return 0; }
