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