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