svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfPolyline.java @ 83
History | View | Annotate | Download (4.66 KB)
1 |
/*
|
---|---|
2 |
* Created on 30-abr-2004
|
3 |
*
|
4 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
5 |
*/
|
6 |
|
7 |
package org.cresques.px.dxf; |
8 |
|
9 |
import java.awt.Color; |
10 |
import java.awt.Graphics2D; |
11 |
import java.awt.geom.AffineTransform; |
12 |
import java.awt.geom.GeneralPath; |
13 |
import java.awt.geom.Point2D; |
14 |
import java.util.Iterator; |
15 |
import java.util.Vector; |
16 |
|
17 |
import org.cresques.geo.Point3D; |
18 |
import org.cresques.geo.Projection; |
19 |
import org.cresques.geo.ReProjection; |
20 |
import org.cresques.geo.ViewPort; |
21 |
import org.cresques.io.DxfGroup; |
22 |
import org.cresques.px.Extent; |
23 |
|
24 |
/**
|
25 |
* Polilynea de autocad
|
26 |
*
|
27 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>* @author administrador
|
28 |
*/
|
29 |
|
30 |
public class DxfPolyline extends DxfEntity { |
31 |
final static Color baseColor = new Color(69, 106, 121); |
32 |
Vector pts = null; |
33 |
Vector faces = null; |
34 |
GeneralPath gp = null; |
35 |
int flags = 0; |
36 |
boolean closed = false; |
37 |
boolean hasFaces = false; |
38 |
|
39 |
public DxfPolyline(Projection proj, DxfLayer layer) {
|
40 |
super(proj, layer);
|
41 |
extent = new Extent();
|
42 |
pts = new Vector(); |
43 |
} |
44 |
|
45 |
public void add(Point2D pt) { |
46 |
pts.add(pt); |
47 |
extent.add(pt); |
48 |
} |
49 |
|
50 |
public void addFace(int [] face) { |
51 |
hasFaces = true;
|
52 |
if (faces == null) |
53 |
faces = new Vector(); |
54 |
faces.add(face); |
55 |
} |
56 |
|
57 |
private Color color = baseColor; //Color(255,214,132,255); |
58 |
|
59 |
public Color c() {return color;} |
60 |
public Color c(Color color) {this.color = color; return color;} |
61 |
|
62 |
public void reProject(ReProjection rp) { |
63 |
Vector savePts = pts;
|
64 |
|
65 |
pts = new Vector(); |
66 |
extent = new Extent();
|
67 |
Point2D ptDest = null; |
68 |
for (int i=0; i<savePts.size(); i++) { |
69 |
ptDest = rp.getPDest().createPoint(0.0,0.0); |
70 |
rp.convert((Point2D) savePts.get(i), ptDest);
|
71 |
pts.add(ptDest); |
72 |
extent.add(ptDest); |
73 |
} |
74 |
setProjection(rp.getPDest()); |
75 |
} |
76 |
|
77 |
public void draw(Graphics2D g, ViewPort vp) { |
78 |
//AffineTransform msave=g.getTransform();
|
79 |
//g.setTransform(vp.mat);
|
80 |
Color color = null; |
81 |
// pinto el poligono si es preciso
|
82 |
if (dxfColor == AcadColor.BYLAYER)
|
83 |
color = layer.getColor(); |
84 |
else
|
85 |
color = AcadColor.getColor(dxfColor); |
86 |
newGP(vp); |
87 |
if (closed) {
|
88 |
g.setColor(new Color(color.getRed(), color.getBlue(), color.getGreen(), 0x20)); |
89 |
g.fill(gp); |
90 |
} |
91 |
g.setColor(color); |
92 |
g.draw(gp); |
93 |
//g.setTransform(msave);
|
94 |
} |
95 |
|
96 |
private void newGP(ViewPort vp) { |
97 |
//if (gp != null) return;
|
98 |
gp = new GeneralPath(); |
99 |
Point2D pt0 = null, pt=null, pt1=null; |
100 |
Point2D.Double ptTmp = new Point2D.Double(0.0, 0.0); |
101 |
if (! hasFaces) {
|
102 |
Iterator iter = pts.iterator();
|
103 |
while (iter.hasNext()) {
|
104 |
pt1 = (Point2D) iter.next();
|
105 |
vp.mat.transform(pt1, ptTmp); |
106 |
if (pt0 == null) { |
107 |
pt0 = ptTmp; |
108 |
gp.moveTo((float)ptTmp.getX(), (float)ptTmp.getY()); |
109 |
} else {
|
110 |
gp.lineTo((float)ptTmp.getX(), (float)ptTmp.getY()); |
111 |
} |
112 |
} |
113 |
if (closed) {
|
114 |
gp.closePath(); |
115 |
} |
116 |
} else {
|
117 |
System.out.println("POLYLINE: caras=" +faces.size()+", puntos="+ pts.size()); |
118 |
int [] face; |
119 |
int i0, i1;
|
120 |
Iterator iter = faces.iterator();
|
121 |
while (iter.hasNext()) {
|
122 |
face = (int []) iter.next(); |
123 |
|
124 |
i0 = face[3];
|
125 |
for (int i=0; i<4; i++) { |
126 |
i1 = face[i]; |
127 |
if (i0 > 0) { |
128 |
pt0 = (Point2D) pts.get(i0-1); |
129 |
vp.mat.transform(pt0, ptTmp); |
130 |
gp.moveTo((float)ptTmp.getX(), (float)ptTmp.getY()); |
131 |
pt1 = (Point2D) pts.get(Math.abs(i1)-1); |
132 |
vp.mat.transform(pt1, ptTmp); |
133 |
gp.lineTo((float)ptTmp.getX(), (float)ptTmp.getY()); |
134 |
} |
135 |
i0 = i1; |
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Convierte a DXF.
|
143 |
*
|
144 |
* @return Entity como String Dxf
|
145 |
*/
|
146 |
public String toDxfString() { |
147 |
StringBuffer sb = null; |
148 |
sb = new StringBuffer( DxfGroup.toString(0, "POLYLINE") ); |
149 |
sb.append( DxfGroup.toString(8, layer.getName()) );
|
150 |
sb.append( DxfGroup.toString(62, dxfColor) );
|
151 |
sb.append( DxfGroup.toString(70, flags) );
|
152 |
sb.append( DxfGroup.toString(66, 1) ); |
153 |
Point2D pt = null; |
154 |
Iterator iter = pts.iterator();
|
155 |
while (iter.hasNext()) {
|
156 |
pt = (Point2D) iter.next();
|
157 |
sb.append( DxfGroup.toString(0, "VERTEX") ); |
158 |
sb.append( DxfGroup.toString(8, layer.getName()) );
|
159 |
sb.append( DxfGroup.toString(10, pt.getX(), 6) ); |
160 |
sb.append( DxfGroup.toString(20, pt.getY(), 6) ); |
161 |
sb.append( DxfGroup.toString(30, 0.0, 6) ); |
162 |
} |
163 |
sb.append( DxfGroup.toString(0, "SEQEND") ); |
164 |
sb.append( DxfGroup.toString(8, layer.getName()) );
|
165 |
return sb.toString();
|
166 |
} |
167 |
/**
|
168 |
* @return
|
169 |
*/
|
170 |
public GeneralPath getGeneralPath(ViewPort vp) { |
171 |
newGP(vp); |
172 |
return (GeneralPath) gp.clone(); |
173 |
} |
174 |
|
175 |
public int getFlags() { |
176 |
return flags;
|
177 |
} |
178 |
|
179 |
//public static DxfEntityList createArc(Point2D coord1, Point2D coord2, double bulge, Point3D xtru) {
|
180 |
public static Vector createArc(Point2D coord1, Point2D coord2, double bulge) { |
181 |
//return new DxfCalArcs(coord1, coord2, bulge).getPoints(1, xtru);
|
182 |
return new DxfCalArcs(coord1, coord2, bulge).getPoints(1); |
183 |
} |
184 |
|
185 |
} |