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