bkirchhoff

How to symbolize a Layer with a layerfile stored in an Add-In

Discussion created by bkirchhoff on Mar 20, 2015

Here's the answer to a question I never got around to asking three days ago (no, I'm not looking for points - I just hope it saves someone else hours of fruitless searching).

I am writing an Add-In that creates featureclasses that I want to add symbology to before they are added to ArcMap.  I created a template layer in an ArcMap session that provides the proper symbology then saved it out to a layerfile.  The idea is to deploy the layerfile along with the add-in.  A solution cobbled together from various sources (thanks to those who contributed, unfortunately I've lost the cites...)

1. In Visual Studio - create a new folder under your add-in project folder called (in this instance) "Layerfiles"

2. Copy / paste the template layerfile into the new folder

4. Set the saved layerfiles properties: Build Action = Content, Copy to Output Directory = Copy Always

3. Add method to project

        private void AddSavedLayerFileToMap(string folderName, string layerName, IFeatureClass gpsFClass, string aliasName)

        {

            try

            {

                //... Get map from current MxDocument

                IMap pMap = pMxDoc.ActiveView.FocusMap;

 

                //... Get the path to the layerfile

                string codeBase = Assembly.GetExecutingAssembly().CodeBase;

                UriBuilder uri = new UriBuilder(codeBase);

                string AsmPath = System.IO.Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));

                string layerFilePath = System.IO.Path.Combine(AsmPath, folderName + "\\" + layerName);

 

                //... Create the layer to add to ArcMap

                ILayerFile layerFile = new LayerFileClass();

                layerFile.New(layerFilePath);

                ILayer pSymbolLayer = layerFile.Layer;

                IFeatureLayer pSymFLayer = (IFeatureLayer)pSymbolLayer;

                pSymFLayer.FeatureClass = gpsFClass;

                pSymbolLayer.Name = aliasName;

 

                pMap.AddLayer(pSymFLayer);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

The values of the parameters:

foldername - name you gave to the folder you added to your project folder in Visual Studio

layername - name of the template layerfile you added to the above folder

featureclass - the featureclass you want to add to ArcMap symbolized with the layerfile template

aliasName - a friendly name to call the layer once its added to the TOC in ArcMap

Outcomes