2

Accessing current workspace using an ArcObjects add-in for ArcMap (Table of Contents has layer groups).

I am trying to retrieve the current WorkSpace using ArcObjects for an ArcMap add-in in C#. Any help would be appreciated. I've tried this:

IWorkspace ws = ((IDataset)ArcMap.Document.ActiveView.FocusMap.Layer[0]).Workspace;

This works if the table of contents has no group layers.

Thanks

whuber
70.4k16 gold badges189 silver badges285 bronze badges
asked Jan 15, 2013 at 21:42
1
  • 3
    I wouldn't necessarily consider the workspace of the first layer to be the "current" layer. Maybe question should be re-phrased as "How to find the workspace of a layer?". Commented Jan 16, 2013 at 0:44

3 Answers 3

3

It may work for you. CreateLayerList() method makes a list of layers of current map. It includes layers under group layers also.

 IWorkspace GetWorkspace()
 {
 var mapLayers = GetAllLayersFromMap();
 if(mapLayers.Count > 0)
 IWorkspace workspace =((IDataset)mapLayers[0]).Workspace;
 }
 List<ILayer> GetAllLayersFromMap()
 {
 var mapLayers = new List<ILayer>();
 CreateLayerList(null, _map, _map.LayerCount, mapLayers);
 return mapLayers;
 }
 void CreateLayerList(ICompositeLayer compositeLayer, IMap map, int mapCount, List<ILayer> mapLayers)
 {
 for (int i = 0; i < mapCount; i++)
 {
 ILayer layer;
 if (map != null && compositeLayer == null)
 layer = map.Layer[i];
 else if (compositeLayer != null)
 layer = compositeLayer.Layer[i];
 else
 return;
 ICompositeLayer comLayer;
 if (((comLayer = layer as ICompositeLayer) != null) && ((layer as IGroupLayer) != null))
 CreateLayerList(comLayer, null, comLayer.Count, mapLayers);
 else
 mapLayers.Add(layer);
 }
 }
answered Jan 16, 2013 at 3:41
2
  • Hi Guys thanks a lot for the help Rich and Emi works for me Commented Jan 17, 2013 at 15:16
  • Hi @Amo, if you get any answer helpful, you should give it a up vote :) Commented Jan 18, 2013 at 16:51
3

If you are on an edit session, it would be an option to get the IEditor.EditWorkspace which is an IWorkspace.

 UID editorUid = new UID();
 editorUid.Value = "esriEditor.Editor";
 IEditor3 editor = m_application.FindExtensionByCLSID(editorUid)as IEditor3;
 IWorkspace workspace = null;
 if(editor.EditState == esriStateEditing)
 {
 workspace = editor.EditWorkspace;
 }
answered Jan 16, 2013 at 0:59
2

A group layer is a coclass of ICompositeLayer. Use the count and layer properties of the ICompositeLayer interface to iterate through your group layers. So when you are looping through the TOC layers, check if the layer type is ICompositeLayer and then implement a sub loop through those layers as well.

answered Jan 15, 2013 at 23:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.