25 |
25 |
import java.awt.event.MouseEvent;
|
26 |
26 |
import java.awt.geom.Point2D;
|
27 |
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.List;
|
28 |
29 |
|
29 |
30 |
import javax.swing.JOptionPane;
|
30 |
31 |
|
|
32 |
import org.slf4j.Logger;
|
|
33 |
import org.slf4j.LoggerFactory;
|
|
34 |
|
31 |
35 |
import org.gvsig.andami.PluginServices;
|
32 |
|
import org.gvsig.andami.messages.NotificationManager;
|
33 |
36 |
import org.gvsig.app.ApplicationLocator;
|
34 |
37 |
import org.gvsig.editing.gui.cad.exception.CommandException;
|
35 |
38 |
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext;
|
36 |
39 |
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
|
37 |
40 |
import org.gvsig.fmap.dal.exception.ReadException;
|
38 |
41 |
import org.gvsig.fmap.geom.Geometry;
|
39 |
|
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
|
|
42 |
import org.gvsig.fmap.geom.exception.CreateGeometryException;
|
40 |
43 |
import org.gvsig.fmap.geom.handler.Handler;
|
41 |
44 |
import org.gvsig.fmap.geom.primitive.Curve;
|
42 |
45 |
import org.gvsig.fmap.geom.primitive.GeneralPathX;
|
|
46 |
import org.gvsig.fmap.geom.primitive.Point;
|
|
47 |
import org.gvsig.fmap.geom.primitive.Surface;
|
43 |
48 |
import org.gvsig.fmap.geom.util.UtilFunctions;
|
44 |
49 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
|
45 |
50 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
|
... | ... | |
52 |
57 |
*/
|
53 |
58 |
public class PolylineCADTool extends AbstractCurveSurfaceCADTool {
|
54 |
59 |
|
|
60 |
private static final Logger LOG = LoggerFactory.getLogger(PolylineCADTool.class);
|
|
61 |
|
55 |
62 |
protected PolylineCADToolContext _fsm;
|
56 |
63 |
protected Point2D firstPoint;
|
57 |
64 |
protected Point2D antPoint;
|
58 |
65 |
protected Point2D antantPoint;
|
59 |
66 |
protected Point2D antCenter;
|
60 |
67 |
protected Point2D antInter;
|
61 |
|
protected ArrayList list = new ArrayList();
|
|
68 |
protected List<Curve> list = new ArrayList<Curve>();
|
62 |
69 |
protected boolean close = false;
|
63 |
|
protected GeneralPathX gpx = null;
|
64 |
70 |
|
65 |
71 |
/**
|
66 |
72 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
... | ... | |
71 |
77 |
}
|
72 |
78 |
|
73 |
79 |
public Geometry getGeometry() {
|
74 |
|
Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
|
75 |
|
MultiPrimitive fgc = createMultiPrimitive(geoms);
|
76 |
|
// No queremos guardar FGeometryCollections:
|
77 |
|
GeneralPathX gp = new GeneralPathX();
|
78 |
|
gp.append(fgc.getPathIterator(null, geomManager.getFlatness()), true);
|
79 |
|
Geometry newGeom = createCurve(gp);
|
|
80 |
Geometry newGeom = null;
|
|
81 |
newGeom = concatenateCurves(list, false);
|
80 |
82 |
return newGeom;
|
81 |
83 |
}
|
82 |
84 |
|
|
85 |
|
|
86 |
|
83 |
87 |
public int getLinesCount() {
|
84 |
88 |
return list.size();
|
85 |
89 |
}
|
... | ... | |
93 |
97 |
}
|
94 |
98 |
|
95 |
99 |
public void endGeometry() {
|
|
100 |
|
|
101 |
/*
|
96 |
102 |
if (gpx == null) {
|
97 |
103 |
gpx = new GeneralPathX();
|
98 |
104 |
Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
|
... | ... | |
101 |
107 |
gpx.append(fgc.getPathIterator(null, geomManager.getFlatness()),
|
102 |
108 |
true);
|
103 |
109 |
}
|
|
110 |
|
104 |
111 |
try {
|
105 |
112 |
if (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE
|
106 |
113 |
&& !close) {
|
... | ... | |
109 |
116 |
} catch (ReadException e) {
|
110 |
117 |
NotificationManager.addError(e.getMessage(), e);
|
111 |
118 |
}
|
|
119 |
*/
|
|
120 |
|
112 |
121 |
|
113 |
122 |
Geometry newGeom = null;
|
114 |
123 |
int type = getCadToolAdapter().getActiveLayerType();
|
|
124 |
|
115 |
125 |
if ((type == Geometry.TYPES.SURFACE)
|
116 |
126 |
|| (type == Geometry.TYPES.MULTISURFACE)) {
|
117 |
|
newGeom = createSurface(gpx);
|
|
127 |
|
|
128 |
newGeom = concatenateCurves(list, true);
|
|
129 |
closeSurfaceIfNecessary((Surface) newGeom);
|
|
130 |
// newGeom = createSurface(gpx);
|
118 |
131 |
} else {
|
119 |
|
newGeom = createCurve(gpx);
|
|
132 |
newGeom = concatenateCurves(list, false);
|
|
133 |
// newGeom = createCurve(gpx);
|
120 |
134 |
}
|
121 |
135 |
insertAndSelectGeometry(newGeom);
|
122 |
136 |
_fsm = new PolylineCADToolContext(this);
|
123 |
137 |
list.clear();
|
124 |
138 |
clearTemporalCache();
|
125 |
139 |
close = false;
|
126 |
|
gpx = null;
|
127 |
140 |
antantPoint = antCenter = antInter = antPoint = firstPoint = null;
|
128 |
141 |
}
|
129 |
142 |
|
|
143 |
/**
|
|
144 |
* @param newGeom
|
|
145 |
*/
|
|
146 |
private void closeSurfaceIfNecessary(Surface geo) {
|
|
147 |
|
|
148 |
if (!close && geo != null && geo.getNumVertices() > 1) {
|
|
149 |
Point firstp = geo.getVertex(0);
|
|
150 |
firstp = (Point) firstp.cloneGeometry();
|
|
151 |
geo.addVertex(firstp);
|
|
152 |
}
|
|
153 |
// TODO Auto-generated method stub
|
|
154 |
|
|
155 |
}
|
|
156 |
|
130 |
157 |
public void closeGeometry() {
|
131 |
|
if (gpx == null) {
|
132 |
|
gpx = new GeneralPathX();
|
133 |
|
Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
|
134 |
|
for (int i=0; i<geoms.length; i++) {
|
135 |
|
gpx.append(
|
136 |
|
geoms[i].getPathIterator(null, geomManager.getFlatness()),
|
137 |
|
true);
|
138 |
|
}
|
|
158 |
|
|
159 |
int ng = list.size();
|
|
160 |
if (ng > 0) {
|
|
161 |
Curve firstcurve = list.get(0);
|
|
162 |
Curve lastcurve = list.get(ng - 1);
|
|
163 |
Point firstp = firstcurve.getVertex(0);
|
|
164 |
firstp = (Point) firstp.cloneGeometry();
|
|
165 |
lastcurve.addVertex(firstp);
|
139 |
166 |
}
|
140 |
167 |
close = true;
|
141 |
|
gpx.closePath();
|
142 |
168 |
}
|
143 |
169 |
|
144 |
170 |
public void transition(double x, double y, InputEvent event) {
|
... | ... | |
185 |
211 |
new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
|
186 |
212 |
elShape.moveTo(antPoint.getX(), antPoint.getY());
|
187 |
213 |
elShape.lineTo(point.getX(), point.getY());
|
188 |
|
Geometry geom = createCurve(elShape);
|
|
214 |
Curve geom = createCurve(elShape);
|
189 |
215 |
if (geom != null) {
|
190 |
216 |
list.add(geom);
|
191 |
217 |
addTemporalCache(geom);
|
... | ... | |
265 |
291 |
UtilFunctions.getPoint(interp, mediop, radio);
|
266 |
292 |
antCenter = centerp;
|
267 |
293 |
|
268 |
|
Geometry ig = createArc(lastp, centerp, point);
|
|
294 |
Curve ig = createArc(lastp, centerp, point);
|
269 |
295 |
|
270 |
296 |
if (ig != null) {
|
271 |
297 |
list.add(ig);
|
... | ... | |
325 |
351 |
Point2D centerp =
|
326 |
352 |
UtilFunctions.getPoint(interp, mediop, radio);
|
327 |
353 |
antCenter = centerp;
|
328 |
|
Geometry geom = createArc(lastp, centerp, point);
|
|
354 |
Curve geom = createArc(lastp, centerp, point);
|
329 |
355 |
|
330 |
356 |
if (geom != null) {
|
331 |
357 |
list.add(geom);
|
... | ... | |
534 |
560 |
public void endTransition(double x, double y, MouseEvent event) {
|
535 |
561 |
_fsm.endPoint(x, y, event);
|
536 |
562 |
}
|
|
563 |
|
|
564 |
/**
|
|
565 |
* @param geoms
|
|
566 |
* @return
|
|
567 |
* @throws CreateGeometryException
|
|
568 |
*/
|
|
569 |
private Geometry concatenateCurves(List<Curve> cus, boolean surf) {
|
|
570 |
|
|
571 |
Curve _curv = null;
|
|
572 |
Surface _surf = null;
|
|
573 |
|
|
574 |
try {
|
|
575 |
if (surf) {
|
|
576 |
_surf = (Surface) geomManager.create(
|
|
577 |
Geometry.TYPES.SURFACE, Geometry.SUBTYPES.GEOM2D);
|
|
578 |
} else {
|
|
579 |
_curv = (Curve) geomManager.create(
|
|
580 |
Geometry.TYPES.CURVE, Geometry.SUBTYPES.GEOM2D);
|
|
581 |
}
|
|
582 |
} catch (CreateGeometryException e) {
|
|
583 |
LOG.info("Unable to create geometry: " + e.getMessage());
|
|
584 |
ApplicationLocator.getManager().message(
|
|
585 |
Messages.getText("_Unable_to_create_geometry"),
|
|
586 |
JOptionPane.ERROR_MESSAGE);
|
|
587 |
return null;
|
|
588 |
}
|
|
589 |
|
|
590 |
|
|
591 |
if (cus == null || cus.size() == 0) {
|
|
592 |
if (surf) {
|
|
593 |
return _surf;
|
|
594 |
} else {
|
|
595 |
return _curv;
|
|
596 |
}
|
|
597 |
}
|
|
598 |
|
|
599 |
Curve item_cu = cus.get(0);
|
|
600 |
int len = item_cu.getNumVertices();
|
|
601 |
Point po = null;
|
|
602 |
/*
|
|
603 |
* All from first curve
|
|
604 |
*/
|
|
605 |
for (int i=0; i<len; i++) {
|
|
606 |
po = item_cu.getVertex(i);
|
|
607 |
if (surf) {
|
|
608 |
_surf.addVertex(po);
|
|
609 |
} else {
|
|
610 |
_curv.addVertex(po);
|
|
611 |
}
|
|
612 |
|
|
613 |
}
|
|
614 |
|
|
615 |
for (int n=1; n<cus.size(); n++) {
|
|
616 |
|
|
617 |
item_cu = cus.get(n);
|
|
618 |
len = item_cu.getNumVertices();
|
|
619 |
for (int i=0; i<len; i++) {
|
|
620 |
po = item_cu.getVertex(i);
|
|
621 |
/*
|
|
622 |
* Add only if not same coords
|
|
623 |
*/
|
|
624 |
if (surf) {
|
|
625 |
if (differentCoordinates(po,
|
|
626 |
_surf.getVertex(
|
|
627 |
_surf.getNumVertices()-1))) {
|
|
628 |
_surf.addVertex(po);
|
|
629 |
}
|
|
630 |
} else {
|
|
631 |
if (differentCoordinates(po,
|
|
632 |
_curv.getVertex(
|
|
633 |
_curv.getNumVertices()-1))) {
|
|
634 |
_curv.addVertex(po);
|
|
635 |
}
|
|
636 |
}
|
|
637 |
}
|
|
638 |
}
|
|
639 |
|
|
640 |
if (surf) {
|
|
641 |
return _surf;
|
|
642 |
} else {
|
|
643 |
return _curv;
|
|
644 |
}
|
|
645 |
|
|
646 |
}
|
537 |
647 |
|
|
648 |
/**
|
|
649 |
* @param po
|
|
650 |
* @param vertex
|
|
651 |
* @return
|
|
652 |
*/
|
|
653 |
private boolean differentCoordinates(Point p1, Point p2) {
|
|
654 |
return !p1.equals(p2);
|
|
655 |
}
|
|
656 |
|
538 |
657 |
}
|