Revision 10823 trunk/extensions/extDwg/src/com/iver/cit/gvsig/drivers/dwg/DwgMemoryDriver.java

View differences:

DwgMemoryDriver.java
43 43
 *   dac@iver.es
44 44
 */
45 45
/* CVS MESSAGES:
46
*
47
* $Id$
48
* $Log$
49
* Revision 1.8  2007-03-06 17:27:31  caballero
50
* Exceptions
51
*
52
* Revision 1.7  2007/02/27 17:18:03  azabala
53
* solved bug with text orientation
54
*
55
* Revision 1.6  2007/02/14 16:17:22  azabala
56
* *** empty log message ***
57
*
58
* Revision 1.5  2007/01/20 18:30:37  azabala
59
* refactoring of blocks
60
*
61
* Revision 1.4  2007/01/18 19:59:10  azabala
62
* refactoring to optimize and simplify the code
63
*
64
* Revision 1.3  2007/01/18 13:36:42  azabala
65
* Refactoring general para evitar dar tantas pasadas en la carga, y para incrementar
66
* la legibilidad del codigo (demasiados if-else-if en vez de usar polimorfismo)
67
*
68
* Revision 1.2  2007/01/12 19:57:44  azabala
69
* *** empty log message ***
70
*
71
* Revision 1.1  2007/01/11 20:31:05  azabala
72
* *** empty log message ***
73
*
74
*
75
*/
46
 *
47
 * $Id$
48
 * $Log$
49
 * Revision 1.9  2007-03-20 20:21:19  azabala
50
 * added roi filtering, reordering of code
51
 *
52
 * Revision 1.7  2007/02/27 17:18:03  azabala
53
 * solved bug with text orientation
54
 *
55
 * Revision 1.6  2007/02/14 16:17:22  azabala
56
 * *** empty log message ***
57
 *
58
 * Revision 1.5  2007/01/20 18:30:37  azabala
59
 * refactoring of blocks
60
 *
61
 * Revision 1.4  2007/01/18 19:59:10  azabala
62
 * refactoring to optimize and simplify the code
63
 *
64
 * Revision 1.3  2007/01/18 13:36:42  azabala
65
 * Refactoring general para evitar dar tantas pasadas en la carga, y para incrementar
66
 * la legibilidad del codigo (demasiados if-else-if en vez de usar polimorfismo)
67
 *
68
 * Revision 1.2  2007/01/12 19:57:44  azabala
69
 * *** empty log message ***
70
 *
71
 * Revision 1.1  2007/01/11 20:31:05  azabala
72
 * *** empty log message ***
73
 *
74
 *
75
 */
76 76
package com.iver.cit.gvsig.drivers.dwg;
77 77

  
78 78
import java.awt.Color;
79 79
import java.awt.Font;
80
import java.awt.geom.Point2D;
80
import java.awt.geom.Rectangle2D;
81 81
import java.io.File;
82 82
import java.io.IOException;
83 83
import java.util.ArrayList;
84 84
import java.util.List;
85 85

  
86
import com.hardcode.gdbms.engine.data.DataSourceFactory;
86 87
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
87 88
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
88 89
import com.hardcode.gdbms.driver.exceptions.UnsupportedVersionDriverException;
89
import com.hardcode.gdbms.engine.data.DataSourceFactory;
90
import com.hardcode.gdbms.engine.data.driver.DriverException;
90 91
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
91 92
import com.hardcode.gdbms.engine.data.edition.DataWare;
92 93
import com.hardcode.gdbms.engine.values.IntValue;
93 94
import com.hardcode.gdbms.engine.values.Value;
94 95
import com.hardcode.gdbms.engine.values.ValueFactory;
95
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
96 96
import com.iver.cit.gvsig.fmap.core.FShape;
97
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
97
import com.iver.cit.gvsig.fmap.core.IGeometry;
98 98
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
99 99
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
100 100
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
......
117 117
 * Driver that allows gvSIG to read files in DWG format Using this driver, a
118 118
 * gvSIG user can manipulate part of the information contained in a DWG file
119 119
 * This driver load the Dwg file in memory This driver uses jdwglib
120
 *
120
 * 
121 121
 * @author jmorell
122 122
 */
123 123
public class DwgMemoryDriver extends AbstractCadMemoryDriver implements
124 124
		VectorialFileDriver, WithDefaultLegend {
125 125

  
126 126
	VectorialUniqueValueLegend defaultLegend;
127

  
128 127
	private File m_Fich;
129

  
130
	private DwgFile dwg;
131

  
132
	private List dwgObjects;
133

  
128
	private String fileVersion;
134 129
	private DriverAttributes attr = new DriverAttributes();
135

  
136 130
	private boolean isInitialized = false;
131
	float heightText = 10;	
132
	
133
	
134
	private boolean debug = false;
135
	/**
136
	 * entities of the dwg file (once applied many transformation,
137
	 * including block insertion) that are IDwgToFMap.
138
	 * */
139
	ArrayList entities = new ArrayList();
140
	
141
	/**
142
	 * Saves an original DWG entity associated to a FMap entity
143
	 * by their index.
144
	 * Only available in debug mode (if debug is false, doesnt save anything)
145
	 * */
146
	private void saveEntity(DwgObject entity) {
147
		if(debug)
148
			entities.add(entity);
149
	}
150
	
151
	public DwgObject getEntity(int i){
152
		if(debug)
153
			return (DwgObject) entities.get(i);
154
		else
155
			return null;
156
	}
137 157

  
138 158
	/*
139 159
	 * (non-Javadoc)
140
	 *
160
	 * 
141 161
	 * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
142 162
	 */
143
	public void open(File f) {
163
	public void open(File f) throws IOException {
144 164
		m_Fich = f;
145 165
	}
146 166

  
147 167
	/**
148
	 * Allows recovering of the DWG Drawing entity
149
	 * associated to the FMap feature whose position
150
	 * is index
151
	 * @param index position of the fmap feature whose
152
	 * dwg entity is required
153
	 *
154
	 * */
155
	public Object getCadSource(int index){
156
		return dwg.getDwgObject(index);
168
	 * Allows recovering of the DWG Drawing entity associated to the FMap
169
	 * feature whose position is index.
170
	 * If debug mode is false, it always returns null.
171
	 * 
172
	 * @param index
173
	 *            position of the fmap feature whose dwg entity is required
174
	 * 
175
	 */
176
	public Object getCadSource(int index) {
177
		if(debug)
178
			return getEntity(index);
179
		else
180
			return null;
157 181
	}
182

  
158 183
	/**
159 184
	 * This method load the DWG file in memory. First, it will be necessary to
160 185
	 * create a DwgFile object with the DWG file path as the argument. Second,
......
166 191
	 * objects contained inside the blocks through the blockManagement method.
167 192
	 * And finally, we can read the objects Vector, and convert this objects to
168 193
	 * the FMap object model.
169
	 * @throws InitializeDriverException
170 194
	 */
171 195
	public void initialize() throws InitializeDriverException {
172 196
		if (isInitialized)
173 197
			return;
174 198
		else
175 199
			isInitialized = true;
176
		float heightText = 10;
177

  
178 200
		attr.setLoadedInMemory(true);
179
		dwg = new DwgFile(m_Fich.getAbsolutePath());
201
		DwgFile dwg = new DwgFile(m_Fich.getAbsolutePath());
180 202

  
181 203
		try {
182 204
			dwg.read();
......
204 226
			throw new InitializeDriverException(getName(),e);
205 227
		}
206 228

  
207
		// dwg.initializeLayerTable();
229
		fileVersion = dwg.getDwgVersion();
208 230
		dwg.calculateGisModelDwgPolylines();
209 231
		dwg.blockManagement2();
210
		dwgObjects = dwg.getDwgObjects();
211

  
232
		List dwgObjects = dwg.getDwgObjects();
233
		
234
		//Each dwg file has two headers vars, EXTMIN and EXTMAX
235
		//that marks bounds of the file.
236
		//Also, it could have spureus entities (deleted objects
237
		//dont removed from the DWG database)
238
		double[] extMin = (double[]) dwg.getHeader("MSPACE_EXTMIN");
239
		double[] extMax = (double[]) dwg.getHeader("MSPACE_EXTMAX");
240
		if(extMin != null && extMax != null){
241
			if(extMin.length >= 2 && extMax.length >= 2){
242
				double xmin = extMin[0];
243
				double ymin = extMin[1];
244
				double xmax = extMax[0];
245
				double ymax = extMax[1];
246
				Rectangle2D roi = new 
247
					Rectangle2D.Double(xmin-100, ymin-100, 
248
							(xmax-xmin)+100, (ymax-ymin)+100);
249
				addRegionOfInterest(roi);
250
			}
251
		}
212 252
		// Campos de las MemoryLayer:
213 253
		Value[] auxRow = new Value[10];
214 254
		ArrayList arrayFields = new ArrayList();
......
225 265
		getTableModel().setColumnIdentifiers(arrayFields.toArray());
226 266

  
227 267
		boolean is3dFile = dwg.isDwg3DFile();
228
		
229
		for (int i = 0; i < dwgObjects.size(); i++) {
230
			DwgObject entity = (DwgObject) dwgObjects.get(i);
268
		try {
269
			for (int i = 0; i < dwgObjects.size(); i++) {
270
				DwgObject entity = (DwgObject) dwgObjects.get(i);
231 271

  
232
			if(entity instanceof IDwg2FMap){
272
				if (entity instanceof IDwg2FMap) {
273
					IDwg2FMap dwgEnt = (IDwg2FMap) entity;
274
					IGeometry geometry = dwgEnt.toFMapGeometry(is3dFile);
275
					if (geometry == null)
276
						continue;
277
					//we check for Region of Interest of the CAD file
278
					if(! checkRois(geometry))
279
						continue;
280
					
281
					String fmapStr = dwgEnt.toFMapString(is3dFile);
282
					// nombre del tipo de geometria fmap
283
					auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(fmapStr);
284
					
285
					// indice del registro
286
					auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
287
					
288
					// nombre de entidad dwg
289
					auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(dwgEnt
290
							.toString());
291
					String layerName = dwg.getLayerName(entity);
292
					auxRow[ID_FIELD_LAYER] = ValueFactory
293
							.createValue(layerName);
294
					int color = entity.getColor();
295
					int colorByLayer = dwg.getColorByLayer(entity);
233 296

  
234
				IDwg2FMap dwgEnt = (IDwg2FMap)entity;
235
				//TODO Ojo, revisar las fshape a null
236
				FShape fshape = dwgEnt.toFMapGeometry(is3dFile);
237
				
238
				String fmapStr = dwgEnt.toFMapString(is3dFile);
239
				//nombre del tipo de geometria fmap
240
				auxRow[ID_FIELD_FSHAPE] = ValueFactory.createValue(fmapStr);
241
				//indice del registro
242
				auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
243
				//nombre de entidad dwg
244
				auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(dwgEnt.toString());
245
				String layerName = dwg.getLayerName(entity);
246
				auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
247
				int color = entity.getColor();
248
				int colorByLayer = dwg.getColorByLayer(entity);
297
					if (color == 256)
298
						color = colorByLayer;
299
					
300
					auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
249 301

  
250
				// TODO: if (color==0) color ByBlock
251
				if (color == 256)
252
					color = colorByLayer;
253
				auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
302
					auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
303
					auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
304
							.createValue(0.0);
305
					auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
306
					auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
254 307

  
255
				auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
256
				auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
257
				auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
258
				auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
308
					if (entity instanceof IDwg3DTestable) {
309
						auxRow[ID_FIELD_ELEVATION] = ValueFactory
310
								.createValue(((IDwg3DTestable) entity).getZ());
311
					}
259 312

  
260
				if(entity instanceof IDwg3DTestable){
261
					auxRow[ID_FIELD_ELEVATION] =
262
						ValueFactory.createValue(((IDwg3DTestable)entity).getZ());
313
					if (entity instanceof DwgMText) {
314
						DwgMText mtext = (DwgMText) entity;
315
						auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(mtext
316
								.getText());
317
						auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
318
								.createValue(mtext.getHeight());
319
					} else if (entity instanceof DwgText) {
320
						DwgText text = (DwgText) entity;
321
						auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(text
322
								.getText());
323
						auxRow[ID_FIELD_THICKNESS] = ValueFactory
324
								.createValue(text.getThickness());
325
						auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory
326
								.createValue(text.getHeight());
327
						auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory
328
								.createValue(Math.toDegrees(text
329
										.getRotationAngle()));
330
					}// if-else
331
					addGeometry(geometry, auxRow);
332
					
333
					if(debug)
334
						saveEntity(entity);
335

  
263 336
				}
337
			}// for
264 338

  
265
				if(entity instanceof DwgMText){
266
					DwgMText mtext = (DwgMText) entity;
267
					auxRow[ID_FIELD_TEXT] = ValueFactory.
268
								createValue(mtext.getText());
269
					auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.
270
								createValue(mtext.getHeight());
271
				}else if(entity instanceof DwgText){
272
					DwgText text = (DwgText) entity;
273
					auxRow[ID_FIELD_TEXT] = ValueFactory.
274
							createValue(text.getText());
275
					auxRow[ID_FIELD_THICKNESS] = ValueFactory.
276
							createValue(text.getThickness());
277
					auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.
278
							createValue(text.getHeight());
279
					auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.
280
							createValue(Math.toDegrees(text.getRotationAngle()));
281
				}//if-else
282
				addShape(fshape, auxRow);
283

  
284
			}//if instanceof
285
			else{
286
				
287
			}
288
		}//for
289

  
339
		} catch (Throwable t) {
340
			t.printStackTrace();
341
		}
342
		dwgObjects = null;
343
		dwg = null;
344
		if(! debug)
345
			dwg = null;
346
		System.gc();
347
		setSymbols();
348
	}
349
	
350
	/**
351
	 * Sets a symbol for each dwg entity's derived feature based in
352
	 * the DWG symbology info.
353
	 * */
354
	private void setSymbols() throws IOException{
290 355
		FSymbol myDefaultSymbol = new FSymbol(getShapeType());
291 356
		myDefaultSymbol.setShapeVisible(false);
292 357
		myDefaultSymbol.setFontSizeInPixels(false);
......
326 391
					theSymbol.setSize(3);
327 392
					theSymbol.setSizeInPixels(true);
328 393
					defaultLegend.addSymbol(clave, theSymbol);
329
				}//if
394
				}// if
330 395
			} // for
331 396
		} catch (ReadDriverException e) {
332 397
			throw new InitializeDriverException(getName(),e);
333 398
		}
334 399
	}
400
	
401
	/**
402
	 * checks if the given geometry intersects or its
403
	 * contained in one of the driver regions of interest.
404
	 * 
405
	 * @param geometry feature geometry we want to check
406
	 * against the drivers rois.
407
	 * 
408
	 *  @return true if feature is contained in any ROI.
409
	 *  false if not.
410
	 * */
411
	private boolean checkRois(IGeometry geometry){
412
		Rectangle2D rect = geometry.getBounds2D();
413
		int numRois = this.getNumOfRois();
414
		for(int i = 0; i < numRois; i++){
415
			Rectangle2D roi = getRegionOfInterest(i);
416
			if( checkIntersect(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
417
					roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY()) ||
418
				checkContains(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
419
						roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY())
420
			){
421
				return true;
422
			}
423
//			if(roi.intersects(rect) || roi.contains(rect))
424
//				return true;
425
		}
426
		return false;
427
	}
428
	
429
	private boolean checkContains(double x1, double x2, double y1, double y2, 
430
			double ax1, double ax2, double ay1, double ay2){
431
		 boolean solution = ( x1 >= ax1 &&
432
	        x2 <= ax2 &&
433
	        y1 >= ay1 &&
434
	        y2 <= ay2);
435
		 return solution;
436
	}
437
	
438
	private boolean checkIntersect(double x1, double x2, double y1, double y2, 
439
								double ax1, double ax2, double ay1, double ay2){
440
		
441
		 return !(x1 > ax2 ||
442
		        x2 < ax1 ||
443
		        y1 > ay2 ||
444
		        y2 < ay1);
445
		 
446
	}
335 447

  
336 448
	public String getFileVersion() {
337
		return dwg.getDwgVersion();
449
		return fileVersion;
338 450
	}
339 451

  
340 452
	private String formatString(String fmt, String[] params) {
......
345 457
		return ret;
346 458
	}
347 459

  
348

  
349

  
350

  
351

  
352

  
353
	/**
354
	 * Method that changes a Point2D array to a FPolygon2D. Is useful to convert
355
	 * a polygon given by it points to a FPolygon2D, a polygon in the FMap model
356
	 * object
357
	 *
358
	 * @param pts
359
	 *            Array of Point2D that defines the polygon that will be
360
	 *            converted in a FPolygon2D
361
	 * @return FPolygon2D This FPolygon2D is build using the array of Point2D
362
	 *         that is the argument of the method
363
	 */
364
	private FPolygon2D points2DToFPolygon2D(Point2D[] pts) {
365
		GeneralPathX genPathX = new GeneralPathX();
366
		genPathX.moveTo(pts[0].getX(), pts[0].getY());
367
		for (int i = 1; i < pts.length; i++) {
368
			genPathX.lineTo(pts[i].getX(), pts[i].getY());
369
		}
370
		genPathX.closePath();
371
		return new FPolygon2D(genPathX);
372
	}
373

  
460
	
374 461
	/*
375 462
	 * (non-Javadoc)
376
	 *
463
	 * 
377 464
	 * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
378 465
	 */
379 466
	public boolean accept(File f) {
......
382 469

  
383 470
	/*
384 471
	 * (non-Javadoc)
385
	 *
472
	 * 
386 473
	 * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
387 474
	 */
388 475
	public int getShapeType() {
......
391 478

  
392 479
	/*
393 480
	 * (non-Javadoc)
394
	 *
481
	 * 
395 482
	 * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
396 483
	 */
397 484
	public String getName() {
......
400 487

  
401 488
	/*
402 489
	 * (non-Javadoc)
403
	 *
490
	 * 
404 491
	 * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
405 492
	 */
406 493
	public Legend getDefaultLegend() {
......
409 496

  
410 497
	/*
411 498
	 * (non-Javadoc)
412
	 *
499
	 * 
413 500
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
414 501
	 */
415 502
	public DriverAttributes getDriverAttributes() {
......
418 505

  
419 506
	/*
420 507
	 * (non-Javadoc)
421
	 *
508
	 * 
422 509
	 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
423 510
	 */
424
	public int[] getPrimaryKeys() {
511
	public int[] getPrimaryKeys() throws DriverException {
425 512
		// TODO Auto-generated method stub
426 513
		return null;
427 514
	}
428 515

  
429 516
	/*
430 517
	 * (non-Javadoc)
431
	 *
518
	 * 
432 519
	 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
433 520
	 */
434
	public void write(DataWare arg0) {
521
	public void write(DataWare arg0) throws DriverException {
435 522
		// TODO Auto-generated method stub
436 523

  
437 524
	}
......
441 528

  
442 529
	}
443 530

  
444
	public void close() {
531
	public void close() throws IOException {
445 532
		// TODO Auto-generated method stub
446 533

  
447 534
	}
......
453 540
	public boolean isWritable() {
454 541
		return m_Fich.canWrite();
455 542
	}
543

  
544

  
545
	public boolean isDebug() {
546
		return debug;
547
	}
548

  
549

  
550
	public void setDebug(boolean debug) {
551
		this.debug = debug;
552
	}
456 553
}

Also available in: Unified diff