svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / dxf / DXFFeaturesWriter.java @ 20414
History | View | Annotate | Download (29.2 KB)
1 | 19844 | vcaballero | package org.gvsig.data.datastores.vectorial.file.dxf; |
---|---|---|---|
2 | 19401 | vcaballero | |
3 | 19603 | vcaballero | import java.awt.geom.PathIterator; |
4 | import java.awt.geom.Point2D; |
||
5 | import java.io.File; |
||
6 | import java.io.IOException; |
||
7 | 20414 | vcaballero | import java.util.Iterator; |
8 | 19603 | vcaballero | import java.util.Vector; |
9 | |||
10 | import org.cresques.cts.IProjection; |
||
11 | 19613 | vcaballero | import org.cresques.geo.Point3D; |
12 | 19603 | vcaballero | import org.cresques.io.DxfFile; |
13 | import org.cresques.io.DxfGroup; |
||
14 | import org.cresques.io.DxfGroupVector; |
||
15 | import org.cresques.px.dxf.DxfEntityMaker; |
||
16 | 19844 | vcaballero | import org.gvsig.data.datastores.vectorial.IFeaturesWriter; |
17 | 19603 | vcaballero | import org.gvsig.data.exception.InitializeWriterException; |
18 | 19675 | vcaballero | import org.gvsig.data.exception.OpenException; |
19 | 20130 | jmvivo | import org.gvsig.data.exception.ReadException; |
20 | 19603 | vcaballero | import org.gvsig.data.exception.WriteException; |
21 | 19401 | vcaballero | import org.gvsig.data.vectorial.IFeature; |
22 | 19603 | vcaballero | import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
23 | 19736 | vcaballero | import org.gvsig.data.vectorial.IFeatureStore; |
24 | 19401 | vcaballero | import org.gvsig.data.vectorial.IFeatureType; |
25 | |||
26 | 19603 | vcaballero | import com.iver.cit.gvsig.fmap.core.FArc2D; |
27 | import com.iver.cit.gvsig.fmap.core.FCircle2D; |
||
28 | import com.iver.cit.gvsig.fmap.core.FEllipse2D; |
||
29 | import com.iver.cit.gvsig.fmap.core.FGeometry; |
||
30 | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
||
31 | import com.iver.cit.gvsig.fmap.core.FPoint3D; |
||
32 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
33 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
34 | import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
||
35 | import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
||
36 | |||
37 | 19401 | vcaballero | public class DXFFeaturesWriter implements IFeaturesWriter { |
38 | 19603 | vcaballero | private String DEFAULT_LAYER="default"; |
39 | private Integer DEFAULT_COLOR=new Integer(0); |
||
40 | private Double DEFAULT_ELEVATION=new Double(0); |
||
41 | private Double DEFAULT_THICKNESS=new Double(0); |
||
42 | private String DEFAULT_TEXT=""; |
||
43 | private Double DEFAULT_HEIGHTTEXT=new Double(10); |
||
44 | private Double DEFAULT_ROTATIONTEXT=new Double(0); |
||
45 | 19401 | vcaballero | |
46 | 19675 | vcaballero | // private DxfFieldsMapping fieldsMapping = null;
|
47 | 19603 | vcaballero | |
48 | private File file; |
||
49 | |||
50 | private DxfFile.EntityFactory entityMaker;
|
||
51 | |||
52 | private IProjection proj = null; |
||
53 | |||
54 | private DxfFile dxfFile;
|
||
55 | |||
56 | int handle = 40; // Revisar porqu? es 40. |
||
57 | |||
58 | int k = 0; |
||
59 | |||
60 | boolean dxf3DFile = false; |
||
61 | private int indexText=-1;//En DXF es 7. |
||
62 | private int indexHeightText=-1;//En DXF es 8. |
||
63 | private int indexRotationText=-1;//En DXF es 9. |
||
64 | private IFeatureType featureType;
|
||
65 | |||
66 | 19736 | vcaballero | public void init(IFeatureStore store) { |
67 | DXFStoreParameters dxfParameters=(DXFStoreParameters)store.getParameters(); |
||
68 | 19603 | vcaballero | file=dxfParameters.getDXFFile(); |
69 | proj=dxfParameters.getProjection(); |
||
70 | 19736 | vcaballero | featureType=store.getDefaultFeatureType(); |
71 | 19675 | vcaballero | // fieldsMapping = new DxfFieldsMapping();
|
72 | // fieldsMapping.setLayerField("Layer");
|
||
73 | // fieldsMapping.setColorField("Color");
|
||
74 | // fieldsMapping.setElevationField("Elevation");
|
||
75 | // fieldsMapping.setThicknessField("Thickness");
|
||
76 | // fieldsMapping.setTextField("Text");
|
||
77 | // fieldsMapping.setHeightText("HeightText");
|
||
78 | // fieldsMapping.setRotationText("Layer");
|
||
79 | 19603 | vcaballero | } |
80 | 19401 | vcaballero | |
81 | 19603 | vcaballero | public void postProcess() throws WriteException { |
82 | // Escribimos realmente lo que hemos montado en memoria.
|
||
83 | dxfFile = new DxfFile(null, file.getAbsolutePath(), entityMaker); |
||
84 | dxfFile.setCadFlag(true);
|
||
85 | if (dxf3DFile)
|
||
86 | dxfFile.setDxf3DFlag(true);
|
||
87 | try {
|
||
88 | dxfFile.save(file.getAbsolutePath()); |
||
89 | } catch (IOException e) { |
||
90 | throw new WriteException("DXF Writer",e); |
||
91 | } |
||
92 | 19401 | vcaballero | } |
93 | |||
94 | 20130 | jmvivo | public void preProcess() throws InitializeWriterException, ReadException { |
95 | 19675 | vcaballero | // if (fieldsMapping == null) {
|
96 | // throw new InitializeWriterException ("DXF Writer",null);
|
||
97 | // }
|
||
98 | 19603 | vcaballero | // NOTA: La proyecci?n no se usa absolutamente para nada (al menos
|
99 | // por ahora). Las entidades se escribir?n con las coordenadas con
|
||
100 | // las que se crean.
|
||
101 | // if (proj == null) {
|
||
102 | // throw new StartWriterVisitorException (getName(),null);
|
||
103 | // }
|
||
104 | 19401 | vcaballero | |
105 | 19603 | vcaballero | entityMaker = new DxfEntityMaker(proj);
|
106 | // FieldDescription[] fieldDescriptions=getTableDefinition().getFieldsDesc();
|
||
107 | 20414 | vcaballero | Iterator iterator=featureType.iterator();
|
108 | int i=0; |
||
109 | while (iterator.hasNext()) {
|
||
110 | IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next(); |
||
111 | 19603 | vcaballero | if (fad.getName().equals("Text")){ |
112 | indexText=i; |
||
113 | }else if (fad.getName().equals("HeightText")){ |
||
114 | indexHeightText=i; |
||
115 | }else if (fad.getName().equals("RotationText")){ |
||
116 | indexRotationText=i; |
||
117 | } |
||
118 | 20414 | vcaballero | i++; |
119 | 19603 | vcaballero | } |
120 | 19401 | vcaballero | } |
121 | |||
122 | 19603 | vcaballero | public void insertFeature(IFeature feature) throws WriteException { |
123 | try {
|
||
124 | IGeometry geom = (IGeometry)feature.getDefaultGeometry(); |
||
125 | // Value[] values=row.getAttributes();
|
||
126 | // TODO: Tratamiento de los campos
|
||
127 | // y modificar los createXXX para que acepten como par?metro
|
||
128 | // los datos de LAYER, COLOR, ELEVATION, THICKNESS, TEXT
|
||
129 | // HEIGHTTEXT, ROTATIONTEXT y el resto que puedan hacer
|
||
130 | // falta.
|
||
131 | |||
132 | if (geom.getGeometryType()==FShape.POINT) {
|
||
133 | k=createPoint2D(handle, k, feature); |
||
134 | } else if (geom.getGeometryType()==(FShape.POINT | FShape.Z)) { |
||
135 | dxf3DFile = true;
|
||
136 | k=createPoint3D(handle, k, feature); |
||
137 | } else if (geom.getGeometryType()==FShape.LINE) { |
||
138 | k=createLwPolyline2D(handle, k, feature, false);
|
||
139 | } else if (geom.getGeometryType()==(FShape.LINE | FShape.Z)) { |
||
140 | dxf3DFile = true;
|
||
141 | k = createPolyline3D(handle, k, feature); |
||
142 | } else if (geom.getGeometryType()==FShape.POLYGON) { |
||
143 | // createPolygon2D(handle, k, geom);
|
||
144 | k=createLwPolyline2D(handle, k, feature, true);
|
||
145 | } else if (geom.getGeometryType()==(FShape.POLYGON | FShape.Z)) { |
||
146 | dxf3DFile = true;
|
||
147 | k = createPolyline3D(handle, k, feature); |
||
148 | // k = createPolygon3D(handle, k, geom);
|
||
149 | } else if (geom.getGeometryType()==FShape.CIRCLE) { |
||
150 | // FCircle2D circle = (FCircle2D) geom.getInternalShape();
|
||
151 | k=createCircle2D(handle, k, feature); |
||
152 | } else if (geom.getGeometryType()==FShape.ARC) { |
||
153 | // FArc2D arc = (FArc2D) geom.getInternalShape();
|
||
154 | k=createArc2D(handle, k, feature); |
||
155 | } else if (geom.getGeometryType()==FShape.ELLIPSE) { |
||
156 | // FEllipse2D ellipse = (FEllipse2D) geom.getInternalShape();
|
||
157 | k=createEllipse2D(handle, k, feature); |
||
158 | } else {
|
||
159 | System.out.println("IGeometry not supported yet"); |
||
160 | k++; |
||
161 | } |
||
162 | } |
||
163 | catch(Exception e){ |
||
164 | throw new WriteException("DXF Writer",e); |
||
165 | } |
||
166 | |||
167 | |||
168 | } |
||
169 | public void updateFeatureType(IFeatureType featureType) { |
||
170 | 19401 | vcaballero | // TODO Ap?ndice de m?todo generado autom?ticamente
|
171 | |||
172 | } |
||
173 | |||
174 | |||
175 | 19603 | vcaballero | private int createPoint2D(int handle, int k, IFeature feature) throws Exception { |
176 | |||
177 | if (indexText!= -1 && !(feature.get(indexText) == null)){ |
||
178 | return createText2D(handle,k,feature);
|
||
179 | } |
||
180 | FPoint2D point = new FPoint2D(0, 0); |
||
181 | double[] pointCoords = new double[6]; |
||
182 | PathIterator pointIt = ((IGeometry)feature.getDefaultGeometry()).getPathIterator(null); |
||
183 | while (!pointIt.isDone()) {
|
||
184 | pointIt.currentSegment(pointCoords); |
||
185 | point = new FPoint2D(pointCoords[0], pointCoords[1]); |
||
186 | pointIt.next(); |
||
187 | } |
||
188 | Point2D pto = new Point2D.Double(point.getX(), point.getY()); |
||
189 | |||
190 | DxfGroup px = new DxfGroup();
|
||
191 | DxfGroup py = new DxfGroup();
|
||
192 | DxfGroup pz = new DxfGroup();
|
||
193 | px.setCode(10);
|
||
194 | px.setData(new Double(pto.getX())); |
||
195 | py.setCode(20);
|
||
196 | py.setData(new Double(pto.getY())); |
||
197 | pz.setCode(30);
|
||
198 | // POINT del DXF tiene cota. Le asigno cero arbitrariamente.
|
||
199 | pz.setData(new Double(0.0)); |
||
200 | DxfGroupVector pv = updateProperties(feature,k); |
||
201 | // pv.add(pointLayer);
|
||
202 | // pv.add(handleGroup);
|
||
203 | // pv.add(handleColor);
|
||
204 | // pv.add(handleElevation);
|
||
205 | // pv.add(handleThickness);
|
||
206 | pv.add(px); |
||
207 | pv.add(py); |
||
208 | pv.add(pz); |
||
209 | entityMaker.createPoint(pv); |
||
210 | k++; |
||
211 | return k;
|
||
212 | 19401 | vcaballero | } |
213 | 19603 | vcaballero | private int createText2D(int handle, int k, IFeature feature) throws Exception { |
214 | String text=DEFAULT_TEXT;
|
||
215 | Double heightText=DEFAULT_HEIGHTTEXT;
|
||
216 | Double rotationText=DEFAULT_ROTATIONTEXT;
|
||
217 | 19401 | vcaballero | |
218 | 19603 | vcaballero | if (indexText!=-1 && !(feature.get(indexText) == null)) { |
219 | text = feature.get(indexText).toString(); |
||
220 | } |
||
221 | if (indexHeightText!=-1 && !(feature.get(indexHeightText) instanceof Number)) { |
||
222 | heightText = new Double(((Number) feature.get(indexHeightText)) |
||
223 | .doubleValue()); |
||
224 | } |
||
225 | if (indexRotationText!=-1 && !(feature.get(indexRotationText) instanceof Number)) { |
||
226 | rotationText = new Double(((Number) feature.get(indexRotationText)) |
||
227 | .doubleValue()); |
||
228 | } |
||
229 | DxfGroup handleText = new DxfGroup();
|
||
230 | handleText.setCode(1);
|
||
231 | handleText.setData(text); |
||
232 | 19401 | vcaballero | |
233 | 19603 | vcaballero | DxfGroup handleHeightText = new DxfGroup();
|
234 | handleHeightText.setCode(40);
|
||
235 | handleHeightText.setData(heightText); |
||
236 | |||
237 | DxfGroup handleRotationText = new DxfGroup();
|
||
238 | handleRotationText.setCode(50);
|
||
239 | handleRotationText.setData(rotationText); |
||
240 | |||
241 | // DxfGroup handleColor = new DxfGroup();
|
||
242 | // handleColor.setCode(62);
|
||
243 | // handleColor.setData(color);
|
||
244 | |||
245 | // DxfGroup handleElevation = new DxfGroup();
|
||
246 | // handleElevation.setCode(38);
|
||
247 | // handleElevation.setData(elevation);
|
||
248 | |||
249 | // DxfGroup handleThickness = new DxfGroup();
|
||
250 | // handleThickness.setCode(39);
|
||
251 | // handleThickness.setData(thickness);
|
||
252 | |||
253 | |||
254 | FPoint2D point = new FPoint2D(0, 0); |
||
255 | double[] pointCoords = new double[6]; |
||
256 | PathIterator pointIt = ((IGeometry)feature.getDefaultGeometry()).getPathIterator(null); |
||
257 | while (!pointIt.isDone()) {
|
||
258 | pointIt.currentSegment(pointCoords); |
||
259 | point = new FPoint2D(pointCoords[0], pointCoords[1]); |
||
260 | pointIt.next(); |
||
261 | } |
||
262 | Point2D pto = new Point2D.Double(point.getX(), point.getY()); |
||
263 | // DxfGroup pointLayer = new DxfGroup(8, layer);
|
||
264 | DxfGroup handleGroup = new DxfGroup();
|
||
265 | handleGroup.setCode(5);
|
||
266 | handleGroup.setData(new Integer(handle + k).toString()); |
||
267 | DxfGroup px = new DxfGroup();
|
||
268 | DxfGroup py = new DxfGroup();
|
||
269 | DxfGroup pz = new DxfGroup();
|
||
270 | px.setCode(10);
|
||
271 | px.setData(new Double(pto.getX())); |
||
272 | py.setCode(20);
|
||
273 | py.setData(new Double(pto.getY())); |
||
274 | pz.setCode(30);
|
||
275 | // POINT del DXF tiene cota. Le asigno cero arbitrariamente.
|
||
276 | pz.setData(new Double(0.0)); |
||
277 | DxfGroupVector pv = updateProperties(feature,k); |
||
278 | // pv.add(pointLayer);
|
||
279 | // pv.add(handleColor);
|
||
280 | // pv.add(handleElevation);
|
||
281 | // pv.add(handleThickness);
|
||
282 | pv.add(handleText); |
||
283 | pv.add(handleHeightText); |
||
284 | pv.add(handleRotationText); |
||
285 | pv.add(handleGroup); |
||
286 | pv.add(px); |
||
287 | pv.add(py); |
||
288 | pv.add(pz); |
||
289 | entityMaker.createText(pv); |
||
290 | k++; |
||
291 | return k;
|
||
292 | 19401 | vcaballero | } |
293 | 19603 | vcaballero | private DxfGroupVector updateProperties(IFeature feature,int k){ |
294 | DxfGroupVector polv = new DxfGroupVector();
|
||
295 | String layer=DEFAULT_LAYER;
|
||
296 | Integer color=DEFAULT_COLOR;
|
||
297 | // Double elevation=DEFAULT_ELEVATION;
|
||
298 | Double thickness=DEFAULT_THICKNESS;
|
||
299 | if (featureType.size() > 6) { |
||
300 | 20414 | vcaballero | IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get("Layer");
|
301 | // IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get(3);
|
||
302 | if (fad!=null && |
||
303 | 19603 | vcaballero | !(feature.get(3) == null) && |
304 | feature.get(3) instanceof String) { |
||
305 | layer = feature.get(3).toString();
|
||
306 | } |
||
307 | 20414 | vcaballero | fad=(IFeatureAttributeDescriptor)featureType.get("Color");
|
308 | // fad=(IFeatureAttributeDescriptor)featureType.get(4);
|
||
309 | if (fad!=null |
||
310 | 19603 | vcaballero | && !(feature.get(4) == null) && |
311 | feature.get(4) instanceof Number) { |
||
312 | color = new Integer(((Number) feature.get(4)).intValue()); |
||
313 | } |
||
314 | // if (!(values[5] instanceof NullValue)){
|
||
315 | // elevation= new Double(((NumericValue)values[5]).doubleValue());
|
||
316 | // }
|
||
317 | 20414 | vcaballero | fad=(IFeatureAttributeDescriptor)featureType.get("Thickness");
|
318 | // fad=(IFeatureAttributeDescriptor)featureType.get(6);
|
||
319 | if (fad!=null && |
||
320 | 19603 | vcaballero | !(feature.get(6) == null) && |
321 | feature.get(6) instanceof Number) { |
||
322 | thickness = new Double(((Number) feature.get(6)).doubleValue()); |
||
323 | } |
||
324 | } |
||
325 | DxfGroup geometryLayer = new DxfGroup(8, layer); |
||
326 | 19401 | vcaballero | |
327 | 19603 | vcaballero | DxfGroup handleGroup = new DxfGroup();
|
328 | handleGroup.setCode(5);
|
||
329 | handleGroup.setData(new Integer(handle + k).toString()); |
||
330 | |||
331 | DxfGroup handleColor = new DxfGroup();
|
||
332 | handleColor.setCode(62);
|
||
333 | handleColor.setData(color); |
||
334 | |||
335 | // DxfGroup handleElevation = new DxfGroup();
|
||
336 | // handleElevation.setCode(38);
|
||
337 | // handleElevation.setData(elevation);
|
||
338 | |||
339 | DxfGroup handleThickness = new DxfGroup();
|
||
340 | handleThickness.setCode(39);
|
||
341 | handleThickness.setData(thickness); |
||
342 | |||
343 | |||
344 | polv.add(geometryLayer); |
||
345 | // polv.add(handleElevation);
|
||
346 | polv.add(handleGroup); |
||
347 | polv.add(handleColor); |
||
348 | return polv;
|
||
349 | 19401 | vcaballero | } |
350 | 19603 | vcaballero | private int createPoint3D(int handle, int k, IFeature feature) throws Exception { |
351 | if (indexText!=-1 && !(feature.get(indexText) == null)){ |
||
352 | return createText3D(handle,k,feature);
|
||
353 | } |
||
354 | FPoint3D point = new FPoint3D(0, 0, 0); |
||
355 | double[] pointCoords = new double[6]; |
||
356 | PathIterator pointIt = ((IGeometry)feature.getDefaultGeometry()).getPathIterator(null); |
||
357 | while (!pointIt.isDone()) {
|
||
358 | pointIt.currentSegment(pointCoords); |
||
359 | point = new FPoint3D(pointCoords[0], pointCoords[1], pointCoords[2]); |
||
360 | pointIt.next(); |
||
361 | } |
||
362 | Point3D pto = new Point3D(point.getX(), point.getY(), point.getZs()[0]); |
||
363 | // DxfGroup pointLayer = new DxfGroup(8, "default");
|
||
364 | // DxfGroup handleGroup = new DxfGroup();
|
||
365 | // handleGroup.setCode(5);
|
||
366 | // handleGroup.setData(new Integer(handle + k).toString());
|
||
367 | DxfGroup px = new DxfGroup();
|
||
368 | DxfGroup py = new DxfGroup();
|
||
369 | DxfGroup pz = new DxfGroup();
|
||
370 | px.setCode(10);
|
||
371 | px.setData(new Double(pto.getX())); |
||
372 | py.setCode(20);
|
||
373 | py.setData(new Double(pto.getY())); |
||
374 | pz.setCode(30);
|
||
375 | pz.setData(new Double(pto.getZ())); |
||
376 | double[] velev = ((FGeometry) feature.getDefaultGeometry()).getZs(); |
||
377 | Double elevation= DEFAULT_ELEVATION;
|
||
378 | elevation = new Double(velev[0]); |
||
379 | DxfGroup handleElevation = new DxfGroup();
|
||
380 | handleElevation.setCode(38);
|
||
381 | handleElevation.setData(elevation); |
||
382 | 19401 | vcaballero | |
383 | 19603 | vcaballero | DxfGroupVector pv = updateProperties(feature,k); |
384 | pv.add(handleElevation); |
||
385 | // pv.add(pointLayer);
|
||
386 | // pv.add(handleGroup);
|
||
387 | pv.add(px); |
||
388 | pv.add(py); |
||
389 | pv.add(pz); |
||
390 | entityMaker.createPoint(pv); |
||
391 | k++; |
||
392 | return k;
|
||
393 | 19401 | vcaballero | } |
394 | 19603 | vcaballero | private int createText3D(int handle, int k, IFeature feature) throws Exception { |
395 | Double elevation = DEFAULT_ELEVATION;
|
||
396 | String text = DEFAULT_TEXT;
|
||
397 | Double heightText = DEFAULT_HEIGHTTEXT;
|
||
398 | Double rotationText = DEFAULT_ROTATIONTEXT;
|
||
399 | 19401 | vcaballero | |
400 | 19603 | vcaballero | double[] velev = ((FGeometry) feature.getDefaultGeometry()).getZs(); |
401 | elevation = new Double(velev[0]); |
||
402 | |||
403 | if (indexText!=-1 && !(feature.get(indexText) == null)) { |
||
404 | text = feature.get(indexText).toString(); |
||
405 | } |
||
406 | if (indexHeightText!=-1 && feature.get(indexHeightText) instanceof Number) { |
||
407 | heightText = new Double(((Number) feature.get(indexHeightText)).doubleValue()); |
||
408 | } |
||
409 | if (indexRotationText!=-1 && feature.get(indexRotationText) instanceof Number) { |
||
410 | rotationText = new Double(((Number) feature.get(indexRotationText)).doubleValue()); |
||
411 | } |
||
412 | |||
413 | DxfGroup handleText = new DxfGroup();
|
||
414 | handleText.setCode(1);
|
||
415 | handleText.setData(text); |
||
416 | |||
417 | DxfGroup handleHeightText = new DxfGroup();
|
||
418 | handleHeightText.setCode(40);
|
||
419 | handleHeightText.setData(heightText); |
||
420 | |||
421 | DxfGroup handleRotationText = new DxfGroup();
|
||
422 | handleRotationText.setCode(50);
|
||
423 | handleRotationText.setData(rotationText); |
||
424 | |||
425 | // DxfGroup handleColor = new DxfGroup();
|
||
426 | // handleColor.setCode(62);
|
||
427 | // handleColor.setData(color);
|
||
428 | |||
429 | DxfGroup handleElevation = new DxfGroup();
|
||
430 | handleElevation.setCode(38);
|
||
431 | handleElevation.setData(elevation); |
||
432 | |||
433 | // DxfGroup handleThickness = new DxfGroup();
|
||
434 | // handleThickness.setCode(39);
|
||
435 | // handleThickness.setData(thickness);
|
||
436 | |||
437 | // FPoint3D point = new FPoint3D(0, 0, 0);
|
||
438 | // double[] pointCoords = new double[6];
|
||
439 | FPoint3D point = (FPoint3D)((IGeometry)feature.getDefaultGeometry()).getInternalShape(); |
||
440 | // while (!pointIt.isDone()) {
|
||
441 | // pointIt.currentSegment(pointCoords);
|
||
442 | // point = new FPoint3D(pointCoords[0], pointCoords[1]);
|
||
443 | // pointIt.next();
|
||
444 | // }
|
||
445 | // Point2D pto = new Point2D.Double(point.getX(), point.getY());
|
||
446 | // DxfGroup pointLayer = new DxfGroup(8, layer);
|
||
447 | DxfGroup handleGroup = new DxfGroup();
|
||
448 | handleGroup.setCode(5);
|
||
449 | handleGroup.setData(new Integer(handle + k).toString()); |
||
450 | DxfGroup px = new DxfGroup();
|
||
451 | DxfGroup py = new DxfGroup();
|
||
452 | DxfGroup pz = new DxfGroup();
|
||
453 | px.setCode(10);
|
||
454 | px.setData(new Double(point.getX())); |
||
455 | py.setCode(20);
|
||
456 | py.setData(new Double(point.getY())); |
||
457 | pz.setCode(30);
|
||
458 | pz.setData(new Double(point.getZs()[0])); |
||
459 | DxfGroupVector pv = updateProperties(feature,k); |
||
460 | // pv.add(pointLayer);
|
||
461 | // pv.add(handleColor);
|
||
462 | pv.add(handleElevation); |
||
463 | // pv.add(handleThickness);
|
||
464 | pv.add(handleText); |
||
465 | pv.add(handleHeightText); |
||
466 | pv.add(handleRotationText); |
||
467 | pv.add(handleGroup); |
||
468 | pv.add(px); |
||
469 | pv.add(py); |
||
470 | pv.add(pz); |
||
471 | entityMaker.createText(pv); |
||
472 | k++; |
||
473 | return k;
|
||
474 | 19401 | vcaballero | } |
475 | 19603 | vcaballero | private int createLwPolyline2D(int handle, int k, IFeature feature, boolean isPolygon) |
476 | throws Exception { |
||
477 | boolean first=true; |
||
478 | DxfGroupVector polv = updateProperties(feature,k); |
||
479 | Vector vpoints = new Vector(); |
||
480 | 19401 | vcaballero | |
481 | 19603 | vcaballero | DxfGroup polylineFlag = new DxfGroup();
|
482 | polylineFlag.setCode(70);
|
||
483 | if (isPolygon)
|
||
484 | polylineFlag.setData(new Integer(1)); // cerrada |
||
485 | else
|
||
486 | polylineFlag.setData(new Integer(0)); // abierta |
||
487 | 19401 | vcaballero | |
488 | 19603 | vcaballero | PathIterator theIterator = ((IGeometry)feature.getDefaultGeometry()).getPathIterator(null, FConverter.FLATNESS); // polyLine.getPathIterator(null, |
489 | // flatness);
|
||
490 | 19401 | vcaballero | |
491 | 19603 | vcaballero | double[] theData = new double[6]; |
492 | while (!theIterator.isDone()) {
|
||
493 | int theType = theIterator.currentSegment(theData);
|
||
494 | switch (theType) {
|
||
495 | case PathIterator.SEG_MOVETO: |
||
496 | if (!first)
|
||
497 | { |
||
498 | for (int j = 0; j < vpoints.size(); j++) { |
||
499 | DxfGroup xvertex = new DxfGroup();
|
||
500 | xvertex.setCode(10);
|
||
501 | xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX())); |
||
502 | DxfGroup yvertex = new DxfGroup();
|
||
503 | yvertex.setCode(20);
|
||
504 | yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY())); |
||
505 | polv.add(xvertex); |
||
506 | polv.add(yvertex); |
||
507 | } |
||
508 | |||
509 | entityMaker.createLwPolyline(polv); |
||
510 | k++; |
||
511 | polv = updateProperties(feature,k); |
||
512 | |||
513 | } |
||
514 | first=false;
|
||
515 | polv.add(polylineFlag); |
||
516 | vpoints.clear(); |
||
517 | vpoints.add(new FPoint2D(theData[0], theData[1])); |
||
518 | break;
|
||
519 | case PathIterator.SEG_LINETO: |
||
520 | vpoints.add(new FPoint2D(theData[0], theData[1])); |
||
521 | break;
|
||
522 | case PathIterator.SEG_QUADTO: |
||
523 | // vpoints.add(new FPoint2D(theData[0], theData[1]));
|
||
524 | // vpoints.add(new FPoint2D(theData[2], theData[3]));
|
||
525 | break;
|
||
526 | case PathIterator.SEG_CUBICTO: |
||
527 | // vpoints.add(new FPoint2D(theData[0], theData[1]));
|
||
528 | // vpoints.add(new FPoint2D(theData[2], theData[3]));
|
||
529 | // vpoints.add(new FPoint2D(theData[4], theData[5]));
|
||
530 | break;
|
||
531 | case PathIterator.SEG_CLOSE: |
||
532 | polylineFlag.setData(new Integer(1)); // cerrada |
||
533 | break;
|
||
534 | |||
535 | } |
||
536 | theIterator.next(); |
||
537 | } |
||
538 | |||
539 | for (int j = 0; j < vpoints.size(); j++) { |
||
540 | DxfGroup xvertex = new DxfGroup();
|
||
541 | xvertex.setCode(10);
|
||
542 | xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX())); |
||
543 | DxfGroup yvertex = new DxfGroup();
|
||
544 | yvertex.setCode(20);
|
||
545 | yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY())); |
||
546 | polv.add(xvertex); |
||
547 | polv.add(yvertex); |
||
548 | } |
||
549 | |||
550 | entityMaker.createLwPolyline(polv); |
||
551 | k++; |
||
552 | return k;
|
||
553 | } |
||
554 | |||
555 | private int createPolyline3D(int handle, int k, IFeature feature) |
||
556 | throws Exception { |
||
557 | DxfGroupVector polv = updateProperties(feature,k); |
||
558 | Vector vpoints = new Vector(); |
||
559 | PathIterator theIterator = ((IGeometry)feature.getDefaultGeometry()).getPathIterator(null, FConverter.FLATNESS); // polyLine.getPathIterator(null, |
||
560 | // flatness);
|
||
561 | double[] theData = new double[6]; |
||
562 | double[] velev = ((FGeometry) feature.getDefaultGeometry()).getZs(); |
||
563 | while (!theIterator.isDone()) {
|
||
564 | int theType = theIterator.currentSegment(theData);
|
||
565 | switch (theType) {
|
||
566 | case PathIterator.SEG_MOVETO: |
||
567 | vpoints.add(new FPoint2D(theData[0], theData[1])); |
||
568 | break;
|
||
569 | case PathIterator.SEG_LINETO: |
||
570 | vpoints.add(new FPoint2D(theData[0], theData[1])); |
||
571 | break;
|
||
572 | } |
||
573 | theIterator.next(); |
||
574 | } |
||
575 | if (constantElevation(velev)) {
|
||
576 | DxfGroup polylineFlag = new DxfGroup();
|
||
577 | polylineFlag.setCode(70);
|
||
578 | polylineFlag.setData(new Integer(0)); |
||
579 | polv.add(polylineFlag); |
||
580 | DxfGroup elevation = new DxfGroup();
|
||
581 | elevation.setCode(38);
|
||
582 | elevation.setData(new Double(velev[0])); |
||
583 | polv.add(elevation); |
||
584 | for (int j = 0; j < vpoints.size(); j++) { |
||
585 | DxfGroup xvertex = new DxfGroup();
|
||
586 | xvertex.setCode(10);
|
||
587 | xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX())); |
||
588 | DxfGroup yvertex = new DxfGroup();
|
||
589 | yvertex.setCode(20);
|
||
590 | yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY())); |
||
591 | polv.add(xvertex); |
||
592 | polv.add(yvertex); |
||
593 | } |
||
594 | entityMaker.createLwPolyline(polv); |
||
595 | k++; |
||
596 | } else {
|
||
597 | DxfGroup polylineFlag = new DxfGroup();
|
||
598 | polylineFlag.setCode(70);
|
||
599 | polylineFlag.setData(new Integer(8)); |
||
600 | polv.add(polylineFlag); |
||
601 | DxfGroup xgroup = new DxfGroup();
|
||
602 | xgroup.setCode(10);
|
||
603 | xgroup.setData(new Double(0.0)); |
||
604 | polv.add(xgroup); |
||
605 | DxfGroup ygroup = new DxfGroup();
|
||
606 | ygroup.setCode(20);
|
||
607 | ygroup.setData(new Double(0.0)); |
||
608 | polv.add(ygroup); |
||
609 | DxfGroup elevation = new DxfGroup();
|
||
610 | elevation.setCode(30);
|
||
611 | elevation.setData(new Double(0.0)); |
||
612 | polv.add(elevation); |
||
613 | DxfGroup subclassMarker = new DxfGroup(100, "AcDb3dPolyline"); |
||
614 | polv.add(subclassMarker); |
||
615 | entityMaker.createPolyline(polv); |
||
616 | k++; |
||
617 | for (int j = 0; j < vpoints.size(); j++) { |
||
618 | DxfGroupVector verv = new DxfGroupVector();
|
||
619 | DxfGroup entityType = new DxfGroup(0, "VERTEX"); |
||
620 | verv.add(entityType); |
||
621 | DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity"); |
||
622 | verv.add(generalSubclassMarker); |
||
623 | DxfGroup layerName = new DxfGroup(8, "default"); |
||
624 | verv.add(layerName); |
||
625 | DxfGroup vertexSubclassMarker = new DxfGroup(100, "AcDbVertex"); |
||
626 | verv.add(vertexSubclassMarker); |
||
627 | // DxfGroup vertex3DSubclassMarker = new DxfGroup(100,
|
||
628 | // "AcDb3dPolylineVertex");
|
||
629 | // verv.add(vertex3DSubclassMarker);
|
||
630 | DxfGroup xvertex = new DxfGroup();
|
||
631 | xvertex.setCode(10);
|
||
632 | xvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getX())); |
||
633 | DxfGroup yvertex = new DxfGroup();
|
||
634 | yvertex.setCode(20);
|
||
635 | yvertex.setData(new Double(((FPoint2D) vpoints.get(j)).getY())); |
||
636 | DxfGroup zvertex = new DxfGroup();
|
||
637 | zvertex.setCode(30);
|
||
638 | zvertex.setData(new Double(velev[j])); |
||
639 | verv.add(xvertex); |
||
640 | verv.add(yvertex); |
||
641 | verv.add(zvertex); |
||
642 | entityMaker.addVertex(verv); |
||
643 | k++; |
||
644 | } |
||
645 | DxfGroupVector seqv = new DxfGroupVector();
|
||
646 | DxfGroup entityType = new DxfGroup(0, "SEQEND"); |
||
647 | seqv.add(entityType); |
||
648 | // DxfGroup handle = new DxfGroup();
|
||
649 | // elevation.setCode(5);
|
||
650 | // elevation.setData(new Integer(getHandle()));
|
||
651 | // seqv.add(handle);
|
||
652 | DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity"); |
||
653 | seqv.add(generalSubclassMarker); |
||
654 | DxfGroup layerName = new DxfGroup(8, "default"); |
||
655 | seqv.add(layerName); |
||
656 | DxfGroup handleSeqGroup = new DxfGroup();
|
||
657 | handleSeqGroup.setCode(5);
|
||
658 | handleSeqGroup.setData(new Integer(handle + k).toString()); |
||
659 | seqv.add(handleSeqGroup); |
||
660 | entityMaker.endSeq(); |
||
661 | k++; |
||
662 | } |
||
663 | return k;
|
||
664 | } |
||
665 | |||
666 | private boolean constantElevation(double[] velev) { |
||
667 | boolean constant = true; |
||
668 | for (int i = 0; i < velev.length; i++) { |
||
669 | for (int j = 0; j < velev.length; j++) { |
||
670 | if (j > i) {
|
||
671 | if (velev[i] != velev[j]) {
|
||
672 | constant = false;
|
||
673 | break;
|
||
674 | } |
||
675 | } |
||
676 | } |
||
677 | break;
|
||
678 | } |
||
679 | return constant;
|
||
680 | } |
||
681 | private int createCircle2D(int handle, int k, IFeature feature) throws Exception { |
||
682 | DxfGroupVector polv = updateProperties(feature,k); |
||
683 | DxfGroup circleFlag = new DxfGroup();
|
||
684 | circleFlag.setCode(100);
|
||
685 | polv.add(circleFlag); |
||
686 | |||
687 | DxfGroup xvertex = new DxfGroup();
|
||
688 | xvertex.setCode(10);
|
||
689 | FCircle2D circle = (FCircle2D) ((IGeometry)feature.getDefaultGeometry()).getInternalShape(); |
||
690 | xvertex.setData(new Double(circle.getCenter().getX())); |
||
691 | DxfGroup yvertex = new DxfGroup();
|
||
692 | yvertex.setCode(20);
|
||
693 | yvertex.setData(new Double(circle.getCenter().getY())); |
||
694 | DxfGroup zvertex = new DxfGroup();
|
||
695 | zvertex.setCode(30);
|
||
696 | zvertex.setData(new Double(0)); // TODO: COORDENADA Z. REVISAR ESTO PARA |
||
697 | // ENTIDADES 3D
|
||
698 | |||
699 | DxfGroup radius = new DxfGroup();
|
||
700 | radius.setCode(40);
|
||
701 | radius.setData(new Double(circle.getRadio())); |
||
702 | |||
703 | polv.add(xvertex); |
||
704 | polv.add(yvertex); |
||
705 | polv.add(zvertex); |
||
706 | polv.add(radius); |
||
707 | |||
708 | entityMaker.createCircle(polv); |
||
709 | k++; |
||
710 | return k;
|
||
711 | } |
||
712 | private int createArc2D(int handle, int k,IFeature feature) throws Exception { |
||
713 | FArc2D arc = (FArc2D) ((IGeometry)feature.getDefaultGeometry()).getInternalShape(); |
||
714 | Point2D[] pts = new Point2D[3]; |
||
715 | pts[0] = arc.getInit();
|
||
716 | pts[1] = arc.getMid();
|
||
717 | pts[2] = arc.getEnd();
|
||
718 | Point2D center = arc.getCenter();
|
||
719 | double radius = center.distance(pts[0]); |
||
720 | double initAngle = UtilFunctions.getAngle(center, pts[0]); |
||
721 | initAngle = Math.toDegrees(initAngle);
|
||
722 | // System.out.println("initAngle = " + initAngle);
|
||
723 | double midAngle = UtilFunctions.getAngle(center, pts[1]); |
||
724 | midAngle = Math.toDegrees(midAngle);
|
||
725 | // System.out.println("midAngle = " + midAngle);
|
||
726 | double endAngle = UtilFunctions.getAngle(center, pts[2]); |
||
727 | endAngle = Math.toDegrees(endAngle);
|
||
728 | |||
729 | // if (!UtilFunctions.isCCW(pts[0],pts[1],pts[2])){
|
||
730 | if (!FConverter.isCCW(pts)){
|
||
731 | double aux=initAngle;
|
||
732 | initAngle=endAngle; |
||
733 | endAngle=aux; |
||
734 | } |
||
735 | // DxfGroup arcLayer = new DxfGroup(8, "default");
|
||
736 | DxfGroup ax = new DxfGroup();
|
||
737 | DxfGroup ay = new DxfGroup();
|
||
738 | DxfGroup ac = new DxfGroup();
|
||
739 | DxfGroup ai = new DxfGroup();
|
||
740 | DxfGroup ae = new DxfGroup();
|
||
741 | ax.setCode(10);
|
||
742 | ax.setData(new Double(center.getX())); |
||
743 | ay.setCode(20);
|
||
744 | ay.setData(new Double(center.getY())); |
||
745 | ac.setCode(40);
|
||
746 | ac.setData(new Double(radius)); |
||
747 | ai.setCode(50);
|
||
748 | ai.setData(new Double(initAngle)); |
||
749 | ae.setCode(51);
|
||
750 | ae.setData(new Double(endAngle)); |
||
751 | DxfGroupVector av = updateProperties(feature,k); |
||
752 | // av.add(arcLayer);
|
||
753 | av.add(ax); |
||
754 | av.add(ay); |
||
755 | av.add(ac); |
||
756 | av.add(ai); |
||
757 | av.add(ae); |
||
758 | entityMaker.createArc(av); |
||
759 | k++; |
||
760 | return k;
|
||
761 | } |
||
762 | private int createEllipse2D(int handle, int k,IFeature feature) throws Exception { |
||
763 | FEllipse2D ellipse = (FEllipse2D)((IGeometry)feature.getDefaultGeometry()).getInternalShape(); |
||
764 | |||
765 | Point2D center = new Point2D.Double((ellipse.getInit().getX() + ellipse |
||
766 | .getEnd().getX()) / 2, (ellipse.getInit().getY() + ellipse.getEnd()
|
||
767 | .getY()) / 2);
|
||
768 | double mAxisL = ellipse.getDist() * 2; |
||
769 | // System.out.println("mAxisL = " + mAxisL);
|
||
770 | /*
|
||
771 | * System.out.println("mAxisL/(center.distance(fElip.getEnd()))*2 = " +
|
||
772 | * mAxisL/(center.distance(fElip.getEnd()))*2); minToMaj.setData(new
|
||
773 | * Double(mAxisL/()*2));
|
||
774 | */
|
||
775 | double maAxisL = ellipse.getInit().distance(ellipse.getEnd());
|
||
776 | |||
777 | Point2D endPointOfMajorAxis = ellipse.getEnd();
|
||
778 | double azimut = Math.atan2(endPointOfMajorAxis.getX() - center.getX(), |
||
779 | endPointOfMajorAxis.getY() - center.getY()); |
||
780 | double azimut2 = azimut + Math.PI / 2.0; |
||
781 | if (azimut2 >= Math.PI * 2) |
||
782 | azimut2 = azimut2 - Math.PI * 2; |
||
783 | Point2D endPointOfMinorAxis = new Point2D.Double(center.getX() |
||
784 | + (ellipse.getDist() * Math.sin(azimut2)), center.getY()
|
||
785 | + (ellipse.getDist() * Math.cos(azimut2)));
|
||
786 | |||
787 | if (mAxisL >= maAxisL) {
|
||
788 | // El menor debe ser menor que el mayor. Los cambiamos.
|
||
789 | double aux = mAxisL;
|
||
790 | mAxisL = maAxisL; |
||
791 | maAxisL = aux; |
||
792 | // Tambi?n cambiamos los puntos finales de los ejes.
|
||
793 | Point2D pAux = endPointOfMinorAxis;
|
||
794 | endPointOfMinorAxis = endPointOfMajorAxis; |
||
795 | endPointOfMajorAxis = pAux; |
||
796 | } |
||
797 | double mToMAR = mAxisL / maAxisL;
|
||
798 | // System.out.println("mToMar = " + mToMAR);
|
||
799 | // DxfGroup arcLayer = new DxfGroup(8, "default");
|
||
800 | DxfGroup x = new DxfGroup();
|
||
801 | DxfGroup y = new DxfGroup();
|
||
802 | DxfGroup xc = new DxfGroup();
|
||
803 | DxfGroup yc = new DxfGroup();
|
||
804 | DxfGroup minToMaj = new DxfGroup();
|
||
805 | // DxfGroup start = new DxfGroup();
|
||
806 | // DxfGroup end = new DxfGroup();
|
||
807 | x.setCode(10);
|
||
808 | x.setData(new Double(center.getX())); |
||
809 | y.setCode(20);
|
||
810 | y.setData(new Double(center.getY())); |
||
811 | xc.setCode(11);
|
||
812 | xc.setData(new Double(endPointOfMajorAxis.getX() - center.getX())); |
||
813 | yc.setCode(21);
|
||
814 | yc.setData(new Double(endPointOfMajorAxis.getY() - center.getY())); |
||
815 | minToMaj.setCode(40);
|
||
816 | minToMaj.setData(new Double(mToMAR)); |
||
817 | DxfGroupVector av = updateProperties(feature,k); |
||
818 | // av.add(arcLayer);
|
||
819 | av.add(x); |
||
820 | av.add(y); |
||
821 | av.add(xc); |
||
822 | av.add(yc); |
||
823 | av.add(minToMaj); |
||
824 | entityMaker.createEllipse(av); |
||
825 | k++; |
||
826 | return k;
|
||
827 | } |
||
828 | 19675 | vcaballero | |
829 | 19736 | vcaballero | public static void create(DXFStoreParameters dxfParameters, IFeatureType featureType) throws OpenException, InitializeWriterException { |
830 | 19675 | vcaballero | boolean dxf3DFile=false; |
831 | |||
832 | DxfEntityMaker entityMaker = new DxfEntityMaker(dxfParameters.getProjection());
|
||
833 | // FieldDescription[] fieldDescriptions=getTableDefinition().getFieldsDesc();
|
||
834 | // for (int i=0;i<featureType.size();i++){
|
||
835 | // IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get(i);
|
||
836 | // if (fad.getName().equals("Text")){
|
||
837 | // indexText=i;
|
||
838 | // }else if (fad.getName().equals("HeightText")){
|
||
839 | // indexHeightText=i;
|
||
840 | // }else if (fad.getName().equals("RotationText")){
|
||
841 | // indexRotationText=i;
|
||
842 | // }
|
||
843 | // }
|
||
844 | DxfFile dxfFile = new DxfFile(null, dxfParameters.getDXFFile().getName(), entityMaker); |
||
845 | dxfFile.setCadFlag(true);
|
||
846 | if (dxf3DFile)
|
||
847 | dxfFile.setDxf3DFlag(true);
|
||
848 | try {
|
||
849 | dxfFile.save(dxfParameters.getDXFFile().getAbsolutePath()); |
||
850 | } catch (IOException e) { |
||
851 | throw new InitializeWriterException("DXF Writer",e); |
||
852 | } |
||
853 | |||
854 | } |
||
855 | 20377 | jmvivo | |
856 | /* (non-Javadoc)
|
||
857 | * @see org.gvsig.data.datastores.vectorial.IFeaturesWriter#cancelProcess()
|
||
858 | */
|
||
859 | public void cancelProcess() throws WriteException { |
||
860 | // TODO Auto-generated method stub
|
||
861 | // Hacer algo????
|
||
862 | |||
863 | } |
||
864 | 19401 | vcaballero | } |