用户需要把多个专题图导出为CAD数据集并合并为一个数据集,用户代码如下,导出不成功,请问以下代码需要怎么修改?
public void export2DMap2Cad()
{
//生成临时内存数据集
DatasourceConnectionInfo info = new DatasourceConnectionInfo();
// 设置数据源位置为内存中,类型为UDB
info.Server = ":memory:";
info.EngineType = EngineType.UDB;
info.Alias = "tmp_mem_ds_2DCAD";
string dsName = "CADTmp";
Datasource mem_ds = null;
if (!this.workspace.Datasources.Contains(info.Alias))
mem_ds = this.workspace.Datasources.Create(info);
else
{
//mem_ds = parameter.workspace.Datasources.Open(info);
mem_ds = this.workspace.Datasources[info.Alias];
}
if (mem_ds.Datasets.Contains(dsName))
{
mem_ds.Datasets.Delete(dsName);
}
DatasetVectorInfo tmpCad = new DatasetVectorInfo();
tmpCad.Type = DatasetType.CAD;
tmpCad.Name = dsName;
DatasetVector dataset = mem_ds.Datasets.Create(tmpCad);
String outDatasetName = mem_ds.Datasets.GetAvailableDatasetName(dsName);
//遍历Layer合并到数据集
foreach (Layer layer in mapControl.Map.Layers)
{
try
{
layer.ThemeToDatasetVector(mem_ds, outDatasetName);
}
catch (Exception e)
{
}
}
//将数据集导出到CAD
this.ExportToDXF(mem_ds, dsName);
//删除不用的数据集
mem_ds.Datasets.Delete(dsName);
}
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));
// Datasource datasource = parameter.workspace.Datasources[0];
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();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}