The feature source and styling for a layer are set using a LayerDefinition, stored in the resource repository. The layer definition conforms to the schema. Raster layers and vector layers have different styling requirements and capabilities.
For both raster and vector data, a single layer definition can contain multiple scale ranges, which define the styling for particular view scales. For example, a vector layer could have the following scale ranges:
Features in the layer could be styled differently or be hidden completely for the different scale ranges.
For a a vector layer, each scale range can contain styling information for the following:
The layer definition can be any valid XML that conforms to the schema. To help generate the XML, the AutoCAD Map 3D samples use classes generated from LayerDefinition.xsd by xsd.exe.
For example, to create a vector layer definition that contains 2 scale ranges, do the following:
The example code that follows uses the following namespace declarations:
The following assumes that the feature source contains only polygons. Feature sources containing line or point data would require styling for those feature types as well.
// Create the first scale range VectorScaleRangeType range = new VectorScaleRangeType(); AreaRule areaRule = new AreaRule(); AreaSymbolizationFillType areaSymFillType = new AreaSymbolizationFillType(); FillType fillType = new FillType(); fillType.FillPattern = "Solid"; fillType.ForegroundColor = "9900ffaa"; fillType.BackgroundColor = "FF000000"; StrokeType strokeType = new StrokeType(); strokeType.LineStyle = "Solid"; strokeType.Thickness = "0.0"; strokeType.Color = "FF000000"; strokeType.Unit = LengthUnitType.Centimeters; areaSymFillType.Fill = fillType; areaSymFillType.Stroke = strokeType; areaRule.Item = areaSymFillType; areaRule.LegendLabel = ""; object[] items = new object[1]; items[0] = new AreaRule[] { areaRule }; range.Items = items; range.MinScale = 0.0; range.MaxScale = 10000000.0; range.MinScaleSpecified = true; range.MaxScaleSpecified = true; // Second scale range VectorScaleRangeType range2 = new VectorScaleRangeType(); AreaRule areaRule2 = new AreaRule(); AreaSymbolizationFillType areaSymFillType2 = new AreaSymbolizationFillType(); FillType fillType2 = new FillType(); fillType2.FillPattern = "Solid"; fillType2.ForegroundColor = "ffffff00"; fillType2.BackgroundColor = "FF000000"; StrokeType strokeType2 = new StrokeType(); strokeType2.LineStyle = "Solid"; strokeType2.Thickness = "0.0"; strokeType2.Color = "FF000000"; strokeType2.Unit = LengthUnitType.Centimeters; areaSymFillType2.Fill = fillType2; areaSymFillType2.Stroke = strokeType2; areaRule2.Item = areaSymFillType2; areaRule2.LegendLabel = ""; object[] items2 = new object[1]; items2[0] = new AreaRule[] { areaRule2 }; range2.Items = items2; range2.MinScale = 10000000.0; range2.MaxScale = Double.MaxValue; range2.MinScaleSpecified = true; range2.MaxScaleSpecified = true; // Now create a vector layer definition and add the scale ranges. VectorLayerDefinitionType vectorLayerDef = new VectorLayerDefinitionType(); vectorLayerDef.VectorScaleRange = new VectorScaleRangeType[2]; vectorLayerDef.VectorScaleRange.SetValue(range, 0); vectorLayerDef.VectorScaleRange.SetValue(range2, 1); // Set the resource id to point to the feature source for // the layer. Set the feature name to a feature class in // the feature source. Set the geometry property to the // default geometry property for the feature class. vectorLayerDef.ResourceId = "Library://Data/SAMPLE.FeatureSource"; vectorLayerDef.FeatureName = "Schema1:FeatureClass1"; vectorLayerDef.Geometry = "Geometry"; // Create a layer definition containing the vector layer // definition. LayerDefinition layerDef = new LayerDefinition(); layerDef.Item = vectorLayerDef; layerDef.version = "1.0.0"; // Convert to an XML string. using (StringWriter writer = new StringWriter()) { XmlSerializer xs = new XmlSerializer(typeof(LayerDefinition)); xs.Serialize(writer, layerDef); // Convert to UTF-8. byte[] unicodeBytes = Encoding.Unicode.GetBytes(writer.ToString()); byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes); MgByteSource source = new MgByteSource(utf8Bytes, utf8Bytes.Length); // Update the resource in Map. MgResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as MgResourceService; // Store the layer definition in the repository. MgResourceIdentifier newLayerDefId = new MgResourceIdentifier( "Library://myNewLayer.LayerDefinition"); rs.SetResource(newLayerDefId, source.GetReader(), null); // Create a new layer that references the layer definition. // Add this to the layers in the current map. AcMapMap currentMap = AcMapMap.GetCurrentMap(); MgLayerCollection layers = currentMap.GetLayers(); AcMapLayer layer; layer = new AcMapLayer(newLayerDefId, rs); layer.Name = "testlayer"; layers.Add(layer); }
This creates the following XML:
<?xml version="1.0" encoding="utf-16"?> <LayerDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0.0"> <VectorLayerDefinition> <ResourceId>Library://Data/SAMPLE.FeatureSource</ResourceId> <FeatureName>Schema1:FeatureClass1</FeatureName> <FeatureNameType>FeatureClass</FeatureNameType> <Geometry>Geometry</Geometry> <VectorScaleRange> <MinScale>0</MinScale> <MaxScale>10000000</MaxScale> <AreaTypeStyle> <AreaRule> <LegendLabel /> <AreaSymbolization2D> <Fill> <FillPattern>Solid</FillPattern> <ForegroundColor>9900ffaa</ForegroundColor> <BackgroundColor>FF000000</BackgroundColor> </Fill> <Stroke> <LineStyle>Solid</LineStyle> <Thickness>0.0</Thickness> <Color>FF000000</Color> <Unit>Centimeters</Unit> </Stroke> </AreaSymbolization2D> </AreaRule> </AreaTypeStyle> </VectorScaleRange> <VectorScaleRange> <MinScale>10000000</MinScale> <MaxScale>1.7976931348623157E+308</MaxScale> <AreaTypeStyle> <AreaRule> <LegendLabel /> <AreaSymbolization2D> <Fill> <FillPattern>Solid</FillPattern> <ForegroundColor>ffffff00</ForegroundColor> <BackgroundColor>FF000000</BackgroundColor> </Fill> <Stroke> <LineStyle>Solid</LineStyle> <Thickness>0.0</Thickness> <Color>FF000000</Color> <Unit>Centimeters</Unit> </Stroke> </AreaSymbolization2D> </AreaRule> </AreaTypeStyle> </VectorScaleRange> </VectorLayerDefinition> </LayerDefinition>