Beruflich Dokumente
Kultur Dokumente
GS304-1 The AutoCAD® Map Geospatial Platform API shares many classes and methods with
Autodesk MapGuide Enterprise and MapGuide Open Source, so code written for one product can work
in another with minimal modification. This class will examine the similarities and differences in how the
API is implemented in each environment. A variety of common tasks will be demonstrated, such as
creating and modifying layer definitions, selecting features, and working with selection sets, zooming and
highlighting selected features, and performing buffer operations. We will also look at additional API
features available in AutoCAD Map, such as events and working with edit sets.
scott.mcfarlane@woolpert.com
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
Introduction
The time has come: Users of Autodesk Geospatial products can now go spatial. There are no
excuses. Rather than maintaining your maps in a DWG format, the recent releases of AutoCAD
Map 3D allow you to work directly with a variety of spatial databases, such as Oracle Spatial or
SQL Server 2008, or a spatial file format such as SHP or SDF. Your spatial data are stored in
one place – there is no import/export procedure, and none of the potential headaches of having
duplicate data. AutoCAD Map 3D 2010 makes working directly with spatial data easier than ever
by enabling the normal “CAD” tools to be used seamlessly with data from external sources.
Not only are your spatial data accessible from your desktop through AutoCAD Map 3D, your
data can also be made available to a much wider audience through the web using MapGuide
Enterprise. AutoCAD Map 3D and MapGuide Enterprise share some key elements with regard
to managing spatial data:
• Feature Data Objects (FDO) – FDO is the common data access technology used by
both products. FDO is an open source API that uses a provider-based methodology to
allow applications to read/write spatial data no matter how or where it is stored.
• XML-Based Resource Metadata – Both products use the same XML schema to
manage metadata, such as feature sources, map definitions, and layer stylization.
• API – The AutoCAD Map 3D Geospatial Platform API shares much of the same objects,
methods and properties with the MapGuide Server API.
This class will demonstrate examples of common tasks that use the Geospatial Platform API,
and show you how to maximize code re-use by separating environment-specific code into a
series of common functions. Code that uses the AutoCAD Map Geospatial Platform API can be
used with the MapGuide Server API (and vice versa) with little or no modification.
Key differences
While the signatures of these APIs are nearly identical, there are some key differences that are
important to understand.
2
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
In AutoCAD Map, once you add this reference, the API is ready to use.
MapGuide
In your MapGuide application, you reference MapGuideDotNetApi.dll, which is typically found
in the following location:
C:\Program Files\Autodesk\MapGuideEnterprise2010
\WebServerExtensions\www\mapviewernet\bin
Before you can start using the API in your web pages, you must include code to initialize the
MapGuide API, connect to a MapGuide server, and establish a session. Your pages will likely
contain code such as this: (C# example)
3
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
Resource Service
To begin working with resources (maps, layers, etc.) you must first obtain an instance of the
MgResourceService class
AutoCAD Map 3D
For AutoCAD Map 3D, this is very easy:
MapGuide
For MapGuide, you use the MgSiteConnection object you obtained with the initialization code
shown earlier.
MgResourceService resourceService =
siteConnection.CreateService(MgServiceType.ResourceService)
as MgResourceService;
Note: The classes that are shared between the two APIs have the prefix Mg. The AutoCAD
Map 3D Geospatial API includes additional classes with the prefix Ac. Many of the Ac
classes extend existing Mg classes, and some provide additional functionality unique to the
AutoCAD Map environment.
4
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
MapGuide
Obtaining the current map in a MapGuide application is a bit more complex. If you want a
generic method to do this (in which the name of the map is not known) you must know the
MapGuide session ID and the Web Layout that is currently being displayed. These two pieces of
information are passed as URL parameters, SESSION and WEBLAYOUT to the page loaded
into the task pane in the AJAX viewer. If you use the “Flexible Layout” viewer, the name of the
current map is passed as the URL parameter MAPNAME.
The following code demonstrates a method that returns the current map as an MgMapBase
object. This code assumes that the session variable MAPNAME exists.
// Initialize MapGuide
string webConfigIni = MapPath("/mapguide") + "\\webconfig.ini";
MapGuideApi.MgInitializeWebTier(webConfigIni);
Common Code
Once we obtain an MgMapBase object, in either MapGuide or AutoCAD Map 3D, most things
you would do with that object can be done using the same code in either environment. For
example, the following code will populate a list box with the names of all the layers in the map.
This source code can be shared between a Map 3D .NET application, and a MapGuide
ASP.NET application.
if (map != null)
{
listBox1.Items.Clear();
foreach (MgLayerBase layer in map.GetLayers())
{
listBox1.Items.Add(layer.Name);
}
}
5
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
The next step to adding the layer to the map is to create a new instance of the MgLayerBase
class. The code for this is slightly different depending on the environment, so we’ll create
another method in our CommonFunctions class to handle this.
AutoCAD Map 3D
In AutoCAD Map, you use the Create method of the AcMapLayer class.
MapGuide
In MapGuide, you simply use the constructor from the MgLayer class.
Now that we have a common function to create a new MgLayerBase instance, the code to add
the new layer to the map in either environment is as follows:
6
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
layer.SetDisplayInLegend(true);
MgMapBase currentMap = CommonFunctions.GetCurrentMap();
currentMap.GetLayers().Add(layer);
CommonFunctions.RefreshMap(currentMap);
MapGuide
Notice that the last step in the code sequence above is a call to a RefreshMap method of our
CommonFunctions class. This is only needed for MapGuide – the AutoCAD Map 3D
implementation is empty. After a layer has been added to the map, there are two more steps
necessary for code written for MapGuide. First you must call the Save method of the MgMap
object, then you must send the necessary JavaScript code to the client to refresh the viewer.
AutoCAD Map 3D
To obtain the currently selected features in AutoCAD Map 3D, use the following code:
7
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
MapGuide
For MapGuide, the process of obtaining the current selection is different depending on whether
you are using the AJAX viewer or the DWF viewer. If you’re using the DWF viewer, you must
pass the selection information to the server before you can use it. The AJAX viewer does this
for you, so the process is a bit easier.
To obtain the currently selected features using the AJAX viewer in a MapGuide application, use
the following code:
while (featureReader.ReadNext())
{
foreach (MgPropertyDefinition propertyDef in propertyDefs)
8
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
{
if (propertyDef.PropertyType == MgFeaturePropertyType.DataProperty)
{
MgDataPropertyDefinition dataProp = propertyDef
as MgDataPropertyDefinition;
if (dataProp != null)
{
// Set default value.
string value = "(null)";
if (!featureReader.IsNull(propertyDef.Name))
{
// Reads the value from featureReader
// according to DataType.
switch (dataProp.GetDataType())
{
case MgPropertyType.String:
value = featureReader.GetString(propertyDef.Name);
break;
case MgPropertyType.Double:
value =
featureReader.GetDouble(propertyDef.Name).ToString();
break;
case MgPropertyType.Int16:
value =
featureReader.GetInt16(propertyDef.Name).ToString();
break;
case MgPropertyType.Int32:
value =
featureReader.GetInt32(propertyDef.Name).ToString();
break;
case MgPropertyType.Int64:
value =
featureReader.GetInt64(propertyDef.Name).ToString();
break;
case MgPropertyType.Boolean:
value =
featureReader.GetBoolean(propertyDef.Name).ToString();
break;
default:
break;
}
}
9
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
We will create a function for each environment that takes an MgSelectionBase object as an
argument, and highlights those features.
AutoCAD Map 3D
For AutoCAD Map 3D, you work with the implied selection set. First, you use the
AddFeaturesToSelectionSet method of the AcMapFeatureEntityService object to get an
AutoCAD SelectionSet object, then you use the SetImpliedSelection method of the Editor
object to highlight the objects. The following C# code demonstrates this.
// Set the new selection set as the implied (active) selection set
editor.SetImpliedSelection(result);
}
MapGuide
For MapGuide, you use the viewer API. First, you get the XML representation of the selection
using the ToXml method of the MgSelectionBase object. Then, in your ASPX page that
processes your selection, you build the JavaScript code needed to set the active selection in the
viewer. The JavaScript code calls the SetSelectionXML function of the viewer API.
10
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
Common Code
The following code, which can be shared between AutoCAD Map 3D and MapGuide,
demonstrates setting the active selection.
return null;
}
listBox1.Items.Clear();
while (reader.ReadNext())
11
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
{
listBox1.Items.Add(reader.GetString("Name"));
}
reader.Close();
}
12
Using the Geospatial Platform API in AutoCAD® Map 3D and Autodesk MapGuide®
13