cpicciri

start ArcMap/load layers from standalone C# application

Discussion created by cpicciri on Jul 13, 2011
Latest reply on Jul 13, 2011 by cpicciri
Hi everyone,

I am writing a standalone application in C# for creating automated jpg maps. Originally, the code was in VBA and I am converting it to C#. I've written a couple of other standalone applications in C# where I queried and edited data but in those I was able to do it without having to call up ArcMap. Unforunately, I think for working with layouts, graphics, etc., I have to use ArcMap. If I am mistaken in this, please correct me.

Anyhow, here's my problem. I can start ArcMap in code and load seveal SDE layers into it. The problem occurs when I try to do anything with the layers in the TOC. If I try to open an attribute table or change symbology, the screen flashes, all of the names of the layers in the TOC go away and all I am left with is the on/off checkbox with the red broken link exclamation mark next to each layer. After a few seconds, ArcMap crashes.

I've seen a couple of similiar posts in the old forums but no solutions. Anyone have an idea of what I am doing wrong?

Below is my code for starting ArcMap and loading SDE layers plus a screen capture of the error when ArcMap crashes.

Thanks for your time,
Carlos

internal static void StartArcMap()
{
    try
    {
 m_MxDocument = new MxDocumentClass();
 m_application = ((IDocument)m_MxDocument).Parent;
 m_application.Visible = true;

 // Wait for ArcMap to initialize before continuing.
 System.Windows.Forms.Application.DoEvents();
    }
    catch (Exception ex)
    {
 MessageBox.Show(ex.Message);
    }
}



internal static ILayer AddSDEFeatureClass(string server, string instance, string user,
       string password, string version, string featureClassName, 
       string tocName, short transparency, bool labels,
       bool visibility, bool selectability)
{
    try
    {
 IPropertySet pPropertySet = new PropertySetClass();
 pPropertySet.SetProperty("SERVER", server);
 pPropertySet.SetProperty("INSTANCE", instance);
 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);
 IGeoFeatureLayer pGeoFeatureLayerOut = pFeatureLayer as IGeoFeatureLayer;

 // Apply TOC name.
 pGeoFeatureLayerOut.Name = tocName;

 // Set transparency.
 ILayerEffects pLayerEffects = pFeatureLayer as ILayerEffects;
 pLayerEffects.Transparency = transparency;

 // Collapse the layer in the TOC.
 ILegendInfo pLegendInfo = pGeoFeatureLayerOut as ILegendInfo;
 ILegendGroup pLegendGroup = new LegendGroupClass();
 pLegendGroup = pLegendInfo.get_LegendGroup(0);
 pLegendGroup.Visible = false;

 // if labels = true, turn labels on, otherwise off.
 if (labels == true)
     pGeoFeatureLayerOut.DisplayAnnotation = true;
 else
     pGeoFeatureLayerOut.DisplayAnnotation = false;

 // If visibility = true, turn layer on, otherwise off.
 if (visibility == true)
     pGeoFeatureLayerOut.Visible = true;
 else
     pGeoFeatureLayerOut.Visible = false;

 // If selectability = true, turn layer on, otherwise off.
 if (selectability == true)
     pGeoFeatureLayerOut.Selectable = true;
 else
     pGeoFeatureLayerOut.Selectable = false;

 m_MxDocument.ActiveView.Refresh();
 m_MxDocument.UpdateContents();

 return pGeoFeatureLayerOut;

    }   //end try
    catch (Exception ex)
    {
 MessageBox.Show(ex.Message);
 return null;
    }
}

Attachments

Outcomes