Revision 10848

View differences:

branches/v10/extensions/extDwg/src/com/iver/cit/gvsig/drivers/dwg/DwgMemoryDriver.java
46 46
*
47 47
* $Id$
48 48
* $Log$
49
* Revision 1.7.2.1  2007-02-28 07:35:06  jmvivo
49
* Revision 1.7.2.2  2007-03-21 19:54:22  azabala
50
* implementation of dwg 12, 13, 14.
51
*
52
* Revision 1.7.2.1  2007/02/28 07:35:06  jmvivo
50 53
* Actualizado desde el HEAD.
51 54
*
52 55
* Revision 1.6  2007/02/14 16:17:22  azabala
......
75 78
import java.awt.Color;
76 79
import java.awt.Font;
77 80
import java.awt.geom.Point2D;
81
import java.awt.geom.Rectangle2D;
78 82
import java.io.File;
79 83
import java.io.IOException;
80 84
import java.util.ArrayList;
......
90 94
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
91 95
import com.iver.cit.gvsig.fmap.core.FShape;
92 96
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
97
import com.iver.cit.gvsig.fmap.core.IGeometry;
93 98
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
94 99
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
95 100
import com.iver.cit.gvsig.fmap.drivers.AbstractCadMemoryDriver;
......
119 124
		VectorialFileDriver, WithDefaultLegend {
120 125

  
121 126
	VectorialUniqueValueLegend defaultLegend;
122

  
123 127
	private File m_Fich;
124

  
125
	private DwgFile dwg;
126

  
127
	private List dwgObjects;
128

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

  
131 130
	private boolean isInitialized = false;
132

  
131
	float heightText = 10;	
132
	private boolean debug = false;
133
	/**
134
	 * entities of the dwg file (once applied many transformation,
135
	 * including block insertion) that are IDwgToFMap.
136
	 * */
137
	ArrayList entities = new ArrayList();
138
	
139
	/**
140
	 * Saves an original DWG entity associated to a FMap entity
141
	 * by their index.
142
	 * Only available in debug mode (if debug is false, doesnt save anything)
143
	 * */
144
	private void saveEntity(DwgObject entity) {
145
		if(debug)
146
			entities.add(entity);
147
	}
148
	
149
	public DwgObject getEntity(int i){
150
		if(debug)
151
			return (DwgObject) entities.get(i);
152
		else
153
			return null;
154
	}
155
	
156
	
133 157
	/*
134 158
	 * (non-Javadoc)
135 159
	 * 
......
148 172
	 * 
149 173
	 * */
150 174
	public Object getCadSource(int index){
151
		return dwg.getDwgObject(index);
175
		if(debug)
176
			return getEntity(index);
177
		else
178
			return null;
152 179
	}
153 180
	/**
154 181
	 * This method load the DWG file in memory. First, it will be necessary to
......
167 194
			return;
168 195
		else
169 196
			isInitialized = true;
170
		float heightText = 10;
171

  
172 197
		attr.setLoadedInMemory(true);
173
		dwg = new DwgFile(m_Fich.getAbsolutePath());
174

  
198
		DwgFile dwg = new DwgFile(m_Fich.getAbsolutePath());
175 199
		try {
176 200
			dwg.read();
177 201
		} catch (DwgVersionNotSupportedException e1) {
......
187 211
									autodeskUrl }));
188 212
		}// catch
189 213

  
190
		// dwg.initializeLayerTable();
214
		fileVersion = dwg.getDwgVersion();
191 215
		dwg.calculateGisModelDwgPolylines();
192 216
		dwg.blockManagement2();
193
		dwgObjects = dwg.getDwgObjects();
194

  
217
		List dwgObjects = dwg.getDwgObjects();
218
		//Each dwg file has two headers vars, EXTMIN and EXTMAX
219
		//that marks bounds of the file.
220
		//Also, it could have spureus entities (deleted objects
221
		//dont removed from the DWG database)
222
		double[] extMin = (double[]) dwg.getHeader("MSPACE_EXTMIN");
223
		double[] extMax = (double[]) dwg.getHeader("MSPACE_EXTMAX");
224
		if(extMin != null && extMax != null){
225
			if(extMin.length >= 2 && extMax.length >= 2){
226
				double xmin = extMin[0];
227
				double ymin = extMin[1];
228
				double xmax = extMax[0];
229
				double ymax = extMax[1];
230
				Rectangle2D roi = new 
231
					Rectangle2D.Double(xmin-100, ymin-100, 
232
							(xmax-xmin)+100, (ymax-ymin)+100);
233
				addRegionOfInterest(roi);
234
			}
235
		}
236
		
195 237
		// Campos de las MemoryLayer:
196 238
		Value[] auxRow = new Value[10];
197 239
		ArrayList arrayFields = new ArrayList();
......
215 257
			if(entity instanceof IDwg2FMap){
216 258
				
217 259
				IDwg2FMap dwgEnt = (IDwg2FMap)entity;
218
				//TODO Ojo, revisar las fshape a null
219
				FShape fshape = dwgEnt.toFMapGeometry(is3dFile);
260
				IGeometry geometry = dwgEnt.toFMapGeometry(is3dFile);
261
				if (geometry == null)
262
					continue;
263
				//we check for Region of Interest of the CAD file
264
				if(! checkRois(geometry))
265
					continue;
220 266
				
221 267
				String fmapStr = dwgEnt.toFMapString(is3dFile);
222 268
				//nombre del tipo de geometria fmap
......
227 273
				auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(dwgEnt.toString());
228 274
				String layerName = dwg.getLayerName(entity);
229 275
				auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
276
				int colorByLayer = dwg.getColorByLayer(entity);
230 277
				int color = entity.getColor();
231
				int colorByLayer = dwg.getColorByLayer(entity);
278
				if(color < 0)
279
					color = Math.abs(color);
280
				if(color > 255)
281
					color = colorByLayer;
282
				// TODO: if (color==0) color ByBlock
232 283
				
233
				// TODO: if (color==0) color ByBlock
234
				if (color == 256)
235
					color = colorByLayer;
236 284
				auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
237 285
				
238 286
				auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0.0);
......
262 310
					auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.
263 311
							createValue(text.getRotationAngle());
264 312
				}//if-else
265
				addShape(fshape, auxRow);
313
				addGeometry(geometry, auxRow);
314
				if(debug)
315
					saveEntity(entity);
266 316
				
267 317
			}//if instanceof
268
			else{
269
				
270
			}
271 318
		}//for
319
		setSymbols();
272 320
		
321
	}
322
	
323
	/**
324
	 * Sets a symbol for each dwg entity's derived feature based in
325
	 * the DWG symbology info.
326
	 * @throws IOException 
327
	 * @throws DriverException 
328
	 * */
329
	private void setSymbols() throws IOException {
273 330
		FSymbol myDefaultSymbol = new FSymbol(getShapeType());
274 331
		myDefaultSymbol.setShapeVisible(false);
275 332
		myDefaultSymbol.setFontSizeInPixels(false);
......
279 336
		myDefaultSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
280 337
		myDefaultSymbol.setSize(3);
281 338
		myDefaultSymbol.setSizeInPixels(true);
282
		
339

  
283 340
		defaultLegend = LegendFactory
284 341
				.createVectorialUniqueValueLegend(getShapeType());
285 342
		defaultLegend.setFieldName("Color");
......
309 366
					theSymbol.setSize(3);
310 367
					theSymbol.setSizeInPixels(true);
311 368
					defaultLegend.addSymbol(clave, theSymbol);
312
				}//if
313
			} // for
369
				}// if
370
			}
314 371
		} catch (DriverException e) {
315
			e.printStackTrace();
316
			throw new IOException(
317
					"Error al poner la leyenda por defecto en el Dwg");
372
			throw new IOException("Error durante la asignacion de simbolos a los features dwg");
373
		} // for
374
		
375
	}
376
	
377
	/**
378
	 * checks if the given geometry intersects or its
379
	 * contained in one of the driver regions of interest.
380
	 * 
381
	 * @param geometry feature geometry we want to check
382
	 * against the drivers rois.
383
	 * 
384
	 *  @return true if feature is contained in any ROI.
385
	 *  false if not.
386
	 * */
387
	private boolean checkRois(IGeometry geometry){
388
		Rectangle2D rect = geometry.getBounds2D();
389
		int numRois = this.getNumOfRois();
390
		for(int i = 0; i < numRois; i++){
391
			Rectangle2D roi = getRegionOfInterest(i);
392
			if( checkIntersect(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
393
					roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY()) ||
394
				checkContains(rect.getMinX(), rect.getMaxX(), rect.getMinY(), rect.getMaxY(),
395
						roi.getMinX(), roi.getMaxX(), roi.getMinY(),roi.getMaxY())
396
			){
397
				return true;
398
			}
399
//			if(roi.intersects(rect) || roi.contains(rect))
400
//				return true;
318 401
		}
402
		return false;
319 403
	}
404
	//TODO Los metodos de java.awt.geom.Rectangle2D estaban
405
	//dando problemas. REVISAR.
406
	private boolean checkContains(double x1, double x2, double y1, double y2, 
407
			double ax1, double ax2, double ay1, double ay2){
408
		 boolean solution = ( x1 >= ax1 &&
409
	        x2 <= ax2 &&
410
	        y1 >= ay1 &&
411
	        y2 <= ay2);
412
		 return solution;
413
	}
414
	
415
	private boolean checkIntersect(double x1, double x2, double y1, double y2, 
416
								double ax1, double ax2, double ay1, double ay2){
417
		
418
		 return !(x1 > ax2 ||
419
		        x2 < ax1 ||
420
		        y1 > ay2 ||
421
		        y2 < ay1);
422
		 
423
	}
320 424

  
321 425
	public String getFileVersion() {
322
		return dwg.getDwgVersion();
426
		return fileVersion;
323 427
	}
324 428

  
325 429
	private String formatString(String fmt, String[] params) {
......
330 434
		return ret;
331 435
	}
332 436

  
333
	
334
	
335

  
336
	
337

  
338 437
	/**
339 438
	 * Method that changes a Point2D array to a FPolygon2D. Is useful to convert
340 439
	 * a polygon given by it points to a FPolygon2D, a polygon in the FMap model
......
438 537
	public boolean isWritable() {
439 538
		return m_Fich.canWrite();
440 539
	}
540
	
541
	public boolean isDebug() {
542
		return debug;
543
	}
544

  
545

  
546
	public void setDebug(boolean debug) {
547
		this.debug = debug;
548
	}
441 549
}

Also available in: Unified diff