Here i have opened arc map programatically add all the shape files from the pgdb. programatically .
I am able to see the names of all layers in the TOC but i cannot see any feature in active view. and after some time the arcmap crashes .
public class Util
{
public IApplication m_application;
public IDocument doc;
public IAppROT m_pAppRot;
public int m_appHWnd = 0;
public IMxDocument m_pMxdocument;
public IMap pMap=null ;
public IPropertySet pPropset;
public IWorkspaceFactory pAccessFact;
public IFeatureWorkspace pFeatureWorkspace;
public IFeatureLayer pfeaturelayer;
public IGeoFeatureLayer pGeofeaturelayer;
public IWorkspaceFactory wFact;
public IWorkspace wSpace;
public Collection AllFeatclassarray;
public Collection featureClassCollection;
public string[] lyrname;
public IFeatureClass pFeatclass;
public void StartArcMap()
{
CheckLicence();
try
{
doc = new ESRI.ArcGIS.ArcMapUI.MxDocumentClass();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
if (doc != null)
{
IObjectFactory objFactory = m_application as IObjectFactory;
m_application = doc.Parent;
m_appHWnd = m_application.hWnd;
m_application.Caption = "Ur appliction name ";
doc = m_application.Document as MxDocument;
AddShapefile();
m_application.Visible = true;
//Hide All toolbars of Arcmap
m_application.Document.CommandBars.HideAllToolbars ();
ICommandBars pCommandBars;
pCommandBars = m_application.Document.CommandBars;
UIDClass u = new UIDClass();
u.Value = "Tools.Administrator";
ICommandBars pCmdBars;
pCmdBars = m_application.Document.CommandBars;
ICommandBar pNewBar;
pNewBar = pCmdBars.Create("Adminstrator", ESRI.ArcGIS.SystemUI.esriCmdBarType.esriCmdBarTypeToolbar);
UIDClass clsSearch = new UIDClass();
clsSearch.Value = "Tools.clsSearch";
int intclsSearch = pNewBar.Count;
object objclsSearch = intclsSearch;
pNewBar.Add(clsSearch, ref objclsSearch);
}
}
public void AddShapefile()
{
try
{
IObjectFactory objFactory = m_application as IObjectFactory;
//Add the layer to document
IBasicDocument doc = (IBasicDocument)m_application.Document;
// Type pAccessFact = typeof(AccessWorkspaceFactoryClass);
string path = "E:\\Data.mdb";
pPropset = new PropertySetClass();
pPropset.SetProperty("DATABASE", path);
wFact = new AccessWorkspaceFactoryClass();
wSpace = wFact.Open(pPropset, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)wSpace;
AllFeatclassarray = GetAllFeatureClasses();
featureClassCollection = AllFeatclassarray;
for ( int i = 1; i <= featureClassCollection.Count; i++)
{
IFeatureClass pFeatclass1;
pfeaturelayer = new FeatureLayerClass();
string pFeatclass = featureClassCollection as string;
pfeaturelayer = (IFeatureLayer)objFactory.Create("esriCarto.FeatureLayer");
pfeaturelayer.FeatureClass = featureWorkspace.OpenFeatureClass(pFeatclass);
// pFeatclass1 = featureWorkspace.OpenFeatureClass(pFeatclass);
pfeaturelayer.Name = pfeaturelayer.FeatureClass.AliasName;
pGeofeaturelayer = (IGeoFeatureLayer)pfeaturelayer;
pGeofeaturelayer.DisplayAnnotation = true;
doc.AddLayer(pGeofeaturelayer);
}
// AllFeatclassarray.Add();
}
catch (Exception ex)
{
MessageBox.Show(""+ex);
}
}//AddShapefile
private Collection GetAllFeatureClasses()
{
Collection featureClassList = new Collection();
if (wSpace.Type == esriWorkspaceType.esriFileSystemWorkspace)
{
GetFeatureClassesForShape(featureClassList);
}
else
{
GetDatasets(featureClassList);
}
return featureClassList;
}
private void GetDatasets(Collection featureClassList)
{
IEnumDataset eDataset;
IDataset ds;
eDataset = wSpace.get_Datasets(esriDatasetType.esriDTAny);
ds = eDataset.Next();
while (ds != null)
{
LoopThroughClasses(ds, featureClassList);
ds = eDataset.Next();
}
}
private void LoopThroughClasses(IDataset dataset, Collection featureClassList)
{
try
{
IFeatureDataset fDataset;
IFeatureClassContainer fcContainer;
IEnumFeatureClass enumFeatCls;
IFeatureClass featCls;
if (dataset.Type == esriDatasetType.esriDTFeatureDataset)
{
fDataset = (IFeatureDataset)dataset;
fcContainer = (IFeatureClassContainer)fDataset;
enumFeatCls = fcContainer.Classes;
featCls = enumFeatCls.Next();
while (featCls != null)
{
IDataset ds = (IDataset)featCls;
featureClassList.Add(featCls, ds.Name, null, null);
featCls = enumFeatCls.Next();
}
}
else
{
IFeatureClass pFeatuclass;
pFeatuclass = dataset as IFeatureClass;
featureClassList.Add(pFeatuclass.AliasName, null, null, null);
}
}
catch (Exception exp)
{
MessageBox.Show(""+exp);
}
}
private void GetFeatureClassesForShape(Collection featureClassList)
{
IEnumDataset eDataset;
IDataset ds;
IFeatureClass featCls;
eDataset = wSpace.get_Datasets(esriDatasetType.esriDTFeatureClass);
ds = eDataset.Next();
while (ds != null)
{
featCls = (IFeatureClass)ds;
featureClassList.Add(featCls, ds.Name, null, null);
ds = eDataset.Next();
}
}
public void ShutDownArcMap()
{
try
{
if (m_application != null)
{
//Try to close any modal dialogs by sending the Escape key
//It doesn't handle the followings:
//- VBA is up and has a modal dialog
//- Modal dialog doesn't close with the Escape key
Microsoft.VisualBasic.Interaction.AppActivate(m_application.Caption);
int nestModalHwnd = 0;
//Manage document dirty flag - abandon changes
IDocumentDirty2 docDirtyFlag = (IDocumentDirty2)m_application.Document;
docDirtyFlag.SetClean();
//Stop listening before exiting
//m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved);
//m_appROTEvent = null;
//Exit
m_application.Shutdown();
m_application = null;
//Reset UI for next automation
}
}
catch { }
}
}
}
The arc map open all the layer are visible in TOC but not in the active view and the arcmap crashes after some time .And the error I have already posted in my earlier post.
Freinds help me my arc map is crashing I cannot know the reason please tell me if any of you know. Because after doing this only i can proceed in my application. Desperately need ur help.