Hi,
i wrote another example called dwg2dxf.c to export dxf from dwg.

Olli


/*****************************************************************************/
/*  LibreDWG - free implementation of the DWG file format                    */
/*                                                                           */
/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
/*  Copyright (C) 2010 Thien-Thi Nguyen                                      */
/*  Copyright (C) 2014 Oliver Dippel                                         */
/*                                                                           */
/*  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/>.    */
/*****************************************************************************/

/*
 * testDXF.c: convert a DWG to DXF
 * written by Oliver Dippel <[email protected]>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <dwg.h>
#include "../src/bits.h"
#include "suffix.c"

#define PI 3.14159265
#define toDeg(x) (x*180.0)/PI

int export_DXF(char *filename);
void output_DXF(Dwg_Data* dwg);

double model_xmin, model_ymin;
double page_width, page_height, scale;

int main (int argc, char *argv[]) {
	REQUIRE_INPUT_FILE_ARG (argc);
	return export_DXF(argv[1]);
}

int export_DXF (char *filename) {
	int error;
	Dwg_Data dwg;
	error = dwg_read_file(filename, &dwg);
	if (!error) {
		output_DXF(&dwg);
	}
	dwg_free(&dwg);
	return error ? 1 : 0;
}

void output_TEXT (Dwg_Object* obj) {
	Dwg_Entity_TEXT* text;
	text = obj->tio.entity->tio.TEXT;
	if (text->text_value[0] == '&') return;
	printf(" 0\nMTEXT\n 999\n#%d\n 10\n%f\n 20\n%f\n 40\n%f\n 1\n%s\n", obj->index, text->insertion_pt.x, text->insertion_pt.y + text->height, text->height, text->text_value);
}

void output_LINE (Dwg_Object* obj) {
	Dwg_Entity_LINE* line;
	line = obj->tio.entity->tio.LINE;
	printf(" 0\nLINE\n 999\n#%d\n 10\n%f\n 20\n%f\n 11\n%f\n 21\n%f\n", obj->index, line->start.x, line->start.y, line->end.x, line->end.y);
}

void output_CIRCLE (Dwg_Object* obj) {
	Dwg_Entity_CIRCLE* circle;
	circle = obj->tio.entity->tio.CIRCLE;
	printf(" 0\nCIRCLE\n 999\n#%d\n 10\n%f\n 20\n%f\n 40\n%f\n", obj->index, circle->center.x, circle->center.y, circle->radius);
}

void output_ARC (Dwg_Object* obj) {
	Dwg_Entity_ARC* arc;
	arc = obj->tio.entity->tio.ARC;
	printf(" 0\nARC\n 999\n#%d\n 10\n%f\n 20\n%f\n 40\n%f\n 50\n%f\n 51\n%f\n", obj->index, arc->center.x, arc->center.y, arc->radius, toDeg(arc->start_angle), toDeg(arc->end_angle));
}

void output_INSERT (Dwg_Object* obj) {
	Dwg_Entity_INSERT* insert;
	insert = obj->tio.entity->tio.INSERT;
}

void output_object (Dwg_Object* obj) {
	if (!obj) {
		fprintf(stderr, "object is NULL\n");
		return;
	}
	if (obj->type == DWG_TYPE_INSERT) {
		output_INSERT(obj);
	}
	if (obj->type == DWG_TYPE_LINE) {
		output_LINE(obj);
	}
	if (obj->type == DWG_TYPE_CIRCLE) {
		output_CIRCLE(obj);
	}
	if (obj->type == DWG_TYPE_TEXT) {
		output_TEXT(obj);
	}
	if (obj->type == DWG_TYPE_ARC) {
		output_ARC(obj);
	}
}

void output_BLOCK_HEADER(Dwg_Object_Ref* ref) {
	Dwg_Object* obj;
	Dwg_Object_BLOCK_HEADER* hdr;
	if (!ref) {
		fprintf(stderr, "Found null object reference. Could not output an DXF symbol for this BLOCK_HEADER\n");
		return;
	}
	if (!ref->obj) {
		fprintf(stderr, "Found null ref->obj\n");
		return;
	}
	if (!ref->obj->tio.object) {
		fprintf(stderr, "Found null ref->obj->tio.object\n");
		return;
	}
	hdr = ref->obj->tio.object->tio.BLOCK_HEADER;
	obj = get_first_owned_object(ref->obj, hdr);
	while(obj) {
		output_object(obj);
		obj = get_next_owned_object(ref->obj, obj, hdr);
	}
}

void output_DXF(Dwg_Data* dwg) {
	unsigned int i;
	Dwg_Object *obj;
	model_xmin = dwg_model_x_min(dwg);
	model_ymin = dwg_model_y_min(dwg);
	double dx = (dwg_model_x_max(dwg) - dwg_model_x_min(dwg));
	double dy = (dwg_model_y_max(dwg) - dwg_model_y_min(dwg));
	double scale_x = dx / (dwg_page_x_max(dwg) - dwg_page_x_min(dwg));
	double scale_y = dy / (dwg_page_y_max(dwg) - dwg_page_y_min(dwg));
	page_width = dx;
	page_height = dy;
	printf("999\n");
	printf("dxflib 2.2.0.0\n");
	printf(" 0\n");
	printf("SECTION\n");
	printf(" 2\n");
	printf("ENTITIES\n");
	obj = &dwg->object[0];
	if (obj->type != DWG_TYPE_BLOCK_CONTROL) {
		fprintf(stderr, "ERROR: First object is not a BLOCK_CONTROL\n");
		return;
	}
	Dwg_Object_BLOCK_CONTROL* block_control;
	block_control = obj->tio.object->tio.BLOCK_CONTROL;
	for (i=0; i<block_control->num_entries; i++) {
		output_BLOCK_HEADER(block_control->block_headers[i]);
	}
	output_BLOCK_HEADER(block_control->model_space);
	output_BLOCK_HEADER(block_control->paper_space);
	printf(" 0\n");
	printf("ENDSEC\n");
}

Reply via email to