Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data_dwg / src / org / gvsig / fmap / data / feature / file / dwg / DWGResource.java @ 23640

History | View | Annotate | Download (9.44 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.data.feature.file.dwg;
32

    
33
import java.io.File;
34
import java.io.IOException;
35
import java.util.List;
36
import java.util.TreeSet;
37

    
38
import org.gvsig.fmap.data.DataException;
39
import org.gvsig.fmap.data.OpenException;
40
import org.gvsig.fmap.data.ReadException;
41
import org.gvsig.fmap.data.feature.Feature;
42
import org.gvsig.fmap.data.feature.FeatureType;
43
import org.gvsig.fmap.data.feature.UnsupportedVersionException;
44
import org.gvsig.fmap.data.feature.file.FileMemoryResource;
45
import org.gvsig.fmap.data.feature.file.FileStoreParameters;
46
import org.gvsig.fmap.data.feature.operation.LegendLabelingManager;
47
import org.gvsig.fmap.data.feature.operation.LegendLabelingManagerFactory;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.i18n.Messages;
52

    
53
import com.iver.cit.jdwglib.dwg.DwgFile;
54
import com.iver.cit.jdwglib.dwg.DwgObject;
55
import com.iver.cit.jdwglib.dwg.DwgVersionNotSupportedException;
56
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
57
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
58
import com.iver.cit.jdwglib.dwg.objects.DwgMText;
59
import com.iver.cit.jdwglib.dwg.objects.DwgText;
60

    
61
/**
62
 * @author jmvivo
63
 *
64
 */
65
public class DWGResource extends FileMemoryResource {
66

    
67
        private static LegendLabelingManagerFactory legendLabelingManagerFactory = null;
68

    
69
        public static final int ID_FIELD_ID = 0;
70
        public static final int ID_FIELD_FSHAPE = 1;
71
        public static final int ID_FIELD_ENTITY = 2;
72
        public static final int ID_FIELD_LAYER = 3;
73
        public static final int ID_FIELD_COLOR = 4;
74
        public static final int ID_FIELD_ELEVATION = 5;
75
        public static final int ID_FIELD_THICKNESS = 6;
76
        public static final int ID_FIELD_HEIGHTTEXT = 7;
77
        public static final int ID_FIELD_ROTATIONTEXT = 8;
78
        public static final int ID_FIELD_TEXT = 9;
79
        public static final int ID_FIELD_GEOMETRY = 10;
80

    
81

    
82

    
83

    
84
        private FeatureType featureType;
85

    
86

    
87
        private LegendLabelingManager legendlabelingManager;
88
        private Envelope fullEnvelope;
89

    
90

    
91
        /**
92
         * @param params
93
         */
94
        DWGResource(FileStoreParameters params) {
95
                super(params);
96
        }
97

    
98
        /* (non-Javadoc)
99
         * @see org.gvsig.fmap.data.Resource#getName()
100
         */
101
        public String getName() {
102
                return "DWG";
103
        }
104

    
105
        private void initializeFeatureType() throws DataException {
106
                this.featureType= DWGStore.newFeatureType();
107
        }
108

    
109

    
110
        /* (non-Javadoc)
111
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#load()
112
         */
113
        protected void load() throws DataException {
114
//                float heightText = 10;
115
//                attr.setLoadedInMemory(true);
116
                initializeFeatureType();
117
                if (legendLabelingManagerFactory != null){
118
                        legendlabelingManager = legendLabelingManagerFactory.newManager();
119
                        legendlabelingManager.startLoading(null);
120
                }else{
121
                        legendlabelingManager = null;
122
                }
123

    
124
                this.fullEnvelope = new DefaultEnvelope(2);
125
                File file = this.getFile();
126
                DwgFile dwg = new DwgFile(file.getAbsolutePath());
127
                try {
128
                        dwg.read();
129
                } catch (DwgVersionNotSupportedException e1) {
130
                        String autodeskUrl = "<a href=\"http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151\">"
131
                                        + "http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=7024151</a>";
132
                        throw new UnsupportedVersionException("la version ("+e1.getDwgVersion().toString()+
133
                                        ") del fichero '"+file.getName()+"' no esta soportada por gvSIG.\n"+
134
                                        Messages.getText("Probe_to_convert")+"\n"+
135
                                        Messages.getText("Autodesk_converter")+"\n"+autodeskUrl, e1);
136
                } catch (IOException e) {
137
                        throw new ReadException(DWGStore.DATASTORE_NAME, e);
138
                }
139

    
140
                dwg.calculateGisModelDwgPolylines();
141
                dwg.blockManagement2();
142
                List dwgObjects = dwg.getDwgObjects();
143
                //Each dwg file has two headers vars, EXTMIN and EXTMAX
144
                //that marks bounds of the file.
145
                //Also, it could have spureus entities (deleted objects
146
                //dont removed from the DWG database)
147
                double[] extMin = (double[]) dwg.getHeader("MSPACE_EXTMIN");
148
                double[] extMax = (double[]) dwg.getHeader("MSPACE_EXTMAX");
149
                if(extMin != null && extMax != null){
150
                        if(extMin.length >= 2 && extMax.length >= 2){
151
                                double xmin = extMin[0];
152
                                double ymin = extMin[1];
153
                                double xmax = extMax[0];
154
                                double ymax = extMax[1];
155
//                                Envelope roi = new
156
//                                        DefaultEnvelope(xmin-100, ymin-100,
157
//                                                        (xmax-xmin)+100, (ymax-ymin)+100); // ????
158
//                                Envelope roi = new DefaultEnvelope(xmin-100, ymin-100,
159
//                                                xmax+100, ymax+100);
160
// No dando margen
161
                                Envelope roi = new DefaultEnvelope(xmin, ymin,
162
                                                xmax, ymax);
163
                                this.fullEnvelope = roi;
164
                        }
165
                }
166

    
167
                TreeSet colorSet=new TreeSet();
168

    
169
                boolean is3dFile = dwg.isDwg3DFile();
170

    
171
                for (int i = 0; i < dwgObjects.size(); i++) {
172
                        try {
173
                                DwgObject entity = (DwgObject) dwgObjects.get(i);
174

    
175
                                if(entity instanceof IDwg2FMap){
176

    
177
                                        IDwg2FMap dwgEnt = (IDwg2FMap)entity;
178
                                        Geometry geometry = dwgEnt.toFMapGeometry(is3dFile);
179
                                        if (geometry == null ){ //|| geometry.getGeneralPath() == null) {
180
                                                continue;
181
                                        }
182
                                        //FIXME: Estas 6 lineas es por si la geometr??a no se ha creado correctamente
183
                                        Envelope gEnvelope = null;
184
                                        try{
185
                                                gEnvelope = geometry.getEnvelope();
186
                                        } catch (Exception e){
187
                                                gEnvelope = null;
188
                                        }
189
                                        if (gEnvelope == null) continue;
190
                                        //we check for Region of Interest of the CAD file
191
                                        if(!this.fullEnvelope.intersects(geometry.getEnvelope())) {
192
                                                continue;
193
                                        }
194
                                        DWGFeature feature = new DWGFeature(this.featureType, false);
195

    
196
                                        feature.loading();
197

    
198
                                        feature.set(ID_FIELD_ID, i);
199

    
200
                                        feature.set(ID_FIELD_ENTITY, dwgEnt.toString());
201

    
202
                                        feature.set(ID_FIELD_FSHAPE, geometry.getGeometryType().getName());
203

    
204
                                        feature.set(ID_FIELD_LAYER, dwg.getLayerName(entity));
205

    
206
                                        int colorByLayer = dwg.getColorByLayer(entity);
207
                                        int color = entity.getColor();
208
                                        if(color < 0) {
209
                                                color = Math.abs(color);
210
                                        }
211
                                        if(color > 255) {
212
                                                color = colorByLayer;
213
                                        }
214

    
215

    
216
                                        feature.set(ID_FIELD_COLOR, color);
217
                                        colorSet.add(new Integer(color));
218

    
219

    
220
                                        if(entity instanceof IDwg3DTestable){
221
                                                feature.set(ID_FIELD_ELEVATION, ((IDwg3DTestable)entity).getZ());
222
                                        }
223

    
224
                                        feature.set(ID_FIELD_THICKNESS, 0.0);
225
                                        feature.set(ID_FIELD_HEIGHTTEXT, 0.0);
226
                                        feature.set(ID_FIELD_ROTATIONTEXT, 0.0);
227
                                        feature.set(ID_FIELD_TEXT, "");
228

    
229
                                        if(entity instanceof DwgMText){
230
                                                DwgMText mtext = (DwgMText) entity;
231
                                                feature.set(ID_FIELD_HEIGHTTEXT, mtext.getHeight());
232
                                                feature.set(ID_FIELD_TEXT, mtext.getText());
233
                                        }else if(entity instanceof DwgText){
234
                                                DwgText text = (DwgText) entity;
235
                                                feature.set(ID_FIELD_THICKNESS, text.getThickness());
236
                                                feature.set(ID_FIELD_HEIGHTTEXT, text.getHeight());
237
                                                feature.set(ID_FIELD_ROTATIONTEXT, text.getRotationAngle());
238
                                                feature.set(ID_FIELD_TEXT, text.getText());
239
                                        }//if-else
240

    
241
                                        feature.setGeometry(ID_FIELD_GEOMETRY, geometry);
242

    
243

    
244
                                        this.addFeature(feature);
245
                                        if (this.legendlabelingManager != null){
246
                                                this.legendlabelingManager.add(feature, null);
247
                                        }
248

    
249
                                }//if instanceof
250
                        } catch (ReadException e) {
251
                                throw new OpenException(this.description(),e);
252
                        }
253

    
254

    
255
                }//for
256

    
257

    
258

    
259
                if (legendlabelingManager != null) {
260
                        legendlabelingManager.endLoading();
261
                }
262
        }
263

    
264
        /* (non-Javadoc)
265
         * @see org.gvsig.fmap.data.feature.file.IFileResource#getTotalFiles()
266
         */
267
        public int getTotalFiles() {
268
                return 1;
269
        }
270

    
271
         protected FeatureType getFeatureType() throws ReadException{
272
                 this.checkOpen();
273
                 return this.featureType;
274
         }
275

    
276
        /**
277
         * @return
278
         * @throws ReadException
279
         */
280
        protected Object getDefaultLegend() throws ReadException {
281
                this.checkOpen();
282
                if (legendlabelingManager == null){
283
                        return null;
284
                }
285
                return legendlabelingManager.getDefaultLegend();
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFeatureCount()
290
         */
291
        protected int getFeatureCount() throws ReadException {
292
                return super.getFeatureCount();
293
        }
294

    
295
        /* (non-Javadoc)
296
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFullExtent()
297
         */
298
        protected Envelope getFullExtent() throws ReadException {
299
                return super.getFullExtent();
300
        }
301

    
302
        /* (non-Javadoc)
303
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFeature(int)
304
         */
305
        protected Feature getFeature(int index) throws ReadException {
306
                return super.getFeature(index);
307
        }
308

    
309
        protected Object getDefaultLabelingStrategy()  throws ReadException {
310
                this.checkOpen();
311
                if (legendlabelingManager == null){
312
                        return null;
313
                }
314
                return legendlabelingManager.getLabeling();
315
        }
316

    
317
        public static void setLegendLabelingManagerFactory(LegendLabelingManagerFactory factory) {
318
                legendLabelingManagerFactory = factory;
319
        }
320

    
321
        public static LegendLabelingManagerFactory getLegendLabelingManagerFactory() {
322
                return legendLabelingManagerFactory;
323
        }
324

    
325
}
326