/// <summary>
/// 内存数据集
/// </summary>
public void ExportCAD()
{
//生成临时内存数据集
DatasourceConnectionInfo info = new DatasourceConnectionInfo();
// 设置数据源位置为内存中,类型为UDB
info.Server = ":memory:";
info.EngineType = EngineType.UDB;
info.Alias = "tmp_mem_ds_2DCAD";
Datasource mem_datasource = null;
//1.创建临时内存数据源mem_datasource tmp_mem_ds_2DCAD
if (!this.workspace.Datasources.Contains(info.Alias))
{
mem_datasource = this.workspace.Datasources.Create(info);
}
else
{
mem_datasource = this.workspace.Datasources[info.Alias];
}
//2.创建临时数据集dsvTempCAD CADTmp
string dsName = "CADTmp";
if (mem_datasource.Datasets.Contains(dsName))
{
mem_datasource.Datasets.Delete(dsName);
}
DatasetVectorInfo tempCAD = new DatasetVectorInfo();
tempCAD.Type = DatasetType.CAD;
tempCAD.Name = dsName;
DatasetVector dsvTempCAD = mem_datasource.Datasets.Create(tempCAD);
String outDatasetName = mem_datasource.Datasets.GetAvailableDatasetName(dsName);
foreach (Layer layer in mapControl.Map.Layers)
{
//try
//{
//3.把二维专题图转换为CAD数据集
DatasetVector datasetCAD = layer.ThemeToDatasetVector(mem_datasource, outDatasetName);
//参数不合法
//DatasetVector datasetCAD = layer.ThemeToDatasetVector(mem_datasource, dsName);
if (datasetCAD != null)
{
Recordset recordset = datasetCAD.GetRecordset(false, CursorType.Static);
dsvTempCAD.Append(recordset);
recordset.Dispose();
mem_datasource.Datasets.Delete(outDatasetName);
datasetCAD.Close();
i++;
}
//}
//catch (Exception ex)
//{
// throw new Exception(ex.Message);
//}
}
//将数据集导出到CAD
ExportToDXF(mem_datasource, dsName);
//删除不用的数据集
mem_datasource.Datasets.Delete(dsName);
}
/// <summary>
/// 导出DXF
/// </summary>
/// <param name="datasource"></param>
/// <param name="layername"></param>
public void ExportToDXF(Datasource datasource, string layername)
{
SaveFileDialog m_fileDialog;
try
{
m_fileDialog = new SaveFileDialog();
m_fileDialog.Filter = "dxf files(*.dxf)|*.dxf|dwg files(*.dwg)|*.dwg|All files(*.*)|*.*";
//点了保存按钮进入
if (m_fileDialog.ShowDialog() == DialogResult.OK)
{
m_fileDialog.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
m_fileDialog.RestoreDirectory = true;
string localFilePath = m_fileDialog.FileName.ToString(); //获得文件路径
string aLastName = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1, (localFilePath.Length - localFilePath.LastIndexOf(".") - 1));
if (aLastName == "dxf")
{
DatasetVector datasetVector = (DatasetVector)datasource.Datasets[layername];
// 2.构建DXF导出设置对象,设置导出文件路径,设置要导出的数据集。
ExportSettingDXF exportSettingDXF = new ExportSettingDXF();
exportSettingDXF.TargetFilePath = localFilePath;
exportSettingDXF.SourceData = datasetVector;
exportSettingDXF.TargetFileType = FileType.DXF;
// 3.构建DataExport对象,设置导出设置对象。
DataExport dataExport = new DataExport();
ExportSettings exportSettings = dataExport.ExportSettings;
exportSettings.Add(exportSettingDXF);
// 4.执行导出。
ExportResult result = dataExport.Run();
result.ToString();
}
else if (aLastName == "dwg")
{
DatasetVector datasetVector = (DatasetVector)datasource.Datasets[layername];
// 2.构建DXF导出设置对象,设置导出文件路径,设置要导出的数据集。
ExportSettingDWG exportSettingDWG = new ExportSettingDWG();
exportSettingDWG.TargetFilePath = localFilePath;
exportSettingDWG.SourceData = datasetVector;
exportSettingDWG.TargetFileType = FileType.DWG;
// 3.构建DataExport对象,设置导出设置对象。
DataExport dataExport = new DataExport();
ExportSettings exportSettings = dataExport.ExportSettings;
exportSettings.Add(exportSettingDWG);
// 4.执行导出。
ExportResult result = dataExport.Run();
result.ToString();
}
else
{
DatasetVector datasetVector = (DatasetVector)datasource.Datasets[layername];
// 2.构建DXF导出设置对象,设置导出文件路径,设置要导出的数据集。
ExportSettingDWG exportSettingDWG = new ExportSettingDWG();
exportSettingDWG.TargetFilePath = localFilePath;
exportSettingDWG.SourceData = datasetVector;
exportSettingDWG.TargetFileType = FileType.DWG;
// 3.构建DataExport对象,设置导出设置对象。
DataExport dataExport = new DataExport();
ExportSettings exportSettings = dataExport.ExportSettings;
exportSettings.Add(exportSettingDWG);
// 4.执行导出。
ExportResult result = dataExport.Run();
result.ToString();
}
}
if (i > 0)
{
MessageBox.Show("导出成功!");
}
else
{
MessageBox.Show("导出失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Layer.ThemeToDatasetVector 方法 |
生的单值专题图总是为null。下面这一行代码datasetCAD一直为null。
DatasetVector datasetCAD = layer.ThemeToDatasetVector(mem_ds, outDatasetName);
数据没有问题,因为我生成的自定义专题图是可以正常显示的,然后在SuperMap iDesktop中导出为CAD数据集是成功的,请问这个是什么问题呢?