Creating SDF files

AutoCAD Map 3D Geospatial Platform API

Creating SDF files

Many geospatial operations require creating new layers, adding features to the layers, and adding the layers to the map. The layers can be temporary or permanent. In AutoCAD Map 3D this is handled using SDF files.

An SDF file contains a single schema that can define multiple feature classes.

To create an SDF file, first define the schema and any feature classes used in the schema. For each feature class, define a list of properties. In most cases, the properties should include an identity property and a geometry property, as well as any other properties.

Use the FDO API to create the schema.

The following creates a simple schema with an autogenerated ID property, a string property, and a geometry property.

// Create the feature class definition
FeatureClass classDefinition = 
  new FeatureClass("newClass", "class description");
// Add an autogenerated identity property
DataPropertyDefinition idProp = 
  new DataPropertyDefinition("id", "property desc");
idProp.DataType = DataType.DataType_Int32;
idProp.IsAutoGenerated = true;
// Add a name property
DataPropertyDefinition nameProp = 
  new DataPropertyDefinition("Name", "property desc");
nameProp.DataType = DataType.DataType_String;
// Add a geometry property
GeometricPropertyDefinition geomProp = 
  new GeometricPropertyDefinition("SHPGEOM", "property desc");
geomProp.GeometryTypes = MgGeometryType.Polygon;
classDefinition.GeometryProperty = geomProp;
// Create the schema and add the class definition
FeatureSchema schema = 
  new FeatureSchema("SHP_Schema", "Schema description");

Once the schema has been created, use the FDO API to create the SDF file, set the spatial context, and apply the schema. The following is modified from the utility classes in the sample applications.

// Set connection properties
IConnectionPropertyDictionary connProperties = 
connProperties.SetProperty("File", sdfPath);
connProperties.SetProperty("ReadOnly", false.ToString());
// Create data store, ie. SDF file
ICreateDataStore createDSCmd = 
  as ICreateDataStore;
createDSCmd.DataStoreProperties.SetProperty("File", sdfPath);
  // Open the connection
  ConnectionState connState = conn.Open();
  int retryRound = 0;
  while (++retryRound < 5 // Try 5 times
    && (connState == ConnectionState.ConnectionState_Pending
    || connState == ConnectionState.ConnectionState_Busy))
    connState = conn.Open();
  if (retryRound >= 5)
    throw new 
    InvalidOperationException("Failed to connect to file " 
    + sdfPath);
  // Create spatial context, including coordinate
  AcMapMap currentMap = AcMapMap.GetCurrentMap();
  string mapSRS = currentMap.GetMapSRS();
  ICreateSpatialContext createSCCmd = 
    as ICreateSpatialContext;
  createSCCmd.CoordinateSystemWkt = mapSRS;
  createSCCmd.Name = "";
  createSCCmd.CoordinateSystem = mapSRS;
  createSCCmd.Extent = new byte[] { 0, 0, 0, 0 };
  createSCCmd.Description = "Description";
  createSCCmd.XYTolerance = 0.0;
  createSCCmd.ZTolerance = 0.0;
  // Apply schema
  if (schema.ElementState != 
    IApplySchema applySchemaCmd = 
      as IApplySchema;
    applySchemaCmd.FeatureSchema = schema;
catch (System.Exception e)
  throw e; // Or add exception handling
  // Close connection
  if (conn != null)