2

I'm trying to do some simple things with the .net framework and ArcObjects, but have little experience with this type of development. I would like to iterate through the layers in a map document and change the transparency of the raster layers who's names begin with the letter "g". Here is what I have so far as part of a windows form.

private void button1_Click(object sender, EventArgs e)
{
    IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
    IMap map = mxdoc.FocusMap;
    for (int i = 0; i < (map.LayerCount - 1); i++)
    {
        ILayer currentLayer = map.get_Layer(i);
        if (currentLayer.Name[0].ToString() == "g")
        {
            //current layer transparency = 50%
        }
    }
}

the line that is commented out is where I need help. I can't seem to find the right code to do what I want within the ILayer class. Is this not what I should be using?

2 Answers 2

3

The easiest way to loop through layers in ArcMap is to use IEnumLayer. You get a reference to that from IMap.Layers. http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//001200000482000000

Then check the type and cast each raster layer returned by the enumerator to IRasterLayer. http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//001200000p9n000000

And then set the transparency with ILayerEffects. http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#/ILayerEffects_Interface/00120000072m000000/

EDIT to show example of IEnumLayer:

protected override void OnClick()
{
    IMap pMap = ArcMap.Document.FocusMap;
    ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
    uid.Value = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}";
    IEnumLayer pEnumLayer = pMap.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true);
    pEnumLayer.Reset();
    ILayer pLayer = pEnumLayer.Next();
    do
    {
        MessageBox.Show(pLayer.Name);  //or code that sets layer transparency.
        pLayer = pEnumLayer.Next();
    } while (pLayer != null);
}

IEnumLayer is nice because it allows you to choose whether or not to drill down into composite layers (using the second argument in the get_Layers function), and it allows you to restrict which types of layers are returned (first argument in the get_Layers function). The above only returns RasterLayers. An alternative way is to get all the layers (first argument in the get_Layers function to null), and then test each layer for its type and do something depending on the type of layer.

GUIDs to be used to restrict layers returned can be found here: http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//00490000005w000000

2
  • Excellent, this works great. I didn't use the IEnumLayer but the casting and transparency setting is exactly what I needed. Could you elaborate on IEnumLayer and possibly give and example of how you would implement it.
    – GeoJohn
    Commented Jun 30, 2015 at 18:56
  • 1
    Edited answer with an example of IEnumLayer. Commented Jun 30, 2015 at 20:00
2

Further to Dan's answer on enumerating layers... with the UID.Value of IRasterLayer (see example UIDs here) supplied to the get_layers function you don't need to check later that it is a raster layer you're playing with, however sometimes you need to use the UID for IDataLayer which will return feature layers and raster layers, so to check this use:

if (pLayer is IRasterLayer)

Another thing you do need to check when working with layers is that the datasource is valid, using ILayer.Valid property before trying to adjust any of the settings of the layer (or trying to get a cursor especially):

if (pLayer.Valid)
{
    if (pLayer.Name.ToUpper()[1] == 'G')
    {
        IRasterLayer pRasterLayer = (IRasterLayer)pLayer;
        ILayerEffects pLayerEffects = (ILayerEffects)pRasterLayer;
        pLayerEffects.Transparency = 60; // set layer transparency to 60%
    }
}

This should interface with Dan's answer to set the transparency of the layer.

2
  • Great, thanks for the extended info. I'm going to try to iterate based on the following: if ((pLayer.Valid) && (pLayer.Name[1] == 'g') && (pLayer is IRasterLayer))
    – GeoJohn
    Commented Jul 1, 2015 at 13:48
  • 1
    I don't recommend doing it that way. Check the Valid separately or the pLayer is IRasterLayer could cause an exception.. (pLayer.Name[1] == 'g') && (pLayer is IRasterLayer) can be done together though, saves one level of indent. Commented Jul 1, 2015 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.