svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libDXF / src / org / cresques / px / dxf / DxfPolyline.java @ 21930
History | View | Annotate | Download (10.4 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.px.dxf; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.geom.GeneralPath; |
29 |
import java.awt.geom.Point2D; |
30 |
import java.util.Iterator; |
31 |
import java.util.Vector; |
32 |
|
33 |
import org.gvsig.projection.cts.ICoordTrans; |
34 |
import org.gvsig.projection.cts.IProjection; |
35 |
import org.cresques.geo.Point2DZ; |
36 |
import org.gvsig.projection.geo.ViewPortData; |
37 |
import org.cresques.io.DxfGroup; |
38 |
import org.gvsig.projection.px.Extent; |
39 |
|
40 |
|
41 |
/**
|
42 |
* Entidad POLYLINE de un fichero DXF.
|
43 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
44 |
* @author jmorell
|
45 |
*/
|
46 |
public class DxfPolyline extends DxfEntity { |
47 |
final static Color baseColor = new Color(69, 106, 121); |
48 |
Vector pts = null; |
49 |
Vector faces = null; |
50 |
GeneralPath gp = null; |
51 |
int flags = 0; |
52 |
boolean closed = false; |
53 |
boolean hasFaces = false; |
54 |
private Vector bulges; |
55 |
private Color color = baseColor; //Color(255,214,132,255); |
56 |
private double elevation; |
57 |
private String subclassMarker; |
58 |
|
59 |
/**
|
60 |
* Constructor de DxfPolyline.
|
61 |
* @param proj, proyecci?n cartogr?fica en la que se encuentra el DxfPolyline.
|
62 |
* @param layer, capa del DXF en la que se encuentra el DxfPolyline.
|
63 |
*/
|
64 |
public DxfPolyline(IProjection proj, DxfLayer layer) {
|
65 |
super(proj, layer);
|
66 |
extent = new Extent();
|
67 |
pts = new Vector(); |
68 |
bulges = new Vector(); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* A?ade un punto a la polil?nea.
|
73 |
* @param pt
|
74 |
*/
|
75 |
public void add(Point2D pt) { |
76 |
pts.add(pt); |
77 |
extent.add(pt); |
78 |
} |
79 |
|
80 |
/**
|
81 |
* A?ade un bulge o par?metro de curvatura a la lista. Estos par?metros se
|
82 |
* corresponden con los v?rtices.
|
83 |
* @param bulge, par?metro de curvatura.
|
84 |
*/
|
85 |
/**
|
86 |
* 050301, jmorell: Soluci?n para implementar la lectura de polil?neas
|
87 |
* con arcos.
|
88 |
*/
|
89 |
public void addBulge(Double bulge) { |
90 |
bulges.add(bulge); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* A?ade una face a la polil?nea.
|
95 |
* @param face
|
96 |
*/
|
97 |
public void addFace(int[] face) { |
98 |
hasFaces = true;
|
99 |
|
100 |
if (faces == null) { |
101 |
faces = new Vector(); |
102 |
} |
103 |
|
104 |
faces.add(face); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* Devuelve el color de la DxfPolyline.
|
109 |
* @return Color
|
110 |
*/
|
111 |
public Color c() { |
112 |
return color;
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* Establece el color de la DxfPolyline.
|
117 |
* @param color
|
118 |
* @return Color
|
119 |
*/
|
120 |
public Color c(Color color) { |
121 |
this.color = color;
|
122 |
|
123 |
return color;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Permite reproyectar una DxfPolyline dado un conjunto de coordenadas de transformaci?n.
|
128 |
* @param rp, coordenadas de transformaci?n.
|
129 |
*/
|
130 |
public void reProject(ICoordTrans rp) { |
131 |
Vector savePts = pts;
|
132 |
|
133 |
pts = new Vector(); |
134 |
extent = new Extent();
|
135 |
|
136 |
Point2D ptDest = null; |
137 |
|
138 |
for (int i = 0; i < savePts.size(); i++) { |
139 |
ptDest = rp.getPDest().createPoint(0.0, 0.0); |
140 |
ptDest = rp.convert((Point2D) savePts.get(i), ptDest);
|
141 |
pts.add(ptDest); |
142 |
extent.add(ptDest); |
143 |
} |
144 |
|
145 |
setProjection(rp.getPDest()); |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Permite dibujar una DxfPolyline.
|
150 |
*/
|
151 |
public void draw(Graphics2D g, ViewPortData vp) { |
152 |
//AffineTransform msave=g.getTransform();
|
153 |
//g.setTransform(vp.mat);
|
154 |
Color color = null; |
155 |
|
156 |
// pinto el poligono si es preciso
|
157 |
if (dxfColor == AcadColor.BYLAYER) {
|
158 |
color = layer.getColor(); |
159 |
} else {
|
160 |
color = AcadColor.getColor(dxfColor); |
161 |
} |
162 |
|
163 |
System.out.println("PLINE color=" + color); |
164 |
newGP(vp); |
165 |
|
166 |
if (closed) {
|
167 |
g.setColor(new Color(color.getRed(), color.getBlue(), |
168 |
color.getGreen(), 0x20));
|
169 |
g.fill(gp); |
170 |
} |
171 |
|
172 |
g.setColor(color); |
173 |
g.draw(gp); |
174 |
|
175 |
//g.setTransform(msave);
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Permite generar un GeneralPath partiendo del Vector de puntos que conforma la
|
180 |
* DxfPolyline.
|
181 |
* @param vp
|
182 |
*/
|
183 |
private void newGP(ViewPortData vp) { |
184 |
//if (gp != null) return;
|
185 |
gp = new GeneralPath(); |
186 |
|
187 |
Point2D pt0 = null; |
188 |
Point2D pt = null; |
189 |
Point2D pt1 = null; |
190 |
Point2D.Double ptTmp = new Point2D.Double(0.0, 0.0); |
191 |
|
192 |
if (!hasFaces) {
|
193 |
Iterator iter = pts.iterator();
|
194 |
|
195 |
while (iter.hasNext()) {
|
196 |
pt1 = (Point2D) iter.next();
|
197 |
vp.mat.transform(pt1, ptTmp); |
198 |
|
199 |
if (pt0 == null) { |
200 |
pt0 = ptTmp; |
201 |
gp.moveTo((float) ptTmp.getX(), (float) ptTmp.getY()); |
202 |
} else {
|
203 |
gp.lineTo((float) ptTmp.getX(), (float) ptTmp.getY()); |
204 |
} |
205 |
} |
206 |
|
207 |
if (closed) {
|
208 |
gp.closePath(); |
209 |
} |
210 |
} else {
|
211 |
System.out.println("POLYLINE: caras=" + faces.size() + ", puntos=" + |
212 |
pts.size()); |
213 |
|
214 |
int[] face; |
215 |
int i0;
|
216 |
int i1;
|
217 |
Iterator iter = faces.iterator();
|
218 |
|
219 |
while (iter.hasNext()) {
|
220 |
face = (int[]) iter.next(); |
221 |
|
222 |
i0 = face[3];
|
223 |
|
224 |
for (int i = 0; i < 4; i++) { |
225 |
i1 = face[i]; |
226 |
|
227 |
if (i0 > 0) { |
228 |
pt0 = (Point2D) pts.get(i0 - 1); |
229 |
vp.mat.transform(pt0, ptTmp); |
230 |
gp.moveTo((float) ptTmp.getX(), (float) ptTmp.getY()); |
231 |
pt1 = (Point2D) pts.get(Math.abs(i1) - 1); |
232 |
vp.mat.transform(pt1, ptTmp); |
233 |
gp.lineTo((float) ptTmp.getX(), (float) ptTmp.getY()); |
234 |
} |
235 |
|
236 |
i0 = i1; |
237 |
} |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
/**
|
243 |
* Permite la escritura de entidades DxfPolyline en un fichero DXF2000.
|
244 |
* @return String, la cadena que se escribir? en el fichero con la informaci?n
|
245 |
* del DxfPolyline.
|
246 |
*/
|
247 |
public String toDxfString() { |
248 |
StringBuffer sb = null; |
249 |
sb = new StringBuffer(DxfGroup.toString(0, "POLYLINE")); |
250 |
sb.append(DxfGroup.toString(5, getHandle()));
|
251 |
sb.append(DxfGroup.toString(100, "AcDbEntity")); |
252 |
sb.append(DxfGroup.toString(100, getSubclassMarker()));
|
253 |
sb.append(DxfGroup.toString(8, layer.getName()));
|
254 |
sb.append(DxfGroup.toString(62, dxfColor));
|
255 |
sb.append(DxfGroup.toString(70, flags));
|
256 |
sb.append(DxfGroup.toString(66, 1)); |
257 |
|
258 |
Point2DZ pt = null;
|
259 |
Iterator iter = pts.iterator();
|
260 |
System.out.println("pts.size() = " + pts.size()); |
261 |
|
262 |
int i=1; |
263 |
while (iter.hasNext()) {
|
264 |
pt = (Point2DZ) iter.next(); |
265 |
sb.append(DxfGroup.toString(0, "VERTEX")); |
266 |
sb.append(DxfGroup.toString(5, getHandle()+i));
|
267 |
sb.append(DxfGroup.toString(100, "AcDbEntity")); |
268 |
sb.append(DxfGroup.toString(8, layer.getName()));
|
269 |
sb.append(DxfGroup.toString(100, "AcDbVertex")); |
270 |
sb.append(DxfGroup.toString(100, "AcDb3dPolylineVertex")); |
271 |
sb.append(DxfGroup.toString(70, 32)); |
272 |
sb.append(DxfGroup.toString(10, pt.getX(), 6)); |
273 |
sb.append(DxfGroup.toString(20, pt.getY(), 6)); |
274 |
sb.append(DxfGroup.toString(30, pt.getZ(), 6)); |
275 |
i++; |
276 |
} |
277 |
|
278 |
sb.append(DxfGroup.toString(0, "SEQEND")); |
279 |
sb.append(DxfGroup.toString(5, getHandle()+i));
|
280 |
sb.append(DxfGroup.toString(100, "AcDbEntity")); |
281 |
sb.append(DxfGroup.toString(8, layer.getName()));
|
282 |
|
283 |
return sb.toString();
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Devuelve el GeneralPath.
|
288 |
* @return GeneralPath
|
289 |
*/
|
290 |
public GeneralPath getGeneralPath(ViewPortData vp) { |
291 |
newGP(vp); |
292 |
|
293 |
return (GeneralPath) gp.clone(); |
294 |
} |
295 |
|
296 |
/**
|
297 |
* Devuelve la variable flags de una polil?nea.
|
298 |
* @return int
|
299 |
*/
|
300 |
public int getFlags() { |
301 |
return flags;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Invoca el m?todo de creaci?n de arcos para polil?neas con par?metros de
|
306 |
* curvatura.
|
307 |
* @param coord1, punto inicial del arco.
|
308 |
* @param coord2, punto final del arco.
|
309 |
* @param bulge, par?metro de curvatura.
|
310 |
* @return Vector con los puntos del arco.
|
311 |
*/
|
312 |
public static Vector createArc(Point2D coord1, Point2D coord2, double bulge) { |
313 |
return new DxfCalArcs(coord1, coord2, bulge).getPoints(1); |
314 |
} |
315 |
|
316 |
/**
|
317 |
* @return Returns the pts.
|
318 |
*/
|
319 |
public Vector getPts() { |
320 |
return pts;
|
321 |
} |
322 |
|
323 |
/**
|
324 |
* @param pts The pts to set.
|
325 |
*/
|
326 |
public void setPts(Vector pts) { |
327 |
this.pts = pts;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* @return Returns the bulges.
|
332 |
*/
|
333 |
public Vector getBulges() { |
334 |
return bulges;
|
335 |
} |
336 |
|
337 |
/**
|
338 |
* @param bulges The bulges to set.
|
339 |
*/
|
340 |
public void setBulges(Vector bulges) { |
341 |
this.bulges = bulges;
|
342 |
} |
343 |
|
344 |
/**
|
345 |
* @return Returns the elevation.
|
346 |
*/
|
347 |
public double getElevation() { |
348 |
return elevation;
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* @param elevation The elevation to set.
|
353 |
*/
|
354 |
public void setElevation(double elevation) { |
355 |
this.elevation = elevation;
|
356 |
} |
357 |
|
358 |
/**
|
359 |
* @return Returns the subclassMarker.
|
360 |
*/
|
361 |
public String getSubclassMarker() { |
362 |
return subclassMarker;
|
363 |
} |
364 |
|
365 |
/**
|
366 |
* @param subclassMarker The subclassMarker to set.
|
367 |
*/
|
368 |
public void setSubclassMarker(String subclassMarker) { |
369 |
this.subclassMarker = subclassMarker;
|
370 |
} |
371 |
} |