Add MXD as group layer instead of map

870
5
12-19-2019 02:15 AM
MaxMax2
Occasional Contributor II

I'm trying to prevent Pro from importing MXD as new map. I want to insert content of the document as a group layer within current map.

What I tried:

var item = ItemFactory.Instance.Create("doc.mxd");

var currentMap = MapView.Active.Map;
var map = await QueuedTask.Run(() => MapFactory.Instance.CreateMapFromItem(item));

await QueuedTask.Run(() =>
{
    var groupLayer = LayerFactory.Instance.CreateGroupLayer(currentMap, 0, map.Name);
    foreach (var layer in map.Layers)
    {
        groupLayer.MoveLayer(layer, int.MaxValue);
    }
});

I get exception on line 11:

ArcGIS.Desktop.Mapping.MappingException
HResult=0x80004005
Message=Source layer's container not found.
Source=ArcGIS.Desktop.Mapping

Inner Exception 1:
COMException: Source layer's container not found.

Is it possible to import map as group layer in some way?

Tags (3)
0 Kudos
5 Replies
UmaHarano
Esri Regular Contributor

Hi Max

The MoveLayer on the group layer allows you to move layers within the group of the same layer.

I used copyLayer instead -

var item = ItemFactory.Instance.Create("doc.mxd");

            var currentMap = MapView.Active.Map;
            var map = await QueuedTask.Run(() => MapFactory.Instance.CreateMapFromItem(item));

            await QueuedTask.Run(() =>
            {
                var groupLayer = LayerFactory.Instance.CreateGroupLayer(currentMap, 0, map.Name);
                foreach (var layer in map.Layers)
                {
                    if (LayerFactory.Instance.CanCopyLayer(layer))
                         LayerFactory.Instance.CopyLayer(layer, groupLayer);
                }
            });‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
MaxMax2
Occasional Contributor II

Hi Uma,

CopyLayer throws:

System.ArgumentException
HResult=0x80070057
Message=Operation on file my_computer_path\AppData\Local\Temp\ArcGISPro11192\F986138A-1986-48FA-AB7D-E5539108F359\Metadata\599ca023c9e23f36f1b3a3e2650c49df.xml failed. Unknown error.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Max,

Are you able to copy the layers manually?

For example (using the UI) -

* Import the mxd into Pro.

* Open another Map and create a group layer.

* Copy the layers from the mxd imported map to the new group layer in the new map.

Does this workflow succeed?

Also, if you can give me more information on the mxd and the types of layers in it, we can get to the bottom of the problem. 

I was able to use your code snippet on a simple mxd I had with a point, line and poly layer. The import and the subsequent copy worked in my test case.

Thanks!

Uma

0 Kudos
MaxMax2
Occasional Contributor II

I've used for test Business_Analyst_Maplex.mxd from Business Analyst Maplex.mxd. I use Pro 2.3.2, but I think the problem exists in later versions too. Please do tests with the map I pointed to.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Max

I have informed the Business Analyst team since this happens with this specific data. If you have a reproducible workflow with this, you could also contact technical support.

Thanks!

Uma

0 Kudos