Adding XY event source programatically

885
8
04-03-2013 09:17 AM
MikeDuppong
New Contributor
I can add a table as an XYEventSource from an Access database, with the following code.  This works.  My problem is described after the code block.

srWorkspace = ao.OpenAccessWorkspace(dataSource);
srTable = ao.OpenTable(srWorkspace, "MyTable");
IDataset dataSet = srTable as IDataset;
IName tableName = dataSet.FullName;

IXYEvent2FieldsProperties xyEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass();
xyEvent2FieldsProperties.XFieldName = "myX";
xyEvent2FieldsProperties.YFieldName = "myY";

IXYEventSourceName xyEventSourceName = new XYEventSourceNameClass();
xyEventSourceName.EventProperties = xyEvent2FieldsProperties;
xyEventSourceName.EventTableName = tableName;

ftrLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();

SpatialReferenceEnvironment spatRefEnv = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem geoCS = spatRefEnv.CreateGeographicCoordinateSystem((int)ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_WGS1984);
geoCS.SetFalseOriginAndUnits(-180, -90, 1000000);
xyEventSourceName.SpatialReference = geoCS as ESRI.ArcGIS.Geometry.ISpatialReference;
IName xyName = xyEventSourceName as IName;
IXYEventSource xyEventSource = xyName.Open() as IXYEventSource;

IStandaloneTableCollection pTableCollection = map as IStandaloneTableCollection;
IStandaloneTable standaloneTable = new StandaloneTableClass();
standaloneTable.Table = srTable;
pTableCollection.AddStandaloneTable(standaloneTable);

ftrLayer.FeatureClass = xyEventSource as IFeatureClass;
ftrLayer.Name = "My Table";
map.AddLayer(ftrLayer);


The problem I'm having is that it's adding the table to the map as a feature layer with a feature class, and I don't want that.  I want it to behave exactly as if the user right-clicked on the table in ArcMap and chose the "Display XY Data..." menu item.

Right-clicking on the layer created as above and choosing Properties shows this for the source:

Data Type: XY Event Source
Location: C:\data\database.mdb
Feature Class: MyTable_Features
Feature Type: Simple
Geometry Type: Point

However, if you conduct the Display XY Data operation manually in ArcMap on a standalone table added to the TOC, the following shows for the source, and this is what I'm after:

Data Type: XY Event Source
Location: C:\data\database.mdb
Table: C:\data\database.mdb\MyTable
X Field: myX
Y Field: myY
Has Object-ID Field: Yes

What in the world am I doing wrong?

Thank you for any assistance!

Mike
0 Kudos
8 Replies
LeoDonahue
Occasional Contributor III
map.AddLayer(xyEventSource);


Wouldn't this work?  XYEventSource implements ITable, which you can pass to your map.AddLayer() method.
0 Kudos
MikeDuppong
New Contributor
Negative.  AddLayer() requires an ILayer.
0 Kudos
LeoDonahue
Occasional Contributor III
doh.  sorry about that...

I meant map.addTable()
0 Kudos
MikeDuppong
New Contributor
Thanks for the response.  However, I don't see an AddTable() method for the IMap interface.
0 Kudos
LeoDonahue
Occasional Contributor III
IMap is an Interface for concrete classes: Globe, Map, Scene.

IMap would not have an addTable() method because Globe and Scene would need to implement that contract.

How did you create your IMap variable?
0 Kudos
MikeDuppong
New Contributor
IMxDocument pMxDoc = m_app.Document as IMxDocument;
IMap map = pMxDoc.FocusMap;


m_app is set within the OnCreate() method of the BaseCommand:

if((hook) is IMxApplication)
  m_app = (IApplication)hook;
0 Kudos
LeoDonahue
Occasional Contributor III
pMxDoc.FocusMap

This returns an IMap reference. 

You need to QI (or cast) to the Map class if you want to use addTable().
0 Kudos
MikeDuppong
New Contributor
pMxDoc.FocusMap

This returns an IMap reference. 

You need to QI (or cast) to the Map class if you want to use addTable().


Thanks for sticking with me, Lee.

I QI'd the IMap to an ITableCollection and used its AddTable() method.  This results in:

Data Type: Standalone Table
Table Name: MyTable_Features
Table Type: XY Event Source
Location: C:\data\database.mdb
Has Object-ID Field: Yes

Still not quite there.  Still shooting for:

Data Type: XY Event Source
Location: C:\data\database.mdb
Table: C:\data\database.mdb\MyTable
X Field: myX
Y Field: myY
Has Object-ID Field: Yes

I feel like I'm painfully close...
0 Kudos