Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgPFacePolyline.java @ 10820

History | View | Annotate | Download (12 KB)

1
/*
2
 * Created on 03-feb-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: DwgPFacePolyline.java 10820 2007-03-20 19:57:09Z azabala $
47
 * $Log$
48
 * Revision 1.3  2007-03-20 19:57:08  azabala
49
 * source code cleaning
50
 *
51
 * Revision 1.2  2007/03/06 19:39:38  azabala
52
 * Changes to adapt dwg 12 to general architecture
53
 *
54
 * Revision 1.1  2007/03/01 19:58:53  azabala
55
 * refactor of pface and mesh names
56
 *
57
 * Revision 1.2  2007/02/07 12:44:27  fdiaz
58
 * A?adido o modificado el metodo clone para que el DwgObject se encargue de las propiedades comunes a todos los objetos.
59
 * A?adido el metodo fill.
60
 *
61
 * Revision 1.1  2007/02/05 07:03:22  azabala
62
 * *** empty log message ***
63
 *
64
 *
65
 */
66
package com.iver.cit.jdwglib.dwg.objects;
67

    
68
import java.awt.geom.Point2D;
69
import java.util.ArrayList;
70
import java.util.List;
71
import java.util.Map;
72

    
73
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
74
import com.iver.cit.gvsig.fmap.core.FPolygon3D;
75
import com.iver.cit.gvsig.fmap.core.IGeometry;
76
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
77
import com.iver.cit.jdwglib.dwg.DwgFile;
78
import com.iver.cit.jdwglib.dwg.DwgHandleReference;
79
import com.iver.cit.jdwglib.dwg.DwgObject;
80
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
81
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
82
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
83
import com.iver.cit.jdwglib.dwg.IDwgVertex;
84
import com.iver.cit.jdwglib.util.FMapUtil;
85

    
86
/**
87
 * This DWG entity is a "Polyface Mesh".
88
 * 
89
 */
90

    
91
/*
92
 * azabala DXF 12 spec says: "If the Vertex defines a face of the mesh, its
93
 * Vertex flags (70) group has the 128 bit set but not the 64 bit. The 10, 20,
94
 * and 30 (location) groups of the face entity are irrelevant and are always
95
 * written as zero in a DXF file.
96
 * 
97
 * The vertex indexes that define the mesh are given by 71, 72, 73, and 74
98
 * groups, the values of which are integers specifying one of the previously
99
 * defined vertices by index.
100
 * 
101
 * If the index is negative, the edge that begins with that vertex is invisible.
102
 * 
103
 * The first zero vertex marks the end of the vertices of the face.
104
 * 
105
 * Since the 71 through 74 groups are optional fields with default values of
106
 * zero, they are present in DXF only if nonzero."
107
 * 
108
 * But DWG 12 spec doesnt say anything about 71,72,73 and 74 fields for VERTEX.
109
 */
110
public class DwgPFacePolyline extends DwgObject
111
        implements IDwgPolyline, IDwg2FMap, IDwgBlockMember{
112
        
113
        public static final int NUM_VERTEX_OF_FACE = 4;
114
        
115
        
116
        /**
117
         * number of vertices in the mesh (as it is readed from the dwg file... very
118
         * often is wrong)
119
         */
120
        private int vertexCount;
121
        /**
122
         * number of faces of the mesh (as it is readed from the dwg file... very
123
         * often is wrong)
124
         */
125
        private int faceCount;
126
        
127
        
128
        private DwgHandleReference firstVertexHandle;
129
        private DwgHandleReference lastVertexHandle;
130
        private DwgHandleReference seqendHandle;
131

    
132
        /**
133
         * Collection of all the vertices of the polyface mesh
134
         */
135
        private List vertices;
136
        /**
137
         * Collection of all the faces of the polyface mesh, whose vertices are in
138
         * vertices List.
139
         */
140
        private List faces;
141
        
142
        /**
143
         * Constructor
144
         */
145
        public DwgPFacePolyline(int index) {
146
                super(index);
147
                vertices = new ArrayList();
148
                faces = new ArrayList();
149
        }
150

    
151
        public void setVertexCount(int vertexCount) {
152
                this.vertexCount = vertexCount;
153
        }
154

    
155
        public void setFaceCount(int faceCount) {
156
                this.faceCount = faceCount;
157
        }
158

    
159
        public void setFirstVertexHandle(DwgHandleReference handle) {
160
                this.firstVertexHandle = handle;
161
        }
162

    
163
        public void setLastVertexHandle(DwgHandleReference handle) {
164
                this.lastVertexHandle = handle;
165
        }
166

    
167
        public void setSeqendHandle(DwgHandleReference handle) {
168
                this.seqendHandle = handle;
169
        }
170

    
171
        public int getFaceCount() {
172
                return faceCount;
173
        }
174

    
175
        public DwgHandleReference getFirstVertexHandle() {
176
                return firstVertexHandle;
177
        }
178

    
179
        public DwgHandleReference getLastVertexHandle() {
180
                return lastVertexHandle;
181
        }
182

    
183
        public DwgHandleReference getSeqendHandle() {
184
                return seqendHandle;
185
        }
186

    
187
        public int getVertexCount() {
188
                return vertexCount;
189
        }
190
        
191
        public Object clone(){
192
                DwgPFacePolyline obj = new DwgPFacePolyline(index);
193
                this.fill(obj);
194
                return obj;
195
        }
196
        
197
        protected void fill(DwgObject obj){
198
                super.fill(obj);
199
                DwgPFacePolyline myObj = (DwgPFacePolyline)obj;
200
                myObj.setFaceCount(faceCount);
201
                myObj.setFirstVertexHandle(firstVertexHandle);
202
                myObj.setLastVertexHandle(lastVertexHandle);
203
                myObj.setSeqendHandle(seqendHandle);
204
                myObj.setVertexCount(vertexCount);
205
                myObj.setVertices(this.vertices);
206
                myObj.setFaces(this.faces);
207

    
208
        }
209

    
210
        public void setFaces(List faces) {
211
                this.faces = faces;
212
        }
213
        
214
        public List getFaces(){
215
                return this.faces;
216
        }
217

    
218
        public List getVertices() {
219
                return vertices;
220
        }
221

    
222
        public void setVertices(List vertices) {
223
                this.vertices = vertices;
224
        }
225

    
226
        public void calculateGisModel(DwgFile dwgFile) {
227
        // In DWG 13, 14 and 2000 version, a Polyface mesh has
228
    // handles references to its vertices.
229
        // In DWG 12 vertices are after the entity
230
         if(firstVertexHandle != null && lastVertexHandle != null){
231
                         DwgObject first = dwgFile.getDwgObjectFromHandle(firstVertexHandle.getOffset());
232
                        DwgObject last = dwgFile.getDwgObjectFromHandle(lastVertexHandle.getOffset());
233
                        if(first == null || last == null){
234
                                System.out.println("DwgPFacePolyline con vertices inicial o final a null");
235
                                return;
236
                        }
237
                        
238
                        if(!(first instanceof IDwgVertex)){
239
                                System.out.println("El primer vertice de PFacePolyline es "+
240
                                                                first.getClass().getName());
241
                                return;
242
                        }
243
                        
244
                        if(!(last instanceof IDwgVertex)){
245
                                System.out.println("El ultimo vertice de PFacePolyline es "+
246
                                                last.getClass().getName());
247
                                return;
248
                        }
249
                        
250
                         int firstObjIdx = dwgFile.getIndexOf(first);
251
                         int lastObjIdx =  dwgFile.getIndexOf(last);
252
                         if(firstObjIdx == -1 || lastObjIdx == -1){
253
                                 System.out.println("Calculate GIS Model: Problemas en "+
254
                                                 "la localizacion de vertices: 1?="
255
                                                         +firstObjIdx+
256
                                                         ",Ultimo="+lastObjIdx);
257
                                 return;
258
                         }
259
                         for(int i = firstObjIdx; i <= lastObjIdx; i++){
260
                                 DwgObject obj = dwgFile.getDwgObject(i);
261
                                 if(obj instanceof IDwgVertex){
262
                                         this.addVertex((IDwgVertex)obj);
263
                                 }else if(obj instanceof DwgSeqend){
264
                                         // TODO SEE IF BREAK THE ITERATION
265
                                         break;
266
                                 }else{
267
                                         System.out.println("Encontrado "+obj.getClass().getName()+" en la lista de vertices de Polyline3D");
268
                                 }
269
                         }// for
270
                         
271
                } else {
272
                        System.out.println("Encontrada polil?nea sin puntos ...");
273
                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si
274
                        // esto
275
                        // ocurre es porque existe un error que hay que corregir ...
276
                }
277

    
278
        }
279
        
280
    
281
        public IGeometry toFMapGeometry(boolean is3DFile) {
282
                FGeometryCollection solution = null;
283
                IGeometry[] geometries = null;
284
                if(vertices != null && faces != null){
285
                        if(vertices.size() == 0 || faces.size() == 0){
286
                                return solution;
287
                        }
288
                        ArrayList geomList = new ArrayList();
289
                        int numFaces = faces.size();
290
                        for(int i = 0; i < numFaces; i++){
291
                                DwgVertexPFaceFace face = (DwgVertexPFaceFace) faces.get(i);
292
                                int[] verticesId = face.getVerticesidx();
293
                                ArrayList pts = new ArrayList();
294
                                boolean lastWasInvisible = true;
295
                                for(int j = 0; j < NUM_VERTEX_OF_FACE; j++){
296
                                        if(verticesId[j] > 0){
297
                            if(lastWasInvisible){
298
                                pts.clear();
299
                                lastWasInvisible = false;
300
                            }
301
                            // the index is 1-based
302
                            try{
303
                            pts.add(vertices.get(verticesId[j] -1));
304
                            }catch(Throwable t){
305
                                    t.printStackTrace();
306
                            }
307
                            
308
                        } else if(verticesId[j] < 0 && !lastWasInvisible){
309
                            lastWasInvisible = true;
310
                            pts.add(vertices.get( (verticesId[j] * -1) -1));
311
                        }        
312
                                }// for j
313
                                
314
                                FPolygon3D polygon = FMapUtil.ptsTo3DPolygon(pts);
315
                                IGeometry geom = ShapeFactory.createGeometry(polygon);
316
                                geomList.add(geom);
317
                                
318
                                
319
// if(!lastWasInvisible){
320
// if(vertex[nrV] < 0){
321
// line.addPoint(knot[-vertex[nrV] - 1]);
322
// } else{
323
// line.addPoint(knot[vertex[nrV] - 1]);
324
// }
325
// set.addDrawable(line);
326
// }
327
                   
328
                        }// for i
329
                        geometries = new IGeometry[geomList.size()];
330
                        geomList.toArray(geometries);
331
                        solution = new FGeometryCollection(geometries);
332
                }
333
                return solution;
334
                
335
        }
336
        /*
337
         * (non-Javadoc)
338
         * 
339
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapString(boolean)
340
         */
341
        public String toFMapString(boolean is3DFile) {
342
                if(is3DFile)
343
                        return "FPolyline3D";
344
                else
345
                        return "FPolyline2D";
346
        }
347
        
348
        public String toString(){
349
                return "PFacePolyline";
350
        }
351

    
352
        public void addVertex(IDwgVertex vertex) {
353
                // here we save the vertex (not the coordinate) because
354
                // this Polyline could have two kind of vertex (pface and pfaceface)
355
                if(vertex instanceof DwgVertexPFaceFace)
356
                        faces.add(vertex);
357
                else 
358
                        vertices.add(vertex);
359
        }
360
        
361
        public void dump(){
362
                System.out.println("<PFacePolyline vertexCount="+vertexCount+" faceCount"+faceCount+">");
363
                int numRealVertex = 1;
364
                for(int i = 0; i < vertices.size(); i++){
365
                        IDwgVertex vertex = (IDwgVertex) vertices.get(i);
366
                        double[] point = vertex.getPoint();
367
                        System.out.println("<Vertex idx="+numRealVertex+" class="+vertex.getClass().getName()+" x="+point[0]+" y="+point[1]+" z="+point[2]+" >");
368
                        numRealVertex++;
369
                }
370
                
371
                for(int i = 0; i < faces.size(); i++){
372
                        IDwgVertex vertex = (IDwgVertex) faces.get(i);
373
                        if(vertex instanceof DwgVertexPFaceFace){
374
                                int[] point = ((DwgVertexPFaceFace)vertex).getVerticesidx();
375
                                System.out.println("<Vertex class="+vertex.getClass().getName()+" 1="+point[0]+" 2="+point[1]+" 3="+point[2]+" 4="+ point[3]+">");
376
                        }
377
                }
378
                System.out.println("</PFacePolyline>");
379
        }
380

    
381
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot, List dwgObjectsWithoutBlocks, Map handle_objectsWithoutBlocks, DwgFile callBack) {
382
                DwgPFacePolyline transformedEntity = null;
383
                List vertices = this.getVertices();
384
                if (vertices != null) {
385
                        if(vertices.size() == 0)
386
                                return;
387
                    List transformedVertices = new ArrayList();
388
                        for (int i=0;i < vertices.size();i++) {
389
                                double[] pointAux = null;
390
                                IDwgVertex vertex = (IDwgVertex) vertices.get(i);
391
                            if(!(vertex instanceof DwgVertexPFaceFace)){
392
                                    // DwgVertexPFaceFace hasnt coordinates, instead
393
                                    // it has indices in the vertex array
394
                                        double[] point = vertex.getPoint();
395
                                    pointAux = new double[]{point[0] - bPoint[0],
396
                                                    point[1] - bPoint[1], point[2] - bPoint[2]};
397
                                    
398
                                        double laX = insPoint.getX() + ((pointAux[0] * scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
399
                                        double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
400
                                        double laZ = pointAux[2];
401
                                        vertex.setPoint(new double[]{laX, laY, laZ});
402
                                        transformedVertices.add(vertex);        
403
                                }
404
                        }// for
405
                        transformedEntity = (DwgPFacePolyline)this.clone();
406
                        fill(transformedEntity);
407
                        dwgObjectsWithoutBlocks.add(transformedEntity);
408
                        handle_objectsWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
409
                }// if
410
        }
411
}
412