package com.example.local; import java.io.IOException; import java.util.Random; import com.esri.arcgis.carto.IActiveView; import com.esri.arcgis.carto.IMap; import com.esri.arcgis.controls.BaseCommand; import com.esri.arcgis.controls.HookHelper; import com.esri.arcgis.display.IDisplayTransformation; import com.esri.arcgis.display.IScreenDisplay; import com.esri.arcgis.display.ISimpleLineSymbol; import com.esri.arcgis.display.ISymbol; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleLineSymbol; import com.esri.arcgis.display.esriSimpleLineStyle; import com.esri.arcgis.enginecore.GraphicTracker; import com.esri.arcgis.enginecore.IGraphicTracker; import com.esri.arcgis.enginecore.IGraphicTrackerSymbol; import com.esri.arcgis.geometry.Envelope; import com.esri.arcgis.geometry.IEnvelope; import com.esri.arcgis.geometry.ILine; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.ISpatialReference; import com.esri.arcgis.geometry.Line; import com.esri.arcgis.geometry.Point; public class AddPointGTCommand extends BaseCommand { private static final long serialVersionUID = 1L; private HookHelper hookHelper = null; private IActiveView activeView; private IScreenDisplay screenDisplay; private IDisplayTransformation displayTransformation; private IEnvelope mapVisibleExtent; private IMap map; private IGraphicTracker graphicTracker_LN; private IGraphicTrackerSymbol graphicTrackerSymbol_LN; private int numOfLines = 10; // number of lines public AddPointGTCommand() throws Exception{ name = "Add GraphicTracker"; caption = "Add GraphicTracker"; toolTip = "Add GraphicTracker"; message = "Add GraphicTracker"; category = "CustomCommand"; enabled = true; } public void onCreate(Object obj) { try { hookHelper = new HookHelper(); hookHelper.setHookByRef( obj ); } catch (IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public void onClick() { try{ System.out.println("Add GraphicTracker is Clicked"); map = hookHelper.getFocusMap(); activeView = hookHelper.getActiveView(); screenDisplay = activeView.getScreenDisplay(); displayTransformation = screenDisplay.getDisplayTransformation(); if (graphicTracker_LN == null) graphicTracker_LN = new GraphicTracker(); if (map != null) { graphicTracker_LN.initialize(map); } // Init Data = draw lines InitData(); } catch(Exception e){ e.printStackTrace(); } } // private void InitData(int charIndex) throws Exception private void InitData() throws Exception { ISpatialReference spatialReference; spatialReference = hookHelper.getFocusMap().getSpatialReference(); if (displayTransformation != null) { mapVisibleExtent = displayTransformation.getFittedBounds(); } else { if (mapVisibleExtent == null) { mapVisibleExtent = new Envelope(); mapVisibleExtent.putCoords(-130.0, 25.0, -70.0, 55.0); mapVisibleExtent.setSpatialReferenceByRef(spatialReference); } } // initialize the points data according to the visible extent double[] XMin = new double[1]; double[] YMin = new double[1]; double[] XMax = new double[1]; double[] YMax = new double[1]; mapVisibleExtent.queryCoords(XMin, YMin, XMax, YMax); double dWidth = XMax[0] - XMin[0]; double dHeight = YMax[0] - YMin[0]; // Create the Line's symbol ISymbol symboleLine = CreateSymbolLine(); // Create the GraphicTracker symbol from LineSymbol graphicTrackerSymbol_LN = graphicTracker_LN.createSymbol(symboleLine, null); // suspend auto updates in order to do a bulk of updates graphicTracker_LN.setSuspendUpdate(true); Random rand = new Random(); // Dessin des LINES ILine line = null; for (int i = 0; i < numOfLines; i++) { IPoint point1 = new Point(); point1.putCoords(XMin[0] + rand.nextDouble() * dWidth, YMin[0] + rand.nextDouble() * dHeight); IPoint point2 = new Point(); point2.putCoords(point1.getX() + 5, point1.getY() + 5); line = new Line(); line.setFromPoint(point1); line.setToPoint(point2); line.setSpatialReferenceByRef(spatialReference); // need to add the new element to the graphic tracker int graphicId = graphicTracker_LN.add(line, graphicTrackerSymbol_LN); } graphicTracker_LN.setSuspendUpdate(false); System.out.println("number of graphics : " + graphicTracker_LN.getCount()); // zoom on last Line added ! activeView.setExtent(line.getEnvelope()); activeView.refresh(); } private ISymbol CreateSymbolLine() throws Exception { RgbColor clr = new RgbColor(); clr.setRed(150); clr.setGreen(50); clr.setBlue(200); // purple ISimpleLineSymbol lineSymbol = new SimpleLineSymbol(); lineSymbol.setColor(clr); lineSymbol.setWidth(1.20); lineSymbol.setStyle(esriSimpleLineStyle.esriSLSSolid); return (ISymbol) lineSymbol; } }
Solved! Go to Solution.
// Draw random polylines Random rand = new Random(); for (int i = 0; i < numOfLines; i++) { // Initialize the Polyline Point[] pointsPath0 = new Point[2]; Path pathColl[] = new Path[1]; Polyline polyline = new Polyline(); polyline.setSpatialReferenceByRef(spatialReference); // Initialize points & path pointsPath0[0] = new Point(); pointsPath0[1] = new Point(); pathColl[0] = new Path(); // Putcoords on the points pointsPath0[0].putCoords(XMin[0] + rand.nextDouble() * dWidth, YMin[0] + rand.nextDouble() * dHeight); pointsPath0[1].putCoords(pointsPath0[0].getX() + 5, pointsPath0[0].getY() + 5); // Add points to separate paths GeometryEnvironment gBridge = new GeometryEnvironment(); gBridge.addPoints(pathColl[0], pointsPath0); // Add all the paths to the polyline using AddGeometries method gBridge.addGeometries(polyline, pathColl); // need to add the new element to the graphic tracker int graphicId = graphicTracker_LN.add(polyline, graphicTrackerSymbol_LN); graphicTracker_LN.setTransparency(graphicId, 50); }
// Draw random POLYGONS Random rand = new Random(); for (int i = 0; i < numOfPolygon; i++) { // Initialize the polygon Polygon polygon = new Polygon(); polygon.setSpatialReferenceByRef(spatialReference); // Initialize the points Point pt[] = new Point[3]; pt[0] = new Point(); pt[0].putCoords(XMin[0] + rand.nextDouble() * dWidth, YMin[0] + rand.nextDouble() * dHeight); pt[1] = new Point(); pt[1].putCoords(pt[0].getX() + 5, pt[0].getY() + 5); pt[2] = new Point(); pt[2].putCoords(pt[1].getX() -10, pt[1].getY()); // Add the points to the polygon GeometryEnvironment gBridge = new GeometryEnvironment(); gBridge.addPoints(polygon, pt); // need to add the new element to the graphic tracker int graphicId = graphicTracker_PL.add(polygon, graphicTrackerSymbol_PL); graphicTracker_PL.setTransparency(graphicId, 50); }
// Draw random polylines Random rand = new Random(); for (int i = 0; i < numOfLines; i++) { // Initialize the Polyline Point[] pointsPath0 = new Point[2]; Path pathColl[] = new Path[1]; Polyline polyline = new Polyline(); polyline.setSpatialReferenceByRef(spatialReference); // Initialize points & path pointsPath0[0] = new Point(); pointsPath0[1] = new Point(); pathColl[0] = new Path(); // Putcoords on the points pointsPath0[0].putCoords(XMin[0] + rand.nextDouble() * dWidth, YMin[0] + rand.nextDouble() * dHeight); pointsPath0[1].putCoords(pointsPath0[0].getX() + 5, pointsPath0[0].getY() + 5); // Add points to separate paths GeometryEnvironment gBridge = new GeometryEnvironment(); gBridge.addPoints(pathColl[0], pointsPath0); // Add all the paths to the polyline using AddGeometries method gBridge.addGeometries(polyline, pathColl); // need to add the new element to the graphic tracker int graphicId = graphicTracker_LN.add(polyline, graphicTrackerSymbol_LN); graphicTracker_LN.setTransparency(graphicId, 50); }
// Draw random POLYGONS Random rand = new Random(); for (int i = 0; i < numOfPolygon; i++) { // Initialize the polygon Polygon polygon = new Polygon(); polygon.setSpatialReferenceByRef(spatialReference); // Initialize the points Point pt[] = new Point[3]; pt[0] = new Point(); pt[0].putCoords(XMin[0] + rand.nextDouble() * dWidth, YMin[0] + rand.nextDouble() * dHeight); pt[1] = new Point(); pt[1].putCoords(pt[0].getX() + 5, pt[0].getY() + 5); pt[2] = new Point(); pt[2].putCoords(pt[1].getX() -10, pt[1].getY()); // Add the points to the polygon GeometryEnvironment gBridge = new GeometryEnvironment(); gBridge.addPoints(polygon, pt); // need to add the new element to the graphic tracker int graphicId = graphicTracker_PL.add(polygon, graphicTrackerSymbol_PL); graphicTracker_PL.setTransparency(graphicId, 50); }
Available with ArcGIS Engine, ArcGIS Desktop, and ArcGIS Server. Requires 3D Analyst Extension.