1. Robert Johnstone
  2. liblayout

Source

liblayout / dxf_read.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
** This code is distributed under a modified BSD license.
** Please refer to http://www.sourceforge.net/projects/adsm/
**
*************************************************************************
**
** Copyright (c) 2004-2008 Robert W. Johnstone
** All rights reserved.
**
*************************************************************************
**
** Description not written.
**
*/

#include "layout_shapes.h"
#include <assert.h>
#include <ctype.h>
#include <iostream>
#include <math.h>
#include <stdexcept>
#include <stdlib.h>

#ifndef HAVE_LROUND
#include "lround.i"
#endif

using namespace liblayout;
using std::string;

#define INVALID_DXF "Invalid DXF file.  "

static bool is_integer( char const* ptr )
{
	while ( isspace(*ptr) ) ++ptr;
	if ( !isdigit(*ptr) ) return false;
	while ( isdigit(*ptr) ) ++ptr;
	while ( isspace(*ptr) ) ++ptr;
	return (*ptr=='\0');
}

static bool get_pair( std::istream& in, long& code, string& text )
{
	char buffer[256];
	size_t s;

	// Get the group code
	in.getline( buffer, 256 );
	if ( !is_integer(buffer) ) return true;
	code = atol(buffer);
	// Get the group text
	in.getline( buffer, 256 );
	text = buffer;
	s = text.size();
	while ( isspace( text[s-1] ) ) --s;
	text.erase( s );

	return false;
}

static string advance_next_group_0( std::istream& in )
{
	long code;
	string text;

	do {
		// Move to the next group zero code
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	} while ( code != 0 );
	
	// Return the group text
	return text;
}

static void import_header( std::istream& in )
{
	long code;
	string text;

	// The header must begin with a section token
	bool err = get_pair( in, code, text );
	if ( err || code!=0 || text!="SECTION" )
		throw std::runtime_error( INVALID_DXF "Expected a SECTION group code." );
	err = get_pair( in, code, text );
	if ( err || code!=2 || text!="HEADER" )
		throw std::runtime_error( INVALID_DXF "Expected a HEADER group code." );

	// Loop through all items until we see end of section
	do {
		err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	} while ( code!=0 && text!="ENDSEC" );
}

static void skip_section( std::istream& in )
{
	long code;
	string text;

	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	} while ( code!=0 || text!="ENDSEC" );
}

static bool lookup_layer( layer_id& lhs, dxf::map_name_id const& name_id, string const& layer )
{
        // Look-up the name to find the ID
	dxf::map_name_id::const_iterator lp = name_id.find( layer );
	if ( lp == name_id.end() ) return false;
	// Found a match
	lhs = lp->second;
	return true;
}

static layer_id lookup_layer( dxf::map_name_id& name_id, string const& layer,
	dxf::on_layer_handler on_layer, void* on_layer_data )
{
	// Look-up the name to find the ID
	dxf::map_name_id::const_iterator lp = name_id.find( layer );
	// If necessary, add the name to the map
	if ( lp == name_id.end() ) {
                // Add a new layer into the map
                layer_id id = dxf::get_next_layer_id( name_id );
                assert( id != 0 );
                name_id[ layer ] = id;
                lp = name_id.find( layer );
                // Inform caller of update
                on_layer( on_layer_data, lp->first.c_str(), lp->second );
	}

	// Add the shape
	assert( lp != name_id.end() );
	return lp->second;
}

static string import_trace( 
	string& layer, std::vector<point>& polygon, unsigned scale, std::istream& in )
{
	double x1 = 0, y1 = 0;
	double x2 = 0, y2 = 0;
	double x3 = 0, y3 = 0;
	double x4 = 0, y4 = 0;
	long code;
	string text;

	// Read in the attributes
	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Entitie incomplete." );

		switch ( code ) {
		case 8:
			layer = text; break;
		case 10:
			x1 = atof(text.c_str()); break;
		case 11:
			x2 = atof(text.c_str()); break;
		case 12:
			x3 = atof(text.c_str()); break;
		case 13:
			x4 = atof(text.c_str()); break;
		case 20:
			y1 = atof(text.c_str()); break;
		case 21:
			y2 = atof(text.c_str()); break;
		case 22:
			y3 = atof(text.c_str()); break;
		case 23:
			y4 = atof(text.c_str()); break;
		default:
			/* do nothing */ break;
		}
	} while ( code!=0 );
	
	/* code == 0 */
	/* Maybe ENDSEC or start of another shape */
	assert( code==0 );

	// Make the shape
	polygon.clear();
	polygon.push_back( point( lround(x1*scale), lround(y1*scale) ) );
	polygon.push_back( point( lround(x2*scale), lround(y2*scale) ) );
	polygon.push_back( point( lround(x3*scale), lround(y3*scale) ) );
	polygon.push_back( point( lround(x4*scale), lround(y4*scale) ) );
	
	// Return the new group text
	return text;
}

static string import_vertex( std::vector<point>& vertices, unsigned scale, std::istream& in )
{
	point pt;
	long code;
	string text;

	// Read in the attributes
	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Entitie incomplete." );

		switch ( code ) {
		case 10:
			pt.x = lround( atof(text.c_str()) * scale ); break;
		case 20:
			pt.y = lround( atof(text.c_str()) * scale ); break;
		default:
			/* do nothing */ break;
		}
	} while ( code!=0 );
	
	// Add the point to the polygon
	vertices.push_back( pt );
	// And return
	return text;
}

static string import_polyline( 
	string& layer, std::vector<point>& polygon, coord_t& width, unsigned scale, std::istream& in )
{
	long code;
	string text;
	int flag = 0;

	width = 0;
	
	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Entity incomplete" );

		switch ( code ) {
		case 8:
			layer = text; break;
		case 39:
			width = lround( atof( text.c_str() ) * scale ); break;
		case 70:
			flag = atoi( text.c_str() ); break;
		default:
			/* do nothing */ break;
		}
	} while ( code!=0 );
	
	// Start importing vertices
	polygon.clear();
	while ( text == "VERTEX" ) {
		text = import_vertex( polygon, scale, in );
	}
	
	/* Maybe ENDSEC or start of another shape */
	assert( code==0 );
	// If the pair is a SEQEND, skip
	if ( text=="SEQEND" ) {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	}
	
	// Check on the type of polyline
	if ( flag == 0 )
		// Polyline or path
		if ( width<1 ) width = 1;
	else if ( flag == 1 )
		// Close polygon
		width = 0;
	else
		throw std::runtime_error( INVALID_DXF "Unsupported polyline entity" );
		
	
	// Return the new group text
	return text;
}

static string import_circle( string& layer, coord_t& xa, coord_t& ya, coord_t& ra,
	unsigned scale, std::istream& in )
{
	double x, y, r;
	long code;
	string text;

	// Read in the attributes
	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Entitie incomplete." );

		switch ( code ) {
		case 8:
			layer = text; break;
		case 10:
			x = atof( text.c_str() ); break;
		case 20:
			y = atof( text.c_str() ); break;
		case 40:
			r = atof( text.c_str() ); break;
		default:
			/* do nothing */ break;
		}
	} while ( code!=0 );

	/* Maybe ENDSEC or start of another shape */
	assert( code==0 );

	// Make the shape
	xa = lround( x * scale );
	ya = lround( y * scale );
	ra = lround( r * scale );

	// Return the new group text
	return text;
}

static string import_line( string& layer, std::vector<point>& pts, coord_t& width, unsigned scale, std::istream& in )
{
	long code;
	string text;
	point a(0,0), b(0,0);

	width = 0;

	do {
		bool err = get_pair( in, code, text );
		if ( err ) throw std::runtime_error( INVALID_DXF "Entitie incomplete." );

		switch ( code ) {
		case 8:
			layer = text; break;
		case 10:
			a.x = lround( atof( text.c_str() ) * scale); break;
		case 11:
			b.x = lround( atof( text.c_str() ) * scale); break;
		case 20:
			a.y = lround( atof( text.c_str() ) * scale); break;
		case 21:
			b.y = lround( atof( text.c_str() ) * scale); break;
		case 39:
			width = lround( atof( text.c_str() ) * scale); break;
		default:
			/* do nothing */ break;
		}
	} while ( code != 0 );

	// Make the shape
	pts.clear();
	pts.push_back( a );
	pts.push_back( b );
	if ( width==0 ) width = 1;

	// Return the new group text
	return text;
}

static void import_entities( layout& view,
	dxf::map_name_id const& name_id, unsigned scale, 
	dxf::on_comment_handler on_comment, void* on_comment_data,
	std::istream& in )
{
	long code;
	string text;

	bool err = get_pair( in, code, text );
	if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	// A Shape or an ENDSEC is expected
	if ( code!=0 ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );

	// Iterate through the shapes
	while ( text != "ENDSEC" ) {
		string layer;
		std::vector<point> polygon;
		coord_t x, y, width;
		layer_id id;

		assert( code == 0 );

		if ( text=="TRACE" ) {
			text = import_trace( layer, polygon, scale, in );
			if ( lookup_layer( id, name_id, layer ) )
				view.insert( id, polygon );
		}
		else if ( text=="POLYLINE" ) {
			text = import_polyline( layer, polygon, width, scale, in );
			if ( lookup_layer( id, name_id, layer ) ) {
				if ( width==0 )
					view.insert( id, polygon );
				else
					view.insert( id, polygon, width, path_end_square );
			}
		}
		else if ( text=="CIRCLE" ) {
			text = import_circle( layer, x, y, width, scale, in );
			if ( lookup_layer( id, name_id, layer ) )
				view.insert( id, width, point(x,y) );
		}
		else if ( text=="LINE" ) {
			text = import_line( layer, polygon, width, scale, in );
			if ( lookup_layer( id, name_id, layer ) ) {
				view.insert( id, polygon, width, path_end_square );
			}
		}
		else if ( text=="VIEWPORT" ) {
			text = advance_next_group_0( in );
		}
		else {
			// Print a warning message
			if ( on_comment ) {
				std::string msg( "WARN:  An unsupported entity (" );
				msg.append( text );
				msg.append( ") was encountered and ignored." );
				on_comment( on_comment_data, msg.c_str() );
			}
			// Advance past the entity
			text = advance_next_group_0( in );
		}
	}

	assert( code == 0 );
	assert( text == "ENDSEC" );
}

static void import_entities( layout& view, dxf::map_name_id& name_id, unsigned scale, 
	dxf::on_layer_handler on_layer, void* on_layer_data,
	dxf::on_comment_handler on_comment, void* on_comment_data,
	std::istream& in )
{
	long code;
	string text;

	bool err = get_pair( in, code, text );
	if ( err ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );
	// A Shape or an ENDSEC is expected
	if ( code!=0 ) throw std::runtime_error( INVALID_DXF "Could not find ENDSEC group code." );

	// Iterate through the shapes
	while ( text != "ENDSEC" ) {
		string layer;
		std::vector<point> polygon;
		coord_t x, y, width;
		layer_id id;

		assert( code == 0 );

		if ( text=="TRACE" ) {
			text = import_trace( layer, polygon, scale, in );
			id = lookup_layer( name_id, layer, on_layer, on_layer_data );
			view.insert( id, polygon );
		}
		else if ( text=="POLYLINE" ) {
			text = import_polyline( layer, polygon, width, scale, in );
			id = lookup_layer( name_id, layer, on_layer, on_layer_data );
			if ( width==0 )
				view.insert( id, polygon );
			else
				view.insert( id, polygon, width, path_end_square );
		}
		else if ( text=="CIRCLE" ) {
			text = import_circle( layer, x, y, width, scale, in );
			id = lookup_layer( name_id, layer, on_layer, on_layer_data );
			view.insert( id, width, point(x,y) );
		}
		else if ( text=="LINE" ) {
			text = import_line( layer, polygon, width, scale, in );
			id = lookup_layer( name_id, layer, on_layer, on_layer_data );
			view.insert( id, polygon, width, path_end_square );
		}
		else if ( text=="VIEWPORT" ) {
			text = advance_next_group_0( in );
		}
		else {
			// Print a warning
			if ( on_comment ) {
				std::string msg( "WARN:  An unsupported entity (" );
				msg.append( text );
				msg.append( ") was encountered and ignored." );
				on_comment( on_comment_data, msg.c_str() );
			}
			// Advance past the entity
			text = advance_next_group_0( in );
		}
	}

	assert( code == 0 );
	assert( text == "ENDSEC" );
}

layout* dxf::import( std::list<layout>& layouts, map_name_id const& name_id, unsigned scale,
	on_comment_handler on_comment, void* on_comment_data,
	std::istream& in )
{
	layout* cell = 0;

	// Read in the header section
	import_header( in );

	// Process the sections
	do {
		long code;
		string text;

		// Process the start of the section
		// Note, we might also see a end-of-file marker
		bool err = get_pair( in, code, text );
		if ( err || code!=0 ) throw std::runtime_error( INVALID_DXF "Expected a SECTION or EOF group code." );
		
		if ( text=="SECTION" ) {
			// okay, do nothing
		}
		else if ( text=="EOF" ) {
			// end of the file
			break;
		}
		else {
			throw std::runtime_error( INVALID_DXF "Expected a SECTION or EOF group code." );
		}

		// What type of section is this?
		err = get_pair( in, code, text );
		if ( err || code!=2 ) throw std::runtime_error( INVALID_DXF "Expected a section name group code." );

		if ( text=="ENTITIES" ) {
			if ( !cell ) {
				layouts.push_front( layout() );
				cell = & layouts.front();
			}
			assert( cell );
			import_entities( *cell, name_id, scale, on_comment, on_comment_data, in );
		}
		else
			skip_section( in );
	} while ( 1 );

	return cell;
}

layout* dxf::import( std::list<layout>& layouts, map_name_id& name_id, unsigned scale,
	on_comment_handler on_comment, void* on_comment_data,
	on_layer_handler on_layer, void* on_layer_data,
	std::istream& in )
{
	layout* cell = 0;

	// Read in the header section
	import_header( in );

	// Process the sections
	do {
		long code;
		string text;

		// Process the start of the section
		// Note, we might also see a end-of-file marker
		bool err = get_pair( in, code, text );
		if ( err || code!=0 ) throw std::runtime_error( INVALID_DXF "Expected a SECTION or EOF group code." );
		
		if ( text=="SECTION" ) {
			// okay, do nothing
		}
		else if ( text=="EOF" ) {
			// end of the file
			break;
		}
		else {
			throw std::runtime_error( INVALID_DXF "Expected a SECTION or EOF group code." );
		}

		// What type of section is this?
		err = get_pair( in, code, text );
		if ( err || code!=2 ) throw std::runtime_error( INVALID_DXF "Expected a section name group code." );

		if ( text=="ENTITIES" ) {
			if ( !cell ) {
				layouts.push_front( layout() );
				cell = & layouts.front();
			}
			assert( cell );
			import_entities( *cell, name_id, scale, on_layer, on_layer_data, on_comment, on_comment_data, in );
		}
		else
			skip_section( in );
	} while ( 1 );

	return cell;
}

bool dxf::is_file_dxf( std::istream& in )
{
	long code;
	string text;

	// The file must start with a section token
	bool err = get_pair( in, code, text );
	if ( err ) return false;
	if ( code != 0 ) return false;
	if ( text != "SECTION" ) return false;
	// The next code must be a header token
	err = get_pair( in, code, text );
	if ( err ) return false;
	if ( code != 2 ) return false;
	if ( text != "HEADER" ) return false;
	// Simply read in tokens, checking that they are well-formed, until
	// the end of the file looking for an EOF token.
	do {
		if ( in.eof() ) return false;
		err = get_pair( in, code, text );
		if ( err ) return false;
	} while ( code!=0 || text!="EOF" );
	// If everything so far has worked, assume that the file is a properly
	// formatted DXF
	return true;
}