Problem loading a .lyr file with "categories"

1285
4
06-17-2021 06:26 AM
AlvaroA
New Contributor II

Hello everyone,
I premise that we work on geodatabase files.

We have developed some code (ArcObject c#) that allows to copy an existing featureclass, creating a new one, and then insert the new feauteclass in the TOC copying the symbology of the existing layer (we save the .lyr of the parent layer and load it on the child layer).
We have layers with simple symbology (features - single: one symbol for all features) and a layer with "Categories" symbology.
During the copying process we encounter an error on the layer with "Categories" symbology.

The problem is as follows:
in a rather random way the symbology is not correctly copied to the child layer. see example

layer.png

(father: PUNTI_DI_RACCOLTA; child a-PUNTI_DI_RACCOLTA)

If we try to open the layer properties-symbology the CATEGORIES part is not there at all and even if the objects are there they are not displayed.


We did some tests on different machines to understand if it is a script problem or not and these are our results:
- on windows 2016 server machine and unpatched ArcMap the bug happens in 10% of the tests.
- on windows 8.1 pro machine and ArcMap patched (ArcGIS-1061-DT-FGSP-Patch, ArcGIS-1061-DT-GU-Patch) the bug happens in 90% of the tests.

This led us to think that it might be a bug.

EDIT: 22/06/2021 this is my code:

Spoiler

ILayer layer = utils.ArcGIS.getLayerByName(mxdoc, layerDAcopiare);

string layerPath = @"";

if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}


if (layer is FeatureLayer)
{
try
{
layerPath = Path.Combine(tempPath, layer.Name);
if (!layer.Name.Contains(".lyr"))
{
layerPath += ".lyr";
}
ILayerFile layerFile = new LayerFileClass();
layerFile.New(layerPath);
layerFile.ReplaceContents(layer);
layerFile.Save();
}
catch (Exception eee)
{ }
}

string nome = nomeScenario.Replace(' ', '_');
string fcName = nome + "__" + nomeLayer; //se si mette il nome vanno inseriti i _
IWorkspace workspace = ProjectCls.getGDBWorkspace(mxdoc);
IFeatureWorkspace fws = workspace as IFeatureWorkspace;
IFeatureClass fc = fws.OpenFeatureClass(fcName);

IGxLayer gxLayer = new GxLayerClass();
IGxFile gxFile = gxLayer as IGxFile;
gxFile.Path = layerPath;
ILayer newLayer = gxLayer.Layer as ILayer;
newLayer.Name = nomeNew;
IFeatureLayer featureLayerNEW = newLayer as IFeatureLayer;
featureLayerNEW.FeatureClass = fc;
string nomeNew = nomeScenario + "-" + nomeLayer;
featureLayerNEW.Name = nomeNew;
newLayer = featureLayerNEW as ILayer;

 

Thanks to all for the help

Astrid

0 Kudos
4 Replies
John_Spence
Occasional Contributor III

Did you consider just loading the parent LYR file and then switching the source to the Child programmatically once it is in the MXD and/or Pro Project?

0 Kudos
AlvaroA
New Contributor II

Hi @John_Spence , I updated my post with my code. 

I create a new GxFile with the .lyr, than create a Layer as a cast of the GxFile, than cast the Layer as a FeatureLayer and change the featureclass and the name.

How can I create a layer from an existing one?

John_Spence
Occasional Contributor III

If the layer is in the TOC, then you can copy and paste it.  It will read the same layer source, but you can tweak the settings, etc.

0 Kudos
AlvaroA
New Contributor II

The problem is that I need to do this by code (Arcobject c#): I must duplicate all layer in TOC an change the source. 

0 Kudos