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