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