Select to view content in your preferred language

ArcMap crashes when trying to use AddLayer with C#

480
2
07-30-2010 12:08 PM
LindaMcCafferty
New Contributor II
Hello -

I'm trying to create a standalone application that will run from TaskScheduler. So far it accomplishes several tasks fine such as: create a directory, copy a GDB to that directory, import tables into the GDB, create indexes, compact the database and create a fileGeodatabase.

At this point it successfully opens ArcMap, gets a handle on the IApplication, MxDocument etc. The next step is to add a layer. This is when ArcMap CRASHES. ugh!

I've tried to add several different types of layers, but ArcMap still quits on the line:
m_MxDocument.FocusMap.AddLayer(pFeatureLayer);

If anyone has an idea of how to start troubleshooting this, I would greatly appreciate the help.

Thanks, Linda

internal static ILayer AddSDEFeatureClass(string server, string instance, string database,
string user, string password, string version,
string featureClassName, string layerFileName,
string tocName, string groupLayerName,
short transparency, bool labels,
bool visibility, bool selectability)
{
//Purpose: Adds an SDE feature class to the active map frame.
try
{
IActiveView pActiveView = m_MxDocument.ActiveView as IActiveView;
IMap pMap = m_MxDocument.FocusMap;
IPropertySet pPropertySet = new PropertySetClass();
pPropertySet.SetProperty("SERVER", server);
pPropertySet.SetProperty("INSTANCE", instance);
pPropertySet.SetProperty("DATABASE", database);
pPropertySet.SetProperty("USER", user);
pPropertySet.SetProperty("PASSWORD", password);
pPropertySet.SetProperty("VERSION", version);
IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.Open(pPropertySet, 0) as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(featureClassName);
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass;
m_MxDocument.FocusMap.AddLayer(pFeatureLayer); CRASH HAPPENS HERE !

......
0 Kudos
2 Replies
JamesFry
New Contributor
Hi Linda,

Did you manage to solve this problem? We had horrendous problems when trying to automate ArcMap from C#, but we did get a bit further than you.

I seem to remember ESRI UK Support suggesting that we use the objectfactory to create the FeatureLayer, so that it is created on the ArcMap threads/compartment, rather than from our code, eg:

IFeatureLayer featureLayer = (IFeatureLayer)objectFactory.Create("esriCarto.FeatureLayer");

With our code we were attempting to automate the addition of data (mainly points, but eventually polylines and polygons) from some C# code, and listen for selection etc. We were able to add the data into geodatabase and create a layer (and see the relevant featureclass etc in the tree), but sadly we were unable to get anything to be displayed in ArcMap unless we removed the layer and readded it manually within ArcMap.

Unfortunately our developers kit licence has now expired, and my management are struggling to justify spending such a large amount of money on renewing the licence, given that we were unable make it work (even with numerous support calls to ESRI UK).
0 Kudos
ChristineHarvey
Deactivated User
Hi Linda,

This is some code that I wrote in C# that adds a layer to a map.
make a call to it: 

static void AddLayerFileToMap(string lyrPath, string shapefilename)
    {
        string document = @"C:\Workspace\Test.mxd";
      

        IDocument pDoc = new MxDocument();

        IApplication pApp = pDoc.Parent;
        pApp.OpenDocument(document);
        ILayer pLayer = MakeLayer((IObjectFactory)pApp, lyrPath, shapefilename);
        IMxDocument pMxDoc = (IMxDocument)pDoc;

        pMxDoc.FocusMap.AddLayer(pLayer);
     }

static ILayer MakeLayer(IObjectFactory pObjFact, string strWSDir, string strFCName)
    {

        IWorkspaceFactory pWSF = (IWorkspaceFactory)pObjFact.Create("esricore.ShapeFileWorkspaceFactory");
        IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSF.OpenFromFile(strWSDir, 0);
        IFeatureClass pFC = pFWS.OpenFeatureClass(strFCName);
        IFeatureLayer pFLayer = (IFeatureLayer)pObjFact.Create("esriCore.FeatureLayer");

        pFLayer.FeatureClass = pFC;
        pFLayer.Name = strFCName;

        ILayer layerToReturn = pFLayer;

        return layerToReturn;


    }
0 Kudos