Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data_dwg / src / org / gvsig / fmap / data / feature / file / dwg / DWGResource.java @ 23273

History | View | Annotate | Download (9.01 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
                }else{
120
                        legendlabelingManager = null;
121
                }
122

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

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

    
161
                TreeSet colorSet=new TreeSet();
162

    
163
                boolean is3dFile = dwg.isDwg3DFile();
164

    
165
                for (int i = 0; i < dwgObjects.size(); i++) {
166
                        try {
167
                                DwgObject entity = (DwgObject) dwgObjects.get(i);
168

    
169
                                if(entity instanceof IDwg2FMap){
170

    
171
                                        IDwg2FMap dwgEnt = (IDwg2FMap)entity;
172
                                        Geometry geometry = dwgEnt.toFMapGeometry(is3dFile);
173
                                        if (geometry == null)
174
                                                continue;
175
                                        //we check for Region of Interest of the CAD file
176
                                        if(!this.fullEnvelope.contains(geometry.getEnvelope()))
177
                                                continue;
178
                                        DWGFeature feature = new DWGFeature(this.featureType, false);
179

    
180
                                        feature.loading();
181

    
182
                                        feature.set(ID_FIELD_ID, i);
183

    
184
                                        feature.set(ID_FIELD_ENTITY, dwgEnt.toString());
185

    
186
                                        feature.set(ID_FIELD_FSHAPE, geometry.getGeometryType().getName());
187

    
188
                                        feature.set(ID_FIELD_LAYER, dwg.getLayerName(entity));
189

    
190
                                        int colorByLayer = dwg.getColorByLayer(entity);
191
                                        int color = entity.getColor();
192
                                        if(color < 0)
193
                                                color = Math.abs(color);
194
                                        if(color > 255)
195
                                                color = colorByLayer;
196

    
197

    
198
                                        feature.set(ID_FIELD_COLOR, color);
199
                                        colorSet.add(new Integer(color));
200

    
201

    
202
                                        if(entity instanceof IDwg3DTestable){
203
                                                feature.set(ID_FIELD_ELEVATION, ((IDwg3DTestable)entity).getZ());
204
                                        }
205

    
206
                                        feature.set(ID_FIELD_THICKNESS, 0.0);
207
                                        feature.set(ID_FIELD_HEIGHTTEXT, 0.0);
208
                                        feature.set(ID_FIELD_ROTATIONTEXT, 0.0);
209
                                        feature.set(ID_FIELD_TEXT, "");
210

    
211
                                        if(entity instanceof DwgMText){
212
                                                DwgMText mtext = (DwgMText) entity;
213
                                                feature.set(ID_FIELD_HEIGHTTEXT, mtext.getHeight());
214
                                                feature.set(ID_FIELD_TEXT, mtext.getText());
215
                                        }else if(entity instanceof DwgText){
216
                                                DwgText text = (DwgText) entity;
217
                                                feature.set(ID_FIELD_THICKNESS, text.getThickness());
218
                                                feature.set(ID_FIELD_HEIGHTTEXT, text.getHeight());
219
                                                feature.set(ID_FIELD_ROTATIONTEXT, text.getRotationAngle());
220
                                                feature.set(ID_FIELD_TEXT, text.getText());
221
                                        }//if-else
222

    
223
                                        feature.setGeometry(ID_FIELD_GEOMETRY, geometry);
224

    
225

    
226
                                        this.addFeature(feature);
227
                                        if (this.legendlabelingManager != null){
228
                                                this.legendlabelingManager.add(feature);
229
                                        }
230

    
231
                                }//if instanceof
232
                        } catch (ReadException e) {
233
                                throw new OpenException(this.description(),e);
234
                        }
235

    
236

    
237
                }//for
238

    
239

    
240

    
241

    
242

    
243
//                String[] types = new String[featureType.size()];
244
//                Iterator iter = featureType.iterator();
245
//                int i=0;
246
//                while (iter.hasNext()){
247
//                        types[i] = ((FeatureAttributeDescriptor)iter.next()).getDataType();
248
//                        i++;
249
//                }
250

    
251
        }
252

    
253
        /* (non-Javadoc)
254
         * @see org.gvsig.fmap.data.feature.file.IFileResource#getTotalFiles()
255
         */
256
        public int getTotalFiles() {
257
                return 1;
258
        }
259

    
260
         protected FeatureType getFeatureType() throws ReadException{
261
                 this.checkOpen();
262
                 return this.featureType;
263
         }
264

    
265
        /**
266
         * @return
267
         * @throws ReadException
268
         */
269
        protected Object getDefaultLegend() throws ReadException {
270
                this.checkOpen();
271
                if (legendlabelingManager == null){
272
                        return null;
273
                }
274
                return legendlabelingManager.getDefaultLegend();
275
        }
276

    
277
        /* (non-Javadoc)
278
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFeatureCount()
279
         */
280
        protected int getFeatureCount() throws ReadException {
281
                return super.getFeatureCount();
282
        }
283

    
284
        /* (non-Javadoc)
285
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFullExtent()
286
         */
287
        protected Envelope getFullExtent() throws ReadException {
288
                return super.getFullExtent();
289
        }
290

    
291
        /* (non-Javadoc)
292
         * @see org.gvsig.fmap.data.feature.file.FileMemoryResource#getFeature(int)
293
         */
294
        protected Feature getFeature(int index) throws ReadException {
295
                return super.getFeature(index);
296
        }
297

    
298
        protected Object getDefaultLabelingStrategy()  throws ReadException {
299
                this.checkOpen();
300
                if (legendlabelingManager == null){
301
                        return null;
302
                }
303
                return legendlabelingManager.getLabeling();
304
        }
305

    
306
        public static void setLegendLabelingManagerFactory(LegendLabelingManagerFactory factory) {
307
                legendLabelingManagerFactory = factory;
308
        }
309

    
310
        public static LegendLabelingManagerFactory getLegendLabelingManagerFactory() {
311
                return legendLabelingManagerFactory;
312
        }
313

    
314
}
315