root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / DwgFile.java @ 7178
History | View | Annotate | Download (49.8 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; |
36 |
|
37 |
import java.awt.geom.Point2D; |
38 |
import java.io.File; |
39 |
import java.io.FileInputStream; |
40 |
import java.io.IOException; |
41 |
import java.nio.ByteBuffer; |
42 |
import java.nio.channels.FileChannel; |
43 |
import java.util.HashMap; |
44 |
//import java.util.Vector;
|
45 |
import java.util.ArrayList; |
46 |
|
47 |
import com.iver.cit.javacad.util.AcadExtrusionCalculator; |
48 |
import com.iver.cit.javacad.util.GisModelCurveCalculator; |
49 |
import com.iver.cit.jdwglib.dwg.objects.DwgArc; |
50 |
import com.iver.cit.jdwglib.dwg.objects.DwgAttdef; |
51 |
import com.iver.cit.jdwglib.dwg.objects.DwgAttrib; |
52 |
import com.iver.cit.jdwglib.dwg.objects.DwgBlock; |
53 |
import com.iver.cit.jdwglib.dwg.objects.DwgBlockControl; |
54 |
import com.iver.cit.jdwglib.dwg.objects.DwgBlockHeader; |
55 |
import com.iver.cit.jdwglib.dwg.objects.DwgCircle; |
56 |
import com.iver.cit.jdwglib.dwg.objects.DwgEllipse; |
57 |
import com.iver.cit.jdwglib.dwg.objects.DwgEndblk; |
58 |
import com.iver.cit.jdwglib.dwg.objects.DwgInsert; |
59 |
import com.iver.cit.jdwglib.dwg.objects.DwgLayer; |
60 |
import com.iver.cit.jdwglib.dwg.objects.DwgLayerControl; |
61 |
import com.iver.cit.jdwglib.dwg.objects.DwgLine; |
62 |
import com.iver.cit.jdwglib.dwg.objects.DwgLinearDimension; |
63 |
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline; |
64 |
import com.iver.cit.jdwglib.dwg.objects.DwgMText; |
65 |
import com.iver.cit.jdwglib.dwg.objects.DwgPoint; |
66 |
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline2D; |
67 |
import com.iver.cit.jdwglib.dwg.objects.DwgPolyline3D; |
68 |
import com.iver.cit.jdwglib.dwg.objects.DwgSeqend; |
69 |
import com.iver.cit.jdwglib.dwg.objects.DwgSolid; |
70 |
import com.iver.cit.jdwglib.dwg.objects.DwgSpline; |
71 |
import com.iver.cit.jdwglib.dwg.objects.DwgText; |
72 |
import com.iver.cit.jdwglib.dwg.objects.DwgVertex2D; |
73 |
import com.iver.cit.jdwglib.dwg.objects.DwgVertex3D; |
74 |
|
75 |
/**
|
76 |
* The DwgFile class provides a revision-neutral interface for reading and handling
|
77 |
* DWG files
|
78 |
* Reading methods are useful for reading DWG files, and handling methods like
|
79 |
* calculateDwgPolylines() are useful for handling more complex
|
80 |
* objects in the DWG file
|
81 |
*
|
82 |
* @author jmorell
|
83 |
*/
|
84 |
public class DwgFile { |
85 |
/**
|
86 |
* It has all known DWG version's header.
|
87 |
* Extracted from Autodesk web.
|
88 |
*/
|
89 |
private static HashMap acadVersions = new HashMap(); |
90 |
static{
|
91 |
acadVersions.put("AC1004", "Autocad R9"); |
92 |
acadVersions.put("AC1006", "Autocad R10"); |
93 |
acadVersions.put("AC1007", "Autocad pre-R11"); |
94 |
acadVersions.put("AC1007", "Autocad pre-R11"); |
95 |
acadVersions.put("AC1008", "Autocad pre-R11b"); |
96 |
acadVersions.put("AC1009", "Autocad R12"); |
97 |
acadVersions.put("AC1010", "Autocad pre-R13 a"); |
98 |
acadVersions.put("AC1011", "Autocad pre-R13 b"); |
99 |
acadVersions.put("AC1012", "Autocad R13"); |
100 |
acadVersions.put("AC1013", "Autocad pre-R14"); |
101 |
acadVersions.put("AC1014", "Autocad R14"); |
102 |
acadVersions.put("AC1500", "Autocad pre-2000"); |
103 |
acadVersions.put("AC1015", "Autocad R2000, R2000i, R2002"); |
104 |
acadVersions.put("AC402a", "Autocad pre-2004a"); |
105 |
acadVersions.put("AC402b", "Autocad pre-2004b"); |
106 |
acadVersions.put("AC1018", "Autocad R2004, R2005, R2006"); |
107 |
acadVersions.put("AC1021", "Autocad R2007"); |
108 |
|
109 |
} |
110 |
|
111 |
private String fileName; |
112 |
private String dwgVersion; |
113 |
private ArrayList dwgSectionOffsets; |
114 |
private ArrayList dwgObjectOffsets; |
115 |
private ArrayList dwgObjects; |
116 |
private ArrayList dwgClasses; |
117 |
private DwgFileReader dwgReader;
|
118 |
private ArrayList layerTable; |
119 |
private boolean dwg3DFile; |
120 |
|
121 |
/**
|
122 |
* Creates a new DwgFile object given the absolute path to
|
123 |
* a DWG file
|
124 |
*
|
125 |
* @param fileName an absolute path to the DWG file
|
126 |
*/
|
127 |
public DwgFile(String fileName) { |
128 |
this.fileName = fileName;
|
129 |
dwgSectionOffsets = new ArrayList(); |
130 |
dwgObjectOffsets = new ArrayList(); |
131 |
dwgObjects = new ArrayList(); |
132 |
dwgClasses = new ArrayList(); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Reads a DWG file and put its objects in the dwgObjects Vector
|
137 |
* This method is version independent
|
138 |
*
|
139 |
* @throws IOException If the file location is wrong
|
140 |
*/
|
141 |
public void read() throws IOException, |
142 |
DwgVersionNotSupportedException |
143 |
{ |
144 |
setDwgVersion(); |
145 |
if (dwgVersion.equalsIgnoreCase("Autocad R2000, R2000i, R2002")) { |
146 |
dwgReader = new DwgFileV15Reader();
|
147 |
dwgReader.read(this);
|
148 |
} else {
|
149 |
DwgVersionNotSupportedException exception = |
150 |
new DwgVersionNotSupportedException("Version de DWG no soportada"); |
151 |
exception.setDwgVersion(dwgVersion); |
152 |
throw exception;
|
153 |
} |
154 |
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* Modify the geometry of the objects applying the Extrusion vector where it
|
159 |
* is necessary
|
160 |
*/
|
161 |
public void applyExtrusions() { |
162 |
for (int i=0;i<dwgObjects.size();i++) { |
163 |
DwgObject dwgObject = (DwgObject)dwgObjects.get(i); |
164 |
if (dwgObject instanceof DwgArc) { |
165 |
double[] arcCenter = ((DwgArc)dwgObject).getCenter(); |
166 |
double[] arcExt = ((DwgArc)dwgObject).getExtrusion(); |
167 |
arcCenter = AcadExtrusionCalculator.CalculateAcadExtrusion(arcCenter, arcExt); |
168 |
((DwgArc)dwgObject).setCenter(arcCenter); |
169 |
} else if (dwgObject instanceof DwgAttdef) { |
170 |
// Extrusion in DwgAttdef is not necessary
|
171 |
} else if (dwgObject instanceof DwgAttrib) { |
172 |
Point2D attribInsertionPoint = ((DwgAttrib)dwgObject).getInsertionPoint();
|
173 |
double attribElevation = ((DwgAttrib)dwgObject).getElevation();
|
174 |
double[] attribInsertionPoint3D = new double[]{attribInsertionPoint.getX(), attribInsertionPoint.getY(), attribElevation}; |
175 |
double[] attribExt = ((DwgAttrib)dwgObject).getExtrusion(); |
176 |
attribInsertionPoint3D = AcadExtrusionCalculator.CalculateAcadExtrusion(attribInsertionPoint3D, attribExt); |
177 |
((DwgAttrib)dwgObject).setInsertionPoint(new Point2D.Double(attribInsertionPoint3D[0], attribInsertionPoint3D[1])); |
178 |
((DwgAttrib)dwgObject).setElevation(attribInsertionPoint3D[2]);
|
179 |
} else if (dwgObject instanceof DwgBlock) { |
180 |
// DwgBlock hasn't Extrusion
|
181 |
} else if (dwgObject instanceof DwgBlockControl) { |
182 |
// DwgBlockControl hasn't Extrusion
|
183 |
} else if (dwgObject instanceof DwgBlockHeader) { |
184 |
// DwgBlockHeader hasn't Extrusion
|
185 |
} else if (dwgObject instanceof DwgCircle) { |
186 |
double[] circleCenter = ((DwgCircle)dwgObject).getCenter(); |
187 |
double[] circleExt = ((DwgCircle)dwgObject).getExtrusion(); |
188 |
circleCenter = AcadExtrusionCalculator.CalculateAcadExtrusion(circleCenter, circleExt); |
189 |
((DwgCircle)dwgObject).setCenter(circleCenter); |
190 |
// Seems that Autocad don't apply the extrusion to Ellipses
|
191 |
/*} else if (dwgObject instanceof DwgEllipse) {
|
192 |
double[] ellipseCenter = ((DwgEllipse)dwgObject).getCenter();
|
193 |
double[] ellipseExt = ((DwgEllipse)dwgObject).getExtrusion();
|
194 |
ellipseCenter = AcadExtrusionCalculator.CalculateAcadExtrusion(ellipseCenter, ellipseExt);
|
195 |
((DwgEllipse)dwgObject).setCenter(ellipseCenter);*/
|
196 |
} else if (dwgObject instanceof DwgInsert) { |
197 |
double[] insertPoint = ((DwgInsert)dwgObject).getInsertionPoint(); |
198 |
double[] insertExt = ((DwgInsert)dwgObject).getExtrusion(); |
199 |
insertPoint = AcadExtrusionCalculator.CalculateAcadExtrusion(insertPoint, insertExt); |
200 |
((DwgInsert)dwgObject).setInsertionPoint(insertPoint); |
201 |
} else if (dwgObject instanceof DwgLayer) { |
202 |
// DwgLayer hasn't Extrusion
|
203 |
} else if (dwgObject instanceof DwgLayerControl) { |
204 |
// DwgLayerControl hasn't Extrusion
|
205 |
} else if (dwgObject instanceof DwgLine) { |
206 |
double[] lineP1 = ((DwgLine)dwgObject).getP1(); |
207 |
double[] lineP2 = ((DwgLine)dwgObject).getP2(); |
208 |
boolean zflag = ((DwgLine)dwgObject).isZflag();
|
209 |
if (zflag) {
|
210 |
// elev = 0.0;
|
211 |
lineP1 = new double[]{lineP1[0], lineP1[1], 0.0}; |
212 |
lineP2 = new double[]{lineP2[0], lineP2[1], 0.0}; |
213 |
} |
214 |
double[] lineExt = ((DwgLine)dwgObject).getExtrusion(); |
215 |
lineP1 = AcadExtrusionCalculator.CalculateAcadExtrusion(lineP1, lineExt); |
216 |
lineP2 = AcadExtrusionCalculator.CalculateAcadExtrusion(lineP2, lineExt); |
217 |
((DwgLine)dwgObject).setP1(lineP1); |
218 |
((DwgLine)dwgObject).setP2(lineP2); |
219 |
} else if (dwgObject instanceof DwgLinearDimension) { |
220 |
// TODO: Extrusions in DwgLinearDimension elements
|
221 |
// TODO: Void LwPolylines are a bug
|
222 |
} else if (dwgObject instanceof DwgLwPolyline && ((DwgLwPolyline)dwgObject).getVertices()!=null) { |
223 |
Point2D[] vertices = ((DwgLwPolyline)dwgObject).getVertices(); |
224 |
double[] lwPolylineExt = ((DwgLwPolyline)dwgObject).getNormal(); |
225 |
// Normals and Extrusions aren`t the same
|
226 |
if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) lwPolylineExt[2] = 1.0; |
227 |
double elev = ((DwgLwPolyline)dwgObject).getElevation();
|
228 |
double[][] lwPolylinePoints3D = new double[vertices.length][3]; |
229 |
for (int j=0;j<vertices.length;j++) { |
230 |
lwPolylinePoints3D[j][0] = vertices[j].getX();
|
231 |
lwPolylinePoints3D[j][1] = vertices[j].getY();
|
232 |
lwPolylinePoints3D[j][2] = elev;
|
233 |
lwPolylinePoints3D[j] = AcadExtrusionCalculator.CalculateAcadExtrusion(lwPolylinePoints3D[j], lwPolylineExt); |
234 |
} |
235 |
((DwgLwPolyline)dwgObject).setElevation(elev); |
236 |
for (int j=0;j<vertices.length;j++) { |
237 |
vertices[j] = new Point2D.Double(lwPolylinePoints3D[j][0], lwPolylinePoints3D[j][1]); |
238 |
} |
239 |
((DwgLwPolyline)dwgObject).setVertices(vertices); |
240 |
} else if (dwgObject instanceof DwgMText) { |
241 |
double[] mtextPoint = ((DwgMText)dwgObject).getInsertionPoint(); |
242 |
double[] mtextExt = ((DwgMText)dwgObject).getExtrusion(); |
243 |
mtextPoint = AcadExtrusionCalculator.CalculateAcadExtrusion(mtextPoint, mtextExt); |
244 |
((DwgMText)dwgObject).setInsertionPoint(mtextPoint); |
245 |
} else if (dwgObject instanceof DwgPoint) { |
246 |
double[] point = ((DwgPoint)dwgObject).getPoint(); |
247 |
double[] pointExt = ((DwgPoint)dwgObject).getExtrusion(); |
248 |
point = AcadExtrusionCalculator.CalculateAcadExtrusion(point, pointExt); |
249 |
((DwgPoint)dwgObject).setPoint(point); |
250 |
} else if (dwgObject instanceof DwgSolid) { |
251 |
double[] corner1 = ((DwgSolid)dwgObject).getCorner1(); |
252 |
double[] corner2 = ((DwgSolid)dwgObject).getCorner2(); |
253 |
double[] corner3 = ((DwgSolid)dwgObject).getCorner3(); |
254 |
double[] corner4 = ((DwgSolid)dwgObject).getCorner4(); |
255 |
double[] solidExt = ((DwgSolid)dwgObject).getExtrusion(); |
256 |
corner1 = AcadExtrusionCalculator.CalculateAcadExtrusion(corner1, solidExt); |
257 |
((DwgSolid)dwgObject).setCorner1(corner1); |
258 |
((DwgSolid)dwgObject).setCorner2(corner2); |
259 |
((DwgSolid)dwgObject).setCorner3(corner3); |
260 |
((DwgSolid)dwgObject).setCorner4(corner4); |
261 |
} else if (dwgObject instanceof DwgSpline) { |
262 |
// DwgSpline hasn't Extrusion
|
263 |
} else if (dwgObject instanceof DwgText) { |
264 |
Point2D tpoint = ((DwgText)dwgObject).getInsertionPoint();
|
265 |
double elev = ((DwgText)dwgObject).getElevation();
|
266 |
double[] textPoint = new double[]{tpoint.getX(), tpoint.getY(), elev}; |
267 |
double[] textExt = ((DwgText)dwgObject).getExtrusion(); |
268 |
textPoint = AcadExtrusionCalculator.CalculateAcadExtrusion(textPoint, textExt); |
269 |
((DwgText)dwgObject).setInsertionPoint(new Point2D.Double(textPoint[0], textPoint[1])); |
270 |
((DwgText)dwgObject).setElevation(elev); |
271 |
} else if (dwgObject instanceof DwgPolyline2D && ((DwgPolyline2D)dwgObject).getPts()!=null) { |
272 |
Point2D[] vertices = ((DwgPolyline2D)dwgObject).getPts(); |
273 |
double[] polyline2DExt = ((DwgPolyline2D)dwgObject).getExtrusion(); |
274 |
double elev = ((DwgPolyline2D)dwgObject).getElevation();
|
275 |
double[][] polylinePoints3D = new double[vertices.length][3]; |
276 |
for (int j=0;j<vertices.length;j++) { |
277 |
polylinePoints3D[j][0] = vertices[j].getX();
|
278 |
polylinePoints3D[j][1] = vertices[j].getY();
|
279 |
polylinePoints3D[j][2] = elev;
|
280 |
polylinePoints3D[j] = AcadExtrusionCalculator.CalculateAcadExtrusion(polylinePoints3D[j], polyline2DExt); |
281 |
} |
282 |
((DwgPolyline2D)dwgObject).setElevation(elev); |
283 |
for (int j=0;j<vertices.length;j++) { |
284 |
vertices[j] = new Point2D.Double(polylinePoints3D[j][0], polylinePoints3D[j][1]); |
285 |
} |
286 |
((DwgPolyline2D)dwgObject).setPts(vertices); |
287 |
} else if (dwgObject instanceof DwgPolyline3D) { |
288 |
// DwgPolyline3D hasn't Extrusion
|
289 |
} else if (dwgObject instanceof DwgVertex2D) { |
290 |
// DwgVertex2D hasn't Extrusion
|
291 |
} else if (dwgObject instanceof DwgVertex3D) { |
292 |
// DwgVertex3D hasn't Extrusion
|
293 |
} else {
|
294 |
//
|
295 |
} |
296 |
} |
297 |
} |
298 |
|
299 |
/**
|
300 |
* Configure the geometry of the polylines in a DWG file from the vertex list in
|
301 |
* this DWG file. This geometry is given by an array of Points.
|
302 |
* Besides, manage closed polylines and polylines with bulges in a GIS Data model.
|
303 |
* It means that the arcs of the polylines will be done through a set of points and
|
304 |
* a distance between these points.
|
305 |
*/
|
306 |
public void calculateGisModelDwgPolylines() { |
307 |
for (int i=0;i<dwgObjects.size();i++) { |
308 |
DwgObject pol = (DwgObject)dwgObjects.get(i); |
309 |
if (pol instanceof DwgPolyline2D) { |
310 |
int flags = ((DwgPolyline2D)pol).getFlags();
|
311 |
int firstHandle = ((DwgPolyline2D)pol).getFirstVertexHandle();
|
312 |
int lastHandle = ((DwgPolyline2D)pol).getLastVertexHandle();
|
313 |
ArrayList pts = new ArrayList(); |
314 |
ArrayList bulges = new ArrayList(); |
315 |
double[] pt = new double[3]; |
316 |
for (int j=0;j<dwgObjects.size();j++) { |
317 |
DwgObject firstVertex = (DwgObject)dwgObjects.get(j); |
318 |
if (firstVertex instanceof DwgVertex2D) { |
319 |
int vertexHandle = firstVertex.getHandle();
|
320 |
if (vertexHandle==firstHandle) {
|
321 |
int k=0; |
322 |
while (true) { |
323 |
DwgObject vertex = (DwgObject)dwgObjects.get(j+k); |
324 |
int vHandle = vertex.getHandle();
|
325 |
if (vertex instanceof DwgVertex2D) { |
326 |
pt = ((DwgVertex2D)vertex).getPoint(); |
327 |
pts.add(new Point2D.Double(pt[0], pt[1])); |
328 |
double bulge = ((DwgVertex2D)vertex).getBulge();
|
329 |
bulges.add(new Double(bulge)); |
330 |
k++; |
331 |
if (vHandle==lastHandle && vertex instanceof DwgVertex2D) { |
332 |
break;
|
333 |
} |
334 |
} else if (vertex instanceof DwgSeqend) { |
335 |
// 051116, jmorell: Polil?neas_ACAD2000.dwg tiene un DwgSeqend en mitad de
|
336 |
//una secuencia de v?rtices. Precauci?n con esto puesto que es posible que esta
|
337 |
// condici?n fuera requerida en la carga de otros DWGs.
|
338 |
//break;
|
339 |
k++; |
340 |
} |
341 |
} |
342 |
} |
343 |
} |
344 |
} |
345 |
if (pts.size()>0) { |
346 |
Point2D[] newPts = new Point2D[pts.size()]; |
347 |
if ((flags & 0x1)==0x1) { |
348 |
newPts = new Point2D[pts.size()+1]; |
349 |
for (int j=0;j<pts.size();j++) { |
350 |
newPts[j] = (Point2D)pts.get(j);
|
351 |
} |
352 |
newPts[pts.size()] = (Point2D)pts.get(0); |
353 |
bulges.add(new Double(0)); |
354 |
} else {
|
355 |
for (int j=0;j<pts.size();j++) { |
356 |
newPts[j] = (Point2D)pts.get(j);
|
357 |
} |
358 |
} |
359 |
double[] bs = new double[bulges.size()]; |
360 |
for (int j=0;j<bulges.size();j++) { |
361 |
bs[j] = ((Double)bulges.get(j)).doubleValue();
|
362 |
} |
363 |
((DwgPolyline2D)pol).setBulges(bs); |
364 |
Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, bs); |
365 |
((DwgPolyline2D)pol).setPts(points); |
366 |
} else {
|
367 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
368 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
369 |
// ocurre es porque existe un error que hay que corregir ...
|
370 |
} |
371 |
} else if (pol instanceof DwgPolyline3D) { |
372 |
int closedFlags = ((DwgPolyline3D)pol).getClosedFlags();
|
373 |
int firstHandle = ((DwgPolyline3D)pol).getFirstVertexHandle();
|
374 |
int lastHandle = ((DwgPolyline3D)pol).getLastVertexHandle();
|
375 |
ArrayList pts = new ArrayList(); |
376 |
double[] pt = new double[3]; |
377 |
for (int j=0;j<dwgObjects.size();j++) { |
378 |
DwgObject firstVertex = (DwgObject)dwgObjects.get(j); |
379 |
if (firstVertex instanceof DwgVertex3D) { |
380 |
int vertexHandle = firstVertex.getHandle();
|
381 |
if (vertexHandle==firstHandle) {
|
382 |
int k=0; |
383 |
while (true) { |
384 |
DwgObject vertex = (DwgObject)dwgObjects.get(j+k); |
385 |
int vHandle = vertex.getHandle();
|
386 |
if (vertex instanceof DwgVertex3D) { |
387 |
pt = ((DwgVertex3D)vertex).getPoint(); |
388 |
pts.add(new double[]{pt[0], pt[1], pt[2]}); |
389 |
k++; |
390 |
if (vHandle==lastHandle && vertex instanceof DwgVertex3D) { |
391 |
break;
|
392 |
} |
393 |
} else if (vertex instanceof DwgSeqend) { |
394 |
break;
|
395 |
} |
396 |
} |
397 |
} |
398 |
} |
399 |
} |
400 |
if (pts.size()>0) { |
401 |
double[][] newPts = new double[pts.size()][3]; |
402 |
if ((closedFlags & 0x1)==0x1) { |
403 |
newPts = new double[pts.size()+1][3]; |
404 |
for (int j=0;j<pts.size();j++) { |
405 |
newPts[j][0] = ((double[])pts.get(j))[0]; |
406 |
newPts[j][1] = ((double[])pts.get(j))[1]; |
407 |
newPts[j][2] = ((double[])pts.get(j))[2]; |
408 |
} |
409 |
newPts[pts.size()][0] = ((double[])pts.get(0))[0]; |
410 |
newPts[pts.size()][1] = ((double[])pts.get(0))[1]; |
411 |
newPts[pts.size()][2] = ((double[])pts.get(0))[2]; |
412 |
} else {
|
413 |
for (int j=0;j<pts.size();j++) { |
414 |
newPts[j][0] = ((double[])pts.get(j))[0]; |
415 |
newPts[j][1] = ((double[])pts.get(j))[1]; |
416 |
newPts[j][2] = ((double[])pts.get(j))[2]; |
417 |
} |
418 |
} |
419 |
((DwgPolyline3D)pol).setPts(newPts); |
420 |
} else {
|
421 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
422 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
423 |
// ocurre es porque existe un error que hay que corregir ...
|
424 |
} |
425 |
} else if (pol instanceof DwgLwPolyline && ((DwgLwPolyline)pol).getVertices()!=null) { |
426 |
int flags = ((DwgLwPolyline)pol).getFlag();
|
427 |
Point2D[] pts = ((DwgLwPolyline)pol).getVertices(); |
428 |
double[] bulges = ((DwgLwPolyline)pol).getBulges(); |
429 |
Point2D[] newPts = new Point2D[pts.length]; |
430 |
double[] newBulges = new double[bulges.length]; |
431 |
// TODO: Aqu? pueden existir casos no contemplados ...
|
432 |
// System.out.println("flags = " + flags);
|
433 |
if (flags==512 || flags==776 || flags==768) { |
434 |
newPts = new Point2D[pts.length+1]; |
435 |
newBulges = new double[bulges.length+1]; |
436 |
for (int j=0;j<pts.length;j++) { |
437 |
newPts[j] = (Point2D)pts[j];
|
438 |
} |
439 |
newPts[pts.length] = (Point2D)pts[0]; |
440 |
newBulges[pts.length] = 0;
|
441 |
} else {
|
442 |
for (int j=0;j<pts.length;j++) { |
443 |
newPts[j] = (Point2D)pts[j];
|
444 |
} |
445 |
} |
446 |
if (pts.length>0) { |
447 |
((DwgLwPolyline)pol).setBulges(newBulges); |
448 |
Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges); |
449 |
((DwgLwPolyline)pol).setVertices(points); |
450 |
} else {
|
451 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
452 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
453 |
// ocurre es porque existe un error que hay que corregir ...
|
454 |
} |
455 |
} |
456 |
} |
457 |
} |
458 |
|
459 |
/**
|
460 |
* Configure the geometry of the polylines in a DWG file from the vertex list in
|
461 |
* this DWG file. This geometry is given by an array of Points
|
462 |
* Besides, manage closed polylines and polylines with bulges in a GIS Data model.
|
463 |
* It means that the arcs of the polylines will be done through a curvature
|
464 |
* parameter called bulge associated with the points of the polyline.
|
465 |
*/
|
466 |
public void calculateCadModelDwgPolylines() { |
467 |
for (int i=0;i<dwgObjects.size();i++) { |
468 |
DwgObject pol = (DwgObject)dwgObjects.get(i); |
469 |
if (pol instanceof DwgPolyline2D) { |
470 |
int flags = ((DwgPolyline2D)pol).getFlags();
|
471 |
int firstHandle = ((DwgPolyline2D)pol).getFirstVertexHandle();
|
472 |
int lastHandle = ((DwgPolyline2D)pol).getLastVertexHandle();
|
473 |
ArrayList pts = new ArrayList(); |
474 |
ArrayList bulges = new ArrayList(); |
475 |
double[] pt = new double[3]; |
476 |
for (int j=0;j<dwgObjects.size();j++) { |
477 |
DwgObject firstVertex = (DwgObject)dwgObjects.get(j); |
478 |
if (firstVertex instanceof DwgVertex2D) { |
479 |
int vertexHandle = firstVertex.getHandle();
|
480 |
if (vertexHandle==firstHandle) {
|
481 |
int k=0; |
482 |
while (true) { |
483 |
DwgObject vertex = (DwgObject)dwgObjects.get(j+k); |
484 |
int vHandle = vertex.getHandle();
|
485 |
if (vertex instanceof DwgVertex2D) { |
486 |
pt = ((DwgVertex2D)vertex).getPoint(); |
487 |
pts.add(new Point2D.Double(pt[0], pt[1])); |
488 |
double bulge = ((DwgVertex2D)vertex).getBulge();
|
489 |
bulges.add(new Double(bulge)); |
490 |
k++; |
491 |
if (vHandle==lastHandle && vertex instanceof DwgVertex2D) { |
492 |
break;
|
493 |
} |
494 |
} else if (vertex instanceof DwgSeqend) { |
495 |
break;
|
496 |
} |
497 |
} |
498 |
} |
499 |
} |
500 |
} |
501 |
if (pts.size()>0) { |
502 |
/*Point2D[] newPts = new Point2D[pts.size()];
|
503 |
if ((flags & 0x1)==0x1) {
|
504 |
newPts = new Point2D[pts.size()+1];
|
505 |
for (int j=0;j<pts.size();j++) {
|
506 |
newPts[j] = (Point2D)pts.get(j);
|
507 |
}
|
508 |
newPts[pts.size()] = (Point2D)pts.get(0);
|
509 |
bulges.add(new Double(0));
|
510 |
} else {
|
511 |
for (int j=0;j<pts.size();j++) {
|
512 |
newPts[j] = (Point2D)pts.get(j);
|
513 |
}
|
514 |
}*/
|
515 |
double[] bs = new double[bulges.size()]; |
516 |
for (int j=0;j<bulges.size();j++) { |
517 |
bs[j] = ((Double)bulges.get(j)).doubleValue();
|
518 |
} |
519 |
((DwgPolyline2D)pol).setBulges(bs); |
520 |
//Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, bs);
|
521 |
Point2D[] points = new Point2D[pts.size()]; |
522 |
for (int j=0;j<pts.size();j++) { |
523 |
points[j] = (Point2D)pts.get(j);
|
524 |
} |
525 |
((DwgPolyline2D)pol).setPts(points); |
526 |
} else {
|
527 |
// System.out.println("Encontrada polil?nea sin puntos ...");
|
528 |
// TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
|
529 |
// ocurre es porque existe un error que hay que corregir ...
|
530 |
} |
531 |
} else if (pol instanceof DwgPolyline3D) { |
532 |
} else if (pol instanceof DwgLwPolyline && ((DwgLwPolyline)pol).getVertices()!=null) { |
533 |
} |
534 |
} |
535 |
} |
536 |
|
537 |
/**
|
538 |
* Modify the geometry of the objects contained in the blocks of a DWG file and
|
539 |
* add these objects to the DWG object list.
|
540 |
*/
|
541 |
public void blockManagement() { |
542 |
ArrayList dwgObjectsWithoutBlocks = new ArrayList(); |
543 |
boolean addingToBlock = false; |
544 |
for (int i=0;i<dwgObjects.size();i++) { |
545 |
DwgObject entity = (DwgObject)dwgObjects.get(i); |
546 |
if (entity instanceof DwgArc && !addingToBlock) { |
547 |
dwgObjectsWithoutBlocks.add(entity); |
548 |
} else if (entity instanceof DwgEllipse && !addingToBlock) { |
549 |
dwgObjectsWithoutBlocks.add(entity); |
550 |
} else if (entity instanceof DwgCircle && !addingToBlock) { |
551 |
dwgObjectsWithoutBlocks.add(entity); |
552 |
} else if (entity instanceof DwgPolyline2D && !addingToBlock) { |
553 |
dwgObjectsWithoutBlocks.add(entity); |
554 |
} else if (entity instanceof DwgPolyline3D && !addingToBlock) { |
555 |
dwgObjectsWithoutBlocks.add(entity); |
556 |
} else if (entity instanceof DwgLwPolyline && !addingToBlock) { |
557 |
dwgObjectsWithoutBlocks.add(entity); |
558 |
} else if (entity instanceof DwgSolid && !addingToBlock) { |
559 |
dwgObjectsWithoutBlocks.add(entity); |
560 |
} else if (entity instanceof DwgLine && !addingToBlock) { |
561 |
dwgObjectsWithoutBlocks.add(entity); |
562 |
} else if (entity instanceof DwgPoint && !addingToBlock) { |
563 |
dwgObjectsWithoutBlocks.add(entity); |
564 |
} else if (entity instanceof DwgMText && !addingToBlock) { |
565 |
dwgObjectsWithoutBlocks.add(entity); |
566 |
} else if (entity instanceof DwgText && !addingToBlock) { |
567 |
dwgObjectsWithoutBlocks.add(entity); |
568 |
} else if (entity instanceof DwgAttrib && !addingToBlock) { |
569 |
dwgObjectsWithoutBlocks.add(entity); |
570 |
} else if (entity instanceof DwgAttdef && !addingToBlock) { |
571 |
dwgObjectsWithoutBlocks.add(entity); |
572 |
} else if (entity instanceof DwgBlock) { |
573 |
addingToBlock = true;
|
574 |
} else if (entity instanceof DwgEndblk) { |
575 |
addingToBlock = false;
|
576 |
} else if (entity instanceof DwgBlockHeader) { |
577 |
addingToBlock = true;
|
578 |
} else if (entity instanceof DwgInsert && !addingToBlock) { |
579 |
double[] p = ((DwgInsert)entity).getInsertionPoint(); |
580 |
Point2D point = new Point2D.Double(p[0], p[1]); |
581 |
double[] scale = ((DwgInsert)entity).getScale(); |
582 |
double rot = ((DwgInsert)entity).getRotation();
|
583 |
int blockHandle = ((DwgInsert)entity).getBlockHeaderHandle();
|
584 |
manageInsert(point, scale, rot, blockHandle, i, dwgObjectsWithoutBlocks); |
585 |
} else {
|
586 |
//System.out.println("Detectado dwgObject pendiente de implementar");
|
587 |
} |
588 |
} |
589 |
dwgObjects = dwgObjectsWithoutBlocks; |
590 |
} |
591 |
|
592 |
/**
|
593 |
* Manages an INSERT of a DWG file. This object is the insertion point of a DWG
|
594 |
* block. It has the next parameters:
|
595 |
* @param insPoint, coordinates of the insertion point.
|
596 |
* @param scale, scale of the elements of the block that will be inserted.
|
597 |
* @param rot, rotation angle of the elements of the block.
|
598 |
* @param bHandle, offset for the coordinates of the elements of the block.
|
599 |
* @param id, count that serves as a id.
|
600 |
* @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
|
601 |
* the blocks.
|
602 |
*/
|
603 |
private void manageInsert(Point2D insPoint, double[] scale, double rot, int bHandle, int id, ArrayList dwgObjectsWithoutBlocks) { |
604 |
for (int i=0;i<dwgObjects.size();i++) { |
605 |
DwgObject obj = (DwgObject)dwgObjects.get(i); |
606 |
if (obj instanceof DwgBlockHeader) { |
607 |
int objHandle = ((DwgBlockHeader)obj).getHandle();
|
608 |
if (objHandle==bHandle) {
|
609 |
//System.out.println("Encontrado DwgBlockHeader con handle = " + bHandle);
|
610 |
double[] bPoint = ((DwgBlockHeader)obj).getBasePoint(); |
611 |
String bname = ((DwgBlockHeader)obj).getName();
|
612 |
//System.out.println("Nombre del bloque = " + bname);
|
613 |
if (!bname.startsWith("*")) { |
614 |
int firstObjectHandle = ((DwgBlockHeader)obj).getFirstEntityHandle();
|
615 |
//System.out.println("firstObjectHandle = " + firstObjectHandle);
|
616 |
int lastObjectHandle = ((DwgBlockHeader)obj).getLastEntityHandle();
|
617 |
//System.out.println("lastObjectHandle = " + lastObjectHandle);
|
618 |
DwgBlock block = null;
|
619 |
for (int j=0;j<dwgObjects.size();j++) { |
620 |
DwgObject ent = (DwgObject)dwgObjects.get(j); |
621 |
if (ent instanceof DwgBlock) { |
622 |
String name = ((DwgBlock)ent).getName();
|
623 |
if (bname.equals(name)) {
|
624 |
block = (DwgBlock)ent; |
625 |
//System.out.println("Encontrado DwgBlock con bname = " + bname);
|
626 |
break;
|
627 |
} |
628 |
} |
629 |
} |
630 |
for (int j=0;j<dwgObjects.size();j++) { |
631 |
DwgObject fObj = (DwgObject)dwgObjects.get(j); |
632 |
if (fObj!=null) { |
633 |
int fObjHandle = fObj.getHandle();
|
634 |
if (fObjHandle==firstObjectHandle) {
|
635 |
int k=0; |
636 |
while (true) { |
637 |
//System.out.println("Encontrado elemento " + k + " del bloque");
|
638 |
DwgObject iObj = (DwgObject)dwgObjects.get(j+k); |
639 |
int iObjHandle = iObj.getHandle();
|
640 |
// System.out.println("iObj.getType() = " + iObj.getType());
|
641 |
// System.out.println("insPoint.getX() = " + insPoint.getX());
|
642 |
// System.out.println("insPoint.getY() = " + insPoint.getY());
|
643 |
// System.out.println("rot = " + rot);
|
644 |
manageBlockEntity(iObj, bPoint, insPoint, scale, rot, id, dwgObjectsWithoutBlocks); |
645 |
k++; |
646 |
if (iObjHandle==lastObjectHandle) break; |
647 |
} |
648 |
} |
649 |
} |
650 |
} |
651 |
break;
|
652 |
} |
653 |
} |
654 |
} |
655 |
} |
656 |
} |
657 |
|
658 |
/**
|
659 |
* Changes the location of an object extracted from a block. This location will be
|
660 |
* obtained through the insertion parameters from the block and the corresponding
|
661 |
* insert.
|
662 |
* @param entity, the entity extracted from the block.
|
663 |
* @param bPoint, offset for the coordinates of the entity.
|
664 |
* @param insPoint, coordinates of the insertion point for the entity.
|
665 |
* @param scale, scale for the entity.
|
666 |
* @param rot, rotation angle for the entity.
|
667 |
* @param id, a count as a id.
|
668 |
* @param dwgObjectsWithoutBlocks, a object list with the elements extracted from
|
669 |
* the blocks.
|
670 |
*/
|
671 |
private void manageBlockEntity(DwgObject entity, double[] bPoint, Point2D insPoint, double[] scale, double rot, int id, ArrayList dwgObjectsWithoutBlocks) { |
672 |
if (entity instanceof DwgArc) { |
673 |
//System.out.println("Encuentra un arco dentro de un bloque ...");
|
674 |
DwgArc transformedEntity = new DwgArc();
|
675 |
double[] center = ((DwgArc)entity).getCenter(); |
676 |
Point2D pointAux = new Point2D.Double(center[0] - bPoint[0], center[1] - bPoint[1]); |
677 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
678 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
679 |
double laZ = center[2] * scale[2]; |
680 |
double[] transformedCenter = new double[]{laX, laY, laZ}; |
681 |
double radius = ((DwgArc)entity).getRadius();
|
682 |
// System.out.println("radius = " + radius);
|
683 |
// System.out.println("scale[0] = " + scale[0]);
|
684 |
// System.out.println("scale[1] = " + scale[1]);
|
685 |
// System.out.println("rot = " + rot);
|
686 |
double transformedRadius = radius * scale[0]; |
687 |
double initAngle = ((DwgArc)entity).getInitAngle();
|
688 |
double endAngle = ((DwgArc)entity).getEndAngle();
|
689 |
// System.out.println("initAngle = " + initAngle);
|
690 |
// System.out.println("endAngle = " + endAngle);
|
691 |
// System.out.println("rot = " + rot);
|
692 |
double transformedInitAngle = initAngle + rot;
|
693 |
if (transformedInitAngle<0) { |
694 |
transformedInitAngle = transformedInitAngle + (2*Math.PI); |
695 |
} else if (transformedInitAngle>(2*Math.PI)) { |
696 |
transformedInitAngle = transformedInitAngle - (2*Math.PI); |
697 |
} |
698 |
double transformedEndAngle = endAngle + rot;
|
699 |
if (transformedEndAngle<0) { |
700 |
transformedEndAngle = transformedEndAngle + (2*Math.PI); |
701 |
} else if (transformedEndAngle>(2*Math.PI)) { |
702 |
transformedEndAngle = transformedEndAngle - (2*Math.PI); |
703 |
} |
704 |
transformedEntity = (DwgArc)((DwgArc)entity).clone(); |
705 |
transformedEntity.setCenter(transformedCenter); |
706 |
transformedEntity.setRadius(transformedRadius); |
707 |
transformedEntity.setInitAngle(transformedInitAngle); |
708 |
transformedEntity.setEndAngle(transformedEndAngle); |
709 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
710 |
} else if (entity instanceof DwgCircle) { |
711 |
//System.out.println("Encuentra un c?rculo dentro de un bloque ...");
|
712 |
DwgCircle transformedEntity = new DwgCircle();
|
713 |
double[] center = ((DwgCircle)entity).getCenter(); |
714 |
Point2D pointAux = new Point2D.Double(center[0] - bPoint[0], center[1] - bPoint[1]); |
715 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
716 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
717 |
double laZ = center[2] * scale[2]; |
718 |
double[] transformedCenter = new double[]{laX, laY, laZ}; |
719 |
double radius = ((DwgCircle)entity).getRadius();
|
720 |
double transformedRadius = radius * scale[0]; |
721 |
transformedEntity = (DwgCircle)((DwgCircle)entity).clone(); |
722 |
transformedEntity.setCenter(transformedCenter); |
723 |
transformedEntity.setRadius(transformedRadius); |
724 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
725 |
} else if (entity instanceof DwgEllipse) { |
726 |
DwgEllipse transformedEntity = new DwgEllipse();
|
727 |
double[] center = ((DwgEllipse)entity).getCenter(); |
728 |
Point2D pointAux = new Point2D.Double(center[0] - bPoint[0], center[1] - bPoint[1]); |
729 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
730 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
731 |
double laZ = center[2] * scale[2]; |
732 |
double[] transformedCenter = new double[]{laX, laY, laZ}; |
733 |
double[] majorAxisVector = ((DwgEllipse)entity).getMajorAxisVector(); |
734 |
double[] transformedMajorAxisVector = new double[]{majorAxisVector[0] * scale[0], majorAxisVector[1] * scale[1], majorAxisVector[2] * scale[2]}; |
735 |
//TODO: Rotar un ?ngulo rot el vector majorAxisVector fijado en
|
736 |
// center.
|
737 |
double axisRatio = ((DwgEllipse)entity).getAxisRatio();
|
738 |
double transformedAxisRatio = axisRatio;
|
739 |
double initAngle = ((DwgEllipse)entity).getInitAngle();
|
740 |
double endAngle = ((DwgEllipse)entity).getEndAngle();
|
741 |
double transformedInitAngle = initAngle + rot;
|
742 |
if (transformedInitAngle<0) { |
743 |
transformedInitAngle = transformedInitAngle + (2*Math.PI); |
744 |
} else if (transformedInitAngle>(2*Math.PI)) { |
745 |
transformedInitAngle = transformedInitAngle - (2*Math.PI); |
746 |
} |
747 |
double transformedEndAngle = endAngle + rot;
|
748 |
if (transformedEndAngle<0) { |
749 |
transformedEndAngle = transformedEndAngle + (2*Math.PI); |
750 |
} else if (transformedEndAngle>(2*Math.PI)) { |
751 |
transformedEndAngle = transformedEndAngle - (2*Math.PI); |
752 |
} |
753 |
transformedEntity = (DwgEllipse)((DwgEllipse)entity).clone(); |
754 |
transformedEntity.setCenter(transformedCenter); |
755 |
transformedEntity.setMajorAxisVector(transformedMajorAxisVector); |
756 |
transformedEntity.setAxisRatio(transformedAxisRatio); |
757 |
transformedEntity.setInitAngle(transformedInitAngle); |
758 |
transformedEntity.setEndAngle(transformedEndAngle); |
759 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
760 |
} else if (entity instanceof DwgLine) { |
761 |
//System.out.println("Encuentra una l?nea dentro de un bloque ...");
|
762 |
DwgLine transformedEntity = new DwgLine();
|
763 |
double[] p1 = ((DwgLine)entity).getP1(); |
764 |
double[] p2 = ((DwgLine)entity).getP2(); |
765 |
Point2D pointAux = new Point2D.Double(p1[0] - bPoint[0], p1[1] - bPoint[1]); |
766 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
767 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
768 |
double[] transformedP1 = null; |
769 |
if (((DwgLine)entity).isZflag()) {
|
770 |
double laZ = p1[2] * scale[2]; |
771 |
transformedP1 = new double[]{laX, laY, laZ}; |
772 |
} else {
|
773 |
transformedP1 = new double[]{laX, laY}; |
774 |
} |
775 |
//double[] transformedP1 = new double[]{laX, laY};
|
776 |
pointAux = new Point2D.Double(p2[0] - bPoint[0], p2[1] - bPoint[1]); |
777 |
laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
778 |
laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
779 |
double[] transformedP2 = null; |
780 |
if (((DwgLine)entity).isZflag()) {
|
781 |
double laZ = p2[2] * scale[2]; |
782 |
transformedP2 = new double[]{laX, laY, laZ}; |
783 |
} else {
|
784 |
transformedP2 = new double[]{laX, laY}; |
785 |
} |
786 |
//double[] transformedP2 = new double[]{laX, laY};
|
787 |
transformedEntity = (DwgLine)((DwgLine)entity).clone(); |
788 |
transformedEntity.setP1(transformedP1); |
789 |
transformedEntity.setP2(transformedP2); |
790 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
791 |
} else if (entity instanceof DwgLwPolyline) { |
792 |
//System.out.println("Encuentra una DwgLwPolyline dentro de un bloque ...");
|
793 |
DwgLwPolyline transformedEntity = new DwgLwPolyline();
|
794 |
Point2D[] vertices = ((DwgLwPolyline)entity).getVertices(); |
795 |
if (vertices!=null) { |
796 |
Point2D[] transformedVertices = new Point2D[vertices.length]; |
797 |
for (int i=0;i<vertices.length;i++) { |
798 |
Point2D pointAux = new Point2D.Double(vertices[i].getX() - bPoint[0], vertices[i].getY() - bPoint[1]); |
799 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
800 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
801 |
transformedVertices[i] = new Point2D.Double(laX, laY); |
802 |
} |
803 |
transformedEntity = (DwgLwPolyline)((DwgLwPolyline)entity).clone(); |
804 |
transformedEntity.setVertices(transformedVertices); |
805 |
transformedEntity.setElevation(((DwgLwPolyline)entity).getElevation() * scale[2]);
|
806 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
807 |
} |
808 |
} else if (entity instanceof DwgMText) { |
809 |
|
810 |
} else if (entity instanceof DwgPoint) { |
811 |
|
812 |
} else if (entity instanceof DwgPolyline2D) { |
813 |
//System.out.println("Encuentra una polil?nea dentro de un bloque ...");
|
814 |
DwgPolyline2D transformedEntity = new DwgPolyline2D();
|
815 |
Point2D[] vertices = ((DwgPolyline2D)entity).getPts(); |
816 |
if (vertices!=null) { |
817 |
Point2D[] transformedVertices = new Point2D[vertices.length]; |
818 |
for (int i=0;i<vertices.length;i++) { |
819 |
Point2D pointAux = new Point2D.Double(vertices[i].getX() - bPoint[0], vertices[i].getY() - bPoint[1]); |
820 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
821 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
822 |
transformedVertices[i] = new Point2D.Double(laX, laY); |
823 |
} |
824 |
transformedEntity = (DwgPolyline2D)((DwgPolyline2D)entity).clone(); |
825 |
transformedEntity.setPts(transformedVertices); |
826 |
transformedEntity.setElevation(((DwgPolyline2D)entity).getElevation() * scale[2]);
|
827 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
828 |
} |
829 |
} else if (entity instanceof DwgPolyline3D) { |
830 |
|
831 |
} else if (entity instanceof DwgSolid) { |
832 |
DwgSolid transformedEntity = new DwgSolid();
|
833 |
double[] corner1 = ((DwgSolid)entity).getCorner1(); |
834 |
double[] corner2 = ((DwgSolid)entity).getCorner2(); |
835 |
double[] corner3 = ((DwgSolid)entity).getCorner3(); |
836 |
double[] corner4 = ((DwgSolid)entity).getCorner4(); |
837 |
Point2D pointAux = new Point2D.Double(corner1[0] - bPoint[0], corner1[1] - bPoint[1]); |
838 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
839 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
840 |
double[] transformedP1 = new double[]{laX, laY}; |
841 |
pointAux = new Point2D.Double(corner2[0] - bPoint[0], corner2[1] - bPoint[1]); |
842 |
laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
843 |
laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
844 |
double[] transformedP2 = new double[]{laX, laY}; |
845 |
pointAux = new Point2D.Double(corner3[0] - bPoint[0], corner3[1] - bPoint[1]); |
846 |
laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
847 |
laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
848 |
double[] transformedP3 = new double[]{laX, laY}; |
849 |
pointAux = new Point2D.Double(corner4[0] - bPoint[0], corner4[1] - bPoint[1]); |
850 |
laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
851 |
laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
852 |
double[] transformedP4 = new double[]{laX, laY}; |
853 |
transformedEntity = (DwgSolid)((DwgSolid)entity).clone(); |
854 |
transformedEntity.setCorner1(transformedP1); |
855 |
transformedEntity.setCorner2(transformedP2); |
856 |
transformedEntity.setCorner3(transformedP3); |
857 |
transformedEntity.setCorner4(transformedP4); |
858 |
transformedEntity.setElevation(((DwgSolid)entity).getElevation() * scale[2]);
|
859 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
860 |
} else if (entity instanceof DwgSpline) { |
861 |
|
862 |
} else if (entity instanceof DwgText) { |
863 |
|
864 |
} else if (entity instanceof DwgInsert) { |
865 |
//System.out.println("Encuentra un insert dentro de un bloque ...");
|
866 |
DwgInsert transformedEntity = new DwgInsert();
|
867 |
double[] p = ((DwgInsert)entity).getInsertionPoint(); |
868 |
Point2D point = new Point2D.Double(p[0], p[1]); |
869 |
double[] newScale = ((DwgInsert)entity).getScale(); |
870 |
double newRot = ((DwgInsert)entity).getRotation();
|
871 |
int newBlockHandle = ((DwgInsert)entity).getBlockHeaderHandle();
|
872 |
Point2D pointAux = new Point2D.Double(point.getX() - bPoint[0], point.getY() - bPoint[1]); |
873 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
874 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
875 |
double laZ = p[2] * scale[2]; |
876 |
Point2D newInsPoint = new Point2D.Double(laX, laY); |
877 |
newScale = new double[]{scale[0]*newScale[0], scale[1]*newScale[1], scale[2]*newScale[2]}; |
878 |
newRot = newRot + rot; |
879 |
if (newRot<0) { |
880 |
newRot = newRot + (2*Math.PI); |
881 |
} else if (newRot>(2*Math.PI)) { |
882 |
newRot = newRot - (2*Math.PI); |
883 |
} |
884 |
manageInsert(newInsPoint, newScale, newRot, newBlockHandle, id, dwgObjectsWithoutBlocks); |
885 |
} |
886 |
} |
887 |
|
888 |
/**
|
889 |
* Initialize a new Vector that contains the DWG file layers. Each layer have three
|
890 |
* parameters. These parameters are handle, name and color
|
891 |
*/
|
892 |
public void initializeLayerTable() { |
893 |
layerTable = new ArrayList(); |
894 |
for (int i=0;i<dwgObjects.size();i++) { |
895 |
DwgObject obj = (DwgObject)dwgObjects.get(i); |
896 |
if (obj instanceof DwgLayer) { |
897 |
ArrayList layerTableRecord = new ArrayList(); |
898 |
layerTableRecord.add(new Integer(obj.getHandle())); |
899 |
layerTableRecord.add(((DwgLayer)obj).getName()); |
900 |
layerTableRecord.add(new Integer(((DwgLayer)obj).getColor())); |
901 |
layerTable.add(layerTableRecord); |
902 |
} |
903 |
} |
904 |
} |
905 |
|
906 |
/**
|
907 |
* Returns the name of the layer of a DWG object
|
908 |
*
|
909 |
* @param entity DWG object which we want to know its layer name
|
910 |
* @return String Layer name of the DWG object
|
911 |
*/
|
912 |
// TODO: Gesti?n de capas pendiente ...
|
913 |
public String getLayerName(DwgObject entity) { |
914 |
String layerName = ""; |
915 |
int layer = entity.getLayerHandle();
|
916 |
for (int j=0;j<layerTable.size();j++) { |
917 |
ArrayList layerTableRecord = (ArrayList)layerTable.get(j); |
918 |
int lHandle = ((Integer)layerTableRecord.get(0)).intValue(); |
919 |
if (lHandle==layer) {
|
920 |
layerName = (String)layerTableRecord.get(1); |
921 |
} |
922 |
} |
923 |
return layerName;
|
924 |
} |
925 |
|
926 |
/**
|
927 |
* Returns the color of the layer of a DWG object
|
928 |
*
|
929 |
* @param entity DWG object which we want to know its layer color
|
930 |
* @return int Layer color of the DWG object in the Autocad color code
|
931 |
*/
|
932 |
public int getColorByLayer(DwgObject entity) { |
933 |
int colorByLayer = 0; |
934 |
int layer = entity.getLayerHandle();
|
935 |
for (int j=0;j<layerTable.size();j++) { |
936 |
ArrayList layerTableRecord = (ArrayList)layerTable.get(j); |
937 |
int lHandle = ((Integer)layerTableRecord.get(0)).intValue(); |
938 |
if (lHandle==layer) {
|
939 |
colorByLayer = ((Integer)layerTableRecord.get(2)).intValue(); |
940 |
} |
941 |
} |
942 |
return colorByLayer;
|
943 |
} |
944 |
|
945 |
private void setDwgVersion() throws IOException { |
946 |
File file = new File(fileName); |
947 |
FileInputStream fileInputStream = new FileInputStream(file); |
948 |
FileChannel fileChannel = fileInputStream.getChannel();
|
949 |
long channelSize = fileChannel.size();
|
950 |
ByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, channelSize); |
951 |
byte[] versionBytes = {byteBuffer.get(0), byteBuffer.get(1), byteBuffer.get(2), byteBuffer.get(3), |
952 |
byteBuffer.get(4), byteBuffer.get(5)}; |
953 |
ByteBuffer versionByteBuffer = ByteBuffer.wrap(versionBytes); |
954 |
String versionString = readDwgVersion(versionByteBuffer);
|
955 |
String version = (String) acadVersions.get(versionString); |
956 |
if(version == null) |
957 |
version = "Unknown Dwg format";
|
958 |
this.dwgVersion = version;
|
959 |
|
960 |
|
961 |
} |
962 |
|
963 |
private String readDwgVersion(ByteBuffer versionBuffer) { |
964 |
String[] bs = new String[versionBuffer.capacity()]; |
965 |
String sv = ""; |
966 |
for (int i=0; i<versionBuffer.capacity(); i++) { |
967 |
bs[i] = new String(new byte[]{(byte)(versionBuffer.get(i))}); |
968 |
sv = sv + bs[i]; |
969 |
} |
970 |
return sv;
|
971 |
} |
972 |
|
973 |
/**
|
974 |
* Test if the DWG file is 2D or 3D. If there is any object with a non cero
|
975 |
* elevation value, the file is considered 3D.
|
976 |
*/
|
977 |
public void testDwg3D() { |
978 |
for (int i=0;i<dwgObjects.size();i++) { |
979 |
DwgObject obj = (DwgObject)dwgObjects.get(i); |
980 |
double z = 0.0; |
981 |
if (obj instanceof DwgArc) { |
982 |
z = ((DwgArc)obj).getCenter()[2];
|
983 |
if (z!=0.0) dwg3DFile = true; |
984 |
} else if (obj instanceof DwgAttrib) { |
985 |
z = ((DwgAttrib)obj).getElevation(); |
986 |
if (z!=0.0) dwg3DFile = true; |
987 |
} else if (obj instanceof DwgBlockHeader) { |
988 |
z = ((DwgBlockHeader)obj).getBasePoint()[2];
|
989 |
if (z!=0.0) dwg3DFile = true; |
990 |
} else if (obj instanceof DwgCircle) { |
991 |
z = ((DwgCircle)obj).getCenter()[2];
|
992 |
if (z!=0.0) dwg3DFile = true; |
993 |
} else if (obj instanceof DwgEllipse) { |
994 |
z = ((DwgEllipse)obj).getCenter()[2];
|
995 |
if (z!=0.0) dwg3DFile = true; |
996 |
} else if (obj instanceof DwgInsert) { |
997 |
z = ((DwgInsert)obj).getInsertionPoint()[2];
|
998 |
if (z!=0.0) dwg3DFile = true; |
999 |
} else if (obj instanceof DwgLine) { |
1000 |
if (!((DwgLine)obj).isZflag()) {
|
1001 |
double z1 = ((DwgLine)obj).getP1()[2]; |
1002 |
double z2 = ((DwgLine)obj).getP2()[2]; |
1003 |
if (z1!=0.0 || z2!=0.0) dwg3DFile = true; |
1004 |
} |
1005 |
//} else if (obj instanceof DwgLinearDimension) {
|
1006 |
// z = ((DwgLinearDimension)obj).getElevation();
|
1007 |
// if (z!=0.0) dwg3DFile = true;
|
1008 |
} else if (obj instanceof DwgLwPolyline) { |
1009 |
z = ((DwgLwPolyline)obj).getElevation(); |
1010 |
if (z!=0.0) dwg3DFile = true; |
1011 |
} else if (obj instanceof DwgMText) { |
1012 |
z = ((DwgMText)obj).getInsertionPoint()[2];
|
1013 |
if (z!=0.0) dwg3DFile = true; |
1014 |
} else if (obj instanceof DwgPoint) { |
1015 |
z = ((DwgPoint)obj).getPoint()[2];
|
1016 |
if (z!=0.0) dwg3DFile = true; |
1017 |
} else if (obj instanceof DwgPolyline2D) { |
1018 |
z = ((DwgPolyline2D)obj).getElevation(); |
1019 |
if (z!=0.0) dwg3DFile = true; |
1020 |
} else if (obj instanceof DwgPolyline3D) { |
1021 |
if (((DwgPolyline3D)obj).getPts()!=null) { |
1022 |
double[][] pts = ((DwgPolyline3D)obj).getPts(); |
1023 |
for (int j=0;j<pts.length;j++) { |
1024 |
z = pts[j][2];
|
1025 |
if (z!=0.0) dwg3DFile = true; |
1026 |
} |
1027 |
} |
1028 |
} else if (obj instanceof DwgSolid) { |
1029 |
z = ((DwgSolid)obj).getElevation(); |
1030 |
if (z!=0.0) dwg3DFile = true; |
1031 |
} else if (obj instanceof DwgSpline) { |
1032 |
double[][] pts = ((DwgSpline)obj).getControlPoints(); |
1033 |
//AZABALA. ARREGLANDO CASQUES
|
1034 |
if(pts != null){ |
1035 |
for (int j=0;j<pts.length;j++) { |
1036 |
z = pts[j][2];
|
1037 |
if (z!=0.0) dwg3DFile = true; |
1038 |
} |
1039 |
} |
1040 |
} else if (obj instanceof DwgText) { |
1041 |
z = ((DwgText)obj).getElevation(); |
1042 |
if (z!=0.0) dwg3DFile = true; |
1043 |
} |
1044 |
} |
1045 |
} |
1046 |
|
1047 |
/**
|
1048 |
* Add a DWG section offset to the dwgSectionOffsets vector
|
1049 |
*
|
1050 |
* @param key Define the DWG section
|
1051 |
* @param seek Offset of the section
|
1052 |
* @param size Size of the section
|
1053 |
*/
|
1054 |
public void addDwgSectionOffset(String key, int seek, int size) { |
1055 |
DwgSectionOffset dso = new DwgSectionOffset(key, seek, size);
|
1056 |
dwgSectionOffsets.add(dso); |
1057 |
} |
1058 |
|
1059 |
/**
|
1060 |
* Returns the offset of DWG section given by its key
|
1061 |
*
|
1062 |
* @param key Define the DWG section
|
1063 |
* @return int Offset of the section in the DWG file
|
1064 |
*/
|
1065 |
public int getDwgSectionOffset(String key) { |
1066 |
int offset = 0; |
1067 |
for (int i=0; i<dwgSectionOffsets.size(); i++) { |
1068 |
DwgSectionOffset dso = (DwgSectionOffset)dwgSectionOffsets.get(i); |
1069 |
String ikey = dso.getKey();
|
1070 |
if (key.equals(ikey)) {
|
1071 |
offset = dso.getSeek(); |
1072 |
break;
|
1073 |
} |
1074 |
} |
1075 |
return offset;
|
1076 |
} |
1077 |
|
1078 |
/**
|
1079 |
* Add a DWG object offset to the dwgObjectOffsets vector
|
1080 |
*
|
1081 |
* @param handle Object handle
|
1082 |
* @param offset Offset of the object data in the DWG file
|
1083 |
*/
|
1084 |
public void addDwgObjectOffset(int handle, int offset) { |
1085 |
DwgObjectOffset doo = new DwgObjectOffset(handle, offset);
|
1086 |
dwgObjectOffsets.add(doo); |
1087 |
} |
1088 |
|
1089 |
/**
|
1090 |
*
|
1091 |
* Add a DWG object to the dwgObject vector
|
1092 |
*
|
1093 |
* @param dwgObject DWG object
|
1094 |
*/
|
1095 |
public void addDwgObject(DwgObject dwgObject){ |
1096 |
dwgObjects.add(dwgObject); |
1097 |
} |
1098 |
|
1099 |
/**
|
1100 |
* Add a DWG class to the dwgClasses vector
|
1101 |
*
|
1102 |
* @param dwgClass DWG class
|
1103 |
*/
|
1104 |
public void addDwgClass(DwgClass dwgClass){ |
1105 |
System.out.println("DwgFile.addDwgClass() executed ..."); |
1106 |
dwgClasses.add(dwgClass); |
1107 |
} |
1108 |
|
1109 |
/**
|
1110 |
* @return Returns the dwgObjectOffsets.
|
1111 |
*/
|
1112 |
public ArrayList getDwgObjectOffsets() { |
1113 |
return dwgObjectOffsets;
|
1114 |
} |
1115 |
/**
|
1116 |
* @return Returns the dwgObjects.
|
1117 |
*/
|
1118 |
public ArrayList getDwgObjects() { |
1119 |
return dwgObjects;
|
1120 |
} |
1121 |
/**
|
1122 |
* @return Returns the fileName.
|
1123 |
*/
|
1124 |
public String getFileName() { |
1125 |
return fileName;
|
1126 |
} |
1127 |
/**
|
1128 |
* @return Returns the dwg3DFile.
|
1129 |
*/
|
1130 |
public boolean isDwg3DFile() { |
1131 |
return dwg3DFile;
|
1132 |
} |
1133 |
/**
|
1134 |
* @param dwg3DFile The dwg3DFile to set.
|
1135 |
*/
|
1136 |
public void setDwg3DFile(boolean dwg3DFile) { |
1137 |
this.dwg3DFile = dwg3DFile;
|
1138 |
} |
1139 |
} |