Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgPFacePolyline.java @ 23234

History | View | Annotate | Download (13 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 23234 2008-09-09 10:14:55Z fdiaz $
47
 * $Log$
48
 * Revision 1.3.2.1  2007-03-21 19:49:16  azabala
49
 * implementation of dwg 12, 13, 14.
50
 *
51
 * Revision 1.3  2007/03/20 19:57:08  azabala
52
 * source code cleaning
53
 *
54
 * Revision 1.2  2007/03/06 19:39:38  azabala
55
 * Changes to adapt dwg 12 to general architecture
56
 *
57
 * Revision 1.1  2007/03/01 19:58:53  azabala
58
 * refactor of pface and mesh names
59
 *
60
 * Revision 1.2  2007/02/07 12:44:27  fdiaz
61
 * A�adido o modificado el metodo clone para que el DwgObject se encargue de las propiedades comunes a todos los objetos.
62
 * A�adido el metodo fill.
63
 *
64
 * Revision 1.1  2007/02/05 07:03:22  azabala
65
 * *** empty log message ***
66
 *
67
 *
68
 */
69
package com.iver.cit.jdwglib.dwg.objects;
70

    
71
import java.awt.geom.Point2D;
72
import java.util.ArrayList;
73
import java.util.List;
74
import java.util.Map;
75

    
76
import org.apache.log4j.Logger;
77

    
78
import org.gvsig.fmap.geom.Geometry;
79
import org.gvsig.fmap.geom.GeometryFactory;
80
import org.gvsig.fmap.geom.GeometryManager;
81
import org.gvsig.fmap.geom.aggregate.MultiSurface2D;
82
import org.gvsig.fmap.geom.primitive.Surface;
83
import org.gvsig.fmap.geom.primitive.Surface2D;
84

    
85
import com.iver.cit.jdwglib.dwg.DwgFile;
86
import com.iver.cit.jdwglib.dwg.DwgHandleReference;
87
import com.iver.cit.jdwglib.dwg.DwgObject;
88
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
89
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
90
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
91
import com.iver.cit.jdwglib.dwg.IDwgVertex;
92
import com.iver.cit.jdwglib.dwg.readers.objreaders.v2004.AbstractDwg2004Reader;
93
import com.iver.cit.jdwglib.util.FMapUtil;
94

    
95
/**
96
 * This DWG entity is a "Polyface Mesh".
97
 *
98
 */
99

    
100
/*
101
 * azabala DXF 12 spec says: "If the Vertex defines a face of the mesh, its
102
 * Vertex flags (70) group has the 128 bit set but not the 64 bit. The 10, 20,
103
 * and 30 (location) groups of the face entity are irrelevant and are always
104
 * written as zero in a DXF file.
105
 *
106
 * The vertex indexes that define the mesh are given by 71, 72, 73, and 74
107
 * groups, the values of which are integers specifying one of the previously
108
 * defined vertices by index.
109
 *
110
 * If the index is negative, the edge that begins with that vertex is invisible.
111
 *
112
 * The first zero vertex marks the end of the vertices of the face.
113
 *
114
 * Since the 71 through 74 groups are optional fields with default values of
115
 * zero, they are present in DXF only if nonzero."
116
 *
117
 * But DWG 12 spec doesnt say anything about 71,72,73 and 74 fields for VERTEX.
118
 */
119
public class DwgPFacePolyline extends DwgObject
120
        implements IDwgPolyline, IDwg2FMap, IDwgBlockMember{
121

    
122
        private static Logger logger = Logger.getLogger(DwgPFacePolyline.class.getName());
123

    
124
        public static final int NUM_VERTEX_OF_FACE = 4;
125

    
126

    
127
        /**
128
         * number of vertices in the mesh (as it is readed from the dwg file... very
129
         * often is wrong)
130
         */
131
        private int vertexCount;
132
        /**
133
         * number of faces of the mesh (as it is readed from the dwg file... very
134
         * often is wrong)
135
         */
136
        private int faceCount;
137

    
138

    
139
        private DwgHandleReference firstVertexHandle;
140
        private DwgHandleReference lastVertexHandle;
141
        private DwgHandleReference seqendHandle;
142

    
143
        /**
144
         * Collection of all the vertices of the polyface mesh
145
         */
146
        private List vertices;
147
        /**
148
         * Collection of all the faces of the polyface mesh, whose vertices are in
149
         * vertices List.
150
         */
151
        private List faces;
152

    
153
        private ArrayList ownedObjectsHandles; //For R2004+
154

    
155

    
156
        /**
157
         * Constructor
158
         */
159
        public DwgPFacePolyline(int index) {
160
                super(index);
161
                vertices = new ArrayList();
162
                faces = new ArrayList();
163
                ownedObjectsHandles = new ArrayList();
164
        }
165

    
166
        public void setVertexCount(int vertexCount) {
167
                this.vertexCount = vertexCount;
168
        }
169

    
170
        public void setFaceCount(int faceCount) {
171
                this.faceCount = faceCount;
172
        }
173

    
174
        public void setFirstVertexHandle(DwgHandleReference handle) {
175
                this.firstVertexHandle = handle;
176
        }
177

    
178
        public void setLastVertexHandle(DwgHandleReference handle) {
179
                this.lastVertexHandle = handle;
180
        }
181

    
182
        public void setSeqendHandle(DwgHandleReference handle) {
183
                this.seqendHandle = handle;
184
        }
185

    
186
        public int getFaceCount() {
187
                return faceCount;
188
        }
189

    
190
        public DwgHandleReference getFirstVertexHandle() {
191
                return firstVertexHandle;
192
        }
193

    
194
        public DwgHandleReference getLastVertexHandle() {
195
                return lastVertexHandle;
196
        }
197

    
198
        public DwgHandleReference getSeqendHandle() {
199
                return seqendHandle;
200
        }
201

    
202
        public int getVertexCount() {
203
                return vertexCount;
204
        }
205

    
206
        public Object clone(){
207
                DwgPFacePolyline obj = new DwgPFacePolyline(index);
208
                this.fill(obj);
209
                return obj;
210
        }
211

    
212
        protected void fill(DwgObject obj){
213
                super.fill(obj);
214
                DwgPFacePolyline myObj = (DwgPFacePolyline)obj;
215
                myObj.setFaceCount(faceCount);
216
                myObj.setFirstVertexHandle(firstVertexHandle);
217
                myObj.setLastVertexHandle(lastVertexHandle);
218
                myObj.setSeqendHandle(seqendHandle);
219
                myObj.setVertexCount(vertexCount);
220
                myObj.setVertices(this.vertices);
221
                myObj.setFaces(this.faces);
222

    
223
        }
224

    
225
        public void setFaces(List faces) {
226
                this.faces = faces;
227
        }
228

    
229
        public List getFaces(){
230
                return this.faces;
231
        }
232

    
233
        public List getVertices() {
234
                return vertices;
235
        }
236

    
237
        public void setVertices(List vertices) {
238
                this.vertices = vertices;
239
        }
240

    
241
        /**
242
         * @return Returns the owned objects handles.
243
         */
244
        public ArrayList getOwnedObjectsHandles() {
245
                return ownedObjectsHandles;
246
        }
247
        /**
248
         * @param objects The owned objects handles to set.
249
         */
250
        public void setOwnedObjectsHandles(ArrayList handles) {
251
                this.ownedObjectsHandles = handles;
252
        }
253
        /**
254
         * Add a handle to the ownedBbjectsHandle vector
255
         *
256
         * @param handle handle
257
         */
258
        public void addOwnedObjectHandle(DwgHandleReference handle) {
259
                this.ownedObjectsHandles.add(handle);
260
        }
261

    
262
        public void calculateGisModel(DwgFile dwgFile) {
263
        // In DWG 13, 14 and 2000 version, a Polyface mesh has
264
    // handles references to its vertices.
265
        // In DWG 12 vertices are after the entity
266
         if(firstVertexHandle != null && lastVertexHandle != null){
267
                         DwgObject first = dwgFile.getDwgObjectFromHandle(firstVertexHandle.getOffset());
268
                        DwgObject last = dwgFile.getDwgObjectFromHandle(lastVertexHandle.getOffset());
269
                        if(first == null || last == null){
270
                                logger.warn("DwgPFacePolyline con vertices inicial o final a null");
271
                                return;
272
                        }
273

    
274
                        if(!(first instanceof IDwgVertex)){
275
                                logger.warn("El primer vertice de PFacePolyline es "+
276
                                                                first.getClass().getName());
277
                                return;
278
                        }
279

    
280
                        if(!(last instanceof IDwgVertex)){
281
                                logger.warn("El ultimo vertice de PFacePolyline es "+
282
                                                last.getClass().getName());
283
                                return;
284
                        }
285

    
286
                         int firstObjIdx = dwgFile.getIndexOf(first);
287
                         int lastObjIdx =  dwgFile.getIndexOf(last);
288
                         if(firstObjIdx == -1 || lastObjIdx == -1){
289
                                 logger.warn("Calculate GIS Model: Problemas en "+
290
                                                 "la localizacion de vertices: primero ="
291
                                                         +firstObjIdx+
292
                                                         ",Ultimo ="+lastObjIdx);
293
                                 return;
294
                         }
295
                         for(int i = firstObjIdx; i <= lastObjIdx; i++){
296
                                 DwgObject obj = dwgFile.getDwgObject(i);
297
                                 if(obj instanceof IDwgVertex){
298
                                         this.addVertex((IDwgVertex)obj);
299
                                 }else if(obj instanceof DwgSeqend){
300
                                         // TODO SEE IF BREAK THE ITERATION
301
                                         break;
302
                                 }else{
303
                                         logger.warn("Encontrado "+obj.getClass().getName()+" en la lista de vertices de Polyline3D");
304
                                 }
305
                         }// for
306

    
307
                } else {
308
                        logger.warn("Encontrada polilínea sin puntos ...");
309
                        // TODO: No se debe mandar nunca una polil�nea sin puntos, si
310
                        // esto
311
                        // ocurre es porque existe un error que hay que corregir ...
312
                }
313

    
314
        }
315

    
316

    
317
        public Geometry toFMapGeometry(boolean is3DFile) {
318
//                FGeometryCollection solution = null;
319
//                GeometryFactory gFactory = GeometryManager.getInstance().getGeometryFactory();
320

    
321
                MultiSurface2D solution = null;
322
                Surface2D[] geometries = null;
323
                if(vertices != null && faces != null){
324
                        if(vertices.size() == 0 || faces.size() == 0){
325
                                return solution;
326
                        }
327
                        ArrayList geomList = new ArrayList();
328
                        int numFaces = faces.size();
329
                        for(int i = 0; i < numFaces; i++){
330
                                DwgVertexPFaceFace face = (DwgVertexPFaceFace) faces.get(i);
331
                                int[] verticesId = face.getVerticesidx();
332
                                ArrayList pts = new ArrayList();
333
                                boolean lastWasInvisible = true;
334
                                for(int j = 0; j < NUM_VERTEX_OF_FACE; j++){
335
                                        if(verticesId[j] > 0){
336
                            if(lastWasInvisible){
337
                                pts.clear();
338
                                lastWasInvisible = false;
339
                            }
340
                            // the index is 1-based
341
                            try{
342
                            pts.add(vertices.get(verticesId[j] -1));
343
                            }catch(Throwable t){
344
                                    t.printStackTrace();
345
                            }
346

    
347
                        } else if(verticesId[j] < 0 && !lastWasInvisible){
348
                            lastWasInvisible = true;
349
                            pts.add(vertices.get( (verticesId[j] * -1) -1));
350
                        }
351
                                }// for j
352

    
353
                                Geometry geom = FMapUtil.ptsTo3DPolygon(pts);
354
//                                Geometry geom = ShapeFactory.createGeometry(polygon);
355
                                geomList.add(geom);
356

    
357

    
358
// if(!lastWasInvisible){
359
// if(vertex[nrV] < 0){
360
// line.addPoint(knot[-vertex[nrV] - 1]);
361
// } else{
362
// line.addPoint(knot[vertex[nrV] - 1]);
363
// }
364
// set.addDrawable(line);
365
// }
366

    
367
                        }// for i
368
                        geometries = new Surface2D[geomList.size()];
369
                        geometries = (Surface2D[]) geomList.toArray(geometries);
370
                        solution = new MultiSurface2D(null, null, geometries);
371
                }
372
                return solution;
373

    
374
        }
375
        /*
376
         * (non-Javadoc)
377
         *
378
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapString(boolean)
379
         */
380
        public String toFMapString(boolean is3DFile) {
381
                if(is3DFile)
382
                        return "FPolyline3D";
383
                else
384
                        return "FPolyline2D";
385
        }
386

    
387
        public String toString(){
388
                return "PFacePolyline";
389
        }
390

    
391
        public void addVertex(IDwgVertex vertex) {
392
                // here we save the vertex (not the coordinate) because
393
                // this Polyline could have two kind of vertex (pface and pfaceface)
394
                if(vertex instanceof DwgVertexPFaceFace)
395
                        faces.add(vertex);
396
                else
397
                        vertices.add(vertex);
398
        }
399

    
400
        public void dump(){
401
                System.out.println("<PFacePolyline vertexCount="+vertexCount+" faceCount"+faceCount+">");
402
                int numRealVertex = 1;
403
                for(int i = 0; i < vertices.size(); i++){
404
                        IDwgVertex vertex = (IDwgVertex) vertices.get(i);
405
                        double[] point = vertex.getPoint();
406
                        System.out.println("<Vertex idx="+numRealVertex+" class="+vertex.getClass().getName()+" x="+point[0]+" y="+point[1]+" z="+point[2]+" >");
407
                        numRealVertex++;
408
                }
409

    
410
                for(int i = 0; i < faces.size(); i++){
411
                        IDwgVertex vertex = (IDwgVertex) faces.get(i);
412
                        if(vertex instanceof DwgVertexPFaceFace){
413
                                int[] point = ((DwgVertexPFaceFace)vertex).getVerticesidx();
414
                                System.out.println("<Vertex class="+vertex.getClass().getName()+" 1="+point[0]+" 2="+point[1]+" 3="+point[2]+" 4="+ point[3]+">");
415
                        }
416
                }
417
                System.out.println("</PFacePolyline>");
418
        }
419

    
420
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot, List dwgObjectsWithoutBlocks, Map handle_objectsWithoutBlocks, DwgFile callBack) {
421
                DwgPFacePolyline transformedEntity = null;
422
                List vertices = this.getVertices();
423
                if (vertices != null) {
424
                        if(vertices.size() == 0)
425
                                return;
426
                    List transformedVertices = new ArrayList();
427
                        for (int i=0;i < vertices.size();i++) {
428
                                double[] pointAux = null;
429
                                IDwgVertex vertex = (IDwgVertex) vertices.get(i);
430
                            if(!(vertex instanceof DwgVertexPFaceFace)){
431
                                    // DwgVertexPFaceFace hasnt coordinates, instead
432
                                    // it has indices in the vertex array
433
                                        double[] point = vertex.getPoint();
434
                                    pointAux = new double[]{point[0] - bPoint[0],
435
                                                    point[1] - bPoint[1], point[2] - bPoint[2]};
436

    
437
                                        double laX = insPoint.getX() + ((pointAux[0] * scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
438
                                        double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
439
                                        double laZ = pointAux[2];
440
                                        vertex.setPoint(new double[]{laX, laY, laZ});
441
                                        transformedVertices.add(vertex);
442
                                }
443
                        }// for
444
                        transformedEntity = (DwgPFacePolyline)this.clone();
445
                        fill(transformedEntity);
446
                        dwgObjectsWithoutBlocks.add(transformedEntity);
447
                        handle_objectsWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
448
                }// if
449
        }
450
}
451