Exemple #1
0
        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);
        }
Exemple #2
0
        /// <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);
         }
     }
 }
 /// <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);
        }
Exemple #6
0
        public static ICadDrawingDataset GetCADDataset(ICadDrawingWorkspace pCadDwgWorkspace, string DwgFileName)
        {
            ICadDrawingDataset pCadDwgDataset = null;

            try
            {
                pCadDwgDataset = pCadDwgWorkspace.OpenCadDrawingDataset(DwgFileName);
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
            return(pCadDwgDataset);
        }
Exemple #7
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);
            }
        }
Exemple #10
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);
            }
        }
Exemple #11
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();
        }