Revision 88 trunk/org.gvsig.dwg/org.gvsig.dwg.lib/src/main/java/org/gvsig/dwg/lib/objects/DwgLwPolyline.java

View differences:

DwgLwPolyline.java
53 53
import org.gvsig.fmap.geom.Geometry;
54 54
import org.gvsig.fmap.geom.exception.CreateGeometryException;
55 55

  
56

  
57 56
/**
58 57
 * The DwgLwPolyline class represents a DWG LwPolyline
59 58
 *
60 59
 * @author jmorell
61 60
 */
62 61
public class DwgLwPolyline extends DwgObject
63
	implements IDwgPolyline, IDwgExtrusionable,
64
	IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
62
        implements IDwgPolyline, IDwgExtrusionable,
63
        IDwg3DTestable, IDwg2FMap, IDwgBlockMember {
65 64

  
66
	private int flag;
67
	private double constWidth;
68
	private double elevation;
69
	private double thickness;
70
	private double[] normal;
71
	private List vertices;
72
	/*
73
	 *The bulge is the tangent of 1/4 of the included angle for the arc
74
	 *between the selected vertex and the next vertex in the polyline's vertex list.
75
	 *A negative bulge value indicates that the arc goes clockwise from
76
	 *the selected vertex to the next vertex.
77
	 *A bulge of 0 indicates a straight segment,
78
	 *and a bulge of 1 is a semicircle.
79
	 *
80
	 *http://www.faqs.org/faqs/CAD/autolisp-faq/part2/section-6.html
81
	 */
82
	private double[] bulges;
83
	private double[][] widths;
65
    private int flag;
66
    private double constWidth;
67
    private double elevation;
68
    private double thickness;
69
    private double[] normal;
70
    private List vertices;
71
    /*
72
     *The bulge is the tangent of 1/4 of the included angle for the arc
73
     *between the selected vertex and the next vertex in the polyline's vertex list.
74
     *A negative bulge value indicates that the arc goes clockwise from
75
     *the selected vertex to the next vertex.
76
     *A bulge of 0 indicates a straight segment,
77
     *and a bulge of 1 is a semicircle.
78
     *
79
     *http://www.faqs.org/faqs/CAD/autolisp-faq/part2/section-6.html
80
     */
81
    private double[] bulges;
82
    private double[][] widths;
84 83

  
84
    public DwgLwPolyline(int index) {
85
        super(index);
86
        vertices = new ArrayList();
87
    }
85 88

  
86
	public DwgLwPolyline(int index) {
87
		super(index);
88
		vertices = new ArrayList();
89
	}
90
	/**
91
	 * @return Returns the bulges.
92
	 */
93
	public double[] getBulges() {
94
		return bulges;
95
	}
96
	/**
97
	 * @param bulges The bulges to set.
98
	 */
99
	public void setBulges(double[] bulges) {
100
		this.bulges = bulges;
101
	}
102
	/**
103
	 * @return Returns the flag.
104
	 */
105
	public int getFlag() {
106
		return flag;
107
	}
108
	/**
109
	 * @param flag The flag to set.
110
	 */
111
	public void setFlag(int flag) {
112
		this.flag = flag;
113
	}
114
	/**
115
	 * @return Returns the vertices.
116
	 */
117
	public List getVertices() {
118
		return vertices;
119
	}
120
	/**
121
	 * @param vertices The vertices to set.
122
	 */
123
	public void setVertices(List vertices) {
124
		this.vertices = vertices;
125
	}
126 89
    /**
90
     * @return Returns the bulges.
91
     */
92
    public double[] getBulges() {
93
        return bulges;
94
    }
95

  
96
    /**
97
     * @param bulges The bulges to set.
98
     */
99
    public void setBulges(double[] bulges) {
100
        this.bulges = bulges;
101
    }
102

  
103
    /**
104
     * @return Returns the flag.
105
     */
106
    public int getFlag() {
107
        return flag;
108
    }
109

  
110
    /**
111
     * @param flag The flag to set.
112
     */
113
    public void setFlag(int flag) {
114
        this.flag = flag;
115
    }
116

  
117
    /**
118
     * @return Returns the vertices.
119
     */
120
    public List getVertices() {
121
        return vertices;
122
    }
123

  
124
    /**
125
     * @param vertices The vertices to set.
126
     */
127
    public void setVertices(List vertices) {
128
        this.vertices = vertices;
129
    }
130

  
131
    /**
127 132
     * @return Returns the elevation.
128 133
     */
129 134
    public double getElevation() {
130 135
        return elevation;
131 136
    }
137

  
132 138
    /**
133 139
     * @param elevation The elevation to set.
134 140
     */
135 141
    public void setElevation(double elevation) {
136 142
        this.elevation = elevation;
137 143
    }
144

  
138 145
    /**
139 146
     * @return Returns the normal.
140 147
     */
141 148
    public double[] getNormal() {
142 149
        return normal;
143 150
    }
144
	/* (non-Javadoc)
145
	 * @see java.lang.Object#clone()
146
	 */
151
    /* (non-Javadoc)
152
     * @see java.lang.Object#clone()
153
     */
147 154
//	public Object clone() {
148 155
//		DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
149 156
//		dwgLwPolyline.setType(type);
......
170 177
//		dwgLwPolyline.setWidths(widths);
171 178
//		return dwgLwPolyline;
172 179
//	}
173
	/**
174
	 * @return Returns the constWidth.
175
	 */
176
	public double getConstWidth() {
177
		return constWidth;
178
	}
179
	/**
180
	 * @param constWidth The constWidth to set.
181
	 */
182
	public void setConstWidth(double constWidth) {
183
		this.constWidth = constWidth;
184
	}
185
	/**
186
	 * @return Returns the thickness.
187
	 */
188
	public double getThickness() {
189
		return thickness;
190
	}
191
	/**
192
	 * @param thickness The thickness to set.
193
	 */
194
	public void setThickness(double thickness) {
195
		this.thickness = thickness;
196
	}
197
	/**
198
	 * @return Returns the widths.
199
	 */
200
	public double[][] getWidths() {
201
		return widths;
202
	}
203
	/**
204
	 * @param widths The widths to set.
205
	 */
206
	public void setWidths(double[][] widths) {
207
		this.widths = widths;
208
	}
209
	/**
210
	 * @param normal The normal to set.
211
	 */
212
	public void setNormal(double[] normal) {
213
		this.normal = normal;
214
	}
215
	/* (non-Javadoc)
216
	 * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
217
	 */
218
	public void calculateGisModel(DwgFile dwgFile) {
219
		if (getVertices() == null) {
220
			return;
221
		}
222
		int flags = getFlag();
223
		List pts = getVertices();
224
		double[] bulges = getBulges();
225
		List newPts = new ArrayList();
226
		double[] newBulges = null;
227
		if(bulges != null){
228
			newBulges = new double[bulges.length];
229
			System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
230
		}else{
231
			bulges = new double[pts.size() ];
232
			newBulges = new double[bulges.length];
180

  
181
    /**
182
     * @return Returns the constWidth.
183
     */
184
    public double getConstWidth() {
185
        return constWidth;
186
    }
187

  
188
    /**
189
     * @param constWidth The constWidth to set.
190
     */
191
    public void setConstWidth(double constWidth) {
192
        this.constWidth = constWidth;
193
    }
194

  
195
    /**
196
     * @return Returns the thickness.
197
     */
198
    public double getThickness() {
199
        return thickness;
200
    }
201

  
202
    /**
203
     * @param thickness The thickness to set.
204
     */
205
    public void setThickness(double thickness) {
206
        this.thickness = thickness;
207
    }
208

  
209
    /**
210
     * @return Returns the widths.
211
     */
212
    public double[][] getWidths() {
213
        return widths;
214
    }
215

  
216
    /**
217
     * @param widths The widths to set.
218
     */
219
    public void setWidths(double[][] widths) {
220
        this.widths = widths;
221
    }
222

  
223
    /**
224
     * @param normal The normal to set.
225
     */
226
    public void setNormal(double[] normal) {
227
        this.normal = normal;
228
    }
229
    /* (non-Javadoc)
230
     * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
231
     */
232

  
233
    public void calculateGisModel(DwgFile dwgFile) {
234
        if (getVertices() == null) {
235
            return;
236
        }
237
        int flags = getFlag();
238
        List pts = getVertices();
239
        double[] bulges = getBulges();
240
        List newPts = new ArrayList();
241
        double[] newBulges = null;
242
        if (bulges != null) {
243
            newBulges = new double[bulges.length];
244
            System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
245
        } else {
246
            bulges = new double[pts.size()];
247
            newBulges = new double[bulges.length];
233 248
			//dwg spec says numVertex (numSegments + 1)
234
			//TODO Check this
235
			for(int i = 0; i < newBulges.length; i++) {
236
				bulges[i] = 0d;
237
			}
238
			newBulges = new double[bulges.length];
239
			System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
240
		}
241
		// TODO: Aqu� pueden existir casos no contemplados ...
249
            //TODO Check this
250
            for (int i = 0; i < newBulges.length; i++) {
251
                bulges[i] = 0d;
252
            }
253
            newBulges = new double[bulges.length];
254
            System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
255
        }
256
        // TODO: Aqu� pueden existir casos no contemplados ...
242 257
//        System.out.println("flags = " + flags);
243
        if (flags==512 || flags==776 || flags==768) {//closed
244
			newBulges = new double[bulges.length+1];
245
			for (int j=0;j<pts.size();j++) {
246
				newPts.add(pts.get(j));
247
			}
248
			newPts.add(pts.get(0));
249
			newBulges[pts.size()] = 0;
250
		} else {
251
			for (int j=0;j<pts.size();j++) {
252
				newPts.add(pts.get(j));
253
			}
254
		}
255
		if (newPts.size() > 0) {
256
			setBulges(newBulges);
257
			List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
258
			setVertices(points);
259
		}
258
        if (flags == 512 || flags == 776 || flags == 768) {//closed
259
            newBulges = new double[bulges.length + 1];
260
            for (int j = 0; j < pts.size(); j++) {
261
                newPts.add(pts.get(j));
262
            }
263
            newPts.add(pts.get(0));
264
            newBulges[pts.size()] = 0;
265
        } else {
266
            for (int j = 0; j < pts.size(); j++) {
267
                newPts.add(pts.get(j));
268
            }
269
        }
270
        if (newPts.size() > 0) {
271
            setBulges(newBulges);
272
            List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
273
            setVertices(points);
274
        }
260 275
//		if (pts.size() > 0) {
261 276
//			setBulges(newBulges);
262 277
//			List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
......
267 282
//			// TODO: No se debe mandar nunca una polil�nea sin puntos, si esto
268 283
//			// ocurre es porque existe un error que hay que corregir ...
269 284
//		}
270
	}
271
	/* (non-Javadoc)
272
	 * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
273
	 */
274
	public void applyExtrussion() {
275
		if (getVertices() == null) {
276
			return;
277
		}
278
		 List vertices = getVertices();
279
         double[] lwPolylineExt = getNormal();
280
         // Normals and Extrusions aren`t the same
281
         if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) {
282
			lwPolylineExt[2] = 1.0;
283
		}
285
    }
286
    /* (non-Javadoc)
287
     * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
288
     */
284 289

  
285
         double elev = getElevation();
286
         List lwPolylinePoints3D = new ArrayList();
287
         for (int j=0;j<vertices.size();j++) {
288
        	 double[] point = new double[3];
289
             point[0] = ((double[])vertices.get(j))[0];
290
             point[1] = ((double[])vertices.get(j))[1];
291
             point[2] = elev;
292
             lwPolylinePoints3D.add(AcadExtrusionCalculator.
293
            		 extrude2(point, lwPolylineExt));
294
         }
295
         setElevation(elev);
296
         List newVertices = new ArrayList();
297
         for (int j=0;j<vertices.size();j++) {
298
        	 double[] point = (double[]) lwPolylinePoints3D.get(j);
299
             newVertices.add(new double[]{point[0], point[1]});
300
         }
301
         setVertices(newVertices);
302
	}
303
	/* (non-Javadoc)
304
	 * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
305
	 */
306
	public boolean has3DData() {
307
		return getElevation() != 0.0;
308
	}
309
	public double getZ() {
310
		return getElevation();
311
	}
290
    public void applyExtrussion() {
291
        if (getVertices() == null) {
292
            return;
293
        }
294
        List vertices = getVertices();
295
        double[] lwPolylineExt = getNormal();
296
        // Normals and Extrusions aren`t the same
297
        if (lwPolylineExt[0] == 0 && lwPolylineExt[1] == 0 && lwPolylineExt[2] == 0) {
298
            lwPolylineExt[2] = 1.0;
299
        }
312 300

  
313
	public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
314
		return FMapUtil.ptsToLine(getVertices(),
315
				getGeometrySubType(is3DFile));
316
	}
301
        double elev = getElevation();
302
        List lwPolylinePoints3D = new ArrayList();
303
        for (int j = 0; j < vertices.size(); j++) {
304
            double[] point = new double[3];
305
            point[0] = ((double[]) vertices.get(j))[0];
306
            point[1] = ((double[]) vertices.get(j))[1];
307
            point[2] = elev;
308
            lwPolylinePoints3D.add(AcadExtrusionCalculator.
309
                    extrude2(point, lwPolylineExt));
310
        }
311
        setElevation(elev);
312
        List newVertices = new ArrayList();
313
        for (int j = 0; j < vertices.size(); j++) {
314
            double[] point = (double[]) lwPolylinePoints3D.get(j);
315
            newVertices.add(new double[]{point[0], point[1]});
316
        }
317
        setVertices(newVertices);
318
    }
319
    /* (non-Javadoc)
320
     * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
321
     */
317 322

  
318
	public String toFMapString(boolean is3DFile) {
319
		if(is3DFile) {
320
			return "FPolyline3D";
321
		} else {
322
			return "FPolyline2D";
323
		}
324
	}
323
    public boolean has3DData() {
324
        return getElevation() != 0.0;
325
    }
325 326

  
326
	public String toString(){
327
		return "LwPolyline";
328
	}
327
    public double getZ() {
328
        return getElevation();
329
    }
329 330

  
331
    public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
332
        return FMapUtil.ptsToLine(getVertices(),
333
                getGeometrySubType(is3DFile));
334
    }
330 335

  
331
	public void transform2Block(double[] bPoint, Point2D insPoint,
332
			double[] scale, double rot,
333
			List dwgObjectsWithoutBlocks,
334
			Map handleObjWithoutBlocks, DwgFile callBack) {
335
		DwgLwPolyline transformedEntity = null;
336
		List vertices = this.getVertices();
337
		Object vertice;
338
		double[] point;
339
		if (vertices!=null) {
340
		    List transformedVertices = new ArrayList();
341
			for (int i=0; i< vertices.size(); i++) {
342
				vertice = vertices.get(i);
343
				if (vertice instanceof Point2D.Double){
344
					point = new double[] {((Point2D.Double)vertices.get(i)).x, ((Point2D.Double)vertices.get(i)).y};
345
				} else {
346
					point = (double[]) vertices.get(i);
347
				}
348
				double[] pointAux = new double[]{point[0] - bPoint[0],
349
							point[1] - bPoint[1]};
350
				double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
351
				double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
352
				transformedVertices.add(new double[]{laX, laY});
353
			}
354
			transformedEntity = (DwgLwPolyline)this.clone();
355
			transformedEntity.setVertices(transformedVertices);
356
			transformedEntity.setElevation((this.getElevation() * scale[2]));
357
			transformedEntity.setHandle(this.getHandle());
358
			dwgObjectsWithoutBlocks.add(transformedEntity);
359
			handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
336
    public String toFMapString(boolean is3DFile) {
337
        if (is3DFile) {
338
            return "FPolyline3D";
339
        } else {
340
            return "FPolyline2D";
341
        }
342
    }
343

  
344
    public String toString() {
345
        return "LwPolyline";
346
    }
347

  
348
    public void transform2Block(double[] bPoint, Point2D insPoint,
349
            double[] scale, double rot,
350
            List dwgObjectsWithoutBlocks,
351
            Map handleObjWithoutBlocks, DwgFile callBack) {
352
        DwgLwPolyline transformedEntity = null;
353
        List vertices = this.getVertices();
354
        Object vertice;
355
        double[] point;
356
        if (vertices != null) {
357
            List transformedVertices = new ArrayList();
358
            for (int i = 0; i < vertices.size(); i++) {
359
                vertice = vertices.get(i);
360
                if (vertice instanceof Point2D.Double) {
361
                    point = new double[]{((Point2D.Double) vertices.get(i)).x, ((Point2D.Double) vertices.get(i)).y};
362
                } else {
363
                    point = (double[]) vertices.get(i);
364
                }
365
                double[] pointAux = new double[]{point[0] - bPoint[0],
366
                    point[1] - bPoint[1]};
367
                double laX = insPoint.getX() + ((pointAux[0] * scale[0]) * Math.cos(rot) + (pointAux[1] * scale[1]) * (-1) * Math.sin(rot));
368
                double laY = insPoint.getY() + ((pointAux[0] * scale[0]) * Math.sin(rot) + (pointAux[1] * scale[1]) * Math.cos(rot));
369
                transformedVertices.add(new double[]{laX, laY});
370
            }
371
            transformedEntity = (DwgLwPolyline) this.clone();
372
            transformedEntity.setVertices(transformedVertices);
373
            transformedEntity.setElevation((this.getElevation() * scale[2]));
374
            transformedEntity.setHandle(this.getHandle());
375
            dwgObjectsWithoutBlocks.add(transformedEntity);
376
            handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
360 377
//			dwgObjectsWithoutBlocks.add(this);
361
		}//if
362
	}
378
        }//if
379
    }
363 380

  
364
	public Object clone(){
365
		DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
366
		this.fill(dwgLwPolyline);
367
		return dwgLwPolyline;
368
	}
381
    public Object clone() {
382
        DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
383
        this.fill(dwgLwPolyline);
384
        return dwgLwPolyline;
385
    }
369 386

  
370
	protected void fill(DwgObject obj){
371
		super.fill(obj);
372
		DwgLwPolyline myObj = (DwgLwPolyline)obj;
387
    protected void fill(DwgObject obj) {
388
        super.fill(obj);
389
        DwgLwPolyline myObj = (DwgLwPolyline) obj;
373 390

  
374
		myObj.setBulges(bulges);
375
		myObj.setConstWidth(constWidth);
376
		myObj.setElevation(elevation);
377
		myObj.setFlag(flag);
378
		myObj.setNormal(normal);
379
		myObj.setThickness(thickness);
380
		myObj.setVertices(vertices);
381
		myObj.setWidths(widths);
391
        myObj.setBulges(bulges);
392
        myObj.setConstWidth(constWidth);
393
        myObj.setElevation(elevation);
394
        myObj.setFlag(flag);
395
        myObj.setNormal(normal);
396
        myObj.setThickness(thickness);
397
        myObj.setVertices(vertices);
398
        myObj.setWidths(widths);
382 399

  
383
	}
384
	public void addVertex(IDwgVertex vertex) {
385
		vertices.add(vertex.getPoint());
400
    }
386 401

  
387
	}
402
    public void addVertex(IDwgVertex vertex) {
403
        vertices.add(vertex.getPoint());
388 404

  
389
	public int getGeometryType() {
390
		return Geometry.TYPES.MULTICURVE;
391
	}
405
    }
392 406

  
407
    public int getGeometryType() {
408
        return Geometry.TYPES.MULTICURVE;
409
    }
410

  
393 411
}

Also available in: Unified diff