public static bool ExportLayer2Dwg(IList <IFeatureLayer> pInPutLayerList, string outputfile) { Geoprocessor GP = new Geoprocessor(); GP.OverwriteOutput = true;//覆盖同名 GP.SetEnvironmentValue("workspace", @"C:\temp"); StringBuilder builder = new StringBuilder(); for (int i = 0; i < pInPutLayerList.Count; i++) { IFeatureLayer layer = pInPutLayerList[i]; IDataset dataset = layer.FeatureClass as IDataset; string layerfullname = dataset.Workspace.PathName + "\\" + dataset.Name; builder.Append(layerfullname + ";"); } string fullfilepath = builder.ToString().Substring(0, builder.Length - 1); ExportCAD exportcad = new ExportCAD(); exportcad.in_features = fullfilepath; exportcad.Output_Type = "DWG_R2010"; exportcad.Output_File = outputfile; try { IGeoProcessorResult results = (IGeoProcessorResult)GP.Execute(exportcad, null); string msg = ""; if (GP.MessageCount > 0) { for (int i = 0; i < GP.MessageCount; i++) { msg += GP.GetMessage(i) + "\n"; } } if (msg.Contains("Successed")) { return(true); } else { return(false); } } catch (Exception ex) { return(false); } }
public void ShpToCAD(IFeatureClass fteclss, string lsshp, string cadpath, string FormatType) { //先转为shp文件 try { FclssToShp(fteclss, lsshp); //shp转为要素 Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; ExportCAD export = new ExportCAD(); export.in_features = lsshp; export.Output_Type = FormatType; export.Output_File = cadpath; export.Ignore_FileNames = "1"; export.Append_To_Existing = "1"; IGeoProcessorResult result = gp.Execute(export, null) as IGeoProcessorResult; //转换成功是否将CAD文件添加进图层 if (result.Status == esriJobStatus.esriJobSucceeded) { if (MessageBox.Show("转换成功,是否添加至图层", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { string filepath = System.IO.Path.GetDirectoryName(cadpath); string filename = System.IO.Path.GetFileName(cadpath); CadWorkspaceFactory cadwspFcy = new CadWorkspaceFactoryClass(); IFeatureWorkspace fteWsp = cadwspFcy.OpenFromFile(filepath, 0) as IFeatureWorkspace; //转换为CAD工作空间 ICadDrawingWorkspace CadWsp = fteWsp as ICadDrawingWorkspace; ICadDrawingDataset CadDataset = CadWsp.OpenCadDrawingDataset(filename); ICadLayer cadLyr = new CadLayerClass(); cadLyr.CadDrawingDataset = CadDataset; cadLyr.Name = filename; this.Mapcontrol.AddLayer(cadLyr, 0); this.Mapcontrol.Refresh(); } } shpDel(lsshp); } catch (Exception ex) { MessageBox.Show("转换失败" + ex.Message); } }
private void btnOK_Click(object sender, EventArgs e) { Geoprocessor GP = new Geoprocessor(); ExportCAD GPExportCAD = new ExportCAD(); //设置in_features属性 if (this.cklstCurLayers.CheckedItems.Count == 0) { return; } progressBar1.Maximum = cklstCurLayers.CheckedItems.Count; string filePath = null; //for (int i = 0; i < this.cklstCurLayers.Items.Count; i++) //{ // if (this.cklstCurLayers.GetItemChecked(i)) // { // filePath = filePath + m_dicPathAliasName[i] + ";"; // } // //filePath = filePath + this.cklstCurLayers.CheckedItems[i].ToString() + ";"; //} //filePath = filePath.TrimEnd(Convert.ToChar(";")); //GPExportCAD.in_features = filePath; //设置Output_Type属性 GPExportCAD.Output_Type = this.cbOutputType.SelectedItem.ToString(); //设置Output_File属性 if (this.tbOutputFile.Text == null || this.tbOutputFile.Text == "") { MessageBox.Show(@"请选择输出路径。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (System.IO.File.Exists(this.tbOutputFile.Text)) { DialogResult dr = MessageBox.Show("当前文件已存在" + Environment.NewLine + "点击“是”将覆盖现有文件" + Environment.NewLine + "点击“否”将追加到现有文件" + Environment.NewLine + "点击“取消”退出本次操作!", "", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Yes) { System.IO.File.Delete(this.tbOutputFile.Text); } else if (dr == DialogResult.No) { } else { return; } } GPExportCAD.Output_File = this.tbOutputFile.Text.Trim(); ///设置Ignore_FileNames属性 ///IGNORE_FILENAMES_IN_TABLES:忽略文档实体字段中的路径, ///并允许将所有实体输出到单个 CAD 文件。 ///USE_FILENAMES_IN_TABLES";//允许使用文档实体字段中的路径, ///并使用每个实体的路径,以使每个 CAD 部分写入到各自的文件。这是默认设置。 //if (this.cbIgnoreFileNames.Checked) GPExportCAD.Ignore_FileNames = "IGNORE_FILENAMES_IN_TABLES"; //else // GPExportCAD.Ignore_FileNames = "USE_FILENAMES_IN_TABLES"; ///设置Append_To_Existing属性 ///APPEND_TO_EXISTING_FILES:允许将输出文件内容添加到现有 CAD 输出文件中。 ///现有 CAD 文件内容不会丢失。 ///OVERWRITE_EXISTING_FILES";输出文件内容将覆盖现有 CAD 文件内容。这是默认设置。 //if (this.cbIgnoreFileNames.Checked) GPExportCAD.Append_To_Existing = "APPEND_TO_EXISTING_FILES"; //else // GPExportCAD.Append_To_Existing = "OVERWRITE_EXISTING_FILES"; try { for (int i = 0; i < this.cklstCurLayers.Items.Count; i++) { if (this.cklstCurLayers.GetItemChecked(i)) { filePath = m_dicPathAliasName[i]; GPExportCAD.in_features = filePath; GP.Execute(GPExportCAD, null); progressBar1.Value++; } } //GP.Execute(GPExportCAD, null); MessageBox.Show(@"转换完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//实现了从sde中导出数据到dwg public static void ExportLayertoDwg(IList <IFeatureLayer> pInPutLayerList, string outputfile, ref List <IFeatureLayer> successedList, ref List <IFeatureLayer> failedList) { try { //string connectionfile = ConfigurationManager.AppSettings["ConnectionSDE"];//读取配置文件中sde的位置,如Connection to sde.sde string connectionfile = ""; string _RemoteLinkDBStr = System.Windows.Forms.Application.StartupPath + "\\" + connectionfile; string workPath = string.Empty; //数据库连接路经 string layerfullname = string.Empty; //数据源 for (int i = 0; i < pInPutLayerList.Count; i++) { IFeatureLayer layer = pInPutLayerList[i]; IDataset dataset = layer.FeatureClass as IDataset; if (dataset.Workspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) { if (i == 0) { workPath = _RemoteLinkDBStr; layerfullname = System.IO.Path.Combine(_RemoteLinkDBStr, dataset.Name); } else { layerfullname += string.Format(";" + dataset.Name); } } else { if (workPath == string.Empty) { workPath = dataset.Workspace.PathName;//未设置本地... } } } Geoprocessor GP = new Geoprocessor(); GP.OverwriteOutput = true;//覆盖同名 GP.SetEnvironmentValue("workspace", workPath); //获取数据源全路径 ExportCAD exportcad = new ExportCAD(); exportcad.in_features = layerfullname; exportcad.Output_Type = "DWG_R2010"; exportcad.Output_File = outputfile; IGeoProcessorResult results = (IGeoProcessorResult)GP.Execute(exportcad, null); string msg = ""; if (GP.MessageCount > 0) { for (int j = 0; j < GP.MessageCount; j++) { msg += GP.GetMessage(j) + "\n"; } } if (msg.Contains("Successed") || msg.Contains("成功")) { string message = msg; } } catch (Exception ex) { throw ex; } }
private void btnOK_Click(object sender, EventArgs e) { Geoprocessor GP = new Geoprocessor(); ExportCAD GPExportCAD = new ExportCAD(); //设置in_features属性 if (this.cklstCurLayers.CheckedItems.Count == 0) return; progressBar1.Maximum = cklstCurLayers.CheckedItems.Count; string filePath = null; //for (int i = 0; i < this.cklstCurLayers.Items.Count; i++) //{ // if (this.cklstCurLayers.GetItemChecked(i)) // { // filePath = filePath + m_dicPathAliasName[i] + ";"; // } // //filePath = filePath + this.cklstCurLayers.CheckedItems[i].ToString() + ";"; //} //filePath = filePath.TrimEnd(Convert.ToChar(";")); //GPExportCAD.in_features = filePath; //设置Output_Type属性 GPExportCAD.Output_Type = this.cbOutputType.SelectedItem.ToString(); //设置Output_File属性 if (this.tbOutputFile.Text == null || this.tbOutputFile.Text == "") { MessageBox.Show(@"请选择输出路径。", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning); return; } if (System.IO.File.Exists(this.tbOutputFile.Text)) { DialogResult dr = MessageBox.Show("当前文件已存在" + Environment.NewLine + "点击“是”将覆盖现有文件" + Environment.NewLine + "点击“否”将追加到现有文件" + Environment.NewLine + "点击“取消”退出本次操作!", "", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Yes) { System.IO.File.Delete(this.tbOutputFile.Text); } else if (dr == DialogResult.No) { } else return; } GPExportCAD.Output_File = this.tbOutputFile.Text.Trim(); ///设置Ignore_FileNames属性 ///IGNORE_FILENAMES_IN_TABLES:忽略文档实体字段中的路径, ///并允许将所有实体输出到单个 CAD 文件。 ///USE_FILENAMES_IN_TABLES";//允许使用文档实体字段中的路径, ///并使用每个实体的路径,以使每个 CAD 部分写入到各自的文件。这是默认设置。 //if (this.cbIgnoreFileNames.Checked) GPExportCAD.Ignore_FileNames = "IGNORE_FILENAMES_IN_TABLES"; //else // GPExportCAD.Ignore_FileNames = "USE_FILENAMES_IN_TABLES"; ///设置Append_To_Existing属性 ///APPEND_TO_EXISTING_FILES:允许将输出文件内容添加到现有 CAD 输出文件中。 ///现有 CAD 文件内容不会丢失。 ///OVERWRITE_EXISTING_FILES";输出文件内容将覆盖现有 CAD 文件内容。这是默认设置。 //if (this.cbIgnoreFileNames.Checked) GPExportCAD.Append_To_Existing = "APPEND_TO_EXISTING_FILES"; //else // GPExportCAD.Append_To_Existing = "OVERWRITE_EXISTING_FILES"; try { for (int i = 0; i < this.cklstCurLayers.Items.Count; i++) { if (this.cklstCurLayers.GetItemChecked(i)) { filePath = m_dicPathAliasName[i]; GPExportCAD.in_features = filePath; GP.Execute(GPExportCAD, null); progressBar1.Value++; } } //GP.Execute(GPExportCAD, null); MessageBox.Show(@"转换完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message); } }