5

After a ShapeFile that was read to IFeatureLayer converted to IGraphicElements, I'm trying to draw that on map (using Sublayer of ICompositeGraphicsLayer). Of course, I succeed to display it on map, however, when I move the map to other position using IScreenDisplay.Pan*(), them automatically redraws.

I want to prevent them redraw. How can I do that?

To solve the above problem, I tried to use ILayer.Cached and IActiveView.PartialRefresh, but I failed.

The sample code is

AxMapControl m_axMapControl;

public AxMapControl getMapControl()
{
  return m_axMapControl;
}

public IActiveView getActiveView()
{
  return getMapControl().ActiveView;
}

public IMap getFocusMap()
{
  return getActiveView().FocusMap;
}

internal static IGraphicElements SelectedFeaturesToGraphics(/*IMap targetMap,*/ IFeatureLayer featureLayer)
{
  var elements = new GraphicElements() as IGraphicElements;

  IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)featureLayer;
  IFeatureRenderer featureRenderer = geoFeatureLayer.Renderer;

  IQueryFilter queryFilter = new QueryFilter();
  var cursor = featureLayer.FeatureClass.Search(queryFilter, false);

  ISymbol symbol = null;
  switch (featureLayer.FeatureClass.ShapeType)
  {
    case esriGeometryType.esriGeometryPoint:
    case esriGeometryType.esriGeometryMultipoint:
      {
        var s = new SimpleMarkerSymbol();
        s.Style = esriSimpleMarkerStyle.esriSMSCircle;
        s.Size = 2;
        s.Color = GeoUtil.GetIColor(Colors.Black);
        symbol = s as ISymbol;
      }
      break;

    case esriGeometryType.esriGeometryLine:
    case esriGeometryType.esriGeometryPolyline:
      {
        var s = new SimpleLineSymbol();
        s.Color = GeoUtil.GetIColor(Colors.Black);
        s.Style = esriSimpleLineStyle.esriSLSSolid;
        s.Width = 1;
        symbol = s as ISymbol;
      }
      break;

    case esriGeometryType.esriGeometryPolygon:
      {
        var l = new SimpleLineSymbol();
        l.Color = GeoUtil.GetIColor(Colors.Black);
        l.Width = 1;
        l.Style = esriSimpleLineStyle.esriSLSSolid;

        var s = new SimpleFillSymbol();
        s.Color = l.Color;
        s.Style = esriSimpleFillStyle.esriSFSSolid;
        s.Outline = l;
        symbol = s as ISymbol;
      }
      break;
  }

  IFeatureCursor featureCursor = (IFeatureCursor)cursor;
  IFeature feature = featureCursor.NextFeature();
  while (feature != null)
  {
    IGeometry geometry = feature.Shape;
    //ISymbol symbol = featureRenderer.get_SymbolByFeature(feature);
    //string elementName = string.Format("{0}_{1}", featureLayer.Name, feature.OID);
    IElement element = null;

    switch (geometry.GeometryType)
    {
      case esriGeometryType.esriGeometryPoint:
        element = new MarkerElementClass();
        (element as IElement).Geometry = geometry;
        //(element as IMarkerElement).Symbol = symbol as IMarkerSymbol;
        //graphicsCon.AddElement(element, 0);
        break;
      case esriGeometryType.esriGeometryPolyline:
        element = new LineElementClass();
        element.Geometry = geometry;
        //(element as ILineElement).Symbol = symbol as ILineSymbol;
        //graphicsCon.AddElement(element, 0);
        break;
      case esriGeometryType.esriGeometryPolygon:
        element = new PolygonElementClass();
        element.Geometry = geometry;
        //(element as IFillShapeElement).Symbol = symbol as IFillSymbol;
        //graphicsCon.AddElement(element, 0);
        break;
    }
    element.Locked = true;
    elements.Add(element as IGraphicElement);
    feature = featureCursor.NextFeature();
  }
  System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);

  return elements;
}

public void AddGeometry(string key, IGraphicsElement elements)
{
  ICompositeGraphicsLayer compositeGraphicsLayer = (ICompositeGraphicsLayer)FindLayer(SuperGraphicsLayerID);
  if (compositeGraphicsLayer == null)
  {
    MessageBox.Show("compositeGraphicsLayer is null");
    return;
  }

  var layer = compositeGraphicsLayer.AddLayer(l.Key, null) as ILayer;

  int cnt = graphicElements.Count;
  IElementCollection collection = new ElementCollectionClass();
  for (int i = 0; i < cnt; i++)
  {
    collection.Add(graphicElements.get_Element(i) as IElement);
  }

  layer.Visible = true;
  layer.Cached = true;

  (layer as IGraphicsLayer).Activate(getActiveView().ScreenDisplay);
  (layer as IGraphicsContainer).AddElements(collection, 0);

  //getActiveView().Deactivate();
  //getActiveView().Activate(getActiveView().ScreenDisplay.hWnd);

  getActiveView().ScreenDisplay.Invalidate(null, true, getActiveView().get_ScreenCacheID(esriViewDrawPhase.esriViewGraphics, null));
}

private void onMouseUp()
{
  if (bGrab == true)
  {
    bGrab = false;

    IEnvelope extent = m_focusScreenDisplay.PanStop();

    double distance = Math.Sqrt(Math.Pow(e.x - ptMouseDownOnDisplay.X, 2) + Math.Pow(e.y - ptMouseDownOnDisplay.Y, 2));
    if (distance > 1)
    {
      //Check if user dragged a rectangle or just clicked.
      //If a rectangle was dragged, m_ipFeedback in non-NULL
      if (extent != null)
      {
        m_focusScreenDisplay.DisplayTransformation.VisibleBounds = extent;
        m_focusScreenDisplay.Invalidate(null, true, (short)esriScreenCache.esriNoScreenCache);
      }

      MainFrame.g_Main.AddScene(GetCenterPoint(), getMapControl().ActiveView.Extent);
      return;
    }
  }

}

private void SampleCode()
{
  var elements = SelectedFeaturesToGraphics(readShapeFile("...."));
  AddGeometry("sample", elements); // Automatically redraw when move.
  getMapContrl().AddLayer(readShapeFile("....")); // Didn't redraw again except first time when move.
}
5
  • It sounds like you only want to see the graphics created from the layer... Have you tried to turn the layer off after it has been converted to graphics? ILayer.Visible resources.arcgis.com/en/help/arcobjects-net/componenthelp/… – Michael Stimson May 16 '16 at 2:29
  • I already converted and painted it on map. My problem is that every graphics except the layer was redrawed after moving to a position of the Map. I want to stop to redraw. So, after searching on Internet, I tried to enable 'cache' that is a property of ILayer(IGraphicLayer). But, I failed. After moving, every graphics layers repainted on screen again. – Sangjin Sim May 16 '16 at 7:03
  • 1
    You can't stop ArcMap from redawing. I have found Dynamic Display to be smoother when panning/zooming with active graphics. It is my understanding that 'cached' means 'on the local workstation' and is meant for enterprise (SDE) data or data residing on local intranet or internet, if your data is a graphics layer it's already on the local workstation so caching means nothing. – Michael Stimson May 16 '16 at 22:01
  • 1
    The ILayer.Cached property should work. It is used to separate the display cache for the layer so that it can be refreshed independently of other layers. My guess is that your onmouseup() call is overriding this cache and refreshing anyway. – Rich Wawrzonek May 19 '16 at 18:20
  • Thank you, Michael and Rich. I tried that after I set cache property to true, I inserted 'activate' and 'deactivate' function at the end of AddGeometry Function(commented part). then, it redrawed without screen clear when I moved the screen to some location. I may retry to do it!! – Sangjin Sim May 23 '16 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.