root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgLwPolyline.java @ 10820
History | View | Annotate | Download (11.5 KB)
1 | 2896 | jmorell | /* 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 | 10632 | azabala | import java.util.ArrayList; |
39 | 9825 | azabala | import java.util.List; |
40 | 10240 | azabala | import java.util.Map; |
41 | 2896 | jmorell | |
42 | 9830 | azabala | import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
43 | 10820 | azabala | import com.iver.cit.gvsig.fmap.core.IGeometry; |
44 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
45 | 9843 | azabala | import com.iver.cit.jdwglib.dwg.DwgFile; |
46 | 2896 | jmorell | import com.iver.cit.jdwglib.dwg.DwgObject; |
47 | 9830 | azabala | import com.iver.cit.jdwglib.dwg.IDwg2FMap; |
48 | 9825 | azabala | import com.iver.cit.jdwglib.dwg.IDwg3DTestable; |
49 | 9843 | azabala | import com.iver.cit.jdwglib.dwg.IDwgBlockMember; |
50 | 9825 | azabala | import com.iver.cit.jdwglib.dwg.IDwgExtrusionable; |
51 | import com.iver.cit.jdwglib.dwg.IDwgPolyline; |
||
52 | 10632 | azabala | import com.iver.cit.jdwglib.dwg.IDwgVertex; |
53 | 9825 | azabala | import com.iver.cit.jdwglib.util.AcadExtrusionCalculator; |
54 | 9830 | azabala | import com.iver.cit.jdwglib.util.FMapUtil; |
55 | 9825 | azabala | import com.iver.cit.jdwglib.util.GisModelCurveCalculator; |
56 | 2896 | jmorell | |
57 | /**
|
||
58 | * The DwgLwPolyline class represents a DWG LwPolyline
|
||
59 | *
|
||
60 | * @author jmorell
|
||
61 | */
|
||
62 | 9825 | azabala | public class DwgLwPolyline extends DwgObject |
63 | 9830 | azabala | implements IDwgPolyline, IDwgExtrusionable,
|
64 | 9843 | azabala | IDwg3DTestable, IDwg2FMap, IDwgBlockMember{ |
65 | 10632 | azabala | |
66 | 2896 | jmorell | private int flag; |
67 | private double constWidth; |
||
68 | private double elevation; |
||
69 | private double thickness; |
||
70 | private double[] normal; |
||
71 | 10632 | azabala | private List vertices; |
72 | 9969 | azabala | /*
|
73 | *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 | 10223 | azabala | *
|
80 | *http://www.faqs.org/faqs/CAD/autolisp-faq/part2/section-6.html
|
||
81 | 9969 | azabala | */
|
82 | 2896 | jmorell | private double[] bulges; |
83 | private double[][] widths; |
||
84 | |||
85 | 10220 | azabala | |
86 | 10632 | 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 | 10632 | azabala | public List getVertices() { |
118 | 2896 | jmorell | return vertices;
|
119 | } |
||
120 | /**
|
||
121 | * @param vertices The vertices to set.
|
||
122 | */
|
||
123 | 10632 | 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 | 10174 | fdiaz | // 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 | 9825 | azabala | /* (non-Javadoc)
|
216 | * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
|
||
217 | */
|
||
218 | 9977 | azabala | public void calculateGisModel(DwgFile dwgFile) { |
219 | 9825 | azabala | if (getVertices() == null) |
220 | return;
|
||
221 | int flags = getFlag();
|
||
222 | 10632 | azabala | List pts = getVertices();
|
223 | 9825 | azabala | double[] bulges = getBulges(); |
224 | 10632 | azabala | List newPts = new ArrayList(); |
225 | 10223 | azabala | double[] newBulges = null; |
226 | if(bulges != null){ |
||
227 | newBulges = new double[bulges.length]; |
||
228 | System.arraycopy(bulges, 0, newBulges, 0, bulges.length); |
||
229 | }else{
|
||
230 | 10632 | azabala | bulges = new double[pts.size() ]; |
231 | 10223 | azabala | newBulges = new double[bulges.length]; |
232 | //dwg spec says numVertex (numSegments + 1)
|
||
233 | //TODO Check this
|
||
234 | for(int i = 0; i < newBulges.length; i++) |
||
235 | bulges[i] = 0d;
|
||
236 | newBulges = new double[bulges.length]; |
||
237 | System.arraycopy(bulges, 0, newBulges, 0, bulges.length); |
||
238 | } |
||
239 | 9825 | azabala | // TODO: Aqu? pueden existir casos no contemplados ...
|
240 | // System.out.println("flags = " + flags);
|
||
241 | 10223 | azabala | if (flags==512 || flags==776 || flags==768) {//closed |
242 | 9825 | azabala | newBulges = new double[bulges.length+1]; |
243 | 10632 | azabala | for (int j=0;j<pts.size();j++) { |
244 | newPts.add(pts.get(j)); |
||
245 | 9825 | azabala | } |
246 | 10632 | azabala | newPts.add(pts.get(0));
|
247 | newBulges[pts.size()] = 0;
|
||
248 | 9825 | azabala | } else {
|
249 | 10632 | azabala | for (int j=0;j<pts.size();j++) { |
250 | newPts.add(pts.get(j)); |
||
251 | 9825 | azabala | } |
252 | } |
||
253 | 10632 | azabala | if (newPts.size() > 0) { |
254 | 9825 | azabala | setBulges(newBulges); |
255 | 10632 | azabala | List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
|
256 | 9825 | azabala | setVertices(points); |
257 | 10632 | azabala | } |
258 | // if (pts.size() > 0) {
|
||
259 | // setBulges(newBulges);
|
||
260 | // List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
|
||
261 | // setVertices(points);
|
||
262 | // }
|
||
263 | // else {
|
||
264 | //// System.out.println("Encontrada polil?nea sin puntos ...");
|
||
265 | // // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
||
266 | // // ocurre es porque existe un error que hay que corregir ...
|
||
267 | // }
|
||
268 | 9825 | azabala | } |
269 | /* (non-Javadoc)
|
||
270 | * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
|
||
271 | */
|
||
272 | public void applyExtrussion() { |
||
273 | if (getVertices() == null) |
||
274 | return;
|
||
275 | 10632 | azabala | List vertices = getVertices();
|
276 | 9825 | azabala | double[] lwPolylineExt = getNormal(); |
277 | // Normals and Extrusions aren`t the same
|
||
278 | 10360 | azabala | if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) |
279 | lwPolylineExt[2] = 1.0; |
||
280 | |||
281 | 9825 | azabala | double elev = getElevation();
|
282 | 10632 | azabala | List lwPolylinePoints3D = new ArrayList(); |
283 | for (int j=0;j<vertices.size();j++) { |
||
284 | double[] point = new double[3]; |
||
285 | point[0] = ((double[])vertices.get(j))[0]; |
||
286 | point[1] = ((double[])vertices.get(j))[1]; |
||
287 | point[2] = elev;
|
||
288 | lwPolylinePoints3D.add(AcadExtrusionCalculator. |
||
289 | extrude2(point, lwPolylineExt)); |
||
290 | 9825 | azabala | } |
291 | setElevation(elev); |
||
292 | 10632 | azabala | List newVertices = new ArrayList(); |
293 | for (int j=0;j<vertices.size();j++) { |
||
294 | double[] point = (double[]) lwPolylinePoints3D.get(j); |
||
295 | newVertices.add(new double[]{point[0], point[1]}); |
||
296 | 9825 | azabala | } |
297 | 10632 | azabala | setVertices(newVertices); |
298 | 9825 | azabala | } |
299 | /* (non-Javadoc)
|
||
300 | * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
|
||
301 | */
|
||
302 | public boolean has3DData() { |
||
303 | return getElevation() != 0.0; |
||
304 | } |
||
305 | 9830 | azabala | public double getZ() { |
306 | return getElevation();
|
||
307 | } |
||
308 | 10820 | azabala | public IGeometry toFMapGeometry(boolean is3DFile) { |
309 | 9830 | azabala | FPolyline2D lwpline = null;
|
310 | 10632 | azabala | List pts = getVertices();
|
311 | 9830 | azabala | double elev = getElevation();
|
312 | 10632 | azabala | if (pts != null && pts.size() > 0) { |
313 | 9830 | azabala | |
314 | if (is3DFile) {
|
||
315 | 10632 | azabala | List pline3D = new ArrayList(); |
316 | for (int j = 0; j < pts.size(); j++) { |
||
317 | 10820 | azabala | Object vertex = pts.get(j);
|
318 | 10632 | azabala | double[] pt = new double[3]; |
319 | 10820 | azabala | if(vertex instanceof double[]){ |
320 | double[] vertexArray = (double[])vertex; |
||
321 | pt[0] = vertexArray[0]; |
||
322 | pt[1] = vertexArray[1]; |
||
323 | }else if(vertex instanceof Point2D){ |
||
324 | Point2D vertexPt = (Point2D)vertex; |
||
325 | pt[0] = vertexPt.getX();
|
||
326 | pt[1] = vertexPt.getY();
|
||
327 | } |
||
328 | 10632 | azabala | pt[2] = elev;
|
329 | pline3D.add(pt); |
||
330 | 9830 | azabala | } |
331 | lwpline = FMapUtil.points3DToFPolyline3D(pline3D); |
||
332 | } else {
|
||
333 | lwpline = FMapUtil.points2DToFPolyline2D(pts); |
||
334 | } |
||
335 | |||
336 | } |
||
337 | 10820 | azabala | return ShapeFactory.createGeometry(lwpline);
|
338 | 9830 | azabala | } |
339 | public String toFMapString(boolean is3DFile) { |
||
340 | if(is3DFile)
|
||
341 | return "FPolyline3D"; |
||
342 | else
|
||
343 | return "FPolyline2D"; |
||
344 | } |
||
345 | |||
346 | public String toString(){ |
||
347 | return "LwPolyline"; |
||
348 | } |
||
349 | 10223 | azabala | |
350 | |||
351 | 10240 | azabala | public void transform2Block(double[] bPoint, Point2D insPoint, |
352 | double[] scale, double rot, |
||
353 | List dwgObjectsWithoutBlocks,
|
||
354 | Map handleObjWithoutBlocks, DwgFile callBack) {
|
||
355 | 10064 | azabala | DwgLwPolyline transformedEntity = null;
|
356 | 10632 | azabala | List vertices = this.getVertices(); |
357 | 9843 | azabala | if (vertices!=null) { |
358 | 10632 | azabala | List transformedVertices = new ArrayList(); |
359 | for (int i=0; i< vertices.size(); i++) { |
||
360 | double[] point = (double[]) vertices.get(i); |
||
361 | double[] pointAux = new double[]{point[0] - bPoint[0], |
||
362 | point[1] - bPoint[1]}; |
||
363 | double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot)); |
||
364 | double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot)); |
||
365 | transformedVertices.add(new double[]{laX, laY}); |
||
366 | 9977 | azabala | } |
367 | 10064 | azabala | transformedEntity = (DwgLwPolyline)this.clone();
|
368 | transformedEntity.setVertices(transformedVertices); |
||
369 | transformedEntity.setElevation((this.getElevation() * scale[2])); |
||
370 | transformedEntity.setHandle(this.getHandle());
|
||
371 | dwgObjectsWithoutBlocks.add(transformedEntity); |
||
372 | 10240 | azabala | handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity); |
373 | 10064 | azabala | // dwgObjectsWithoutBlocks.add(this);
|
374 | 9843 | azabala | }//if
|
375 | } |
||
376 | 10174 | fdiaz | |
377 | public Object clone(){ |
||
378 | DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
|
||
379 | this.fill(dwgLwPolyline);
|
||
380 | return dwgLwPolyline;
|
||
381 | } |
||
382 | |||
383 | protected void fill(DwgObject obj){ |
||
384 | super.fill(obj);
|
||
385 | DwgLwPolyline myObj = (DwgLwPolyline)obj; |
||
386 | |||
387 | myObj.setBulges(bulges); |
||
388 | myObj.setConstWidth(constWidth); |
||
389 | myObj.setElevation(elevation); |
||
390 | myObj.setFlag(flag); |
||
391 | myObj.setNormal(normal); |
||
392 | myObj.setThickness(thickness); |
||
393 | myObj.setVertices(vertices); |
||
394 | myObj.setWidths(widths); |
||
395 | |||
396 | } |
||
397 | 10632 | azabala | public void addVertex(IDwgVertex vertex) { |
398 | vertices.add(vertex.getPoint()); |
||
399 | |||
400 | } |
||
401 | 10174 | fdiaz | |
402 | 2896 | jmorell | } |