Revision 10421
trunk/libraries/libDwg/src/com/iver/cit/jdwglib/dwg/readers/DwgFileV12Reader.java | ||
---|---|---|
46 | 46 |
import java.io.IOException; |
47 | 47 |
import java.nio.ByteBuffer; |
48 | 48 |
import java.nio.ByteOrder; |
49 |
import java.util.ArrayList; |
|
49 | 50 |
|
50 | 51 |
import com.iver.cit.jdwglib.dwg.CorruptedDwgEntityException; |
51 | 52 |
import com.iver.cit.jdwglib.dwg.DwgFile; |
53 |
import com.iver.cit.jdwglib.dwg.DwgHandleReference; |
|
52 | 54 |
import com.iver.cit.jdwglib.dwg.DwgObject; |
53 | 55 |
|
54 | 56 |
/** |
55 | 57 |
* Reads version 12 dwg files |
56 | 58 |
* |
57 | 59 |
* @author azabala |
60 |
* |
|
61 |
* TODO REVISAR TODOS LOS bb.getShort() y bb.getInt() pues puede que haya |
|
62 |
* algun tipo cambiado |
|
63 |
* (l--> es un int, h--> es un short) |
|
58 | 64 |
*/ |
59 | 65 |
public class DwgFileV12Reader implements IDwgFileReader{ |
60 | 66 |
|
61 | 67 |
private DwgFile dwgFile; |
62 | 68 |
private ByteBuffer bb; |
69 |
ArrayList readers = new ArrayList(); |
|
70 |
/* |
|
71 |
* En Python: |
|
72 |
* if _version != 'AC1009': |
|
73 |
if _version == 'AC1010': |
|
74 |
r13 = True |
|
75 |
else: |
|
76 |
print "unknown version" |
|
77 |
* |
|
78 |
* */ |
|
79 |
private boolean r13 = false; |
|
80 |
|
|
81 |
|
|
82 |
public DwgFileV12Reader(boolean isR13){ |
|
83 |
r13 = isR13; |
|
84 |
readers.add(null); |
|
85 |
readers.add(new LineReader()); |
|
86 |
readers.add(new PointReader()); |
|
87 |
readers.add(new CircleReader()); |
|
88 |
readers.add(new ShapeReader()); |
|
89 |
readers.add(null); |
|
90 |
readers.add(null); |
|
91 |
readers.add(new TextReader()); |
|
92 |
readers.add(new ArcReader()); |
|
93 |
readers.add(new TraceReader()); |
|
94 |
readers.add(null); |
|
95 |
readers.add(null); |
|
96 |
readers.add(new SolidReader()); |
|
97 |
readers.add(new BlkReader()); |
|
98 |
readers.add(new EndBlkReader()); |
|
99 |
readers.add(new InsertReader()); |
|
100 |
readers.add(new AttDefReader()); |
|
101 |
readers.add(new AttribReader()); |
|
102 |
readers.add(new SbEndReader()); |
|
103 |
readers.add(null); |
|
104 |
readers.add(new PlineReader()); |
|
105 |
readers.add(new VertexReader()); |
|
106 |
readers.add(null); |
|
107 |
readers.add(new Face3DReader()); |
|
108 |
readers.add(new DimReader()); |
|
109 |
readers.add(new VPortReader()); |
|
110 |
} |
|
111 |
|
|
112 |
|
|
63 | 113 |
|
64 | 114 |
/* (non-Javadoc) |
65 | 115 |
* @see com.iver.cit.jdwglib.dwg.readers.IDwgFileReader#read(com.iver.cit.jdwglib.dwg.DwgFile, java.nio.ByteBuffer) |
... | ... | |
205 | 255 |
} |
206 | 256 |
|
207 | 257 |
|
258 |
/** |
|
259 |
* @param styleTable |
|
260 |
*/ |
|
261 |
private void readStyleTable(Dwg12Table styleTable) { |
|
262 |
short size = styleTable.s1; |
|
263 |
int numRecs = styleTable.i1; |
|
264 |
int start = styleTable.i2; |
|
265 |
int end = -1; |
|
266 |
bb.position(start); |
|
267 |
for(int i = 0; i < numRecs; i++){ |
|
268 |
end = bb.position() + size; |
|
269 |
byte flag = bb.get(); |
|
270 |
byte[] nameBytes = new byte[32]; |
|
271 |
bb.get(nameBytes); |
|
272 |
String name = new String(nameBytes); |
|
273 |
bb.order(ByteOrder.LITTLE_ENDIAN); |
|
274 |
short w1 = bb.getShort(); |
|
275 |
double d1 = bb.getDouble(); |
|
276 |
double d2 = bb.getDouble(); |
|
277 |
double d3 = bb.getDouble(); |
|
278 |
byte b1 = bb.get(); |
|
279 |
double d4 = bb.getDouble(); |
|
280 |
|
|
281 |
byte[] s1 = new byte[128]; |
|
282 |
bb.get(s1); |
|
283 |
|
|
284 |
short crc = bb.getShort(); |
|
285 |
int offset = end - bb.position(); |
|
286 |
if(offset > 0) |
|
287 |
bb.position(bb.position() + offset); |
|
288 |
} |
|
289 |
byte[] crc32 = new byte[32]; |
|
290 |
bb.get(crc32); |
|
291 |
} |
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
/** |
|
296 |
* @param layerTable |
|
297 |
*/ |
|
298 |
private void readLayerTable(Dwg12Table layerTable) { |
|
299 |
short size = layerTable.s1; |
|
300 |
int numR = layerTable.i1; |
|
301 |
int start = layerTable.i2; |
|
302 |
int end = -1; |
|
303 |
bb.position(start); |
|
304 |
|
|
305 |
for(int i = 0; i < numR; i++){ |
|
306 |
end = bb.position() + size; |
|
307 |
byte flag = bb.get(); |
|
308 |
byte[] nameByte = new byte[32]; |
|
309 |
bb.get(nameByte); |
|
310 |
String name = new String(nameByte); |
|
311 |
|
|
312 |
bb.order(ByteOrder.LITTLE_ENDIAN); |
|
313 |
short used = bb.getShort(); |
|
314 |
short color = bb.getShort(); |
|
315 |
short style = bb.getShort(); |
|
316 |
short crc = bb.getShort(); |
|
317 |
|
|
318 |
int offset = end - bb.position(); |
|
319 |
if(offset > 0) |
|
320 |
bb.position(bb.position() + offset); |
|
321 |
|
|
322 |
} |
|
323 |
byte[] crc32 = new byte[32]; |
|
324 |
bb.get(crc32); |
|
325 |
} |
|
326 |
|
|
327 |
|
|
328 |
interface EntityReader{ |
|
329 |
void read(ByteBuffer bb, char flags, short opts); |
|
330 |
} |
|
331 |
|
|
332 |
class LineReader implements EntityReader{ |
|
333 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
334 |
} |
|
335 |
} |
|
336 |
|
|
337 |
class PointReader implements EntityReader{ |
|
338 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
339 |
} |
|
340 |
} |
|
341 |
class CircleReader implements EntityReader{ |
|
342 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
343 |
} |
|
344 |
} |
|
345 |
class ShapeReader implements EntityReader{ |
|
346 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
347 |
} |
|
348 |
} |
|
349 |
class TextReader implements EntityReader{ |
|
350 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
351 |
} |
|
352 |
} |
|
353 |
class ArcReader implements EntityReader{ |
|
354 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
class TraceReader implements EntityReader{ |
|
359 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
360 |
} |
|
361 |
} |
|
362 |
|
|
363 |
class SolidReader implements EntityReader{ |
|
364 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
365 |
} |
|
366 |
} |
|
367 |
|
|
368 |
class BlkReader implements EntityReader{ |
|
369 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
370 |
} |
|
371 |
} |
|
372 |
class EndBlkReader implements EntityReader{ |
|
373 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
374 |
} |
|
375 |
} |
|
376 |
|
|
377 |
class InsertReader implements EntityReader{ |
|
378 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
379 |
} |
|
380 |
} |
|
381 |
|
|
382 |
class AttDefReader implements EntityReader{ |
|
383 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
384 |
} |
|
385 |
} |
|
386 |
|
|
387 |
class AttribReader implements EntityReader{ |
|
388 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
389 |
} |
|
390 |
} |
|
391 |
class SbEndReader implements EntityReader{ |
|
392 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
class PlineReader implements EntityReader{ |
|
397 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
398 |
} |
|
399 |
} |
|
400 |
|
|
401 |
class VertexReader implements EntityReader{ |
|
402 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
406 |
class Face3DReader implements EntityReader{ |
|
407 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
408 |
} |
|
409 |
} |
|
410 |
|
|
411 |
class DimReader implements EntityReader{ |
|
412 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
413 |
} |
|
414 |
} |
|
415 |
|
|
416 |
class VPortReader implements EntityReader{ |
|
417 |
public void read(ByteBuffer bb, char flags, short opts) { |
|
418 |
} |
|
419 |
} |
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
/** |
|
424 |
* @param start |
|
425 |
* @param end |
|
426 |
*/ |
|
427 |
private void readEntities(int start, int end) { |
|
428 |
|
|
429 |
|
|
430 |
bb.position(start); |
|
431 |
int ant = bb.position(); |
|
432 |
int emax = readers.size(); |
|
433 |
while(ant < (end - 32)){ |
|
434 |
bb.order(ByteOrder.LITTLE_ENDIAN); |
|
435 |
char kind = bb.getChar(); |
|
436 |
char flag = bb.getChar(); |
|
437 |
short lenght = bb.getShort(); |
|
438 |
|
|
439 |
int crcpos = ant + (lenght - 2); |
|
440 |
short layer = bb.getShort(); |
|
441 |
short opts = bb.getShort(); |
|
442 |
|
|
443 |
byte color = 0; |
|
444 |
if ((flag & 0x1) > 0){ |
|
445 |
color = bb.get(); |
|
446 |
} |
|
447 |
byte extra = 0; |
|
448 |
if ((flag & 0x40) > 0) |
|
449 |
extra = bb.get(); |
|
450 |
|
|
451 |
Object xdata = null; |
|
452 |
if((extra & 0x2) > 0) |
|
453 |
xdata = readXdata(); |
|
454 |
|
|
455 |
short type = 0; |
|
456 |
if((flag & 0x2) > 0) |
|
457 |
type = bb.getShort(); |
|
458 |
|
|
459 |
double z = 0d; |
|
460 |
if( (flag & 0x4) > 0 && (kind > 2) && (kind != 22)){ |
|
461 |
z = bb.getDouble(); |
|
462 |
} |
|
463 |
|
|
464 |
double th = 0d; |
|
465 |
if((flag & 0x8) > 0){ |
|
466 |
th = bb.getDouble(); |
|
467 |
} |
|
468 |
|
|
469 |
Object handle = null; |
|
470 |
if((flag & 0x20) > 0) |
|
471 |
handle = getHandle(); |
|
472 |
short paper = 0; |
|
473 |
if((extra & 0x4) > 0){ |
|
474 |
paper = bb.getShort(); |
|
475 |
} |
|
476 |
|
|
477 |
if(kind < emax){ |
|
478 |
EntityReader reader = (EntityReader) readers.get(kind); |
|
479 |
if(reader != null){ |
|
480 |
reader.read(bb, flag, opts); |
|
481 |
ant = bb.position(); |
|
482 |
if(ant < crcpos){ |
|
483 |
bb.position(bb.position() + (crcpos - ant)); |
|
484 |
}else{ |
|
485 |
System.out.println("nos hemos pasado"); |
|
486 |
} |
|
487 |
short crc = bb.getShort(); |
|
488 |
} |
|
489 |
}//if |
|
490 |
ant = bb.position(); |
|
491 |
byte[] crc32 = new byte[32]; |
|
492 |
bb.get(crc32); |
|
493 |
|
|
494 |
}//while |
|
495 |
} |
|
496 |
|
|
497 |
|
|
498 |
/** |
|
499 |
* @return |
|
500 |
*/ |
|
501 |
private byte[] getHandle() { |
|
502 |
byte[] bytes = null; |
|
503 |
bb.order(ByteOrder.nativeOrder()); |
|
504 |
byte len = bb.get(); |
|
505 |
bytes = new byte[len]; |
|
506 |
bb.get(bytes); |
|
507 |
return bytes; |
|
508 |
//Ver si los handles de DWG 12 concurdan con nuestra clase |
|
509 |
//DwgHandleReference |
|
510 |
// return (5, _len) + tuple(_bytes) |
|
511 |
} |
|
512 |
|
|
513 |
|
|
514 |
|
|
515 |
/** |
|
516 |
* @return |
|
517 |
*/ |
|
518 |
private String readXdata() { |
|
519 |
String extData = ""; |
|
520 |
short xlen = bb.getShort(); |
|
521 |
while (xlen > 0){ |
|
522 |
byte xval = bb.get(); |
|
523 |
xlen--; |
|
524 |
if(xval == 0){ |
|
525 |
if(xlen < 1) |
|
526 |
break; |
|
527 |
byte val = bb.get(); |
|
528 |
xlen--; |
|
529 |
if(r13){ |
|
530 |
if(xlen < 1) |
|
531 |
break; |
|
532 |
byte codePage = bb.get(); |
|
533 |
xlen--; |
|
534 |
}//if r13 |
|
535 |
if(xlen < val) |
|
536 |
break; |
|
537 |
byte[] strByte = new byte[val]; |
|
538 |
bb.get(strByte); |
|
539 |
xlen -= val; |
|
540 |
extData += new String(strByte); |
|
541 |
}//if xval == 0 |
|
542 |
else if(xval == 1 || xval == 3 || xval == 70){ |
|
543 |
if(xlen < 2) |
|
544 |
break; |
|
545 |
short val = bb.getShort(); |
|
546 |
extData += val; |
|
547 |
xlen += 2; |
|
548 |
}else if(xval == 2){ |
|
549 |
if(xlen < 1) |
|
550 |
break; |
|
551 |
byte val = bb.get(); |
|
552 |
if(val == 0) |
|
553 |
extData += "{"; |
|
554 |
else if(val == 1) |
|
555 |
extData += "}"; |
|
556 |
else |
|
557 |
System.out.println("Char no esperado:"+val); |
|
558 |
}else if(xval == 5){ |
|
559 |
if(xlen < 8) |
|
560 |
break; |
|
561 |
long val = bb.getLong(); |
|
562 |
//probably it is an entity handle |
|
563 |
xlen -= 8; |
|
564 |
}else if (xval == 40 || xval == 41 || xval == 42){ |
|
565 |
if (xlen < 8) |
|
566 |
break; |
|
567 |
double val = bb.getDouble(); |
|
568 |
xlen -= 8; |
|
569 |
extData += val; |
|
570 |
|
|
571 |
}else if (xval == 10 || xval == 11 || xval == 12 || xval == 13){ |
|
572 |
//13 not in R12 spec, is in R13/R14 |
|
573 |
if(xlen < 24) |
|
574 |
break; |
|
575 |
Object point = getPoint(true); |
|
576 |
extData += point.toString(); |
|
577 |
xlen -= 24; |
|
578 |
}else if(xval == 71){ |
|
579 |
if (xlen < 4) |
|
580 |
break; |
|
581 |
int val = bb.getInt(); |
|
582 |
extData += val; |
|
583 |
xlen -= 4; |
|
584 |
}else |
|
585 |
xlen = 0; |
|
586 |
return extData; |
|
587 |
} |
|
588 |
/* |
|
589 |
|
|
590 |
|
|
591 |
|
|
592 |
elif _xval == 71: |
|
593 |
if _xlen < 4: break |
|
594 |
_val = struct.unpack('<l', handle.read(4))[0] |
|
595 |
_data.append(_val) |
|
596 |
_xlen = _xlen - 4 |
|
597 |
else: |
|
598 |
_xlen = 0 |
|
599 |
return _data |
|
600 |
* */ |
|
601 |
return null; |
|
602 |
} |
|
603 |
|
|
604 |
|
|
605 |
|
|
606 |
/** |
|
607 |
* @param b |
|
608 |
* @return |
|
609 |
*/ |
|
610 |
private double[] getPoint(boolean b) { |
|
611 |
bb.order(ByteOrder.LITTLE_ENDIAN); |
|
612 |
double x = bb.getDouble(); |
|
613 |
double y = bb.getDouble(); |
|
614 |
double z = 0d; |
|
615 |
if(b){ |
|
616 |
z = bb.getDouble(); |
|
617 |
} |
|
618 |
return new double[]{x, y, z}; |
|
619 |
} |
|
620 |
|
|
621 |
|
|
622 |
|
|
623 |
//TODO Que se hacen con los registros que se leen??? |
|
624 |
private void readBlockTable(Dwg12Table blockTable) { |
|
625 |
short size = blockTable.s1; |
|
626 |
int numRecords = blockTable.i1; |
|
627 |
int start = blockTable.i2; |
|
628 |
bb.position(start); |
|
629 |
|
|
630 |
int end = -1; |
|
631 |
|
|
632 |
for(int i = 0; i < numRecords; i++){ |
|
633 |
end = bb.position() + size; |
|
634 |
char flag = bb.getChar(); |
|
635 |
//TODO No estoy muy seguro de que esto sea |
|
636 |
//igual que handle.read(32) en Python |
|
637 |
byte[] nameByte = new byte[32]; |
|
638 |
bb.get(nameByte); |
|
639 |
String name = new String(nameByte); |
|
640 |
|
|
641 |
bb.order(ByteOrder.LITTLE_ENDIAN); |
|
642 |
|
|
643 |
short used = bb.getShort(); |
|
644 |
char b1 = bb.getChar(); |
|
645 |
short w1 = bb.getShort(); |
|
646 |
char b2 = bb.getChar(); |
|
647 |
short w2 = bb.getShort(); |
|
648 |
short crc = bb.getShort(); |
|
649 |
|
|
650 |
int offset = end - bb.position(); |
|
651 |
if(offset > 0) |
|
652 |
bb.position(bb.position() + end); |
|
653 |
} |
|
654 |
byte[] crc32 = new byte[32]; |
|
655 |
bb.get(crc32); |
|
656 |
} |
|
657 |
|
|
208 | 658 |
public void readHeader(){ |
209 | 659 |
|
210 | 660 |
|
Also available in: Unified diff