svn-gvsig-desktop / branches / v10 / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgPolyline2D.java @ 10539
History | View | Annotate | Download (15 KB)
1 |
/* jdwglib. Java Library for reading Dwg files.
|
---|---|
2 |
*
|
3 |
* Author: Jose Morell Rama (jose.morell@gmail.com).
|
4 |
* Port from the Pythoncad Dwg library by Art Haas.
|
5 |
*
|
6 |
* Copyright (C) 2005 Jose Morell, IVER TI S.A. 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 |
* Jose Morell (jose.morell@gmail.com)
|
25 |
*
|
26 |
* or
|
27 |
*
|
28 |
* IVER TI S.A.
|
29 |
* C/Salamanca, 50
|
30 |
* 46005 Valencia
|
31 |
* Spain
|
32 |
* +34 963163400
|
33 |
* dac@iver.es
|
34 |
*/
|
35 |
package com.iver.cit.jdwglib.dwg.objects; |
36 |
|
37 |
import java.awt.geom.Point2D; |
38 |
import java.util.ArrayList; |
39 |
import java.util.List; |
40 |
import java.util.Map; |
41 |
|
42 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
43 |
import com.iver.cit.gvsig.fmap.core.FShape; |
44 |
import com.iver.cit.jdwglib.dwg.DwgFile; |
45 |
import com.iver.cit.jdwglib.dwg.DwgHandleReference; |
46 |
import com.iver.cit.jdwglib.dwg.DwgObject; |
47 |
import com.iver.cit.jdwglib.dwg.IDwg2FMap; |
48 |
import com.iver.cit.jdwglib.dwg.IDwg3DTestable; |
49 |
import com.iver.cit.jdwglib.dwg.IDwgBlockMember; |
50 |
import com.iver.cit.jdwglib.dwg.IDwgExtrusionable; |
51 |
import com.iver.cit.jdwglib.dwg.IDwgPolyline; |
52 |
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator; |
53 |
import com.iver.cit.jdwglib.util.FMapUtil; |
54 |
import com.iver.cit.jdwglib.util.GisModelCurveCalculator; |
55 |
|
56 |
/**
|
57 |
* The DwgPolyline2D class represents a DWG Polyline2D
|
58 |
*
|
59 |
* @author jmorell
|
60 |
*/
|
61 |
public class DwgPolyline2D extends DwgObject |
62 |
implements IDwgPolyline, IDwgExtrusionable, IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
|
63 |
|
64 |
|
65 |
public DwgPolyline2D(int index) { |
66 |
super(index);
|
67 |
// TODO Auto-generated constructor stub
|
68 |
} |
69 |
private int flags; |
70 |
private int curveType; |
71 |
private double initWidth; |
72 |
private double endWidth; |
73 |
private double thickness; |
74 |
private double elevation; |
75 |
private double[] extrusion; |
76 |
private DwgHandleReference firstVertexHandle = null; |
77 |
private DwgHandleReference lastVertexHandle = null; |
78 |
private DwgHandleReference seqendHandle = null; |
79 |
private Point2D[] pts; |
80 |
private double[] bulges; |
81 |
|
82 |
/**
|
83 |
* @return Returns the firstVertexHandle.
|
84 |
*/
|
85 |
public DwgHandleReference getFirstVertexHandle() {
|
86 |
return firstVertexHandle;
|
87 |
} |
88 |
/**
|
89 |
* @param firstVertexHandle The firstVertexHandle to set.
|
90 |
*/
|
91 |
public void setFirstVertexHandle(DwgHandleReference firstVertexHandle) { |
92 |
this.firstVertexHandle = firstVertexHandle;
|
93 |
} |
94 |
/**
|
95 |
* @return Returns the flags.
|
96 |
*/
|
97 |
public int getFlags() { |
98 |
return flags;
|
99 |
} |
100 |
/**
|
101 |
* @param flags The flags to set.
|
102 |
*/
|
103 |
public void setFlags(int flags) { |
104 |
this.flags = flags;
|
105 |
} |
106 |
/**
|
107 |
* @return Returns the lastVertexHandle.
|
108 |
*/
|
109 |
public DwgHandleReference getLastVertexHandle() {
|
110 |
return lastVertexHandle;
|
111 |
} |
112 |
/**
|
113 |
* @param lastVertexHandle The lastVertexHandle to set.
|
114 |
*/
|
115 |
public void setLastVertexHandle(DwgHandleReference lastVertexHandle) { |
116 |
this.lastVertexHandle = lastVertexHandle;
|
117 |
} |
118 |
/**
|
119 |
* @return Returns the pts.
|
120 |
*/
|
121 |
public Point2D[] getPts() { |
122 |
return pts;
|
123 |
} |
124 |
/**
|
125 |
* @param pts The pts to set.
|
126 |
*/
|
127 |
public void setPts(Point2D[] pts) { |
128 |
this.pts = pts;
|
129 |
} |
130 |
/**
|
131 |
* @return Returns the bulges.
|
132 |
*/
|
133 |
public double[] getBulges() { |
134 |
return bulges;
|
135 |
} |
136 |
/**
|
137 |
* @param bulges The bulges to set.
|
138 |
*/
|
139 |
public void setBulges(double[] bulges) { |
140 |
this.bulges = bulges;
|
141 |
} |
142 |
/**
|
143 |
* @return Returns the initWidth.
|
144 |
*/
|
145 |
public double getInitWidth() { |
146 |
return initWidth;
|
147 |
} |
148 |
/**
|
149 |
* @param initWidth The initWidth to set.
|
150 |
*/
|
151 |
public void setInitWidth(double initWidth) { |
152 |
this.initWidth = initWidth;
|
153 |
} |
154 |
/**
|
155 |
* @return Returns the seqendHandle.
|
156 |
*/
|
157 |
public DwgHandleReference getSeqendHandle() {
|
158 |
return seqendHandle;
|
159 |
} |
160 |
/**
|
161 |
* @param seqendHandle The seqendHandle to set.
|
162 |
*/
|
163 |
public void setSeqendHandle(DwgHandleReference seqendHandle) { |
164 |
this.seqendHandle = seqendHandle;
|
165 |
} |
166 |
/**
|
167 |
* @return Returns the thickness.
|
168 |
*/
|
169 |
public double getThickness() { |
170 |
return thickness;
|
171 |
} |
172 |
/**
|
173 |
* @param thickness The thickness to set.
|
174 |
*/
|
175 |
public void setThickness(double thickness) { |
176 |
this.thickness = thickness;
|
177 |
} |
178 |
/**
|
179 |
* @return Returns the curveType.
|
180 |
*/
|
181 |
public int getCurveType() { |
182 |
return curveType;
|
183 |
} |
184 |
/**
|
185 |
* @param curveType The curveType to set.
|
186 |
*/
|
187 |
public void setCurveType(int curveType) { |
188 |
this.curveType = curveType;
|
189 |
} |
190 |
/**
|
191 |
* @return Returns the elevation.
|
192 |
*/
|
193 |
public double getElevation() { |
194 |
return elevation;
|
195 |
} |
196 |
/**
|
197 |
* @param elevation The elevation to set.
|
198 |
*/
|
199 |
public void setElevation(double elevation) { |
200 |
this.elevation = elevation;
|
201 |
} |
202 |
/**
|
203 |
* @return Returns the endWidth.
|
204 |
*/
|
205 |
public double getEndWidth() { |
206 |
return endWidth;
|
207 |
} |
208 |
/**
|
209 |
* @param endWidth The endWidth to set.
|
210 |
*/
|
211 |
public void setEndWidth(double endWidth) { |
212 |
this.endWidth = endWidth;
|
213 |
} |
214 |
/**
|
215 |
* @return Returns the extrusion.
|
216 |
*/
|
217 |
public double[] getExtrusion() { |
218 |
return extrusion;
|
219 |
} |
220 |
/**
|
221 |
* @param extrusion The extrusion to set.
|
222 |
*/
|
223 |
public void setExtrusion(double[] extrusion) { |
224 |
this.extrusion = extrusion;
|
225 |
} |
226 |
|
227 |
public void calculateGisModel(DwgFile dwgFile){ |
228 |
int flags = getFlags();
|
229 |
// En estas dos lineas de abajo y en el resto del metodo
|
230 |
// se mantiene el mecanismo anterior al refactoring.
|
231 |
// TODO: Pensar si deberiamos coger el handle completo.
|
232 |
// Tal vez deberiamos tomar el handle completo y evaluar
|
233 |
// a donde apuntan (pueden haber 2 handles con codigo y offset
|
234 |
// distintos y que, sin embargo apunten al mismo objeto).
|
235 |
int firstHandle = getFirstVertexHandle().getOffset();
|
236 |
int lastHandle = getLastVertexHandle().getOffset();
|
237 |
ArrayList pts = new ArrayList(); |
238 |
ArrayList bulges = new ArrayList(); |
239 |
double[] pt = new double[3]; |
240 |
double bulge = 0d; |
241 |
|
242 |
DwgObject first = dwgFile.getDwgObjectFromHandle(firstHandle); |
243 |
DwgObject last = dwgFile.getDwgObjectFromHandle(lastHandle); |
244 |
if(first == null || last == null){ |
245 |
System.out.println("Polyline2D con vertices inicial o final a null"); |
246 |
return;
|
247 |
} |
248 |
|
249 |
if(!(first instanceof DwgVertex2D)){ |
250 |
System.out.println("El primer vertice de Polyline2D es "+ |
251 |
first.getClass().getName()); |
252 |
return;
|
253 |
} |
254 |
|
255 |
if(!(last instanceof DwgVertex2D)){ |
256 |
System.out.println("El primer vertice de Polyline2D es "+ |
257 |
first.getClass().getName()); |
258 |
return;
|
259 |
} |
260 |
|
261 |
int firstObjIdx = dwgFile.getIndexOf(first);
|
262 |
int lastObjIdx = dwgFile.getIndexOf(last);
|
263 |
if(firstObjIdx == -1 || lastObjIdx == -1){ |
264 |
System.out.println("Calculate GIS Model: Problemas en la LinkedList: 1?="+firstObjIdx+",Ultimo="+lastObjIdx); |
265 |
return;
|
266 |
} |
267 |
|
268 |
// pt = ((DwgVertex2D)first).getPoint();
|
269 |
// pts.add(new Point2D.Double(pt[0], pt[1]));
|
270 |
// bulge = ((DwgVertex2D)first).getBulge();
|
271 |
// bulges.add(new Double(bulge));
|
272 |
|
273 |
for(int i = firstObjIdx; i <= lastObjIdx; i++){ |
274 |
DwgObject obj = dwgFile.getDwgObject(i); |
275 |
if(obj instanceof DwgVertex2D){ |
276 |
DwgVertex2D vertex = (DwgVertex2D) obj; |
277 |
pt = vertex.getPoint(); |
278 |
pts.add(new Point2D.Double(pt[0], pt[1])); |
279 |
bulge = vertex.getBulge(); |
280 |
bulges.add(new Double(bulge)); |
281 |
}else{
|
282 |
System.out.println("Encontrado "+obj.getClass().getName()+" en la lista de vertices de Polyline2D"); |
283 |
} |
284 |
}//for
|
285 |
|
286 |
if (pts.size()>0) { |
287 |
Point2D[] newPts = new Point2D[pts.size()]; |
288 |
if ((flags & 0x1)==0x1) { |
289 |
newPts = new Point2D[pts.size()+1]; |
290 |
for (int j=0;j<pts.size();j++) { |
291 |
newPts[j] = (Point2D)pts.get(j);
|
292 |
} |
293 |
newPts[pts.size()] = (Point2D)pts.get(0); |
294 |
bulges.add(new Double(0)); |
295 |
} else {
|
296 |
for (int j=0;j<pts.size();j++) { |
297 |
newPts[j] = (Point2D)pts.get(j);
|
298 |
} |
299 |
} |
300 |
double[] bs = new double[bulges.size()]; |
301 |
for (int j=0;j<bulges.size();j++) { |
302 |
bs[j] = ((Double)bulges.get(j)).doubleValue();
|
303 |
} |
304 |
setBulges(bs); |
305 |
Point2D[] points = GisModelCurveCalculator. |
306 |
calculateGisModelBulge(newPts, bs); |
307 |
setPts(points); |
308 |
} else {
|
309 |
System.out.println("Encontrada polil?nea sin puntos ..."); |
310 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
311 |
// ocurre es porque existe un error que hay que corregir ...
|
312 |
} |
313 |
} |
314 |
//TODO Este metodo es antiguo y MUY INEFICIENTE
|
315 |
//No obstante lo dejo porque hace referencia a SeqEnd y a un posible bug
|
316 |
public void calculateGisModel(List dwgObjects) { |
317 |
int flags = getFlags();
|
318 |
// En estas dos lineas de abajo y en el resto del metodo
|
319 |
// se mantiene el mecanismo anterior al refactoring.
|
320 |
// TODO: Pensar si deberiamos coger el handle completo.
|
321 |
// Tal vez deberiamos tomar el handle completo y evaluar
|
322 |
// a donde apuntan (pueden haber 2 handles con codigo y offset
|
323 |
// distintos y que, sin embargo apunten al mismo objeto).
|
324 |
int firstHandle = getFirstVertexHandle().getOffset();
|
325 |
int lastHandle = getLastVertexHandle().getOffset();
|
326 |
ArrayList pts = new ArrayList(); |
327 |
ArrayList bulges = new ArrayList(); |
328 |
double[] pt = new double[3]; |
329 |
|
330 |
//TODO Esto cambiarlo. Es lento y poco elegante
|
331 |
|
332 |
for (int j=0;j<dwgObjects.size();j++) { |
333 |
DwgObject firstVertex = (DwgObject)dwgObjects.get(j); |
334 |
|
335 |
if (firstVertex instanceof DwgVertex2D) { |
336 |
int vertexHandle = firstVertex.getHandle().getOffset();
|
337 |
if (vertexHandle==firstHandle) {
|
338 |
int k=0; |
339 |
while (true) { |
340 |
DwgObject vertex = (DwgObject)dwgObjects.get(j+k); |
341 |
int vHandle = vertex.getHandle().getOffset();
|
342 |
if (vertex instanceof DwgVertex2D) { |
343 |
pt = ((DwgVertex2D)vertex).getPoint(); |
344 |
pts.add(new Point2D.Double(pt[0], pt[1])); |
345 |
double bulge = ((DwgVertex2D)vertex).getBulge();
|
346 |
bulges.add(new Double(bulge)); |
347 |
k++; |
348 |
if (vHandle==lastHandle && vertex instanceof DwgVertex2D) { |
349 |
break;
|
350 |
} |
351 |
} else if (vertex instanceof DwgSeqend) { |
352 |
// 051116, jmorell: Polil?neas_ACAD2000.dwg tiene un DwgSeqend en mitad de
|
353 |
//una secuencia de v?rtices. Precauci?n con esto puesto que es posible que esta
|
354 |
// condici?n fuera requerida en la carga de otros DWGs.
|
355 |
//break;
|
356 |
k++; |
357 |
} |
358 |
}//while
|
359 |
}//if first handle
|
360 |
}//if
|
361 |
}//for
|
362 |
|
363 |
if (pts.size()>0) { |
364 |
Point2D[] newPts = new Point2D[pts.size()]; |
365 |
if ((flags & 0x1)==0x1) { |
366 |
newPts = new Point2D[pts.size()+1]; |
367 |
for (int j=0;j<pts.size();j++) { |
368 |
newPts[j] = (Point2D)pts.get(j);
|
369 |
} |
370 |
newPts[pts.size()] = (Point2D)pts.get(0); |
371 |
bulges.add(new Double(0)); |
372 |
} else {
|
373 |
for (int j=0;j<pts.size();j++) { |
374 |
newPts[j] = (Point2D)pts.get(j);
|
375 |
} |
376 |
} |
377 |
double[] bs = new double[bulges.size()]; |
378 |
for (int j=0;j<bulges.size();j++) { |
379 |
bs[j] = ((Double)bulges.get(j)).doubleValue();
|
380 |
} |
381 |
setBulges(bs); |
382 |
Point2D[] points = GisModelCurveCalculator. |
383 |
calculateGisModelBulge(newPts, bs); |
384 |
setPts(points); |
385 |
} else {
|
386 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
387 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
388 |
// ocurre es porque existe un error que hay que corregir ...
|
389 |
} |
390 |
} |
391 |
/* (non-Javadoc)
|
392 |
* @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
|
393 |
*/
|
394 |
public void applyExtrussion() { |
395 |
if(getPts() == null) |
396 |
return;
|
397 |
Point2D[] vertices = getPts(); |
398 |
double[] polyline2DExt = getExtrusion(); |
399 |
double elev = getElevation();
|
400 |
double[][] polylinePoints3D = new double[vertices.length][3]; |
401 |
for (int j=0;j<vertices.length;j++) { |
402 |
polylinePoints3D[j][0] = vertices[j].getX();
|
403 |
polylinePoints3D[j][1] = vertices[j].getY();
|
404 |
polylinePoints3D[j][2] = elev;
|
405 |
polylinePoints3D[j] = AcadExtrusionCalculator.extrude2(polylinePoints3D[j], polyline2DExt); |
406 |
} |
407 |
setElevation(elev); |
408 |
for (int j=0;j<vertices.length;j++) { |
409 |
vertices[j] = new Point2D.Double(polylinePoints3D[j][0], polylinePoints3D[j][1]); |
410 |
} |
411 |
setPts(vertices); |
412 |
} |
413 |
/* (non-Javadoc)
|
414 |
* @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
|
415 |
*/
|
416 |
public boolean has3DData() { |
417 |
return (getElevation() != 0.0); |
418 |
} |
419 |
/* (non-Javadoc)
|
420 |
* @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#getZ()
|
421 |
*/
|
422 |
public double getZ() { |
423 |
return getElevation();
|
424 |
} |
425 |
/* (non-Javadoc)
|
426 |
* @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapGeometry(boolean)
|
427 |
*/
|
428 |
public FShape toFMapGeometry(boolean is3DFile) { |
429 |
FPolyline2D pline = null;
|
430 |
Point2D[] points = getPts(); |
431 |
double elev = getElevation();
|
432 |
|
433 |
if (points != null) { |
434 |
if (is3DFile) {
|
435 |
double[][] pline3D = new double[points.length][3]; |
436 |
for (int j = 0; j < points.length; j++) { |
437 |
pline3D[j][0] = points[j].getX();
|
438 |
pline3D[j][1] = points[j].getY();
|
439 |
pline3D[j][2] = elev;
|
440 |
} |
441 |
pline = FMapUtil.points3DToFPolyline3D(pline3D); |
442 |
} else {
|
443 |
pline = FMapUtil.points2DToFPolyline2D(points); |
444 |
} |
445 |
} |
446 |
return pline;
|
447 |
} |
448 |
/* (non-Javadoc)
|
449 |
* @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapString(boolean)
|
450 |
*/
|
451 |
public String toFMapString(boolean is3DFile) { |
452 |
if(is3DFile)
|
453 |
return "FPolyline3D"; |
454 |
else
|
455 |
return "FPolyline2D"; |
456 |
} |
457 |
|
458 |
public String toString(){ |
459 |
return "Polyline2D"; |
460 |
} |
461 |
public void transform2Block(double[] bPoint, Point2D insPoint, |
462 |
double[] scale, double rot, |
463 |
List dwgObjectsWithoutBlocks,
|
464 |
Map handleObjWithoutBlocks, DwgFile callBack) {
|
465 |
DwgPolyline2D transformedEntity = null;
|
466 |
Point2D[] vertices = this.getPts(); |
467 |
if (vertices != null) { |
468 |
Point2D[] transformedVertices = new Point2D[vertices.length]; |
469 |
for (int i=0;i<vertices.length;i++) { |
470 |
Point2D pointAux = new Point2D.Double(vertices[i].getX() - bPoint[0], vertices[i].getY() - bPoint[1]); |
471 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
472 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
473 |
transformedVertices[i] = new Point2D.Double(laX, laY); |
474 |
} |
475 |
transformedEntity = (DwgPolyline2D)this.clone();
|
476 |
transformedEntity.setPts(transformedVertices); |
477 |
transformedEntity.setElevation((this.getElevation() * scale[2])); |
478 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
479 |
handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity); |
480 |
} |
481 |
} |
482 |
/* (non-Javadoc)
|
483 |
* @see java.lang.Object#clone()
|
484 |
*/
|
485 |
public Object clone(){ |
486 |
DwgPolyline2D obj = new DwgPolyline2D(index);
|
487 |
this.fill(obj);
|
488 |
return obj;
|
489 |
} |
490 |
|
491 |
protected void fill(DwgObject obj){ |
492 |
super.fill(obj);
|
493 |
DwgPolyline2D myObj = (DwgPolyline2D)obj; |
494 |
|
495 |
myObj.setBulges(bulges); |
496 |
myObj.setCurveType(curveType); |
497 |
myObj.setElevation(elevation); |
498 |
myObj.setEndWidth(endWidth); |
499 |
myObj.setExtrusion(extrusion); |
500 |
myObj.setFirstVertexHandle(firstVertexHandle); |
501 |
myObj.setFlags(flags); |
502 |
myObj.setInitWidth(initWidth); |
503 |
myObj.setLastVertexHandle(lastVertexHandle); |
504 |
myObj.setPts(pts); |
505 |
myObj.setSeqendHandle(seqendHandle); |
506 |
myObj.setThickness(thickness); |
507 |
|
508 |
} |
509 |
|
510 |
} |