/// <summary> /// 以矢量的方式加载CAD数据 /// </summary> /// <param name="mapControl">要加载的地图控件</param> public void AddCADByShp(AxMapControl mapControl) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "CAD(*.dwg)|*.dwg"; openFileDialog.Title = "打开CAD数据文件"; openFileDialog.ShowDialog(); string fullPath = openFileDialog.FileName; if (fullPath == "") { return; } //获取文件名和文件路径 int SubIndex = fullPath.LastIndexOf("\\"); string fileDir = fullPath.Substring(0, SubIndex); string fileName = fullPath.Substring(SubIndex + 1); IWorkspaceFactory workspaceFactory = new CadWorkspaceFactory(); IFeatureWorkspace workspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(fileDir, 0); IFeatureClass feature = workspace.OpenFeatureClass(fileName + ":polygon"); IFeatureLayer featureLayer = new FeatureLayer(); featureLayer.FeatureClass = feature; featureLayer.Name = fileName; mapControl.AddLayer(featureLayer); mapControl.ActiveView.Refresh(); }
/// <summary> /// 作用:打开CAD文件Workspace /// 作者:汪建龙 /// 编写时间:2016年11月22日12:54:02 /// </summary> /// <param name="directory"></param> /// <returns></returns> public static IWorkspace OpenCADWorkSpace(string directory) { IWorkspaceFactory workspaceFactory = new CadWorkspaceFactory(); IWorkspace workspace = workspaceFactory.OpenFromFile(directory, 0); return(workspace); }
/// <summary> /// CAD文件作为矢量图层加载 /// 分图层加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddCADByLayerToolStripMenuItem_Click(object sender, EventArgs e) { string filePath; string fileName; string filter = "CAD(*.dwg)|*.dwg"; string title = "打开CAD数据文件"; outFilePathAndFileName(title, filter, out filePath, out fileName); if (fileName == null || filePath == null) { return; } IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace; // 加载CAD文件中的线文件 IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName + ":polyline"); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.Name = fileName; pFeatureLayer.FeatureClass = pFeatureClass; // 新增删除数据 ClearAllData(); MainMapControl.AddLayer(pFeatureLayer); MainMapControl.Refresh(); //同步鹰眼 SynchronizeEagleEye(); }
/// <summary> /// 以栅格背景图的形式添加CAD数据 /// </summary> /// <param name="mapControl">要添加CAD数据的地图控件</param> public void AddCADByRaster(AxMapControl mapControl) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "CAD(*.dwg)|*.dwg"; openFileDialog.Title = "打开CAD数据文件"; openFileDialog.ShowDialog(); string fullPath = openFileDialog.FileName; if (fullPath == "") { return; } //获取文件名和文件路径 int subIndex = fullPath.LastIndexOf("\\"); string fileDir = fullPath.Substring(0, subIndex); string fileName = fullPath.Substring(subIndex + 1); IWorkspaceFactory CadWorkspaceFactory = new CadWorkspaceFactory(); ICadDrawingWorkspace CadWorkspace = (ICadDrawingWorkspace)CadWorkspaceFactory.OpenFromFile(fileDir, 0); ICadDrawingDataset cadDrawingDataset = CadWorkspace.OpenCadDrawingDataset(fileName); ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDrawingDataset; mapControl.AddLayer(cadLayer); mapControl.ActiveView.Refresh(); }
private void method1ToolStripMenuItem_Click(object sender, EventArgs e) { System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); openFileDialog.Filter = "CAD (*.dwg)|*.dwg"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filename = openFileDialog.FileName; if (filename != "") { //axMapControl1.ClearLayers(); IWorkspaceFactory pCadWorkspaceFactory = new CadWorkspaceFactory(); IWorkspace pWorkspace = pCadWorkspaceFactory.OpenFromFile( System.IO.Path.GetDirectoryName(filename), 0); ICadDrawingWorkspace pCadDrawingWorkspace = (ICadDrawingWorkspace)pWorkspace; ICadDrawingDataset pCadDataset = pCadDrawingWorkspace.OpenCadDrawingDataset( System.IO.Path.GetFileName(filename)); ICadLayer pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDataset; axMapControl1.AddLayer(pCadLayer, 0); } } }
// 测试导入道路 public static Form TestCase1() { var factory2 = new CadWorkspaceFactory(); try { var path = System.IO.Path.GetDirectoryName(@"C:\Temp\Traffic\temp.dxf"); var ws2 = factory2.OpenFromFile(path, 0) as IFeatureWorkspace; var fc2 = ws2.OpenFeatureClass(System.IO.Path.GetFileName(@"C:\Temp\Traffic\temp.dxf") + ":polyline"); var cursor = fc2.Search(null, true); var f = cursor.NextFeature(); var lst = new List<IPolyline>(); while (f != null) { var geo = f.ShapeCopy; if (!(geo is IPolyline) || geo.IsEmpty == true) { MessageBox.Show("当前CAD文件中包含的路线类型信息不正确,请检查。", "注意"); Marshal.ReleaseComObject(cursor); return null; } else { lst.Add(geo as IPolyline); } f = cursor.NextFeature(); } Marshal.ReleaseComObject(cursor); if (lst.Count == 0) { MessageBox.Show("当前CAD文件中不包含路线信息,请检查。", "注意"); return null; } RoadMerger.FragmentThreshold = 20; var ls = RoadMerger.SplitLine(lst); var factory = new AccessWorkspaceFactory(); var ws = factory.OpenFromFile(@"C:\Temp\Traffic\db.mdb", 0) as IFeatureWorkspace; var fc = ws.OpenFeatureClass("AROAD"); var historyFC = ws.OpenFeatureClass("ROADHIS"); var nodeFC = ws.OpenFeatureClass("NODE"); var lines = RoadMerger.QueryIntersects(ls, fc); var form = new ImportRoadForm(ls, fc, historyFC, nodeFC, lines); return form; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "注意"); return null; } }
/// <summary> /// CAD文件作为矢量图层加载 /// 整幅图加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddWhileCADToolStripMenuItem_Click(object sender, EventArgs e) { string filePath; string fileName; string filter = "CAD(*.dwg)|*.dwg"; string title = "打开CAD数据文件"; outFilePathAndFileName(title, filter, out filePath, out fileName); if (fileName == null || filePath == null) { return; } // 打开一个CAD数据集 IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace; // 打开一个要素集 IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName); // IFeatureClassContainer 可以管理IFeatureDataset中的每个要素类 IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer; // 新增删除数据 ClearAllData(); // 对CAD文件中的要素进行遍历处理 for (int i = 0; i < pFeatureClassContainer.ClassCount; i++) { IFeatureClass pFeatClass = pFeatureClassContainer.get_Class(i); // 如果是注记,则添加注记层 if (pFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation) { IFeatureLayer pFeatureLayer = new CadAnnotationLayerClass(); pFeatureLayer.Name = pFeatClass.AliasName; pFeatureLayer.FeatureClass = pFeatClass; MainMapControl.AddLayer(pFeatureLayer); } // 如果是点线面则添加要素层 else { IFeatureLayer pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.Name = pFeatClass.AliasName; pFeatureLayer.FeatureClass = pFeatClass; MainMapControl.AddLayer(pFeatureLayer); } MainMapControl.Refresh(); } // 同步鹰眼 SynchronizeEagleEye(); }
/// <summary> /// 整体添加CAD数据 /// </summary> /// <param name="mapControl">要添加的地图控件</param> public void AddWholeCAD(AxMapControl mapControl) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "CAD(*.dwg)|*.dwg"; openFileDialog.Title = "打开CAD数据文件"; openFileDialog.ShowDialog(); string fullPath = openFileDialog.FileName; if (fullPath == "") { return; } //获取文件名和文件路径 int SubIndex = fullPath.LastIndexOf("\\"); string fileDir = fullPath.Substring(0, SubIndex); string fileName = fullPath.Substring(SubIndex + 1); IWorkspaceFactory workspaceFactory = new CadWorkspaceFactory(); IFeatureWorkspace workspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(fileDir, 0); IFeatureDataset FeatureDataset = workspace.OpenFeatureDataset(fileName); IFeatureClassContainer featureClassContainer = FeatureDataset as IFeatureClassContainer; IFeatureClass featureClass; IFeatureLayer featureLayer; for (int i = 0; i < featureClassContainer.ClassCount; i++) { featureClass = featureClassContainer.get_Class(i); if (featureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation) { featureLayer = new CadAnnotationLayerClass(); featureLayer.FeatureClass = featureClass; featureLayer.Name = featureClass.AliasName; mapControl.AddLayer(featureLayer); } else { featureLayer = new FeatureLayerClass(); featureLayer.FeatureClass = featureClass; featureLayer.Name = featureClass.AliasName; mapControl.AddLayer(featureLayer); } mapControl.ActiveView.Refresh(); } }
private void method2ToolStripMenuItem_Click(object sender, EventArgs e) { System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); openFileDialog.Filter = "CAD (*.dwg)|*.dwg"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filename = openFileDialog.FileName; if (filename != "") { //axMapControl1.ClearLayers(); IWorkspaceFactory pCadWorkspaceFactory = new CadWorkspaceFactory(); IFeatureWorkspace pWorkspace = pCadWorkspaceFactory.OpenFromFile( System.IO.Path.GetDirectoryName(filename), 0) as IFeatureWorkspace; IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset( System.IO.Path.GetFileName(filename)); IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer; IFeatureClass pFeatureClass; IFeatureLayer pFeatureLayer; for (int i = 0; i < pFeatureClassContainer.ClassCount; i++) { pFeatureClass = pFeatureClassContainer.get_Class(i); if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation) { pFeatureLayer = new CadAnnotationLayerClass(); } else { pFeatureLayer = new FeatureLayerClass(); } pFeatureLayer.Name = pFeatureClass.AliasName; pFeatureLayer.FeatureClass = pFeatureClass; axMapControl1.AddLayer(pFeatureLayer, 0); } } } }
/// <summary> /// 获取CAD数据工作空间 /// </summary> /// <param name="cadPath"></param> /// <returns></returns> public static IWorkspace GetCADWorkspcae(string cadPath) { if (string.IsNullOrWhiteSpace(cadPath) || !File.Exists(cadPath)) { throw new Exception(string.Format("CAD文件不存在,文件路径:“{0}”", cadPath)); } else { try { IWorkspaceFactory pWorkspaceFac = new CadWorkspaceFactory(); IWorkspace workspace = pWorkspaceFac.OpenFromFile(cadPath, 0); return(workspace); } catch (Exception ex) { throw new Exception(string.Format("读取CAD文件失败,文件路径:“{0}”", cadPath), ex); } } }
/// <summary> /// CAD文件作为栅格图层加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddRasterByCADToolStripMenuItem_Click(object sender, EventArgs e) { string filePath; string fileName; string filter = "CAD(*.dwg)|*.dwg"; string title = "打开CAD数据文件"; outFilePathAndFileName(title, filter, out filePath, out fileName); if (fileName == null || filePath == null) { return; } IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactory(); IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0); ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace; // 获取CAD数据集 ICadDrawingDataset pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(fileName); ICadLayer pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDrawingDataset; MainMapControl.AddLayer(pCadLayer); MainMapControl.Refresh(); }
// // 添加CAD,dxf,dgn图层 public static bool AddCADLayer(ref IBasicMap pBasicMap, string vPath, List <IFeatureLayer> pSelectedCln, bool blnAddData) { bool functionReturnValue = false; //WHFErrorHandle.clsErrorHandle pdisplayinfo = new WHFErrorHandle.clsErrorHandle(); IWorkspaceFactory pFact = default(IWorkspaceFactory); IWorkspace pWorkSpace = default(IWorkspace); IFeatureWorkspace pFeatureWorkspace = default(IFeatureWorkspace); IFeatureLayer pFtLyr = default(IFeatureLayer); IFeatureClass pFtCls = default(IFeatureClass); string pFile = null; pFact = new CadWorkspaceFactory(); pWorkSpace = pFact.OpenFromFile(vPath, 0); pFeatureWorkspace = (IFeatureWorkspace)pWorkSpace; int i = 0; int j = 0; IFeatureDataset pFtDataset = default(IFeatureDataset); for (j = 1; j <= pSelectedCln.Count; j++) { pFile = (string)pSelectedCln[j].Name; pFtDataset = pFeatureWorkspace.OpenFeatureDataset(pFile); IFeatureClassContainer pFtClsContainer = default(IFeatureClassContainer); pFtClsContainer = (IFeatureClassContainer)pFtDataset; //检查当前目录是否是cad工作空间 if (pFtClsContainer == null | pFtClsContainer.ClassCount == 0) { //pdisplayinfo.DisplayInformation("CAD文件:" + pFile + "不存在数据层", false); MessageBoxEx.Show("CAD文件:" + pFile + "不存在数据层", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); functionReturnValue = false; return(functionReturnValue); } for (i = 0; i <= pFtClsContainer.ClassCount - 1; i++) { pFtCls = pFtClsContainer.get_Class(i); if (pFtCls.FeatureType == esriFeatureType.esriFTSimple) { pFtLyr = new CadFeatureLayerClass(); pFtLyr.FeatureClass = pFtCls; pFtLyr.Name = pFile + ":" + pFtCls.AliasName; pSelectedCln.Add(pFtLyr); functionReturnValue = true; } else if (pFtCls.FeatureType == esriFeatureType.esriFTAnnotation) { pFtLyr = new CadAnnotationLayerClass(); pFtLyr.FeatureClass = pFtCls; pFtLyr.Name = pFile + " " + pFtCls.AliasName; pSelectedCln.Add(pFtLyr); functionReturnValue = true; } } } return(functionReturnValue); }
/// <summary> /// 导入CAD文件 /// </summary> public void Import() { OpenFileDialog openDwg = new OpenFileDialog(); openDwg.Filter = "CAD file(*.DWG)|*.DWG|CAD DGN file(*.DGN)|*.DGN|CAD DXF file(*.DXF)|*.DXF"; openDwg.Title = "DWG格式数据的输入"; if (openDwg.ShowDialog() == DialogResult.OK) { string dwgfileName = openDwg.FileName; string fileName = dwgfileName.Substring(dwgfileName.LastIndexOf(@"\") + 1); string filePath = dwgfileName.Substring(0, dwgfileName.Length - fileName.Length - 1); IFeatureLayer pFeatureLayer; //打开CAD数据集 IWorkspaceFactory2 pWorkspaceFactory = new CadWorkspaceFactory() as IWorkspaceFactory2; IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0); //打开一个要素集 IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName); //IFeaturClassContainer可以管理IFeatureDataset中的每个要素类 IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset; //对CAD文件中的要素进行遍历处理 IEnvelope envelope = new Envelope() as IEnvelope; for (int i = 0; i < pFeatureClassContainer.ClassCount; i++) { try { IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i); if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation) { //如果是注记,则添加注记层 pFeatureLayer = new CadAnnotationLayer() as IFeatureLayer; } else//如果是点、线、面,则添加要素层 { pFeatureLayer = new FeatureLayer() as IFeatureLayer; } pFeatureLayer.Name = pFeatureClass.AliasName; pFeatureLayer.FeatureClass = pFeatureClass; this.AxMapControl.Map.AddLayer(pFeatureLayer); // IQueryFilter queryFilter = new QueryFilter() as IQueryFilter; queryFilter.WhereClause = ""; IFeatureCursor featureCursor = pFeatureLayer.FeatureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); while (feature != null) { IGeometry geometry = feature.Shape; IEnvelope featureExtent = geometry.Envelope; envelope.Union(featureExtent); System.Runtime.InteropServices.Marshal.ReleaseComObject(feature); feature = featureCursor.NextFeature(); } } catch (Exception ex) { continue; } } this.AxMapControl.ActiveView.Refresh(); } }
private void btnAddRoad_Click(object sender, EventArgs e) { this.operateMode = OperateMode.None; var dialog = new OpenFileDialog { Title = "请选择需要导入的CAD文件", Filter = "DXF文件 (*.dxf)|*.dxf" }; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return; var factory = new CadWorkspaceFactory(); try { var path = System.IO.Path.GetDirectoryName(dialog.FileName); var ws = factory.OpenFromFile(path, 0) as IFeatureWorkspace; var fc = ws.OpenFeatureClass(System.IO.Path.GetFileName(dialog.FileName) + ":polyline"); var cursor = fc.Search(null, true); var f = cursor.NextFeature(); var lst = new List<IPolyline>(); while(f!= null) { var geo = f.ShapeCopy; if (!(geo is IPolyline) || geo.IsEmpty == true) { MessageBox.Show("当前CAD文件中包含的路线类型信息不正确,请检查。", "注意"); Marshal.ReleaseComObject(cursor); return; } else { lst.Add(geo as IPolyline); } f = cursor.NextFeature(); } Marshal.ReleaseComObject(cursor); if (lst.Count == 0) { MessageBox.Show("当前CAD文件中不包含路线信息,请检查。", "注意"); return; } ImportRoad(lst); } catch(Exception ex) { MessageBox.Show("打开CAD文件错误,请检查CAD文件。\r\n详情:" + ex, "注意"); } }