root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / DwgFile.java @ 9986
History | View | Annotate | Download (27.7 KB)
1 |
/* jdwglib. Java Library for reading Dwg files.
|
---|---|
2 |
*
|
3 |
* Author: Jose Morell Rama (jose.morell@gmail.com).
|
4 |
* Port from the Pythoncad Dwg library by Art Haas.
|
5 |
*
|
6 |
* Copyright (C) 2005 Jose Morell, IVER TI S.A. and Generalitat Valenciana
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Jose Morell (jose.morell@gmail.com)
|
25 |
*
|
26 |
* or
|
27 |
*
|
28 |
* IVER TI S.A.
|
29 |
* C/Salamanca, 50
|
30 |
* 46005 Valencia
|
31 |
* Spain
|
32 |
* +34 963163400
|
33 |
* dac@iver.es
|
34 |
*/
|
35 |
package com.iver.cit.jdwglib.dwg; |
36 |
|
37 |
import java.awt.geom.Point2D; |
38 |
import java.io.File; |
39 |
import java.io.FileInputStream; |
40 |
import java.io.IOException; |
41 |
import java.nio.ByteBuffer; |
42 |
import java.nio.channels.FileChannel; |
43 |
import java.util.ArrayList; |
44 |
import java.util.HashMap; |
45 |
import java.util.LinkedList; |
46 |
import java.util.List; |
47 |
import java.util.Map; |
48 |
|
49 |
import com.iver.cit.jdwglib.dwg.objects.DwgArc; |
50 |
import com.iver.cit.jdwglib.dwg.objects.DwgAttdef; |
51 |
import com.iver.cit.jdwglib.dwg.objects.DwgAttrib; |
52 |
import com.iver.cit.jdwglib.dwg.objects.DwgBlock; |
53 |
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader; |
54 |
import com.iver.cit.jdwglib.dwg.objects.DwgCircle; |
55 |
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse; |
56 |
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk; |
57 |
import com.iver.cit.jdwglib.dwg.objects.DwgInsert; |
58 |
import com.iver.cit.jdwglib.dwg.objects.DwgLayer; |
59 |
import com.iver.cit.jdwglib.dwg.objects.DwgLine; |
60 |
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline; |
61 |
import com.iver.cit.jdwglib.dwg.objects.DwgMText; |
62 |
import com.iver.cit.jdwglib.dwg.objects.DwgPoint; |
63 |
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D; |
64 |
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D; |
65 |
import com.iver.cit.jdwglib.dwg.objects.DwgSeqend; |
66 |
import com.iver.cit.jdwglib.dwg.objects.DwgSolid; |
67 |
import com.iver.cit.jdwglib.dwg.objects.DwgText; |
68 |
import com.iver.cit.jdwglib.dwg.objects.DwgVertex2D; |
69 |
import com.iver.cit.jdwglib.dwg.readers.IDwgFileReader; |
70 |
import com.iver.cit.jdwglib.dwg.readers.DwgFileV15Reader; |
71 |
|
72 |
/**
|
73 |
* The DwgFile class provides a revision-neutral interface for reading and handling
|
74 |
* DWG files
|
75 |
* Reading methods are useful for reading DWG files, and handling methods like
|
76 |
* calculateDwgPolylines() are useful for handling more complex
|
77 |
* objects in the DWG file
|
78 |
*
|
79 |
* @author jmorell
|
80 |
* @author azabala
|
81 |
*/
|
82 |
public class DwgFile { |
83 |
/**
|
84 |
* It has all known DWG version's header.
|
85 |
* Extracted from Autodesk web.
|
86 |
*/
|
87 |
private static HashMap acadVersions = new HashMap(); |
88 |
static{
|
89 |
acadVersions.put("AC1004", "Autocad R9"); |
90 |
acadVersions.put("AC1006", "Autocad R10"); |
91 |
acadVersions.put("AC1007", "Autocad pre-R11"); |
92 |
acadVersions.put("AC1007", "Autocad pre-R11"); |
93 |
acadVersions.put("AC1008", "Autocad pre-R11b"); |
94 |
acadVersions.put("AC1009", "Autocad R12"); |
95 |
acadVersions.put("AC1010", "Autocad pre-R13 a"); |
96 |
acadVersions.put("AC1011", "Autocad pre-R13 b"); |
97 |
acadVersions.put("AC1012", "Autocad R13"); |
98 |
acadVersions.put("AC1013", "Autocad pre-R14"); |
99 |
acadVersions.put("AC1014", "Autocad R14"); |
100 |
acadVersions.put("AC1500", "Autocad pre-2000"); |
101 |
acadVersions.put("AC1015", "Autocad R2000, R2000i, R2002"); |
102 |
acadVersions.put("AC402a", "Autocad pre-2004a"); |
103 |
acadVersions.put("AC402b", "Autocad pre-2004b"); |
104 |
acadVersions.put("AC1018", "Autocad R2004, R2005, R2006"); |
105 |
acadVersions.put("AC1021", "Autocad R2007"); |
106 |
|
107 |
} |
108 |
|
109 |
private String fileName; |
110 |
private String dwgVersion; |
111 |
|
112 |
/**
|
113 |
* Offsets to the DWG sections
|
114 |
*/
|
115 |
private ArrayList dwgSectionOffsets; |
116 |
/**
|
117 |
* Header vars readed from the HEADER section of the DWG file
|
118 |
* */
|
119 |
private Map headerVars; |
120 |
|
121 |
/**
|
122 |
* This list contains what in OpenDWG specification is called
|
123 |
* the object map ( a collection of entries where each entry contains
|
124 |
* the seek of each object, and its size)
|
125 |
* */
|
126 |
private ArrayList dwgObjectOffsets; |
127 |
|
128 |
/**
|
129 |
* For each entry in dwgObjectOffsets, we have an instance of
|
130 |
* DwgObject in the dwgObjects collection
|
131 |
*
|
132 |
* */
|
133 |
private LinkedList dwgObjects; |
134 |
private HashMap handle_objects; |
135 |
|
136 |
|
137 |
private ArrayList dwgClasses; |
138 |
|
139 |
|
140 |
/**
|
141 |
* hash map that indexes all DwgLayer objects
|
142 |
* by its handle property
|
143 |
* */
|
144 |
private HashMap layerTable; |
145 |
|
146 |
/**
|
147 |
* Specific reader of the DWG file version (12, 13, 14, 2000, etc., each
|
148 |
* version will have an specific reader)
|
149 |
* */
|
150 |
private IDwgFileReader dwgReader;
|
151 |
private boolean dwg3DFile; |
152 |
/**
|
153 |
* Memory mapped byte buffer of the whole DWG file
|
154 |
* */
|
155 |
private ByteBuffer bb; |
156 |
|
157 |
/*
|
158 |
* Optimizaciones para evitar leer el fichero completo en cada
|
159 |
* pasada.
|
160 |
* */
|
161 |
/**
|
162 |
* Contains all IDwgPolyline implementations
|
163 |
* */
|
164 |
private List dwgPolylines; |
165 |
/**
|
166 |
* map of blocks dwg entities indexed by their names.
|
167 |
*/
|
168 |
private Map names_blocks; |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
/**
|
174 |
* Constructor.
|
175 |
* @param fileName an absolute path to the DWG file
|
176 |
*/
|
177 |
public DwgFile(String fileName) { |
178 |
this.fileName = fileName;
|
179 |
|
180 |
dwgSectionOffsets = new ArrayList(); |
181 |
dwgObjectOffsets = new ArrayList(); |
182 |
headerVars = new HashMap(); |
183 |
|
184 |
//TODO Si finalmente no se leen las clases, quitar
|
185 |
//dwgClasses
|
186 |
dwgClasses = new ArrayList(); |
187 |
|
188 |
dwgObjects = new LinkedList(); |
189 |
handle_objects = new HashMap(); |
190 |
|
191 |
layerTable = new HashMap(); |
192 |
|
193 |
dwgPolylines = new ArrayList(); |
194 |
|
195 |
names_blocks = new HashMap(); |
196 |
} |
197 |
|
198 |
public String getDwgVersion() { |
199 |
return dwgVersion;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* Reads a DWG file and put its objects in the dwgObjects Vector
|
204 |
* This method is version independent
|
205 |
*
|
206 |
* @throws IOException If the file location is wrong
|
207 |
*/
|
208 |
public void read() throws IOException, |
209 |
DwgVersionNotSupportedException |
210 |
{ |
211 |
setDwgVersion(); |
212 |
if (dwgVersion.equalsIgnoreCase("Autocad R2000, R2000i, R2002")) { |
213 |
dwgReader = new DwgFileV15Reader();
|
214 |
dwgReader.read(this, bb);
|
215 |
} else {
|
216 |
DwgVersionNotSupportedException exception = |
217 |
new DwgVersionNotSupportedException("Version de DWG no soportada"); |
218 |
exception.setDwgVersion(dwgVersion); |
219 |
throw exception;
|
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
private void setDwgVersion() throws IOException { |
225 |
File file = new File(fileName); |
226 |
FileInputStream fileInputStream = new FileInputStream(file); |
227 |
FileChannel fileChannel = fileInputStream.getChannel();
|
228 |
long channelSize = fileChannel.size();
|
229 |
bb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, channelSize); |
230 |
byte[] versionBytes = {bb.get(0), |
231 |
bb.get(1),
|
232 |
bb.get(2),
|
233 |
bb.get(3),
|
234 |
bb.get(4),
|
235 |
bb.get(5)};
|
236 |
ByteBuffer versionByteBuffer = ByteBuffer.wrap(versionBytes); |
237 |
String[] bs = new String[versionByteBuffer.capacity()]; |
238 |
String versionString = ""; |
239 |
for (int i=0; i<versionByteBuffer.capacity(); i++) { |
240 |
bs[i] = new String(new byte[]{(byte)(versionByteBuffer.get(i))}); |
241 |
versionString = versionString + bs[i]; |
242 |
} |
243 |
String version = (String) acadVersions.get(versionString); |
244 |
if(version == null) |
245 |
version = "Unknown Dwg format";
|
246 |
this.dwgVersion = version;
|
247 |
} |
248 |
|
249 |
public void setHeader(String key, Object value){ |
250 |
headerVars.put(key, value); |
251 |
} |
252 |
|
253 |
|
254 |
|
255 |
/**
|
256 |
* Initialize a new Vector that contains the DWG file layers.
|
257 |
* Each layer have three parameters.
|
258 |
* These parameters are handle, name and color
|
259 |
*/
|
260 |
// public void initializeLayerTable() {
|
261 |
// layerTable = new ArrayList();
|
262 |
// for (int i=0;i<dwgObjects.size();i++) {
|
263 |
// DwgObject obj = (DwgObject)dwgObjects.get(i);
|
264 |
// if (obj instanceof DwgLayer) {
|
265 |
// ArrayList layerTableRecord = new ArrayList();
|
266 |
// layerTableRecord.add(new Integer(obj.getHandle()));
|
267 |
// layerTableRecord.add(((DwgLayer)obj).getName());
|
268 |
// layerTableRecord.add(new Integer(((DwgLayer)obj).getColor()));
|
269 |
// layerTable.add(layerTableRecord);
|
270 |
// }
|
271 |
// }
|
272 |
// }
|
273 |
|
274 |
/*
|
275 |
* A DwgLayer is a DWG drawing entity, so all DwgLayer objects are
|
276 |
* saved in dwgObjects collection.
|
277 |
* Instead of iterate over the full collection, is faster check every object
|
278 |
* and save all the DwgLayer instances in their own collection.
|
279 |
*
|
280 |
*/
|
281 |
public void addDwgLayer(DwgLayer dwgLayer){ |
282 |
layerTable.put(new Integer(dwgLayer.getHandle()), |
283 |
dwgLayer); |
284 |
} |
285 |
|
286 |
public DwgLayer getDwgLayer(DwgObject entity){
|
287 |
|
288 |
int handleCode = entity.getLayerHandleCode();
|
289 |
int layerHandle = -1; |
290 |
|
291 |
int entityRecord;
|
292 |
DwgObject object; |
293 |
|
294 |
// switch(handleCode){
|
295 |
// case 0x2:
|
296 |
// case 0x3:
|
297 |
// case 0x4:
|
298 |
// case 0x5:
|
299 |
// layerHandle = entity.getLayerHandle();
|
300 |
// break;
|
301 |
//
|
302 |
// case 0x6:
|
303 |
// entityRecord = entity.getIndex();
|
304 |
// object = this.getDwgObject(entityRecord + 1);
|
305 |
// layerHandle = object.getLayerHandle();
|
306 |
// break;
|
307 |
// case 0x8:
|
308 |
// entityRecord = entity.getIndex();
|
309 |
// object = this.getDwgObject(entityRecord - 1);
|
310 |
// layerHandle = object.getLayerHandle();
|
311 |
//
|
312 |
// break;
|
313 |
// case 0xA:
|
314 |
// //reference handle plus offset of layer handle??
|
315 |
// int handle = entity.getHandle();
|
316 |
// int offset = entity.getLayerHandle();
|
317 |
// layerHandle = handle + offset;
|
318 |
//
|
319 |
//
|
320 |
// break;
|
321 |
// case 0xC:
|
322 |
// //reference handle minus offset of layer handle??
|
323 |
// handle = entity.getHandle();
|
324 |
// offset = entity.getLayerHandle();
|
325 |
// layerHandle = handle + offset;
|
326 |
//
|
327 |
// break;
|
328 |
// default:
|
329 |
// System.out.println("Unknown layer handle code:"+handleCode);
|
330 |
// return null;
|
331 |
// }
|
332 |
|
333 |
|
334 |
|
335 |
/*
|
336 |
* OpenDWG spec, v3, page 11. HANDLE REFERENCES
|
337 |
*
|
338 |
* A handle is formed by this fields:
|
339 |
* a) CODE (4 bits)
|
340 |
* b) COUNTER (4 bits)
|
341 |
* c) OFFSET of the object (handle itself).
|
342 |
*
|
343 |
* CODE could take these values:
|
344 |
* 1) 0x2, 0x3, 0x4, 0x5 -> offset is the handle of the layer
|
345 |
* 2) 0x6 -> offset is the next object handle
|
346 |
* 3) 0x8 -> offset is the previous object handle
|
347 |
* 4) 0xA -> result is reference handle plus offset
|
348 |
* 5) 0xC -> result is reference handle minus offset
|
349 |
*
|
350 |
* */
|
351 |
|
352 |
// Iterator lyrIterator = layerTable.values().iterator();
|
353 |
// while(lyrIterator.hasNext()){
|
354 |
// DwgLayer lyr = (DwgLayer) lyrIterator.next();
|
355 |
// int lyrHdl = lyr.getHandle();
|
356 |
// int entityHdlCode = entity.getLayerHandleCode();
|
357 |
// int entityHdl = entity.getLayerHandle();
|
358 |
// int newLyrHdl = layerHandle;
|
359 |
// System.currentTimeMillis();
|
360 |
// }
|
361 |
|
362 |
return (DwgLayer) layerTable.
|
363 |
get(new Integer(layerHandle)); |
364 |
} |
365 |
|
366 |
|
367 |
public String getLayerName(DwgObject entity) { |
368 |
DwgLayer dwgLayer = getDwgLayer(entity); |
369 |
if(dwgLayer == null){//TODO Puede haber entidades sin layer??? |
370 |
return ""; |
371 |
}else
|
372 |
return dwgLayer.getName();
|
373 |
} |
374 |
|
375 |
/**
|
376 |
* Returns the color of the layer of a DWG object
|
377 |
*
|
378 |
* @param entity DWG object which we want to know its layer color
|
379 |
* @return int Layer color of the DWG object in the Autocad color code
|
380 |
*/
|
381 |
public int getColorByLayer(DwgObject entity) { |
382 |
int colorByLayer;
|
383 |
DwgLayer dwgLyr = getDwgLayer(entity); |
384 |
if(dwgLyr == null) |
385 |
colorByLayer = 0;
|
386 |
else
|
387 |
colorByLayer = dwgLyr.getColor(); |
388 |
return colorByLayer;
|
389 |
} |
390 |
|
391 |
|
392 |
//azabala: esto ahora se hace conforme se van creando los objetos
|
393 |
//(y no al final en una segunda pasada
|
394 |
// public void applyExtrusions() {
|
395 |
// for (int i=0;i<dwgObjects.size();i++) {
|
396 |
// DwgObject dwgObject = (DwgObject)dwgObjects.get(i);
|
397 |
// if(dwgObject instanceof IDwgExtrusionable){
|
398 |
// ((IDwgExtrusionable)dwgObject).applyExtrussion();
|
399 |
// }
|
400 |
// // Seems that Autocad don't apply the extrusion to Ellipses
|
401 |
// /*} else if (dwgObject instanceof DwgEllipse) {
|
402 |
// double[] ellipseCenter = ((DwgEllipse)dwgObject).getCenter();
|
403 |
// double[] ellipseExt = ((DwgEllipse)dwgObject).getExtrusion();
|
404 |
// ellipseCenter = AcadExtrusionCalculator.CalculateAcadExtrusion(ellipseCenter, ellipseExt);
|
405 |
// ((DwgEllipse)dwgObject).setCenter(ellipseCenter);*/
|
406 |
// }//for
|
407 |
// }
|
408 |
|
409 |
/**
|
410 |
* Configure the geometry of the polylines in a DWG file from the vertex list in
|
411 |
* this DWG file. This geometry is given by an array of Points.
|
412 |
* Besides, manage closed polylines and polylines with bulges in a GIS Data model.
|
413 |
* It means that the arcs of the polylines will be done through a set of points and
|
414 |
* a distance between these points.
|
415 |
*/
|
416 |
public void calculateGisModelDwgPolylines() { |
417 |
/*
|
418 |
* Ahora mismo esto se est? haciendo al final (en una segunda pasada)
|
419 |
* porque posiblemente una Linea requiera de vertices que vengan
|
420 |
* despues, lo que complicar?a bastante todo....
|
421 |
* */
|
422 |
|
423 |
// for (int i=0;i<dwgObjects.size();i++) {
|
424 |
for (int i = 0; i < dwgPolylines.size(); i++){ |
425 |
// DwgObject pol = (DwgObject)dwgObjects.get(i);
|
426 |
DwgObject pol = (DwgObject)dwgPolylines.get(i); |
427 |
if (pol instanceof IDwgPolyline) { |
428 |
// ((IDwgPolyline)pol).calculateGisModel(dwgObjects);
|
429 |
((IDwgPolyline)pol).calculateGisModel(this);
|
430 |
} |
431 |
} |
432 |
} |
433 |
|
434 |
/**
|
435 |
* Configure the geometry of the polylines in a DWG file from the vertex list in
|
436 |
* this DWG file. This geometry is given by an array of Points
|
437 |
* Besides, manage closed polylines and polylines with bulges in a GIS Data model.
|
438 |
* It means that the arcs of the polylines will be done through a curvature
|
439 |
* parameter called bulge associated with the points of the polyline.
|
440 |
*/
|
441 |
//TODO Refactorizar para que solo se aplique a las Polilineas
|
442 |
public void calculateCadModelDwgPolylines() { |
443 |
for (int i=0;i<dwgObjects.size();i++) { |
444 |
DwgObject pol = (DwgObject)dwgObjects.get(i); |
445 |
if (pol instanceof DwgPolyline2D) { |
446 |
int flags = ((DwgPolyline2D)pol).getFlags();
|
447 |
int firstHandle = ((DwgPolyline2D)pol).getFirstVertexHandle();
|
448 |
int lastHandle = ((DwgPolyline2D)pol).getLastVertexHandle();
|
449 |
ArrayList pts = new ArrayList(); |
450 |
ArrayList bulges = new ArrayList(); |
451 |
double[] pt = new double[3]; |
452 |
for (int j=0;j<dwgObjects.size();j++) { |
453 |
DwgObject firstVertex = (DwgObject)dwgObjects.get(j); |
454 |
if (firstVertex instanceof DwgVertex2D) { |
455 |
int vertexHandle = firstVertex.getHandle();
|
456 |
if (vertexHandle==firstHandle) {
|
457 |
int k=0; |
458 |
while (true) { |
459 |
DwgObject vertex = (DwgObject)dwgObjects.get(j+k); |
460 |
int vHandle = vertex.getHandle();
|
461 |
if (vertex instanceof DwgVertex2D) { |
462 |
pt = ((DwgVertex2D)vertex).getPoint(); |
463 |
pts.add(new Point2D.Double(pt[0], pt[1])); |
464 |
double bulge = ((DwgVertex2D)vertex).getBulge();
|
465 |
bulges.add(new Double(bulge)); |
466 |
k++; |
467 |
if (vHandle==lastHandle && vertex instanceof DwgVertex2D) { |
468 |
break;
|
469 |
} |
470 |
} else if (vertex instanceof DwgSeqend) { |
471 |
break;
|
472 |
} |
473 |
} |
474 |
} |
475 |
} |
476 |
} |
477 |
if (pts.size()>0) { |
478 |
/*Point2D[] newPts = new Point2D[pts.size()];
|
479 |
if ((flags & 0x1)==0x1) {
|
480 |
newPts = new Point2D[pts.size()+1];
|
481 |
for (int j=0;j<pts.size();j++) {
|
482 |
newPts[j] = (Point2D)pts.get(j);
|
483 |
}
|
484 |
newPts[pts.size()] = (Point2D)pts.get(0);
|
485 |
bulges.add(new Double(0));
|
486 |
} else {
|
487 |
for (int j=0;j<pts.size();j++) {
|
488 |
newPts[j] = (Point2D)pts.get(j);
|
489 |
}
|
490 |
}*/
|
491 |
double[] bs = new double[bulges.size()]; |
492 |
for (int j=0;j<bulges.size();j++) { |
493 |
bs[j] = ((Double)bulges.get(j)).doubleValue();
|
494 |
} |
495 |
((DwgPolyline2D)pol).setBulges(bs); |
496 |
//Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, bs);
|
497 |
Point2D[] points = new Point2D[pts.size()]; |
498 |
for (int j=0;j<pts.size();j++) { |
499 |
points[j] = (Point2D)pts.get(j);
|
500 |
} |
501 |
((DwgPolyline2D)pol).setPts(points); |
502 |
} else {
|
503 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
504 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
505 |
// ocurre es porque existe un error que hay que corregir ...
|
506 |
} |
507 |
} else if (pol instanceof DwgPolyline3D) { |
508 |
} else if (pol instanceof DwgLwPolyline && ((DwgLwPolyline)pol).getVertices()!=null) { |
509 |
} |
510 |
} |
511 |
} |
512 |
|
513 |
/**
|
514 |
* Modify the geometry of the objects contained in the blocks of a DWG file and
|
515 |
* add these objects to the DWG object list.
|
516 |
*
|
517 |
*/
|
518 |
public void blockManagement() { |
519 |
LinkedList dwgObjectsWithoutBlocks = new LinkedList(); |
520 |
boolean addingToBlock = false; |
521 |
try{
|
522 |
for (int i=0; i < dwgObjects.size(); i++) { |
523 |
DwgObject entity = (DwgObject)dwgObjects.get(i); |
524 |
if (entity instanceof DwgArc && !addingToBlock) { |
525 |
dwgObjectsWithoutBlocks.add(entity); |
526 |
} else if (entity instanceof DwgEllipse && !addingToBlock) { |
527 |
dwgObjectsWithoutBlocks.add(entity); |
528 |
} else if (entity instanceof DwgCircle && !addingToBlock) { |
529 |
dwgObjectsWithoutBlocks.add(entity); |
530 |
} else if (entity instanceof DwgPolyline2D && !addingToBlock) { |
531 |
dwgObjectsWithoutBlocks.add(entity); |
532 |
} else if (entity instanceof DwgPolyline3D && !addingToBlock) { |
533 |
dwgObjectsWithoutBlocks.add(entity); |
534 |
} else if (entity instanceof DwgLwPolyline && !addingToBlock) { |
535 |
dwgObjectsWithoutBlocks.add(entity); |
536 |
} else if (entity instanceof DwgSolid && !addingToBlock) { |
537 |
dwgObjectsWithoutBlocks.add(entity); |
538 |
} else if (entity instanceof DwgLine && !addingToBlock) { |
539 |
dwgObjectsWithoutBlocks.add(entity); |
540 |
} else if (entity instanceof DwgPoint && !addingToBlock) { |
541 |
dwgObjectsWithoutBlocks.add(entity); |
542 |
} else if (entity instanceof DwgMText && !addingToBlock) { |
543 |
dwgObjectsWithoutBlocks.add(entity); |
544 |
} else if (entity instanceof DwgText && !addingToBlock) { |
545 |
dwgObjectsWithoutBlocks.add(entity); |
546 |
} else if (entity instanceof DwgAttrib && !addingToBlock) { |
547 |
dwgObjectsWithoutBlocks.add(entity); |
548 |
} else if (entity instanceof DwgAttdef && !addingToBlock) { |
549 |
dwgObjectsWithoutBlocks.add(entity); |
550 |
} |
551 |
|
552 |
else if (entity instanceof DwgBlock) { |
553 |
addingToBlock = true;
|
554 |
} |
555 |
|
556 |
else if (entity instanceof DwgEndblk) { |
557 |
addingToBlock = false;
|
558 |
} |
559 |
|
560 |
else if (entity instanceof DwgBlockHeader) { |
561 |
addingToBlock = true;
|
562 |
} |
563 |
//Segun esto, un insert solo se procesa dentro de un Block (o BlockHeader)
|
564 |
//?Puede haber INSERT fuera de BLOCK-ENDBLK?
|
565 |
else if (entity instanceof DwgInsert && !addingToBlock) { |
566 |
double[] p = ((DwgInsert)entity).getInsertionPoint(); |
567 |
Point2D point = new Point2D.Double(p[0], p[1]); |
568 |
double[] scale = ((DwgInsert)entity).getScale(); |
569 |
double rot = ((DwgInsert)entity).getRotation();
|
570 |
int blockHandle = ((DwgInsert)entity).getBlockHeaderHandle();
|
571 |
manageInsert(point, scale, rot, blockHandle, dwgObjectsWithoutBlocks); |
572 |
} else {
|
573 |
// System.out.println(entity.getClass().getName() +" en un bloque");
|
574 |
//Se ha encontrado una entidad rara, o forma parte de un bloque
|
575 |
//y ser? procesada despu?s (con los punteros del propio bloque)
|
576 |
|
577 |
//TODO No se podr?a hacer mas rapido, quitando de la lista original
|
578 |
//las entidades que forman parte de bloque??
|
579 |
} |
580 |
} |
581 |
}catch(RuntimeException re){ |
582 |
re.printStackTrace(); |
583 |
} |
584 |
dwgObjects = dwgObjectsWithoutBlocks; |
585 |
} |
586 |
|
587 |
/**
|
588 |
* Manages an INSERT of a DWG file. This object is the insertion point of a DWG
|
589 |
* block.
|
590 |
*
|
591 |
* @param insPoint, coordinates of the insertion point.
|
592 |
* @param scale, scale of the elements of the block that will be inserted.
|
593 |
* @param rot, rotation angle of the elements of the block.
|
594 |
*
|
595 |
* @param bHandle, offset for the coordinates of the elements of the block.
|
596 |
* @param id, count that serves as a id.
|
597 |
* @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
|
598 |
* the blocks.
|
599 |
*/
|
600 |
public void manageInsert(Point2D insPoint, double[] scale, |
601 |
double rot, int bHandle, |
602 |
List dwgObjectsWithoutBlocks) {
|
603 |
|
604 |
DwgObject object = (DwgObject) handle_objects.get(new Integer(bHandle)); |
605 |
if(! (object instanceof DwgBlockHeader)){ |
606 |
//Hay un problema con la asignaci?n de handle
|
607 |
//Un INSERT tiene como handle de Block una entidad que no es block
|
608 |
System.out.println("handle incorrecto." + object.getClass().getName() + " no es un blockheader"); |
609 |
return;
|
610 |
} |
611 |
DwgBlockHeader blockHeader = (DwgBlockHeader)object; |
612 |
double[] bPoint = blockHeader.getBasePoint(); |
613 |
String bname = blockHeader.getName();
|
614 |
if (bname.startsWith("*")) |
615 |
return;
|
616 |
int firstObjectHandle = blockHeader.getFirstEntityHandle();
|
617 |
int lastObjectHandle = blockHeader.getLastEntityHandle();
|
618 |
|
619 |
//TODO Ver que hacia el metodo antiguo cuando llegaban handles a 0
|
620 |
//como extremos de un bloque (sera bloque vacio, supongo)
|
621 |
if(firstObjectHandle == 0 || lastObjectHandle == 0){ |
622 |
System.out.println("Problemas en el bloque "+bname); |
623 |
System.out.println("1er obj="+firstObjectHandle+" 2o obj="+lastObjectHandle); |
624 |
return;
|
625 |
} |
626 |
|
627 |
/*
|
628 |
* Ahora localiza la primera entidad del bloque,
|
629 |
* y a partir de ah?, a?ade
|
630 |
* al bloque todas las entidades que encuentre hasta la
|
631 |
* ?ltima entidad del bloque
|
632 |
*
|
633 |
* */
|
634 |
DwgObject firstObj = (DwgObject) handle_objects. |
635 |
get(new Integer(firstObjectHandle)); |
636 |
DwgObject lastObj = (DwgObject) handle_objects. |
637 |
get(new Integer(lastObjectHandle)); |
638 |
|
639 |
int firstObjIdx = dwgObjects.indexOf(firstObj);
|
640 |
int lastObjIdx = dwgObjects.indexOf(lastObj);
|
641 |
if(firstObjIdx == -1){ |
642 |
System.out.println("El primer objeto del bloque "+ |
643 |
blockHeader.getName()+ |
644 |
" no ha sido localizado a partir del handle "+
|
645 |
firstObjectHandle); |
646 |
return;
|
647 |
} |
648 |
|
649 |
if(lastObjIdx == -1){ |
650 |
System.out.println("El ultimo objeto del bloque "+ |
651 |
blockHeader.getName()+ |
652 |
" no ha sido localizado a partir del handle "+
|
653 |
lastObjectHandle); |
654 |
|
655 |
//Intentamos ver si como delimitador final de bloque aparece EndBlk
|
656 |
LinkedList tempDwgObj = new LinkedList(); |
657 |
DwgObject scannedEntity = (DwgObject) dwgObjects.get(firstObjIdx); |
658 |
while(! (scannedEntity instanceof DwgEndblk)){ |
659 |
manageBlockEntity(scannedEntity, bPoint, |
660 |
insPoint, scale, rot, |
661 |
tempDwgObj); |
662 |
firstObjIdx++; |
663 |
scannedEntity = (DwgObject) dwgObjects.get(firstObjIdx); |
664 |
if( (scannedEntity instanceof DwgBlock) || |
665 |
(scannedEntity instanceof DwgBlockHeader)){
|
666 |
//No puede haber bloques anidados
|
667 |
//Descartar
|
668 |
System.out.println("Error, aparecen bloques anidados"); |
669 |
return;
|
670 |
} |
671 |
}//while
|
672 |
if(tempDwgObj.size() > 0) |
673 |
dwgObjectsWithoutBlocks.addAll(tempDwgObj); |
674 |
return;
|
675 |
|
676 |
}//if
|
677 |
for(int i = firstObjIdx; i <= lastObjIdx; i++){ |
678 |
DwgObject obj = (DwgObject) dwgObjects.get(i); |
679 |
manageBlockEntity(obj, bPoint, |
680 |
insPoint, scale, |
681 |
rot, dwgObjectsWithoutBlocks); |
682 |
}//for
|
683 |
} |
684 |
|
685 |
public int getIndexOf(DwgObject dwgObject){ |
686 |
return dwgObjects.indexOf(dwgObject);
|
687 |
|
688 |
|
689 |
} |
690 |
|
691 |
|
692 |
|
693 |
/**
|
694 |
* Changes the location of an object extracted from a block. This location will be
|
695 |
* obtained through the insertion parameters from the block and the corresponding
|
696 |
* insert.
|
697 |
* @param entity, the entity extracted from the block.
|
698 |
* @param bPoint, offset for the coordinates of the entity.
|
699 |
* @param insPoint, coordinates of the insertion point for the entity.
|
700 |
* @param scale, scale for the entity.
|
701 |
* @param rot, rotation angle for the entity.
|
702 |
* @param id, a count as a id.
|
703 |
* @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
|
704 |
* the blocks.
|
705 |
*/
|
706 |
private void manageBlockEntity(DwgObject entity, |
707 |
double[] bPoint, |
708 |
Point2D insPoint,
|
709 |
double[] scale, |
710 |
double rot,
|
711 |
List dwgObjectsWithoutBlocks) {
|
712 |
|
713 |
if(entity instanceof IDwgBlockMember){ |
714 |
IDwgBlockMember blockMember = (IDwgBlockMember)entity; |
715 |
blockMember.transform2Block(bPoint, insPoint, scale, rot, dwgObjectsWithoutBlocks, this);
|
716 |
}else{
|
717 |
//TODO REIMPLEMENTAR ENTIDADES QUE PUEDEN FORMAR PARTE DE BLOQUES.
|
718 |
//POR EJEMPLO: DwgText y DwgMText pueden formar parte
|
719 |
//de bloques, y de hecho, est?n apareciendo
|
720 |
|
721 |
// System.out.println(entity.getClass().getName()+" dentro de un bloque");
|
722 |
} |
723 |
} |
724 |
|
725 |
|
726 |
/*
|
727 |
* azabala: Esto ahora se hace mientras las entidades dwg
|
728 |
* se van creando, y no en una segunda pasada
|
729 |
* */
|
730 |
/*
|
731 |
public void testDwg3D() {
|
732 |
for (int i=0;i<dwgObjects.size();i++) {
|
733 |
DwgObject obj = (DwgObject)dwgObjects.get(i);
|
734 |
|
735 |
if(obj instanceof IDwg3DTestable){
|
736 |
if(((IDwg3DTestable)obj).has3DData())
|
737 |
dwg3DFile = true;
|
738 |
return;
|
739 |
}
|
740 |
|
741 |
//} else if (obj instanceof DwgLinearDimension) {
|
742 |
// z = ((DwgLinearDimension)obj).getElevation();
|
743 |
// if (z!=0.0) dwg3DFile = true;
|
744 |
}//for
|
745 |
dwg3DFile = false;
|
746 |
}
|
747 |
*/
|
748 |
|
749 |
/**
|
750 |
* Add a DWG section offset to the dwgSectionOffsets vector
|
751 |
*
|
752 |
* @param key Define the DWG section
|
753 |
* @param seek Offset of the section
|
754 |
* @param size Size of the section
|
755 |
*/
|
756 |
public void addDwgSectionOffset(String key, int seek, int size) { |
757 |
DwgSectionOffset dso = new DwgSectionOffset(key, seek, size);
|
758 |
dwgSectionOffsets.add(dso); |
759 |
} |
760 |
|
761 |
/**
|
762 |
* Returns the offset of DWG section given by its key
|
763 |
*
|
764 |
* @param key Define the DWG section
|
765 |
* @return int Offset of the section in the DWG file
|
766 |
*/
|
767 |
public int getDwgSectionOffset(String key) { |
768 |
int offset = 0; |
769 |
for (int i=0; i<dwgSectionOffsets.size(); i++) { |
770 |
DwgSectionOffset dso = (DwgSectionOffset)dwgSectionOffsets.get(i); |
771 |
String ikey = dso.getKey();
|
772 |
if (key.equals(ikey)) {
|
773 |
offset = dso.getSeek(); |
774 |
break;
|
775 |
} |
776 |
} |
777 |
return offset;
|
778 |
} |
779 |
|
780 |
/**
|
781 |
* Add a DWG object offset to the dwgObjectOffsets vector
|
782 |
*
|
783 |
* @param handle Object handle
|
784 |
* @param offset Offset of the object data in the DWG file
|
785 |
*/
|
786 |
public void addDwgObjectOffset(int handle, int offset) { |
787 |
DwgObjectOffset doo = new DwgObjectOffset(handle, offset);
|
788 |
dwgObjectOffsets.add(doo); |
789 |
} |
790 |
|
791 |
/**
|
792 |
*
|
793 |
* Add a DWG object to the dwgObject vector
|
794 |
*
|
795 |
* @param dwgObject DWG object
|
796 |
*/
|
797 |
public void addDwgObject(DwgObject dwgObject){ |
798 |
//TODO Ver si puedo inicializar las listas especificas
|
799 |
//(IDwgPolyline, etc) aqu?
|
800 |
dwgObjects.add(dwgObject); |
801 |
handle_objects.put(new Integer(dwgObject.getHandle()), dwgObject); |
802 |
/*
|
803 |
* TODO Quitar todos estos if-then y sustituirlos por un metodo callbackj
|
804 |
*
|
805 |
*
|
806 |
* (dwgObject.init(this), y que cada objeto haga lo que tenga que hacer
|
807 |
* */
|
808 |
if(dwgObject instanceof DwgLayer){ |
809 |
this.addDwgLayer((DwgLayer) dwgObject);
|
810 |
} |
811 |
if(dwgObject instanceof IDwgExtrusionable){ |
812 |
((IDwgExtrusionable)dwgObject).applyExtrussion(); |
813 |
} |
814 |
if(dwgObject instanceof IDwgPolyline){ |
815 |
dwgPolylines.add(dwgObject); |
816 |
} |
817 |
if(dwgObject instanceof IDwg3DTestable){ |
818 |
if(! dwg3DFile){//if its true, we dont check again |
819 |
dwg3DFile = ((IDwg3DTestable)dwgObject).has3DData(); |
820 |
} |
821 |
} |
822 |
|
823 |
if(dwgObject instanceof DwgBlock){ |
824 |
DwgBlock block = (DwgBlock) dwgObject; |
825 |
names_blocks.put(block.getName(), block); |
826 |
} |
827 |
|
828 |
|
829 |
} |
830 |
|
831 |
/**
|
832 |
* Returns dwgObjects from its insertion order (position
|
833 |
* in the dwg file)
|
834 |
*
|
835 |
* @param index order in the dwg file
|
836 |
* @return position
|
837 |
* */
|
838 |
public DwgObject getDwgObject(int index){ |
839 |
return (DwgObject) dwgObjects.get(index);
|
840 |
} |
841 |
|
842 |
public DwgObject getDwgObjectFromHandle(int handle){ |
843 |
return (DwgObject) handle_objects.get(new Integer(handle)); |
844 |
} |
845 |
|
846 |
/**
|
847 |
* Add a DWG class to the dwgClasses vector
|
848 |
*
|
849 |
* @param dwgClass DWG class
|
850 |
*/
|
851 |
public void addDwgClass(DwgClass dwgClass){ |
852 |
dwgClasses.add(dwgClass); |
853 |
} |
854 |
|
855 |
public List getDwgClasses(){ |
856 |
return dwgClasses;
|
857 |
} |
858 |
|
859 |
/**
|
860 |
* @return Returns the dwgObjectOffsets.
|
861 |
*/
|
862 |
public ArrayList getDwgObjectOffsets() { |
863 |
return dwgObjectOffsets;
|
864 |
} |
865 |
/**
|
866 |
* @return Returns the dwgObjects.
|
867 |
*/
|
868 |
public LinkedList getDwgObjects() { |
869 |
return dwgObjects;
|
870 |
} |
871 |
/**
|
872 |
* @return Returns the fileName.
|
873 |
*/
|
874 |
public String getFileName() { |
875 |
return fileName;
|
876 |
} |
877 |
/**
|
878 |
* @return Returns the dwg3DFile.
|
879 |
*/
|
880 |
public boolean isDwg3DFile() { |
881 |
return dwg3DFile;
|
882 |
} |
883 |
/**
|
884 |
* @param dwg3DFile The dwg3DFile to set.
|
885 |
*/
|
886 |
public void setDwg3DFile(boolean dwg3DFile) { |
887 |
this.dwg3DFile = dwg3DFile;
|
888 |
} |
889 |
} |