Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfEntityMaker.java @ 37

History | View | Annotate | Download (12.2 KB)

1
/*
2
 * Created on 04-may-2004
3
 */
4
 
5
package org.cresques.px.dxf;
6

    
7
import java.awt.geom.Point2D;
8

    
9
import org.cresques.geo.Point3D;
10
import org.cresques.geo.Projected;
11
import org.cresques.geo.Projection;
12
import org.cresques.geo.ReProjection;
13
import org.cresques.io.DxfFile;
14
import org.cresques.io.DxfGroup;
15
import org.cresques.io.DxfGroupVector;
16
import org.cresques.px.Extent;
17
import org.cresques.px.PxObj;
18

    
19
/**
20
 * Contructor de objetos Dxf 
21
 * 
22
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
23
 */
24

    
25
public class DxfEntityMaker implements DxfFile.EntityFactory, Projected {
26
        Projection proj = null;
27
        DxfEntity lastEntity = null;
28
        DxfEntityList entities = null;
29
        DxfTable layers = null;
30
        double bulge = 0.0;
31

    
32
    double xtruX=0.0, xtruY=0.0, xtruZ=1.0;
33
    //double x=Double.NaN, y=Double.NaN, z=Double.NaN;
34
    
35
    int polylineFlag = 0;
36
    Point2D firstPt = new Point2D.Double();
37
        
38
        public DxfEntityMaker (Projection proj) {
39
                this.proj = proj;
40
                layers = new DxfTable();
41
                entities = new DxfEntityList(proj);
42
        }
43
        
44
        public PxObj getObjects() { return entities; }
45
        public Extent getExtent() { return entities.getExtent(); }
46

    
47
        public void createLayer(DxfGroupVector grp) throws Exception {
48
                DxfLayer layer = new DxfLayer(grp.getDataAsString(2), grp.getDataAsInt(62));
49
                layer.lType = grp.getDataAsString(6);
50
                layer.setFlags(grp.getDataAsInt(70));
51
                // compruebo flags
52
                if ((layer.flags & 0x01) == 0x01) {
53
                        layer.frozen = true;
54
                }
55
                if ((layer.flags & 0x02) == 0x02) {
56
                        layer.frozen = true;
57
                }
58
                layers.add(layer);
59
        }
60
        
61
        public void createPolyline(DxfGroupVector grp) throws Exception {
62
                System.out.println("Creando la polylinea");
63
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
64
                DxfPolyline entity = new DxfPolyline(proj, layer);
65
                double x = 0.0, y = 0.0, z = 0.0;
66
                int thickness = 0;
67
                
68
                if (grp.hasCode(10))
69
                        x = grp.getDataAsDouble(10);
70
                if (grp.hasCode(20))
71
                        y = grp.getDataAsDouble(20);
72
                if (grp.hasCode(30))
73
                        z = grp.getDataAsDouble(30);
74
                /*if (grp.hasCode(39))
75
                        System.out.println("Leer el thickness provoca un error");
76
                        thickness = grp.getDataAsInt(39);*/
77
                if (grp.hasCode(62))
78
                        entity.dxfColor = grp.getDataAsInt(62);
79
                if (grp.hasCode(70))
80
                        entity.flags = grp.getDataAsInt(70);
81
                if (grp.hasCode(210))
82
                        xtruX = grp.getDataAsDouble(210);
83
                if (grp.hasCode(220))
84
                        xtruY = grp.getDataAsDouble(220);
85
                if (grp.hasCode(230))
86
                        xtruZ = grp.getDataAsDouble(230);
87
                // compruebo flags
88
                if ((entity.flags & 0x01) == 0x01) {
89
                        entity.closed = true;
90
                }
91
                lastEntity = entity;
92
        }
93
        public void endSeq() throws Exception {
94
                entities.add(lastEntity);
95
                lastEntity = null;
96
                xtruX = 0.0;
97
                xtruY = 0.0;
98
                xtruZ = 1.0;
99
                bulge = 0.0;
100
        }
101
        public void addVertex(DxfGroupVector grp) throws Exception {
102
                double x = 0.0, y = 0.0, z = 0.0;
103
                
104
                x  = grp.getDataAsDouble(10);
105
                y  = grp.getDataAsDouble(20);
106
                if (grp.hasCode(30)) z = grp.getDataAsDouble(30);
107
                if (grp.hasCode(42)) {
108
                        bulge = grp.getDataAsDouble(42);
109
                } else { bulge = 0.0; }
110
                Point3D point_in = new Point3D(x, y, z);
111
                Point3D xtru = new Point3D(xtruX, xtruY, xtruZ);
112
                Point2D point_out = DxfCalXtru.CalculateXtru(point_in, xtru);
113
                x = point_out.getX();
114
                y = point_out.getY();
115
                
116
                //int cnt = ((DxfPolyline) lastEntity).pts.size();
117
                //CoordinateList arc = DxfPOLYLINE.createArc(((DxfPolyline) lastEntity).pts.getCoordinate(cnt-2), ((DxfPolyline) lastEntity).pts.getCoordinate(cnt-1), bulge[0], xtru);
118
                //((DxfPolyline) lastEntity).pts.remove(cnt-1);
119
                
120
                //System.out.println(x+","+y);
121
                Point2D pt = proj.createPoint( x, y);
122
                ((DxfPolyline) lastEntity).add(pt);
123
                
124
                if (((DxfPolyline)lastEntity).pts.size() == 1) {
125
                        firstPt = pt;
126
                }
127
                
128
        }
129
        public void createLwPolyline(DxfGroupVector grp) throws Exception {
130
                double x = 0.0, y = 0.0, elev=0.0;
131
                DxfGroup g = null;
132
                
133
                if (grp.hasCode(38))
134
                        elev = grp.getDataAsDouble(38);
135
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
136
                DxfLwPolyline entity = new DxfLwPolyline(proj, layer);
137
                for (int i=0; i<grp.size(); i++) {
138
                        g = (DxfGroup) grp.get(i);
139
                        if (g.getCode() == 10)
140
                                x = ((Float) g.getData()).doubleValue();
141
                        else if (g.getCode() == 20) {
142
                                y = ((Float) g.getData()).doubleValue();
143
                                //if (y <= 1.0) throw new Exception("Y == "+y);
144
                                entity.add( proj.createPoint( x, y ) );
145
                                x = 0.0; y = 0.0;
146
                        }
147
                }
148
                if (grp.hasCode(62))
149
                        entity.dxfColor = grp.getDataAsInt(62);
150
                if (grp.hasCode(70))
151
                        entity.flags = grp.getDataAsInt(70);
152
                entities.add(entity);
153
        }
154
        
155
        public void createLine(DxfGroupVector grp) throws Exception {
156
                System.out.println("Pasa por aqui");
157
                double x = 0.0, y = 0.0, z1 = 0.0, z2 = 0.0;
158
                DxfGroup g = null;
159
                Point2D pt1 = null, pt2 = null;
160
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
161

    
162
                x = grp.getDataAsDouble(10);
163
                y = grp.getDataAsDouble(20);
164
                if (grp.hasCode(30)) z1 = grp.getDataAsDouble(30);
165
                pt1 = proj.createPoint(x, y);
166
                x = grp.getDataAsDouble(11);
167
                y = grp.getDataAsDouble(21);
168
                if (grp.hasCode(31)) z2 = grp.getDataAsDouble(31);
169
                pt2 = proj.createPoint(x, y);
170
                if (grp.hasCode(210))
171
                        xtruX = grp.getDataAsDouble(210);
172
                if (grp.hasCode(220))
173
                        xtruY = grp.getDataAsInt(220);
174
                if (grp.hasCode(230))
175
                        xtruZ = grp.getDataAsInt(230);
176
                Point3D point_in1 = new Point3D(pt1.getX(), pt1.getY(), z1);
177
                Point3D point_in2 = new Point3D(pt2.getX(), pt2.getY(), z2);
178
                Point3D xtru = new Point3D(xtruX, xtruY, xtruZ);
179
                Point2D point_out1 = DxfCalXtru.CalculateXtru(point_in1, xtru);
180
                Point2D point_out2 = DxfCalXtru.CalculateXtru(point_in2, xtru);
181
                pt1.setLocation(point_out1);
182
                pt2.setLocation(point_out2);
183
                DxfLine entity = new DxfLine(proj, layer, pt1, pt2);
184
                if (grp.hasCode(62))
185
                        entity.dxfColor = grp.getDataAsInt(62);
186
                entities.add(entity);
187
        }
188
        public void createText(DxfGroupVector grp) throws Exception {
189
                double x = 0.0, y = 0.0, h= 0.0, rot= 0.0;
190
                DxfGroup g = null;
191
                Point2D pt1 = null, pt2 = null;
192
                String txt = null;
193
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
194

    
195
                txt = grp.getDataAsString(1);
196
                DxfText entity = new DxfText(proj, layer, txt);
197

    
198
                entity.h = grp.getDataAsDouble(40);
199
                x = grp.getDataAsDouble(10);
200
                y = grp.getDataAsDouble(20);
201
                entity.setPt1(proj.createPoint(x, y));
202
                if (grp.hasCode(11)) {
203
                        x = grp.getDataAsDouble(11);
204
                        y = grp.getDataAsDouble(21);
205
                        entity.setPt2(proj.createPoint(x, y));
206
                }
207
                if (grp.hasCode(50))
208
                        entity.rot = grp.getDataAsDouble(50);
209
                if (grp.hasCode(62))
210
                        entity.dxfColor = grp.getDataAsInt(62);
211
                if (grp.hasCode(72))
212
                        entity.align = grp.getDataAsInt(72);
213
                entities.add(entity);
214
        }
215
        public void createPoint(DxfGroupVector grp) throws Exception {
216
                double x = 0.0, y = 0.0, z = 0.0; //, h= 0.0, rot= 0.0;
217
                DxfGroup g = null;
218
                Point2D pt = null;
219
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
220

    
221
                DxfPoint entity = new DxfPoint(proj, layer);
222

    
223
                x = grp.getDataAsDouble(10);
224
                y = grp.getDataAsDouble(20);
225
                if (grp.hasCode(30)) z = grp.getDataAsDouble(30);
226
                if (grp.hasCode(62))
227
                        entity.dxfColor = grp.getDataAsInt(62);
228
                if (grp.hasCode(210))
229
                        xtruX = grp.getDataAsDouble(210);
230
                if (grp.hasCode(220))
231
                        xtruY = grp.getDataAsInt(220);
232
                if (grp.hasCode(230))
233
                        xtruZ = grp.getDataAsInt(230);
234
                Point3D point_in = new Point3D(x, y, z);
235
                Point3D xtru = new Point3D(xtruX, xtruY, xtruZ);
236
                Point2D point_out = DxfCalXtru.CalculateXtru(point_in, xtru);
237
                x = point_out.getX();
238
                y = point_out.getY();
239
                entity.setPt(proj.createPoint(x, y));
240
                entities.add(entity);
241
        }
242
        public void createCircle(DxfGroupVector grp) throws Exception {
243
                double x = 0.0, y = 0.0, z = 0.0; //, h= 0.0, rot= 0.0;
244
                double r = 0.0;
245
                //Point2D[] pts = new Point2D;
246
                DxfGroup g = null;
247
                //Point2D pt1 = new Point2D.Double();
248
                //Point2D pt2 = new Point2D.Double();
249
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
250

    
251
                x = grp.getDataAsDouble(10);
252
                y = grp.getDataAsDouble(20);
253
                if (grp.hasCode(30)) z = grp.getDataAsDouble(30);
254
                if (grp.hasCode(40)) r = grp.getDataAsDouble(40);
255
                if (grp.hasCode(210))
256
                        xtruX = grp.getDataAsDouble(210);
257
                if (grp.hasCode(220))
258
                        xtruY = grp.getDataAsInt(220);
259
                if (grp.hasCode(230))
260
                        xtruZ = grp.getDataAsInt(230);
261
                Point3D point_in = new Point3D(x, y, z);
262
                Point3D xtru = new Point3D(xtruX, xtruY, xtruZ);
263
                Point2D point_out = DxfCalXtru.CalculateXtru(point_in, xtru);
264
                x = point_out.getX();
265
                y = point_out.getY();
266
                
267
                Point2D center = proj.createPoint( x, y);
268
                Point2D[] pts = new Point2D[360];
269
                int angulo = 0;
270
                //Point2D pt1 = new Point2D.Double(center.getX() + r * Math.sin(angulo*Math.PI/(double)180.0), center.getY() + r * Math.cos(angulo*Math.PI/(double)180.0));
271
                for (angulo=0; angulo<360; angulo++) {
272
                        //System.out.println("pt1 = " + pt1);
273
                        //pts[angulo] = new Point2D.Double(pt1.getX(), pt1.getY());
274
                        pts[angulo] = new Point2D.Double(center.getX(), center.getY());
275
                        pts[angulo].setLocation(pts[angulo].getX() + r * Math.sin(angulo*Math.PI/(double)180.0), pts[angulo].getY() + r * Math.cos(angulo*Math.PI/(double)180.0));
276
                        if (pts.length == 1) {
277
                                firstPt = pts[angulo];
278
                        }
279
                }
280
                DxfCircle entity = new DxfCircle(proj, layer, pts);
281
                if (grp.hasCode(62))
282
                        entity.dxfColor = grp.getDataAsInt(62);
283
                System.out.println("A?ade un circulo a la lista de entidades");
284
                entities.add(entity);
285
        }
286
        /*public void createArc(DxfGroupVector grp) throws Exception {
287
                double x = 0.0, y = 0.0, z = 0.0; //, h= 0.0, rot= 0.0;
288
                double r = 0.0, empieza = 0.0, acaba = 0.0;
289
                //Point2D[] pts = new Point2D;
290
                DxfGroup g = null;
291
                //Point2D pt1 = new Point2D.Double();
292
                //Point2D pt2 = new Point2D.Double();
293
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
294

295
                x = grp.getDataAsDouble(10);
296
                y = grp.getDataAsDouble(20);
297
                if (grp.hasCode(30)) z = grp.getDataAsDouble(30);
298
                if (grp.hasCode(40)) r = grp.getDataAsDouble(40);
299
                if (grp.hasCode(50)) empieza = grp.getDataAsDouble(50);
300
                if (grp.hasCode(51)) acaba = grp.getDataAsDouble(51);
301
                if (grp.hasCode(210))
302
                        xtruX = grp.getDataAsDouble(210);
303
                if (grp.hasCode(220))
304
                        xtruY = grp.getDataAsInt(220);
305
                if (grp.hasCode(230))
306
                        xtruZ = grp.getDataAsInt(230);
307
                Point3D point_in = new Point3D(x, y, z);
308
                Point3D xtru = new Point3D(xtruX, xtruY, xtruZ);
309
                Point2D point_out = DxfCalXtru.CalculateXtru(point_in, xtru);
310
                x = point_out.getX();
311
                y = point_out.getY();
312
                
313
                Point2D center = proj.createPoint( x, y);
314
                Point2D[] pts = new Point2D[360];
315
                double angulo = 0;
316
                
317
                int iempieza = (int)empieza;
318
                int iacaba = (int)acaba;
319
                
320
                if (empieza <= acaba) {
321
                        angulo = empieza;
322
                        pts[0].setLocation(center.getX() + r * Math.sin(angulo*Math.PI/(double)180.0), center.getY() + r * Math.cos(angulo*Math.PI/(double)180.0));
323
                        for (int i=iempieza+1; i<=iacaba; i++) {
324
                                angulo = (double)i;
325
                                pts[i].setLocation(center.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), center.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
326
                        }
327
                        angulo = acaba;
328
                        pts[360].setLocation(center.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), center.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
329
                }
330
                else {
331
                        angulo = empieza;
332
                        pts[0].setLocation(center.getX() + r * Math.sin(angulo*Math.PI/(double)180.0), center.getY() + r * Math.cos(angulo*Math.PI/(double)180.0));
333
                        for (int i=iempieza+1; i<=360; i++) {
334
                                angulo = (double)i;
335
                                pts[i].setLocation(center.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), center.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
336
                        }
337
                        for (int i=1; i<=iacaba; i++) {
338
                                angulo = (double)i;
339
                                pts[i].setLocation(center.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), center.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
340
                        }
341
                        angulo = acaba;
342
                        pts[360].setLocation(center.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), center.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
343
                }
344
                DxfCircle entity = new DxfCircle(proj, layer, pts);
345
                if (grp.hasCode(62))
346
                        entity.dxfColor = grp.getDataAsInt(62);
347
                System.out.println("A?ade un arco a la lista de entidades");
348
                entities.add(entity);
349
        }*/
350
        public void createSolid(DxfGroupVector grp) throws Exception {
351
                DxfLayer layer = (DxfLayer) layers.getByName(grp.getDataAsString(8));
352
        }
353

    
354
        public Projection getProjection() { return proj;}
355

    
356
        /**
357
         * Cambia de proyeccion.
358
         * 
359
         * @param rp
360
         */
361
        public void reProject(ReProjection rp) {
362
                entities.reProject(rp);
363
        }
364
        public DxfEntityList getEntities() { return entities;}
365
        public DxfTable getLayers() { return layers;}
366

    
367
}
368

    
369

    
370