/// <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 Form1_Load(object sender, System.EventArgs e) { //Set passed file to variable string[] arguments = Environment.GetCommandLineArgs(); if (arguments.Length == 1) { MessageBox.Show("No filename passed", "CAD Fileviewer"); this.Close(); return; } string workspacePath = System.IO.Path.GetDirectoryName(arguments[1]); string fileName = System.IO.Path.GetFileName(arguments[1]); //Add passed file to MapControl ICadDrawingDataset cadDrawingDataset = GetCadDataset(workspacePath, fileName); if (cadDrawingDataset == null) { return; } ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDrawingDataset; cadLayer.Name = fileName; axMapControl1.AddLayer(cadLayer, 0); }
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); } } }
/// <summary> /// 加载CAD文件 /// </summary> /// <param name="axMapControl">地图控件引用</param> /// <param name="strFilePath">文件路径</param> /// <returns>正常:“”,异常:异常字符;</returns> private SystemErrorType LoadCADFile(ref AxMapControl axMapControl, string strFilePath) { if (strFilePath == "") { return(SystemErrorType.enumFilePathIsNull); } try { //workspace声明 IWorkspaceFactory pCadWorkspaceFactory = null; IWorkspace pWorkspace = null; ICadDrawingWorkspace pCadDrawingWorkspace = null; ICadDrawingDataset pCadDataset = null; ICadLayer pCadLayer = null; //地图数据操作 pCadWorkspaceFactory = new CadWorkspaceFactoryClass(); pWorkspace = pCadWorkspaceFactory.OpenFromFile(strFilePath, 0); pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace; pCadDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(strFilePath); pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDataset; //控件操作 axMapControl.ClearLayers(); axMapControl.AddLayer(pCadLayer, 0); axMapControl.Refresh(); } catch (Exception) { return(SystemErrorType.enumArcObjectHandleError); } return(SystemErrorType.enumOK); }
/// <summary> /// 通过IWorkspace直接获取到CadDrawingDataset--是cad数据 /// </summary> /// <param name="mapControlDefault">地图控件</param> /// <param name="pWorkspace">工作空间</param> /// <param name="strFileName"></param> public static void GetALLCADLayer(IMapControlDefault mapControlDefault, IWorkspace pWorkspace, string strFileName) { ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace; ICadDrawingDataset pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(strFileName); ICadLayer pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDrawingDataset; mapControlDefault.AddLayer(pCadLayer as ILayer, 0); }
/// <summary> /// 向地图控件中添加CAD所有图层 /// </summary> /// <param name="mapControl"></param> /// <param name="cadFullPath"></param> public static void addCadToMapAsRaster(AxMapControl mapControl, string cadFullPath) { string fileName = System.IO.Path.GetFileName(cadFullPath); string fileDir = System.IO.Path.GetDirectoryName(cadFullPath); ICadDrawingWorkspace ws = openCadWorkspace(fileDir) as ICadDrawingWorkspace; ICadDrawingDataset ds = ws.OpenCadDrawingDataset(fileName); ICadLayer layer = new CadLayerClass(); layer.Name = fileName; layer.CadDrawingDataset = ds; mapControl.Map.AddLayer(layer); mapControl.ActiveView.Refresh(); }
/// <summary> /// 通过ICadDrawingDataset与ICadLayer获取cad /// </summary> /// <param name="mapControlDefault">地图控件</param> /// <param name="strPathWithoutFileName">文件所在文件夹路径</param> /// <param name="strFileName">文件名,包括后缀</param> public static void GetAllCADbyCADLayer(IMapControlDefault mapControlDefault, string strPathWithoutFileName, string strFileName) { ICadDrawingDataset cadDrawingDataset = GetCadDataset(strPathWithoutFileName, strFileName); if (cadDrawingDataset == null) { return; } ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDrawingDataset; cadLayer.Name = strFileName; ILayer mLayer = cadLayer as ILayer; mapControlDefault.AddLayer(mLayer, 0); }
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); } }
public override void OnClick() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "打开CAD文件"; openFileDialog1.Filter = "CAD图形文件(*.dwg)|*.DWG"; if (openFileDialog1.ShowDialog() == DialogResult.OK && m_hookHelper.Hook is IMapControlDefault) { FileInfo fileinfo = new FileInfo(openFileDialog1.FileName); string path = fileinfo.DirectoryName; string name = fileinfo.Name; IWorkspaceFactory cadWorkSpaceFactory = new CadWorkspaceFactoryClass(); ICadDrawingWorkspace cadworkspace = cadWorkSpaceFactory.OpenFromFile(path, 0) as ICadDrawingWorkspace; ICadDrawingDataset cadDataset = cadworkspace.OpenCadDrawingDataset(name); ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDataset; ((IMapControlDefault)m_hookHelper.Hook).AddLayer(cadLayer, 0); } }
public override void OnClick() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "打开CAD文件"; openFileDialog1.Filter = "CAD图形文件(*.dwg)|*.DWG"; if (openFileDialog1.ShowDialog() == DialogResult.OK && m_hookHelper.Hook is IMapControlDefault) { FileInfo fileinfo = new FileInfo(openFileDialog1.FileName); string path = fileinfo.DirectoryName; string name = fileinfo.Name; IWorkspaceFactory cadWorkSpaceFactory = new CadWorkspaceFactoryClass(); ICadDrawingWorkspace cadworkspace = cadWorkSpaceFactory.OpenFromFile(path, 0) as ICadDrawingWorkspace; ICadDrawingDataset cadDataset = cadworkspace.OpenCadDrawingDataset(name); ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDataset; ((IMapControlDefault)m_hookHelper.Hook).AddLayer(cadLayer,0); } }
private void AddCadLayer() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "AutoCad file(*.dxf)|*.dxf"; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { string cadFile = ofd.FileName; string cadName = System.IO.Path.GetFileName(cadFile); string cadDirec = System.IO.Path.GetDirectoryName(cadFile); try { ICadDrawingDataset cadDs = GetCadDataset(cadDirec, cadName); if (cadDs == null) { XtraMessageBox.Show("打开Cad文件失败", "提示"); return; } ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDs; cadLayer.Name = cadName; DF2DApplication app = DF2DApplication.Application; if (_dictCadLyr == null) { _dictCadLyr = new Dictionary <TreeListNode, ICadLayer>(); } TreeListNode node = AddCadNodeToTreeList(cadLayer); if (node == null) { return; } _dictCadLyr[node] = cadLayer; app.Current2DMapControl.Map.AddLayer(cadLayer); app.Current2DMapControl.ActiveView.Refresh(); } catch (System.Exception 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(); }
private void 加载CAD数据ToolStripMenuItem_Click(object sender, EventArgs e) { IWorkspaceFactory pCadWorkspaceFactory; IWorkspace pWorkspace; ICadDrawingWorkspace pCadDrawingWorkspace; ICadDrawingDataset pCadDrawingDataset; ICadLayer pCadLayer; OpenFileDialog pOpenFileDialog = new OpenFileDialog(); pOpenFileDialog.Filter = "CAD(*.dwg)|*.dwg"; pOpenFileDialog.Title = "打开CAD数据文件"; pOpenFileDialog.ShowDialog(); string pFullPath = pOpenFileDialog.FileName; if (pFullPath == "") { return; } //获取文件名和文件路径 int pIndex = pFullPath.LastIndexOf("\\"); string pFilePath = pFullPath.Substring(0, pIndex); string pFileName = pFullPath.Substring(pIndex + 1); pCadWorkspaceFactory = new CadWorkspaceFactoryClass(); pWorkspace = pCadWorkspaceFactory.OpenFromFile(pFilePath, 0); pCadDrawingWorkspace = (ICadDrawingWorkspace)pWorkspace; //获得CAD文件的数据集 pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(pFileName); pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDrawingDataset; axMapControl1.Map.AddLayer(pCadLayer); }
public void LoadData(IList ilist_0) { IEnumDataset subsets; IDataset j; string str; IFeatureClass dataset; IFeatureLayer cadFeatureLayerClass; ITopologyLayer topologyLayerClass; IEnumFeatureClass classes; IFeatureClass m; IFeatureClass n; IFeatureLayer featureLayerClass; List <object> objs = new List <object>(); foreach (object ilist0 in ilist_0) { if (ilist0 is IGxDataset) { if ((ilist0 as IGxDataset).Type != esriDatasetType.esriDTFeatureDataset) { objs.Add(ilist0); } else { IEnumGxObject children = (ilist0 as IGxObjectContainer).Children; children.Reset(); for (IGxObject i = children.Next(); i != null; i = children.Next()) { objs.Add(i); } } } else if (!(ilist0 is IDataset)) { objs.Add(ilist0); } else { if ((ilist0 as IDataset).Type != esriDatasetType.esriDTFeatureDataset) { continue; } subsets = (ilist0 as IDataset).Subsets; subsets.Reset(); for (j = subsets.Next(); j != null; j = subsets.Next()) { objs.Add(j); } } } ProcessAssist processAssist = new ProcessAssist(); processAssist.InitProgress(); processAssist.SetMaxValue(objs.Count); processAssist.SetMessage("正在加载数据,请稍候..."); processAssist.Start(); try { ILayer layer = null; IMap map = this.GetMap(); this.list_0 = new List <ILayer>(); List <string> strs = new List <string>(); IEnvelope envelope = null; IDataset item = null; IGxDataset gxDataset = null; for (int k = 0; k < objs.Count; k++) { string name = ""; if (objs[k] is IGxObject) { name = (objs[k] as IGxObject).Name; } else if (objs[k] is IDataset) { name = (objs[k] as IDataset).Name; } bool flag = true; str = string.Format("加载图层[{0}],第{1}/{2}个", name, k + 1, objs.Count); processAssist.Increment(1); processAssist.SetMessage(str); if (objs[k] is IGxLayer) { layer = (objs[k] as IGxLayer).Layer; if (layer is IGeoDataset && !SpatialReferenctOperator.ValideFeatureClass(layer as IGeoDataset)) { strs.Add(layer.Name); flag = false; } this.AddLayer(map, layer, ref envelope, flag); } else if (!(objs[k] is IGxAGSObject)) { if (objs[k] is IGxDataset) { gxDataset = objs[k] as IGxDataset; item = gxDataset.Dataset; } else if (ilist_0[k] is IDataset) { item = objs[k] as IDataset; } if (item != null) { if (item is IGeoDataset) { flag = SpatialReferenctOperator.ValideFeatureClass(item as IGeoDataset); } if (item.Type == esriDatasetType.esriDTFeatureClass) { dataset = (IFeatureClass)item; if ((gxDataset as IGxObject).Category.IndexOf("CAD") != -1) { if (dataset.FeatureType != esriFeatureType.esriFTCoverageAnnotation) { cadFeatureLayerClass = new CadFeatureLayerClass(); } else { cadFeatureLayerClass = new CadAnnotationLayerClass(); } cadFeatureLayerClass.FeatureClass = dataset; cadFeatureLayerClass.Name = dataset.AliasName; } else if (dataset.FeatureType == esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else if (dataset.FeatureType != esriFeatureType.esriFTDimension) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else { cadFeatureLayerClass = new DimensionLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } if (!flag) { strs.Add(cadFeatureLayerClass.Name); } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } else if (item.Type == esriDatasetType.esriDTCadDrawing) { if ((gxDataset as IGxObject).Category != "CAD绘图") { IEnumGxObject enumGxObject = (gxDataset as IGxObjectContainer).Children; enumGxObject.Reset(); for (IGxDataset l = enumGxObject.Next() as IGxDataset; l != null; l = enumGxObject.Next() as IGxDataset) { dataset = l.Dataset as IFeatureClass; if (dataset != null) { if (dataset.FeatureType != esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new CadFeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag1 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag1; if (!flag1) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } } else { ICadLayer cadLayerClass = new CadLayerClass() { CadDrawingDataset = gxDataset.Dataset as ICadDrawingDataset, Name = gxDataset.Dataset.Name }; if (!flag) { strs.Add(cadLayerClass.Name); } this.AddLayer(map, cadLayerClass, ref envelope, flag); } } else if (item.Type == esriDatasetType.esriDTFeatureDataset) { subsets = item.Subsets; subsets.Reset(); for (j = subsets.Next(); j != null; j = subsets.Next()) { if (j.Type == esriDatasetType.esriDTFeatureClass) { dataset = j as IFeatureClass; if (dataset.FeatureType != esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag2 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag2; if (!flag2) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } else { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag3 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag3; if (!flag3) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } else if (j.Type == esriDatasetType.esriDTTopology) { topologyLayerClass = new TopologyLayerClass() { Topology = j as ITopology }; (topologyLayerClass as ILayer).Name = j.Name; this.AddLayer(map, topologyLayerClass as ILayer, ref envelope, true); } } } else if (item.Type == esriDatasetType.esriDTTopology) { if (MessageBox.Show(string.Concat("是否将参加拓扑-", item.Name, "-的所有要素添加到地图中"), "添加拓扑层", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { classes = (item as IFeatureClassContainer).Classes; classes.Reset(); for (m = classes.Next(); m != null; m = classes.Next()) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = m, Name = m.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag4 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag4; if (!flag4) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } topologyLayerClass = new TopologyLayerClass() { Topology = item as ITopology }; (topologyLayerClass as ILayer).Name = item.Name; this.AddLayer(map, topologyLayerClass as ILayer, ref envelope, true); } else if (item.Type == esriDatasetType.esriDTTin) { ITinLayer tinLayerClass = new TinLayerClass() { Dataset = (ITin)item, Name = item.Name }; flag = true; if (tinLayerClass is IGeoDataset) { bool flag5 = SpatialReferenctOperator.ValideFeatureClass(tinLayerClass as IGeoDataset); flag = flag5; if (!flag5) { strs.Add(tinLayerClass.Name); } } this.AddLayer(map, tinLayerClass, ref envelope, flag); } else if (item.Type == esriDatasetType.esriDTRasterCatalog) { IGdbRasterCatalogLayer gdbRasterCatalogLayerClass = null; gdbRasterCatalogLayerClass = new GdbRasterCatalogLayerClass(); if (gdbRasterCatalogLayerClass.Setup((ITable)(item as IRasterCatalog))) { bool flag6 = SpatialReferenctOperator.ValideFeatureClass(gdbRasterCatalogLayerClass as IGeoDataset); flag = flag6; if (!flag6) { strs.Add((gdbRasterCatalogLayerClass as ILayer).Name); } this.AddLayer(map, gdbRasterCatalogLayerClass as ILayer, ref envelope, flag); } } else if (!(item.Type == esriDatasetType.esriDTRasterDataset ? false : item.Type != esriDatasetType.esriDTRasterBand)) { bool flag7 = true; if (!((IRasterPyramid)item).Present) { if (this.m_pApp.PyramidPromptType == PyramidPromptType.AlwaysBuildNoPrompt) { ((IRasterPyramid)item).Create(); } else if (this.m_pApp.PyramidPromptType == PyramidPromptType.AlwaysPrompt) { DialogResult dialogResult = BuildPyramidSet.Show(); if (dialogResult == DialogResult.Yes) { ((IRasterPyramid)item).Create(); } else if (dialogResult == DialogResult.Cancel) { flag7 = false; } } } if (flag7) { IRasterLayer rasterLayerClass = new RasterLayerClass() { Cached = true }; rasterLayerClass.CreateFromDataset((IRasterDataset)item); rasterLayerClass.Name = item.Name; bool flag8 = SpatialReferenctOperator.ValideFeatureClass(rasterLayerClass as IGeoDataset); flag = flag8; if (!flag8) { strs.Add(rasterLayerClass.Name); } this.AddLayer(map, rasterLayerClass, ref envelope, flag); } } else if (item.Type == esriDatasetType.esriDTTable) { try { IStandaloneTableCollection standaloneTableCollection = this.GetMap() as IStandaloneTableCollection; IPropertySet connectionProperties = item.Workspace.ConnectionProperties; bool flag9 = false; int num = 0; while (true) { if (num < standaloneTableCollection.StandaloneTableCount) { ITable table = standaloneTableCollection.StandaloneTable[num].Table; if (!connectionProperties.IsEqual((table as IDataset).Workspace.ConnectionProperties) || !((table as IDataset).Name == item.Name)) { num++; } else { flag9 = true; break; } } else { break; } } if (!flag9) { standaloneTableCollection.AddStandaloneTable(new StandaloneTableClass() { Table = item as ITable }); } } catch (Exception exception) { CErrorLog.writeErrorLog(this, exception, ""); } } else if (item.Type == esriDatasetType.esriDTGeometricNetwork) { IGeometricNetwork geometricNetwork = item as IGeometricNetwork; if (geometricNetwork != null) { IEnumFeatureClass classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleJunction]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexJunction]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleEdge]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexEdge]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } } } else if (item.Type == esriDatasetType.esriDTNetworkDataset) { INetworkLayer networkLayerClass = new NetworkLayerClass() { NetworkDataset = item as INetworkDataset }; this.AddLayer(map, networkLayerClass as ILayer, ref envelope, true); if (MessageBox.Show(string.Concat("是否将参加网络要素集-", item.Name, "-的所有要素添加到地图中"), "添加拓扑层", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { classes = (item as IFeatureClassContainer).Classes; classes.Reset(); for (m = classes.Next(); m != null; m = classes.Next()) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = m, Name = m.AliasName }; this.AddLayer(map, cadFeatureLayerClass, ref envelope, true); } } } } } else { IMapServerLayer mapServerLayerClass = new MapServerLayerClass(); mapServerLayerClass.ServerConnect((objs[k] as IGxAGSObject).AGSServerObjectName, (objs[k] as IGxAGSObject).DefaultMapName); this.AddLayer(map, mapServerLayerClass as ILayer, ref envelope, false); } } processAssist.End(); bool layerCount = this.GetMap().LayerCount > 0; if (strs.Count > 0) { MessageBox.Show("警告:数据范围不一致。\r\n 一个或多个添加的图层范围与关联的空间坐标系范围不一致。请检查数据问题。存在问题图层信息请查看错误日志!", "管理平台", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); StringBuilder stringBuilder = new StringBuilder(); foreach (string str1 in strs) { if (stringBuilder.Length > 0) { stringBuilder.Append(","); } stringBuilder.Append(str1); } CErrorLog.writeErrorLog(stringBuilder, null, "图层范围不一致!"); } foreach (ILayer list0 in this.list_0) { list0.Visible = true; } if (layerCount && envelope != null) { IMap map1 = this.GetMap(); ISpatialReference spatialReference = map1.SpatialReference; if ((spatialReference is IUnknownCoordinateSystem || envelope.SpatialReference is IUnknownCoordinateSystem || envelope.SpatialReference == null ? false : spatialReference != null)) { envelope.Project(spatialReference); } (map1 as IActiveView).Extent = envelope; (map1 as IActiveView).Refresh(); } if (this.m_pApp != null) { this.m_pApp.MapDocumentChanged(); } else if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } catch (Exception exception1) { str = exception1.ToString(); } if (this.onLoadCompleteHand_0 != null) { this.onLoadCompleteHand_0(); } }
public static void AddDataset(IBasicMap pMap, IDataset pDataset, string dsName) { IFeatureLayer fDOGraphicsLayerClass; IFeatureClass j; IFeatureLayer featureLayerClass; if (dsName == null) { dsName = ""; } switch (pDataset.Type) { case esriDatasetType.esriDTFeatureDataset: { IEnumDataset subsets = pDataset.Subsets; subsets.Reset(); for (IDataset i = subsets.Next(); i != null; i = subsets.Next()) { MapHelper.AddDataset(pMap, i, dsName); } return; } case esriDatasetType.esriDTFeatureClass: { IFeatureClass idataset0 = (IFeatureClass)pDataset; if (idataset0.FeatureType == esriFeatureType.esriFTAnnotation) { fDOGraphicsLayerClass = new FDOGraphicsLayerClass(); try { fDOGraphicsLayerClass.FeatureClass = idataset0; fDOGraphicsLayerClass.Name = string.Concat(dsName, idataset0.AliasName); pMap.AddLayer(fDOGraphicsLayerClass); return; } catch (Exception exception) { exception.ToString(); return; } } else if (idataset0.FeatureType != esriFeatureType.esriFTDimension) { fDOGraphicsLayerClass = new FeatureLayerClass() { FeatureClass = idataset0, Name = string.Concat(dsName, idataset0.AliasName) }; pMap.AddLayer(fDOGraphicsLayerClass); return; } else { fDOGraphicsLayerClass = new DimensionLayerClass() { FeatureClass = idataset0, Name = string.Concat(dsName, idataset0.AliasName) }; pMap.AddLayer(fDOGraphicsLayerClass); return; } break; } case esriDatasetType.esriDTPlanarGraph: case esriDatasetType.esriDTText: case esriDatasetType.esriDTRelationshipClass: { return; } case esriDatasetType.esriDTGeometricNetwork: { IGeometricNetwork geometricNetwork = pDataset as IGeometricNetwork; if (geometricNetwork == null) { return; } IEnumFeatureClass classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleJunction]; classesByType.Reset(); for (j = classesByType.Next(); j != null; j = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = j, Name = string.Concat(dsName, (j as IDataset).Name) }; pMap.AddLayer(featureLayerClass); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexJunction]; classesByType.Reset(); for (j = classesByType.Next(); j != null; j = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = j, Name = string.Concat(dsName, (j as IDataset).Name) }; pMap.AddLayer(featureLayerClass); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleEdge]; classesByType.Reset(); for (j = classesByType.Next(); j != null; j = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = j, Name = string.Concat(dsName, (j as IDataset).Name) }; pMap.AddLayer(featureLayerClass); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexEdge]; classesByType.Reset(); for (j = classesByType.Next(); j != null; j = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = j, Name = string.Concat(dsName, (j as IDataset).Name) }; pMap.AddLayer(featureLayerClass); } return; } case esriDatasetType.esriDTTopology: { ITopologyLayer topologyLayerClass = new TopologyLayerClass() { Topology = pDataset as ITopology }; (topologyLayerClass as ILayer).Name = string.Concat(dsName, pDataset.Name); pMap.AddLayer(topologyLayerClass as ILayer); return; } case esriDatasetType.esriDTTable: { try { IRasterCatalogTable rasterCatalogTableClass = new RasterCatalogTable() { Table = (ITable)pDataset }; rasterCatalogTableClass.Update(); IRasterCatalogLayer rasterCatalogLayerClass = new RasterCatalogLayerClass(); rasterCatalogLayerClass.Create(rasterCatalogTableClass); rasterCatalogLayerClass.Name = string.Concat(dsName, pDataset.BrowseName); pMap.AddLayer(rasterCatalogLayerClass); return; } catch { try { IStandaloneTableCollection ibasicMap0 = pMap as IStandaloneTableCollection; IPropertySet connectionProperties = pDataset.Workspace.ConnectionProperties; bool flag = false; int num = 0; while (true) { if (num < ibasicMap0.StandaloneTableCount) { ITable table = ibasicMap0.StandaloneTable[num].Table; if ( !connectionProperties.IsEqual((table as IDataset).Workspace.ConnectionProperties) || !((table as IDataset).Name == pDataset.Name)) { num++; } else { flag = true; break; } } else { break; } } if (!flag) { ibasicMap0.AddStandaloneTable(new StandaloneTableClass() { Table = pDataset as ITable }); } } catch (Exception exception1) { Logger.Current.Write(exception1.Message, LogLevel.Error, null); } return; } break; } case esriDatasetType.esriDTRasterDataset: case esriDatasetType.esriDTRasterBand: { IRasterLayer rasterLayerClass = new RasterLayerClass(); rasterLayerClass.CreateFromDataset((IRasterDataset)pDataset); rasterLayerClass.Name = string.Concat(dsName, pDataset.Name); pMap.AddLayer(rasterLayerClass); return; } case esriDatasetType.esriDTTin: { ITinLayer tinLayerClass = new TinLayerClass() { Dataset = (ITin)pDataset, Name = string.Concat(dsName, pDataset.Name) }; pMap.AddLayer(tinLayerClass); return; } case esriDatasetType.esriDTCadDrawing: { ICadLayer cadLayerClass = new CadLayerClass() { CadDrawingDataset = pDataset as ICadDrawingDataset, Name = pDataset.Name }; pMap.AddLayer(cadLayerClass); return; } default: { return; } } }
private void Form1_Load(object sender, System.EventArgs e) { //Set passed file to variable string[] arguments = Environment.GetCommandLineArgs(); if (arguments.Length == 1) { MessageBox.Show("No filename passed", "CAD Fileviewer"); this.Close(); return; } string workspacePath = System.IO.Path.GetDirectoryName(arguments[1]); string fileName = System.IO.Path.GetFileName(arguments[1]); //Add passed file to MapControl ICadDrawingDataset cadDrawingDataset = GetCadDataset(workspacePath, fileName); if (cadDrawingDataset == null) return; ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDrawingDataset; cadLayer.Name = fileName; axMapControl1.AddLayer(cadLayer,0); }
private void btnAddCadAsDraw_Click(object sender, EventArgs e) { this.openFileDialog.Filter = "CAD文件(*.dwg)|*.dwg"; this.openFileDialog.Title = "请选择CAD文件"; if (this.openFileDialog.ShowDialog() == DialogResult.OK) { Application.DoEvents(); try { IWorkspaceFactory pCADWorksapceFactory; pCADWorksapceFactory = new CadWorkspaceFactoryClass(); ICadDrawingWorkspace pCadDrawingWorkspace; pCadDrawingWorkspace = pCADWorksapceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(this.openFileDialog.FileName), 0) as ICadDrawingWorkspace; ICadDrawingDataset pCadDrawingDataset; pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(System.IO.Path.GetFileName(this.openFileDialog.FileName)); ICadLayer pCadLayer; pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDrawingDataset; pCadLayer.Name = System.IO.Path.GetFileName(this.openFileDialog.FileName); this.mainMapControl.AddLayer(pCadLayer); this.EagleaxMapControl.AddLayer(pCadLayer); this.EagleaxMapControl.Extent = this.mainMapControl.FullExtent; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// 通过IWorkspace直接获取到CadDrawingDataset--是cad数据 /// </summary> /// <param name="mapControlDefault">地图控件</param> /// <param name="pWorkspace">工作空间</param> /// <param name="strFileName"></param> public static void GetALLCADLayer(IMapControlDefault mapControlDefault, IWorkspace pWorkspace, string strFileName) { ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace; ICadDrawingDataset pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(strFileName); ICadLayer pCadLayer = new CadLayerClass(); pCadLayer.CadDrawingDataset = pCadDrawingDataset; mapControlDefault.AddLayer(pCadLayer as ILayer, 0); }
/// <summary> /// 通过ICadDrawingDataset与ICadLayer获取cad /// </summary> /// <param name="mapControlDefault">地图控件</param> /// <param name="strPathWithoutFileName">文件所在文件夹路径</param> /// <param name="strFileName">文件名,包括后缀</param> public static void GetAllCADbyCADLayer(IMapControlDefault mapControlDefault, string strPathWithoutFileName, string strFileName) { ICadDrawingDataset cadDrawingDataset = GetCadDataset(strPathWithoutFileName, strFileName); if (cadDrawingDataset == null) return; ICadLayer cadLayer = new CadLayerClass(); cadLayer.CadDrawingDataset = cadDrawingDataset; cadLayer.Name = strFileName; ILayer mLayer = cadLayer as ILayer; mapControlDefault.AddLayer(mLayer, 0); }