Adding Feature Classes to a Map

AutoCAD Map 3D Geospatial Platform API

Adding Feature Classes to a Map

To add a feature class to a map, create a layer definition for the new layer and add it to the resource repository. Create an AcMapLayer and set its layer definition. Add the layer to the map using AcMapMap.GetCurrentMap().GetLayers().Add().

Creating the layer definition is similar to creating the feature source definition. See Defining Feature Sources. The layer definition uses the LayerDefinition.xsd schema, which includes styling information. See Layer Definition for details.

The <FeatureName> element in the layer definition is of the form


where schemaName and className are valid for the feature source. Call MgFeatureService.GetSchemas() to get the schema names for a feature source and MgFeatureService.GetClasses() to get the class names for a schema.

The following example creates a layer definition for a raster layer and adds the layer to the current map.

AcMapMap currentMap = AcMapMap.GetCurrentMap();
string layerDefName = "Library://rasterlayer.LayerDefinition";
MgResourceIdentifier layerId = new
// Use classes from xsd.exe to build the layer
LayerDefinitionType layerDef = new LayerDefinitionType();
GridLayerDefinitionType gridLayerDef = new
layerDef.Item = gridLayerDef;
gridLayerDef.ResourceId = rasterId.ToString();
gridLayerDef.FeatureName = "rasters:classname";
gridLayerDef.Geometry = "Image";
GridScaleRangeType[] ranges = new GridScaleRangeType[1];
gridLayerDef.GridScaleRange = ranges;
ranges[0] = new GridScaleRangeType();
ranges[0].ColorStyle = new GridColorStylizationType();
GridColorRuleType[] colorRules = new GridColorRuleType[1];
ranges[0].ColorStyle.ColorRule = colorRules;
colorRules[0] = new GridColorRuleType();
colorRules[0].LegendLabel = "";
colorRules[0].Color = new GridColorType();
colorRules[0].Color.ItemElementName = ItemChoiceType.Band;
colorRules[0].Color.Item = "1";
ranges[0].RebuildFactor = 1;
// Serialize the layer definition to XML
string layerDefString;
using (StringWriter writer = new StringWriter())
  XmlSerializer xs = new XmlSerializer(layerDef.GetType());
  xs.Serialize(writer, layerDef);
  layerDefString = writer.ToString();
// Convert Unicode to UTF-8
unicodeBytes = Encoding.Unicode.GetBytes(layerDefString);
utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8,
// Create a byte reader containing the layer
source definition
xmlSource = new MgByteSource(utf8Bytes, utf8Bytes.Length);
rs.SetResource(layerId, xmlSource.GetReader(), null);
// Add the layer to the Map
MgLayerBase layer = new AcMapLayer(layerId, rs);
layer.Name = "newLayer";

This adds the new layer to the end of the layer collection, at the top of the draw order.

Layers can be organized into groups. To add a layer to a group call AcMapLayer.SetGroup() with the group name.