svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libDwg / src / org / gvsig / dwg / lib / objects / DwgLwPolyline.java @ 29001
History | View | Annotate | Download (11.2 KB)
1 | 2896 | jmorell | /* jdwglib. Java Library for reading Dwg files.
|
---|---|---|---|
2 | 23097 | fdiaz | *
|
3 | 2896 | jmorell | * 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 | 23097 | fdiaz | *
|
26 | 2896 | jmorell | * 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 | 28969 | jmvivo | package org.gvsig.dwg.lib.objects; |
36 | 2896 | jmorell | |
37 | import java.awt.geom.Point2D; |
||
38 | 10847 | azabala | import java.util.ArrayList; |
39 | 10539 | jmvivo | import java.util.List; |
40 | import java.util.Map; |
||
41 | 2896 | jmorell | |
42 | 28969 | jmvivo | import org.gvsig.dwg.lib.DwgFile; |
43 | import org.gvsig.dwg.lib.DwgObject; |
||
44 | import org.gvsig.dwg.lib.IDwg2FMap; |
||
45 | import org.gvsig.dwg.lib.IDwg3DTestable; |
||
46 | import org.gvsig.dwg.lib.IDwgBlockMember; |
||
47 | import org.gvsig.dwg.lib.IDwgExtrusionable; |
||
48 | import org.gvsig.dwg.lib.IDwgPolyline; |
||
49 | import org.gvsig.dwg.lib.IDwgVertex; |
||
50 | import org.gvsig.dwg.lib.util.AcadExtrusionCalculator; |
||
51 | import org.gvsig.dwg.lib.util.FMapUtil; |
||
52 | import org.gvsig.dwg.lib.util.GisModelCurveCalculator; |
||
53 | 23234 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
54 | 29001 | jmvivo | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
55 | 23234 | fdiaz | |
56 | 2896 | jmorell | |
57 | /**
|
||
58 | * The DwgLwPolyline class represents a DWG LwPolyline
|
||
59 | 23097 | fdiaz | *
|
60 | 2896 | jmorell | * @author jmorell
|
61 | */
|
||
62 | 23097 | fdiaz | public class DwgLwPolyline extends DwgObject |
63 | implements IDwgPolyline, IDwgExtrusionable,
|
||
64 | 10539 | jmvivo | IDwg3DTestable, IDwg2FMap, IDwgBlockMember{ |
65 | 23097 | fdiaz | |
66 | 2896 | jmorell | private int flag; |
67 | private double constWidth; |
||
68 | private double elevation; |
||
69 | private double thickness; |
||
70 | private double[] normal; |
||
71 | 10847 | azabala | private List vertices; |
72 | 10539 | jmvivo | /*
|
73 | 23097 | fdiaz | *The bulge is the tangent of 1/4 of the included angle for the arc
|
74 | *between the selected vertex and the next vertex in the polyline's vertex list.
|
||
75 | *A negative bulge value indicates that the arc goes clockwise from
|
||
76 | *the selected vertex to the next vertex.
|
||
77 | *A bulge of 0 indicates a straight segment,
|
||
78 | *and a bulge of 1 is a semicircle.
|
||
79 | 10539 | jmvivo | *
|
80 | *http://www.faqs.org/faqs/CAD/autolisp-faq/part2/section-6.html
|
||
81 | */
|
||
82 | 2896 | jmorell | private double[] bulges; |
83 | private double[][] widths; |
||
84 | 23097 | fdiaz | |
85 | |||
86 | 10847 | azabala | public DwgLwPolyline(int index) { |
87 | super(index);
|
||
88 | vertices = new ArrayList(); |
||
89 | } |
||
90 | 2896 | jmorell | /**
|
91 | * @return Returns the bulges.
|
||
92 | */
|
||
93 | public double[] getBulges() { |
||
94 | return bulges;
|
||
95 | } |
||
96 | /**
|
||
97 | * @param bulges The bulges to set.
|
||
98 | */
|
||
99 | public void setBulges(double[] bulges) { |
||
100 | this.bulges = bulges;
|
||
101 | } |
||
102 | /**
|
||
103 | * @return Returns the flag.
|
||
104 | */
|
||
105 | public int getFlag() { |
||
106 | return flag;
|
||
107 | } |
||
108 | /**
|
||
109 | * @param flag The flag to set.
|
||
110 | */
|
||
111 | public void setFlag(int flag) { |
||
112 | this.flag = flag;
|
||
113 | } |
||
114 | /**
|
||
115 | * @return Returns the vertices.
|
||
116 | */
|
||
117 | 10847 | azabala | public List getVertices() { |
118 | 2896 | jmorell | return vertices;
|
119 | } |
||
120 | /**
|
||
121 | * @param vertices The vertices to set.
|
||
122 | */
|
||
123 | 10847 | azabala | public void setVertices(List vertices) { |
124 | 2896 | jmorell | this.vertices = vertices;
|
125 | } |
||
126 | /**
|
||
127 | * @return Returns the elevation.
|
||
128 | */
|
||
129 | public double getElevation() { |
||
130 | return elevation;
|
||
131 | } |
||
132 | /**
|
||
133 | * @param elevation The elevation to set.
|
||
134 | */
|
||
135 | public void setElevation(double elevation) { |
||
136 | this.elevation = elevation;
|
||
137 | } |
||
138 | /**
|
||
139 | * @return Returns the normal.
|
||
140 | */
|
||
141 | public double[] getNormal() { |
||
142 | return normal;
|
||
143 | } |
||
144 | /* (non-Javadoc)
|
||
145 | * @see java.lang.Object#clone()
|
||
146 | */
|
||
147 | 10539 | jmvivo | // public Object clone() {
|
148 | // DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
|
||
149 | // dwgLwPolyline.setType(type);
|
||
150 | // dwgLwPolyline.setHandle(handle);
|
||
151 | // dwgLwPolyline.setVersion(version);
|
||
152 | // dwgLwPolyline.setMode(mode);
|
||
153 | // dwgLwPolyline.setLayerHandle(layerHandle);
|
||
154 | // dwgLwPolyline.setColor(color);
|
||
155 | // dwgLwPolyline.setNumReactors(numReactors);
|
||
156 | // dwgLwPolyline.setNoLinks(noLinks);
|
||
157 | // dwgLwPolyline.setLinetypeFlags(linetypeFlags);
|
||
158 | // dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
|
||
159 | // dwgLwPolyline.setSizeInBits(sizeInBits);
|
||
160 | // dwgLwPolyline.setExtendedData(extendedData);
|
||
161 | // dwgLwPolyline.setGraphicData(graphicData);
|
||
162 | // //dwgLwPolyline.setInsideBlock(insideBlock);
|
||
163 | // dwgLwPolyline.setFlag(flag);
|
||
164 | // dwgLwPolyline.setElevation(elevation);
|
||
165 | // dwgLwPolyline.setConstWidth(constWidth);
|
||
166 | // dwgLwPolyline.setThickness(thickness);
|
||
167 | // dwgLwPolyline.setNormal(normal);
|
||
168 | // dwgLwPolyline.setVertices(vertices);
|
||
169 | // dwgLwPolyline.setBulges(bulges);
|
||
170 | // dwgLwPolyline.setWidths(widths);
|
||
171 | // return dwgLwPolyline;
|
||
172 | // }
|
||
173 | 2896 | jmorell | /**
|
174 | * @return Returns the constWidth.
|
||
175 | */
|
||
176 | public double getConstWidth() { |
||
177 | return constWidth;
|
||
178 | } |
||
179 | /**
|
||
180 | * @param constWidth The constWidth to set.
|
||
181 | */
|
||
182 | public void setConstWidth(double constWidth) { |
||
183 | this.constWidth = constWidth;
|
||
184 | } |
||
185 | /**
|
||
186 | * @return Returns the thickness.
|
||
187 | */
|
||
188 | public double getThickness() { |
||
189 | return thickness;
|
||
190 | } |
||
191 | /**
|
||
192 | * @param thickness The thickness to set.
|
||
193 | */
|
||
194 | public void setThickness(double thickness) { |
||
195 | this.thickness = thickness;
|
||
196 | } |
||
197 | /**
|
||
198 | * @return Returns the widths.
|
||
199 | */
|
||
200 | public double[][] getWidths() { |
||
201 | return widths;
|
||
202 | } |
||
203 | /**
|
||
204 | * @param widths The widths to set.
|
||
205 | */
|
||
206 | public void setWidths(double[][] widths) { |
||
207 | this.widths = widths;
|
||
208 | } |
||
209 | /**
|
||
210 | * @param normal The normal to set.
|
||
211 | */
|
||
212 | public void setNormal(double[] normal) { |
||
213 | this.normal = normal;
|
||
214 | } |
||
215 | 10539 | jmvivo | /* (non-Javadoc)
|
216 | * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
|
||
217 | */
|
||
218 | public void calculateGisModel(DwgFile dwgFile) { |
||
219 | 29001 | jmvivo | if (getVertices() == null) { |
220 | 10539 | jmvivo | return;
|
221 | 29001 | jmvivo | } |
222 | 10539 | jmvivo | int flags = getFlag();
|
223 | 10847 | azabala | List pts = getVertices();
|
224 | 10539 | jmvivo | double[] bulges = getBulges(); |
225 | 10847 | azabala | List newPts = new ArrayList(); |
226 | 10539 | jmvivo | double[] newBulges = null; |
227 | if(bulges != null){ |
||
228 | newBulges = new double[bulges.length]; |
||
229 | System.arraycopy(bulges, 0, newBulges, 0, bulges.length); |
||
230 | }else{
|
||
231 | 10847 | azabala | bulges = new double[pts.size() ]; |
232 | 10539 | jmvivo | newBulges = new double[bulges.length]; |
233 | //dwg spec says numVertex (numSegments + 1)
|
||
234 | //TODO Check this
|
||
235 | 29001 | jmvivo | for(int i = 0; i < newBulges.length; i++) { |
236 | 10539 | jmvivo | bulges[i] = 0d;
|
237 | 29001 | jmvivo | } |
238 | 10539 | jmvivo | newBulges = new double[bulges.length]; |
239 | System.arraycopy(bulges, 0, newBulges, 0, bulges.length); |
||
240 | } |
||
241 | 23097 | fdiaz | // TODO: Aqu� pueden existir casos no contemplados ...
|
242 | 10539 | jmvivo | // System.out.println("flags = " + flags);
|
243 | if (flags==512 || flags==776 || flags==768) {//closed |
||
244 | newBulges = new double[bulges.length+1]; |
||
245 | 10847 | azabala | for (int j=0;j<pts.size();j++) { |
246 | newPts.add(pts.get(j)); |
||
247 | 10539 | jmvivo | } |
248 | 10847 | azabala | newPts.add(pts.get(0));
|
249 | newBulges[pts.size()] = 0;
|
||
250 | 10539 | jmvivo | } else {
|
251 | 10847 | azabala | for (int j=0;j<pts.size();j++) { |
252 | newPts.add(pts.get(j)); |
||
253 | 10539 | jmvivo | } |
254 | } |
||
255 | 10847 | azabala | if (newPts.size() > 0) { |
256 | 10539 | jmvivo | setBulges(newBulges); |
257 | 10847 | azabala | List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
|
258 | 10539 | jmvivo | setVertices(points); |
259 | 23097 | fdiaz | } |
260 | 10847 | azabala | // if (pts.size() > 0) {
|
261 | // setBulges(newBulges);
|
||
262 | // List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
|
||
263 | // setVertices(points);
|
||
264 | 23097 | fdiaz | // }
|
265 | 10847 | azabala | // else {
|
266 | 23097 | fdiaz | //// System.out.println("Encontrada polil�nea sin puntos ...");
|
267 | // // TODO: No se debe mandar nunca una polil�nea sin puntos, si esto
|
||
268 | 10847 | azabala | // // ocurre es porque existe un error que hay que corregir ...
|
269 | // }
|
||
270 | 10539 | jmvivo | } |
271 | /* (non-Javadoc)
|
||
272 | * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
|
||
273 | */
|
||
274 | public void applyExtrussion() { |
||
275 | 29001 | jmvivo | if (getVertices() == null) { |
276 | 10539 | jmvivo | return;
|
277 | 29001 | jmvivo | } |
278 | 10847 | azabala | List vertices = getVertices();
|
279 | 10539 | jmvivo | double[] lwPolylineExt = getNormal(); |
280 | // Normals and Extrusions aren`t the same
|
||
281 | 29001 | jmvivo | if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) { |
282 | lwPolylineExt[2] = 1.0; |
||
283 | } |
||
284 | 23097 | fdiaz | |
285 | 10539 | jmvivo | double elev = getElevation();
|
286 | 10847 | azabala | List lwPolylinePoints3D = new ArrayList(); |
287 | for (int j=0;j<vertices.size();j++) { |
||
288 | double[] point = new double[3]; |
||
289 | point[0] = ((double[])vertices.get(j))[0]; |
||
290 | point[1] = ((double[])vertices.get(j))[1]; |
||
291 | point[2] = elev;
|
||
292 | lwPolylinePoints3D.add(AcadExtrusionCalculator. |
||
293 | extrude2(point, lwPolylineExt)); |
||
294 | 10539 | jmvivo | } |
295 | setElevation(elev); |
||
296 | 10847 | azabala | List newVertices = new ArrayList(); |
297 | for (int j=0;j<vertices.size();j++) { |
||
298 | double[] point = (double[]) lwPolylinePoints3D.get(j); |
||
299 | newVertices.add(new double[]{point[0], point[1]}); |
||
300 | 10539 | jmvivo | } |
301 | 10847 | azabala | setVertices(newVertices); |
302 | 10539 | jmvivo | } |
303 | /* (non-Javadoc)
|
||
304 | * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
|
||
305 | */
|
||
306 | public boolean has3DData() { |
||
307 | return getElevation() != 0.0; |
||
308 | } |
||
309 | public double getZ() { |
||
310 | return getElevation();
|
||
311 | } |
||
312 | 23097 | fdiaz | |
313 | 29001 | jmvivo | public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException { |
314 | return FMapUtil.ptsToMultiCurve(getVertices(),
|
||
315 | getGeometrySubType(is3DFile)); |
||
316 | 10539 | jmvivo | } |
317 | 29001 | jmvivo | |
318 | 10539 | jmvivo | public String toFMapString(boolean is3DFile) { |
319 | 29001 | jmvivo | if(is3DFile) {
|
320 | 10539 | jmvivo | return "FPolyline3D"; |
321 | 29001 | jmvivo | } else {
|
322 | 10539 | jmvivo | return "FPolyline2D"; |
323 | 29001 | jmvivo | } |
324 | 10539 | jmvivo | } |
325 | 23097 | fdiaz | |
326 | 10539 | jmvivo | public String toString(){ |
327 | return "LwPolyline"; |
||
328 | } |
||
329 | 23097 | fdiaz | |
330 | |||
331 | 10539 | jmvivo | public void transform2Block(double[] bPoint, Point2D insPoint, |
332 | 23097 | fdiaz | double[] scale, double rot, |
333 | List dwgObjectsWithoutBlocks,
|
||
334 | 10539 | jmvivo | Map handleObjWithoutBlocks, DwgFile callBack) {
|
335 | DwgLwPolyline transformedEntity = null;
|
||
336 | 10847 | azabala | List vertices = this.getVertices(); |
337 | 23097 | fdiaz | Object vertice;
|
338 | double[] point; |
||
339 | 10539 | jmvivo | if (vertices!=null) { |
340 | 10847 | azabala | List transformedVertices = new ArrayList(); |
341 | for (int i=0; i< vertices.size(); i++) { |
||
342 | 23097 | fdiaz | vertice = vertices.get(i); |
343 | if (vertice instanceof Point2D.Double){ |
||
344 | point = new double[] {((Point2D.Double)vertices.get(i)).x, ((Point2D.Double)vertices.get(i)).y}; |
||
345 | } else {
|
||
346 | point = (double[]) vertices.get(i); |
||
347 | } |
||
348 | double[] pointAux = new double[]{point[0] - bPoint[0], |
||
349 | 10847 | azabala | point[1] - bPoint[1]}; |
350 | double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot)); |
||
351 | double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot)); |
||
352 | transformedVertices.add(new double[]{laX, laY}); |
||
353 | 10539 | jmvivo | } |
354 | transformedEntity = (DwgLwPolyline)this.clone();
|
||
355 | transformedEntity.setVertices(transformedVertices); |
||
356 | transformedEntity.setElevation((this.getElevation() * scale[2])); |
||
357 | transformedEntity.setHandle(this.getHandle());
|
||
358 | dwgObjectsWithoutBlocks.add(transformedEntity); |
||
359 | handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity); |
||
360 | // dwgObjectsWithoutBlocks.add(this);
|
||
361 | }//if
|
||
362 | } |
||
363 | 23097 | fdiaz | |
364 | 10539 | jmvivo | public Object clone(){ |
365 | DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
|
||
366 | this.fill(dwgLwPolyline);
|
||
367 | return dwgLwPolyline;
|
||
368 | } |
||
369 | 23097 | fdiaz | |
370 | 10539 | jmvivo | protected void fill(DwgObject obj){ |
371 | super.fill(obj);
|
||
372 | DwgLwPolyline myObj = (DwgLwPolyline)obj; |
||
373 | |||
374 | myObj.setBulges(bulges); |
||
375 | myObj.setConstWidth(constWidth); |
||
376 | myObj.setElevation(elevation); |
||
377 | myObj.setFlag(flag); |
||
378 | myObj.setNormal(normal); |
||
379 | myObj.setThickness(thickness); |
||
380 | myObj.setVertices(vertices); |
||
381 | myObj.setWidths(widths); |
||
382 | |||
383 | } |
||
384 | 10847 | azabala | public void addVertex(IDwgVertex vertex) { |
385 | vertices.add(vertex.getPoint()); |
||
386 | 23097 | fdiaz | |
387 | 10847 | azabala | } |
388 | 10539 | jmvivo | |
389 | 29001 | jmvivo | public int getGeometrySubType(boolean is3DFile) { |
390 | if (is3DFile) {
|
||
391 | return Geometry.SUBTYPES.GEOM3D;
|
||
392 | } else {
|
||
393 | return Geometry.SUBTYPES.GEOM2D;
|
||
394 | } |
||
395 | } |
||
396 | |||
397 | public int getGeometryType() { |
||
398 | return Geometry.TYPES.MULTICURVE;
|
||
399 | } |
||
400 | |||
401 | 2896 | jmorell | } |