autocad map 3dplatform api training
DESCRIPTION
AutoCAD Map 3DPlatform API Training. Agenda. Overview Map Resources Working with Features. Overview. AutoCAD Map 3D 2009 API. Map 3D Platform API. Map LISP/ADSRX API. Map ObjectARX API. Map ActiveX API. Map .NET API. FDO API. AutoCAD API. Entities. Features. AcDb. - PowerPoint PPT PresentationTRANSCRIPT
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 1
AutoCAD Map 3DPlatform API Training
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 2
Agenda
OverviewMap ResourcesWorking with Features
© 2007 Autodesk - "Beyond the Box” Expanding the Developers Toolbox 3
Overview
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 4
AutoCAD Map 3D 2009 API
AutoCAD API
Map
Obj
ectA
RX
API
Map
LIS
P/A
DSR
X A
PI
Map
.NET
API
Map
Act
iveX
API
FDO API
Map
3D
Pla
tform
API
AcDb
Entities
FDO Data Store
Features
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 5
The Geospatial APIs
FDO
Geospatial Platform
MapGuide
Coordinate System
Resource Service
Feature Service
Geometry
Exceptions
System TypesCollections
Mapping Service
Drawing Service
Rendering Service
Tiling Service
Shared Code SharedInterface
Site Service Map 3D Platform
API
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 6
API Components
ExceptionsCollections
Map and Layers Geometry
Resource Service
Common
Feature Service Feature-Entity Service
Coordinate System
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 7
Resource ServiceResources Files and configuration information
required for map and layer display Layer definition and Feature source Stored in drawings or drawing
templates and are specific to the drawingResource Service Used for manipulating resources Adding resources to Map Copying resources Checking for existence of resources Getting the contents of a resource SDF
SHPOracle
Library://
LayerDef.
FeatureSrc.DW
G D
atab
ase
Repository
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 8
Feature Service Provides access to FDO functionality
Determine available providers and their capabilities
Verify connection property values and test connection
Provides schema information Insert, delete, update and select
features Execute SQL commands
Feature Service
FDO
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 9
Feature-Entity Service
Enables AutoCAD Selection API support for Features Selection set management
Support for feature highlighting
New
AcMapFeatureEntityService
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 10
Map and Layers
Provides access to map, layer groups and layers
AcMapMap represents the map shown in the Display Manager
Map layers are FDO layers
Get and set map and layer properties
MgLayerCollectionMgLayerGroupCollection
MgMapBase
AcMapMap
MgLayerBase
AcMapLayer
MgLayerGroup
AcMapLayerGroup
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 11
Geometry Contains geometric
object types and operations to manipulate themSupports
spatial comparisons between objects
creation of new objects based on the intersection, difference, or union of existing objects
creation of buffers around objects
MgMultiPolygon
MgPoint
MgLineString
MgPolygon
MgCurveString
MgCurvePolygon
MgMultiPoint
MgMultiLineString
MgMultiCurveString
MgMultiCurvePolygon
MgMultiGeometry
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 12
Coordinate Systems
MgCoordinateSystem
MgCoordinateSystemType
MgCoordinateSystemTransform
MgCoordinateSystemMeasure
MgCoordinateSystemFactory
Provides unified access to coordinate system information Enables coordinate system transformations
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 13
Utility ClassesCommon The Common classes
provide a set of utility classes
MgByte
MgByteSink
MgService
MgMimeType
MgFeatureInformation
MgByteReader
MgByteSource
MgColor
MgDateTime
MgWarning
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 14
Utility ClassesMgByteSource Byte data source with a specific
content type Can be initialized from an array
of bytes in memory or a file
MgByteReader Reads data from MgByteSource
MgByteSource::GetReader()
MgByteSink Used for writing MgByteReader
data to file Construct with MgByteReader and
call ToFIle()
MgByteSource
MgByteReader
MgByteSink
File
byte [ ]
Other APIs
A<{9>$
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 15
Utility ClassesCollections Container for a set
of objects of the same type
MgCollection
MgStringCollection
MgPolygonCollection
MgLineStringCollection
MgCoordinateCollection
MgGeometryCollection
MgIntCollection
MgPointCollection
MgPropertyCollection
MgLinerRingCollection
MgCurveRingCollection
MgFeatureSchemaCollection
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 16
Utility Classes
Exceptions A set of classes
representing all possible exceptions that can be generated by the application.
100+ classes
MgFileNotFoundException
MgGeometryException
MgInvalidCoordinate-SystemException
MgFileIoException
MgFdoException
MgOutOfMemoryException
MgClassNotFoundException MgInvalidCastException
MgResourcesException MgOverflowException
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 17
Getting StartedDevelopment Environment Visual Studio 2005
Assemblies Geospatial Platform Autodesk.Map.Platform.dll (Map installation folder) AutoCAD acmgd.dll, acdbmgd.dll (Map installation folder) FDO OSGeo.FDO.dll, OSGeo.FDO.Common.dll, OSGeo.FDO.Geometry.dll
(<Map folder>\FDO\bin)
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 18
Getting Started
FDO
Geo
spat
ial P
latfo
rm A
PI M
ap P
latfo
rm A
PI
Namespaces
Autodesk.Gis.Map.Platform Autodesk.Gis.Map.Platform.Interop OSGeo.MapGuide OSGeo.FDO* OSGeo.FDO.Common*
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 19
Getting StartedDEMO – Basic information about a map Project language (C# or VB) and type (Class library) Assembly references Map 3D - Autodesk.Map.Platform.dll AutoCAD – acmgd.dll
Set the Copy Local properties of the assembly to False Command Create command function with CommandMethod attribute Access map properties
© 2007 Autodesk - "Beyond the Box” Expanding the Developers Toolbox 20
Map Resources
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 21
Map Resources Files and configuration information
required for map and layer display Stored in XML format in the resource
repository Has a unique path in the repository. e.g.
Library://Data/Raster/Redding.LayerDefinition, Library://Data/SDF/Zoning.FeatureSource Resource types Layer definition (LayerDefinition) Feature source (FeatureSource) Resource service Methods of AcMapResourceService are used for
creating resources
MgResourceService
AcMapResourceService
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 22
Map ResourcesLayerDefinition and FeatureSource Resource Identifier of the FeatureSource is used in LayerDefinition
.sdf
Map Layers
Data Source
FeatureSource
Libr
ary:
//SDF
_1.F
eatu
reSo
urce
Libr
ary:
//SDF
_1.L
ayer
Defin
ition
LayerDefinition
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 23
Map ResourcesCreating a resource service
Resource Identifier Defines the location of a resource in the repository Encapsulated in MgResourceIdentifier Resource pointed to does not need to exist yet
// Create a resource Id for a layer definition resourceMgResourceIdentifier LayerDefResId = new MgResourceIdentifier("Library://Data/Raster/Redding.LayerDefinition");
// Create a resource Id for a feature source resourceMgResourceIdentifier FtrSrcResId = new MgResourceIdentifier("Library://Data/Raster/Redding.FeatureSource");
// Get an MgService object from AcMapServiceFactory and cast it to AcMapResourceServiceAcMapResourceService resServ = (AcMapResourceService)AcMapServiceFactory.GetService(MgServiceType.ResourceService);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 24
Map ResourcesGetting Resource Content Get the XML content of a LayerDefinition or FeatureSource
using MgResourceService::GetResourceContent
//Get the content of a FeatureSourceMgResourceIdentifier id = new MgResourceIdentifier("Library://Data/Raster/Redding.FeatureSource");MgResourceService resServ = (AcMapResourceService)AcMapServiceFactory.GetService(MgServiceType.ResourceService);bool resourceExists = resServ.ResourceExists(id);MgByteReader byteRdr = null; if (resourceExists ) { byteRdr = resServ.GetResourceContent(id); MgByteSink byteSink = new MgByteSink(byteRdr); byteSink.ToFile(@"c:\FeatureResource.Xml"); }
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 25
Map ResourcesFeature Source Describes the information
required to connect to the data source
See FeatureSource-1.0.0.xsd in “<Map 3D 2009 SDK Folder>\Schema”
Referenced via “ResourceId” element in a LayerDefinition XML
Connect to an FDO provider by creating a feature source xml file and adding it to the repository
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 26
Map ResourcesFeature Source Elements “Provider” specifies the name of the FDO provider used “Parameter” specifies a collection of name/value pairs for
connecting to the data source <FeatureSource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FeatureSource-1.0.0.xsd" version="1.0.0"><Provider>OSGeo.SDF.3.3</Provider><Parameter><Name>ReadOnly</Name><Value>false</Value></Parameter><Parameter><Name>File</Name><Value>C:\Data\SDF\Signals.sdf</Value></Parameter><ConfigurationDocument></ConfigurationDocument><LongTransaction></LongTransaction></FeatureSource>
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 27
Map ResourcesCreating a feature source in the repository Step 1: Generate feature source XML data
//XML Data from existing FeatureSource XML file// Load XML into memory and modifyXmlDocument doc = new XmlDocument();doc.Load(C:\XML\SampleFeatureSource.xml")// Make modificationsXmlNode providerNode = doc.GetElementsByTagName(“Provider");providerNode.InnerText = “OSGeo.SHP.3.3”;
//Save changes into a memory streamMemoryStream xmlStream = new MemoryStream();doc.Save(xmlStream);
//Get Feature source as a byte array byte [] ftrSrc = xmlStream.ToArray();
Encoding utf8 = Encoding.UTF8;String StrFtrSrc = new String(utf8.GetChars(ftrSrc));ftrSrc = new byte[StrFtrSrc .Length-1];
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 28
Map ResourcesCreating a feature source in the repository+ Step 2: Construct MgByteSource object from XML data Step 3: Extract data from MgByteSource object and add as
(feature source) resource to the repository
// Construct byteSource object from dataMgByteSource byteSource = new MgByteSource(ftrSrc, ftrSrc.Length);byteSource.SetMimeType(MgMimeType.Xml);
// Add to repositoryMgResourceIdentifier resId = new MgResourceIdentifier(“Library://Data/Raster/Redding.FeatureSource“);AcMapResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as AcMapResourceService;rs.SetResource(resId, byteSource .GetReader(), null);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 29
Map Resources Layer Definition Specification of the data source and stylization information for a
layer See LayerDefinition-1.2.0.xsd in “<Map 3D 2009 SDK Folder>\
Schema” Required for creating a layer
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 30
Map ResourcesLayer Definition Elements “ResourceId” defines the link to the data source through the
FeatureSource “VectorScaleRange”/”GridScaleRange” defines the
stylization to be applied to the features for a given scale range “Geometry” specifies the geometry property that should be
used to get the feature geometries “FeatureName” specifies a feature class or named
extension
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 31
Map ResourcesLayer Definition
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 32
Map ResourcesCreating a layer definition in the repository Step 1: Generate layer definition XML data
//XML Data from existing LayerDefinition XML file// Load XML into memory and modifyXmlDocument doc = new XmlDocument();doc.Load(C:\XML\SampleLayerDefinition.xml")// Make modificationsXmlNode ftrSrcNode = doc.GetElementsByTagName("ResourceId").Item(0);ftrSrcNode.InnerText = "Library://Data/Shp/Roads.FeatureSource”;
//Save changes into a memory streamMemoryStream xmlStream = new MemoryStream();doc.Save(xmlStream);
//Get Feature source as a byte array byte [] layerDef = xmlStream.ToArray();
Encoding utf8 = Encoding.UTF8;String strLayerDef = new String(utf8.GetChars(layerDef ));bytes = new byte[strLayerDef.Length-1];
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 33
Map ResourcesCreating a layer definition in the repository Step 2: Construct MgByteSource object from XML data Step 3: Extract data from MgByteSource object and add as
layer definition resource to the repository
// Construct byteSource object from dataMgByteSource byteSource = new MgByteSource(bytes , bytes.Length);byteSource.SetMimeType(MgMimeType.Xml);
// Add to repositoryMgResourceIdentifier resId = new MgResourceIdentifier(“Library://Data/Raster/Redding.LayerDefinition“);AcMapResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as AcMapResourceService;rs.SetResource(resId, byteSource .GetReader(), null);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 34
Resource Service Events Occur when resource-related actions, such as resource
addition, modification or removal occur
Event functions are members of AcMapResourceService
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 35
ResourceService Events//Handling the ResourceAdded event //declare delegate objectprivate static ResourceAddedHandler g_eventHandler = null;public bool RegisterEvent(){ AcMapResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as AcMapResourceService; //Create an instance of the delegate g_eventHandler = new ResourceAddedHandler(this.MyEventHandlerFunction); //Associate delegate with the event rs.ResourceAdded += g_eventHandler; }
//Implement handler functionprivate void MyEventHandlerFunction (object sender, AcMapResourceEventArgs args){ MgResourceIdentifier resId = args.GetResourceIdentifier(); MessageBox.Show(resId.ToString() + “ added”);}
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 36
Map Layers A map (AcMapMap) is composed
of one or more layers (AcMapLayer)
Get the resource identifier of a layer’s definition in the repository using GetLayerDefinition()
MgLayerCollection
MgMapBase
AcMapMap
MgLayerBase
AcMapLayer
//Getting the map object AcMapMap map = AcMapMap.GetCurrentMap();
//Getting a layer in the map//MgLayerCollection::GetItem() returns MgLayerBase AcMapLayer layer = (AcMapLayer) map.GetLayers(). GetItem(0); //Getting the layer definition contentMgResourceIdentifier layerResId = layer.GetLayerDefinition();AcMapResourceService rs = (AcMapResourceService)AcMapServiceFactory.GetService(MgServiceType.ResourceService);MgByteReader byteRdr = rs.GetResourceContent(layerResId);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 37
Adding Layers to a Map Add .LAYER file using AcMapMap::LoadLayer() Creating and adding a new layer
Add layer definition to repository Create new layer using resource ID of layer definition Add new layer to map layer collection
//Addding layer definition to repository AcMapResourceService rs = (AcMapResourceService)AcMapServiceFactory.GetService(MgServiceType.ResourceServiceMgByteSource layer_byteSource //Assume byte source contains layer definition data MgResourceIdentifier layerResId = new (“Library://Data/SDF/Newlayer.LayerDefinition”);Rs.SetResource(layerResId, layer_byteSource.GetReader(), null);
//Creating a new layerAcMapLayer layer = AcMapLayer.Create(layerResId,rs); //NEW!Layer.SetName(“Roads”);
//Addding new layer to map layer collectionMgLayerCollection layers = (AcMapLayer) AcMapMap.GetCurrentMap().GetLayers();Layers.Add(layer);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 38
Map Events Triggered when layers or features are added to or
removed from the map
Event functions are members of AcMapMap
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 39
Map Resources
DEMO – Creating Resources Connect to feature sources Create layers
© 2007 Autodesk - "Beyond the Box” Expanding the Developers Toolbox 40
Working with Features
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 41
Feature Service Uses Storage and retrieval of features Information about FDO Providers and their capabilities Information about schemas and feature classes
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 42
Creating the Feature Service Feature service is created from
AcMapServiceFactory object using the GetService method
// Get the feature service AcMapFeatureService featureService = AcMapServiceFactory.GetService(ServiceType.FeatureService) as AcMapFeatureService;
MgFeatureService
AcMapFeatureService
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 43
FDO Registry Get FDO registry and list the providers
with the GetFeatureProviders method// Get the registered feature providersMgByteReader reader = featureService.GetFeatureProviders(); String providers = reader.ToString();
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 44
FDO Provider Capabilities Different FDO providers have
different capabilities. For example
SHP and SDF don’t support topology.
Oracle and SQL Server support database transaction.
Get a provider’s capability programmatically using the GetCapabilities method
// Get the capabilities of a provider MgByteReader reader = featureService.GetCapabilities(fullProviderName);
FDO Provider Capabilities
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 45
Feature Schema (MgFeatureSchema) Defines the structure of data in a
feature source Associated with a feature source
Contains one or more feature classes
// Get the XML representation of a feature schema MgResourceIdentifier id = new MgResourceIdentifier("Library://Data/Raster/Redding.FeatureSource");String schema = featureService.DescribeSchemaAsXml(id, “ReddingSchema");
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 46
Feature Schema
Getting existing schemas
Creating a new schema – use FDO API
// Get the names of all schemas in a feature source MgResourceIdentifier id = new MgResourceIdentifier("Library://Data/Raster/Redding.FeatureSource");MgStringCollection schemaNames = featureService.GetSchemas(id);MgFeatureSchema schema = null;
// Get a particular schemaforeach( String schemaName in schemaNames){ if(schemaName.ToLower() == “reddingschema") { schema = featureService.DescribeSchema(id, schemaName).GetItem(0); break; }}
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 47
Feature Class (MgClassDefinition) Database-table-like structure Feature class contains
properties corresponding to table columns.
Property types: Geometry Data Raster Object
Identity properties are used to uniquely identify a feature in a feature class.
IDGEO
M
OW
NER
VALU
E
SIZE
Geom
etryIdentity
Data
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 48
MgClassDefinition Sets class properties Used to describe or create
feature classes Important methods
SetDefaultGeometryPropertyName() GetProperties()
Returns MgPropertyDefinitionCollection
GetIdentityProperties() Returns
MgPropertyDefinitionCollection
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 49
MgPropertyDefinition
MgPropertyDefinition The details of a feature class property Has 4 subclasses or types
MgDataPropertyDefinition MgGeometricPropertyDefinition MgRasterPropertyDefinition MgObjectPropertyDefinition
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 50
MgGeometricPropertyDefinition Defines a geometric property Important methods
SetHasElevation() SetReadOnly() SetSpatialContextAssociation SetGeometryTypes()
Types of geometric properties MgFeatureGeometricType
Point – 1 Curve – 2 Surface – 4 Solid – 8
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 51
MgDataPropertyDefinition Defines a data property Important methods
SetAutoGeneration() SetDefaultValue() SetLength() SetNullable() SetPrecision() SetReadOnly() SetScale()
Types of data properties MgPropertyType
Double (5), Int32 (7), String (9)...
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 52
Feature Class(MgClassDefinition) Examining a schema
MgResourceIdentifier id = new MgResourceIdentifier("Library://Data/Raster/Redding.FeatureSource");MgClassDefinition classDef; MgStringCollection schemaNames = featureService.GetSchemas(id);
// Get a schema and look at a class definitionforeach( String schemaName in schemaNames){ if(schemaName.ToLower() == “reddingschema") { classDef = featureService.GetClassDefinition(id,schemaName,”className”); // MgPropertyDefinitionCollection allPropDefs = classDef.GetProperties(); foreach(MgPropertyDefinition propDef in allPropDefs) { if(propDef.GetPropertyType() == MgFeaturePropertyType.DataProperty) { MgDataPropertyDefinition somePropDef = (MgDataPropertyDefinition) propDef ;
int dataType = somePropDef.GetDataType() ; } else if(propDef.GetPropertyType() == MgFeaturePropertyType.GeometricProperty) { //We examine the type of geometry stored in the class MgGeometricPropertyDefinition geomPropDef = (MgGeometricPropertyDefinition) propDef ; int geomTypes = geomPropDef.GetGeometryTypes(); //2=curve; 4=surface, etc. } } break; }}
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 53
Feature PropertiesMgPropertyCollection Class Holds the property values used to
create or update features Must contain the values for the non-
nullable properties For example, to update a feature
class with schema like the one shown, the MgPropertyCollection object must contain these properties ID – MgInt32Property GEOM - MgGeometryProperty OWNER - MgStringProperty
ID
GEOM
OWNER
VALUE
SIZE
Properties Nullable
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 54
Feature Geometry Representation Geometry data formats
AGF text format (extension of OGC WKT), represents geometry as a character string “LINESTRING XY (0 0 , 1 -1)”, “POLYGON XY ((1 -3, 4 -3, 4 -6, 1 -6, 1 -3), (2 -4, 3 -4, 3 -5, 2 -5, 2 -4))”
Binary AGF format - MgByteReader Internal representation, using MgGeometry or subclasses
API for getting feature coordinates
Translation MgAgfReaderWriter is used to translate between binary AGF and
MgGeometry MgWktReaderWriter is used to translate between AGF Text and
MgGeometry
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 55
Geometry Format Conversion Use MgAgfReaderWriter to translate between binary AGF and
MgGeometry, and MgWktReaderWriter to translate between AGF Text and MgGeometry
// Binary AGF -> MgGeometryMgAgfReaderWriter agfRW = new MgAgfReaderWriter();// assume byteRdr, an MgByteReader object, contains a (binary) geometryMgGeometry geom = agfRW.Read(byteRdr);
// MgGeometry -> Binary AGFMgAgfReaderWriter agfRW = new MgAgfReaderWriter();// assume geom, an MgGeometry is already definedMgByteReader byteRdr= agfRW.Write(geom);
// AGF Text -> MgGeometryMgWktReaderWriter wktRW = new MgWktReaderWriter();String wktStr = “POINT XY (1 -1)”;MgGeometry geom = wktRW.Read(wktStr);
// MgGeometry -> AGF TextMgWktReaderWriter wktRW = new MgWktReaderWriter();// assume geom, an MgGeometry is already definedString wktStr = wktRW.Write(geom);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 56
Retrieving Features MgFeatureService and AcMapLayer have methods for retrieving
features – use AcMapLayer methods AcMapLayer::SelectFeatures() retrieves features based on
conditions specified through MgFeatureQueryOptions MgFeatureQueryOptions may also contain no conditions
AcMapLayer::GetFeatures() retrieves features given their Ids AcMapLayer::GetLockedFeatures() retrieves locked (i.e.
checked out) features Methods return an MgFeatureReader object
MgFeatureReader implements a forward-only, read-only iterator for reading features
Use ReadNext() to advance iterator – call at least once Use the GetXXX functions to get property values, given the name,
e.g. GetInt32(“ID”) – returns the ID of the feature the iterator is currently pointing to
Use feature property to interact with feature, e.g. highlight it Always close MgFeatureReader object after use
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 57
Retrieving Features+//Get all features on “Roads” layer – no conditions (filtering)MgLayerCollection layers = AcMapMap.GetCurrentMap().GetLayers();//Get layerAcMapLayer rdLayer = (AcMapLayer)layers.GetItem("Roads");//Define (empty) query objectMgFeatureQueryOptions query = new MgFeatureQueryOptions()//Retrieve featuresMgFeatureReader featureReader = rdLayer.SelectFeatures(query);//Get feature propertieswhile (featureReader.ReadNext()) { int streetID = featureReader.GetInt32("ID"); string streetName = featureReader.GetString(“ST_NAME”); double streetLength = featureReader.GetDouble(“LENGTH”);}featureReader.Close();
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 58
FiltersBasic Filter Used to perform queries on
feature attributes Uses SQL-type conditions
Comparison>, <, <>, >=, <=
Like In
May contain expressions functions Avg Sum Count Min, Max …
YEAR > 1990 and Year < 2000NAME like “Richmond%”RVALUE in (500000, 1000000)DATE > ‘1995-3-15’ADDRESS NULL
Filter String Examples
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 59
Filters+Spatial Filter Query based on the relationship of two geometries Two ways to set spatial filters on MgFeatureQueryOptions
SetFilter() – basic filter format, specifying geometry using GEOMFROMTEXT()
SetSpatialFilter() – requires 3 parameters: a name which identifies the geometry property of a feature, a geometry object, and a spatial operation identifier
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 60
Spatial Filter Operations Contains Crosses Disjoint Equals Intersects Overlaps Touches Within Inside
Note: Supported operation is determined by provider’s capabilities
Crosses
Intersects
Touches
Within
Disjoint
Overlaps
Spatial Relationship
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 61
Filter-based Selection// Define a query option and specify it’s criteria using a basic filterMgFeatureQueryOptions query = new MgFeatureQueryOptions();query.SetFilter("Year >= 1990");
// Get the features MgFeatureReader featureReader = layer.SelectFeatures(query);
// SPATIAL FILTER – using SetFilter()//WKT representation of a geometrystring areaWkt = "'POLYGON XY((1874913.0097 430949.2330, 1874913.0097 463071.9745,1898944.6847 ";areaWkt += "463071.9745,1898944.6847 430949.2330, 1874913.0097 430949.2330))'"; //spatial relationship inside a basic filterMgFeatureQueryOptions query = new MgFeatureQueryOptions();query.SetFilter("Geometry inside GEOMFROMTEXT(" + areaWkt + ")");// Get the features MgFeatureReader featureReader = layer.SelectFeatures(query);
// SPATIAL FILTER – using SetSpatialFilterMgGeometry aGeometryObject;query.SetSpatialFilter("SHPGEOM", aGeometryObject, MgFeatureSpatialOperations.Inside);MgFeatureReader featureReader = layer.SelectFeatures(query);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 62
Inserting Features Inserting features is performed using MgInsertFeatures Steps
Create an MgPropertyCollection object, and add the property values of the new feature
Create an MgInsertFeatures object using the MgPropertyCollection object and the name of the feature class, which is to be inserted
Commit insertionMgPropertyCollection props = new MgPropertyCollection();MgAgfReaderWriter agfWriter = new MgAgfReaderWriter();MgGeometry geom; //assuming this is already defined
//Convert MgGeometry to MgByteReaderMgByteReader byteRdr = agfWriter.Write(geom);
props.Add(new MgGeometryProperty("GEOM",byteRdr));props.Add(new MgStringProperty ("LOTDIM", "540X400"));props.Add(new MgInt32Property("SQFT", 6600));props.Add(new MgStringProperty("ZONE", "RES" ));
MgInsertFeatures insertFeatures = new MgInserrtFeatues ("Parcels", properties);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 63
Updating Features Updating features is performed using MgUpdateFeatures Steps
Create an MgPropertyCollection object, which holds the new property values for the features to be updated. Create MgUpdateFeatures using the name of the feature class on which
the updating will be performed, the MgPropertyCollection object and a query string identifying the features to be updated
Commit update MgPropertyCollection props = new MgPropertyCollection();
props.Add(new MgStringProperty("LOTDIM", "540X400"));props.Add(new MgInt32Property("SQFT", 6600));props.Add(new MgStringProperty("ZONE", "RES" ));MgUpdateFeatures updateFeatures = new MgUpdateFeatures ("Parcels", properties, "OWNER LIKE 'JOHN%'");
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 64
Deleting Features
Deleting features is performed using MgDeleteFeatures Create MgDeleteFeatures using the name of the feature class on
which the deletion will be carried out and a query string identifying the features to delete
Commit deletion
MgDeleteFeatures deleteFeatures = new MgDeleteFeatures ("Parcels", "ID=2354");
MgDeleteFeatures deleteFeatures = new MgDeleteFeatures ("Parcels", "GEOM INTERSECTS GEOMFROMTEXT ('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))')");
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 65
Committing Feature Edits Edits of deleting, updating, and inserting are committed to the feature
class by using an MgFeatureCommandCollection object MgDeleteFeatures, MgInsertFeatures and MgUpdateFeatures are
all types of MgFeatureCommand objects added to MgFeatureCommandCollection
Feature commands are executed in the order they are added to the MgFeatureCommandCollection
Command execution is initiated using AcMapLayer::UpdateFeatures()
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();commands.Add(deleteFeatures);//Assuming we are executing further commandscommands.Add(updateFeatures);commands.Add(insertFeatures);//Execute commandsMgLayerCollection layers = AcMapMap.GetCurrentMap().GetLayers();AcMapLayer rdLayer = (AcMapLayer)layers.GetItem("Roads");rdLayer.UpdateFeatures(commands);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 66
Feature Selection Sets Use AutoCAD GetSelection() to select features into an AutoCAD selection set Use AcMapFeatureEntityService::GetSelection() to convert AutoCAD election set to Map selection set (MgSelectionBase)
using Autodesk.Gis.Map.Platform.Interop;Autodesk.AutoCAD.EditorInput.SelectionSet acadSel = null;Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;//Select features in the mapselRes = ed.GetSelection();if (selRes.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK) {
ed.WriteMessage("\nNo features selected"); return;
}//Get the AutoCAD selection setacadSel = selRes.Value;//Convert AutoCAD selection set to Map (Platform) selection setMgSelectionBase _selectionSet = AcMapFeatureEntityService.GetSelection(acadSel);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 67
Feature Selection Sets+Selecting features programmatically Add features Map selection set (MgSelectionBase) Use AcMapFeatureEntityService::HighlightFeatures() to select on screen AcMapMap map = AcMapMap.GetCurrentMap();
MgLayerCollection layers = map.GetLayers(); AcMapLayer rdLayer = (AcMapLayer)layers.GetItem("Roads");
//Create a selection setMgSelectionBase ss = new MgSelectionBase(map);
//Add 2 features (ID = 4736,4709) to selection setss.AddFeatureIdInt32(rdLayer, rdLayer.FeatureClassName, 4736);ss.AddFeatureIdInt32(rdLayer, rdLayer.FeatureClassName, 4709);
//Highlight featuresAcMapFeatureEntityService.HighlightFeatures(ss);
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 68
Feature Service Events Occur when feature-related actions, such as deletion,
insertion or update are initiated, concluded or cancelled
Event functions are members of AcMapFeatureService
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 69
Feature Service Events+//Handling the FeatureInserted event //declare delegate objectprivate static FeatureInsertedHandler g_eventHandler = null;public bool RegisterEvent(){ AcMapFeatureService fs = AcMapServiceFactory.GetService(MgServiceType.FeatureService) as AcMapFeatureService; //Create an instance of the delegate g_eventHandler = new FeatureInsertedHandler(this.MyEventHandlerFunction); //Associate delegate with the event fs.FeatureInserted += g_eventHandler; }
//Implement handler functionprivate void MyEventHandlerFunction (object sender, AcMapFeatureEventArgs args){ MgResourceIdentifier resId = args.GetResourceIdentifier(); AcMapFeature mapFeature = args.GetFeature(); string featureClassName = mapFeature.GetClassDefinition().GetName();}
© 2007 Autodesk - "Beyond the Box” Expanding the Developers Toolbox 70
Q and A
© 2007 Autodesk - AutoCAD Map 3D Geospatial Platform API 71
"Beyond the Box"
Expanding the Developers Toolbox
Gabriel Ajayi