본문 바로가기
C++ 200제/코딩 IT 정보

DXF 파일 로드하는 C# 프로그램 코드 (ArcGIS 윈도우)

by vicddory 2018. 1. 1.

DXF 파일 로드하는 C# 프로그램 코드 (ArcGIS 윈도우)


DXF 파일 로드하는 C# 프로그램 코드[C# ArcGIS Example] dxf viewer source


DXF 파일 열기



소스 코드에 대한 설명은 생략하고, 프로그램에 대해서만 설명해 드리겠습니다. 기본적으로 .DXF 파일과 .SHP파일의 로딩이 가능하고 거기에 레이어를 추가하거나 저장하는 것도 가능합니다.


ArcGIS - DXF 파일 로딩ArcGIS - DXF 파일 로딩


ArcGIS - 마커를 눌러 지점 간 거리 재기[C# ArcGIS Example] dxf viewer source


소스는 ESRI 공식 API 사이[링크]에서 얻을 수 있습니다. 기본 소스는 참고했고, 이래저래 수정해 본 프로그램은 맨 위의 링크를 눌러서 다운 받을 수 있어요.


.DXF 파일 Loading

1. 선행 작업

ArcGIS_Engine의 DevKit을 이용해서 주변 프로그램들을 모두 설치하면, VisualStudio의 도구 상자에 아래의 그림처럼 "ArcGIS WindowsForms" 메뉴가 추가됩니다.


ArcGIS - 비주얼 스튜디오 도구 상자[GIS 닷넷 윈도우 Library 예제 소스]


2. 프로젝트 구성

임의의 프로젝트를 생성한 뒤 아래의 그림처럼 3개의 도구를 폼에 삽입합니다.


좌측 - TOCControl (name : axTOCControl1)

우측 - MapControl (name : axMapControlMain), LicenseControl (하나의 아이콘은 하나의 도구를 의미)


ArcGIS - 임의의 프로젝트 생성[C# ArcGIS Example] dxf viewer source


도구 삽입 후, 솔루션 탐색기를 열어 참조 파일이 자동으로 추가가 되었는지 확인합니다.


ArcGIS - 도구 상자 파일 추가 확인[GIS 닷넷 윈도우 Library 예제 소스]


이번 데모 프로그램 실행을 위해서 아래의 참조를 더 추가합니다. 만약, 위와 같이 자동으로 참조 추가가 되지 않는다면, 위의 그림에 언급된 Arc 라이브러리와 아래의 참조를 함께 프로젝트에 추가합니다.


추가 파일 : Geodatabase, Geoprocessing, Geoprocessor, DataSourcesFile, AdfLocal

추가 방법 : 참조 우클릭 – Add ArcGIS Reference… - References창에서 Engine(Core) – 사용 여부 선택


ArcGIS - 비주얼 스튜디오 참조 추가[C# ArcGIS Example] dxf viewer source


ArcGIS - Add ArcGIS Reference[GIS 닷넷 윈도우 Library 예제 소스]

3. 소스 코드

데모 프로그램의 namespace는 ArcTest01이며 기본폼의 이름은 Form1입니다. 그리고 1항에서 언급했듯 추가되는 도구의 이름은 각각 아래와 같습니다.


TOCControl (axTOCControl1)

MapControl (axMapControlMain)


아래 코드는 아주 간단한 예제입니다. ESRI.ArcGIS의 각종 라이브러리를 불러와 위에서 설명한 기능들을 구현합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS;
using ESRI.ArcGIS.ADF;
 
namespace ArcTest01{
    public partial class Form1 : Form
    {
        static List<ESRI.ArcGIS.Geometry.Envelope> MapExtents = new List<Envelope>();
        public static Form1 f1;
 
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
 
            f1 = this;
 
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
 
            string strFileName = dlg.FileName;
            string strFilePath = dlg.FileName;
            string strFileNameTemp;
 
            if (strFileName == "")
            {
                return;
            }
            else
            {
                int nPos = strFileName.LastIndexOf("\\");
                int nLen = strFileName.Length;
 
                if (nPos != -1)
                {
                    strFilePath = strFileName.Remove(nPos, nLen - nPos);
                    strFileName = strFileName.Remove(0, nPos);
                    strFileNameTemp = strFileName.Replace("\\""");
                    strFileNameTemp = strFileNameTemp.ToLower();
                    strFileNameTemp.Trim();
                    axMapControlMain.Extent = axMapControlMain.FullExtent;
 
                    if (strFileNameTemp.Contains(".lyr"))
                    {
                        string strTemp = strFileNameTemp.Trim();
                        axMapControlMain.AddLayerFromFile(dlg.FileName);
                    }
 
                    else if (strFileNameTemp.Contains(".shp"))
                    {
                        string strTemp = strFileNameTemp.Trim();
                        IFeatureClass XZFeatureClass = 
                                         GetFeatureClassFromShapefileOnDisk(strFilePath, strTemp);
                       //RunBuffer(strFilePath, strTemp); 
                       //IFeatureDataset iFdataSet = XZFeatureClass.FeatureDataset;
                       axMapControlMain.AddShapeFile(strFilePath, strFileNameTemp);
                    }
 
                    else if (strFileNameTemp.Contains(".dxf")) // dxf 파일 열기
                    {
                        string strTemp = strFileNameTemp.Trim();
                        strFileNameTemp = strTemp;
                        ICadDrawingDataset cadDrawingDataset = GetCadDataset(strFilePath, strFileNameTemp);
 
                        if (cadDrawingDataset == nullreturn;
 
                        ICadLayer cadLayer = new CadLayerClass();
                        cadLayer.CadDrawingDataset = cadDrawingDataset;
                        cadLayer.Name = strFileNameTemp;
                        int nCount = axMapControlMain.LayerCount;
 
                        if (cadLayer.IsAutoCad)
                        {
                            axMapControlMain.AddLayer(cadLayer, nCount);
                        }
                    }
                }
                axTOCControl1.EnableLayerDragDrop = true;
                axTOCControl1.SetBuddyControl(axMapControlMain);
               //MapExtents.Add(((Envelope)axTOCControl1.Extent));
            }
        }
 
        private ICadDrawingDataset GetCadDataset(string cadWorkspacePath, string cadFileName)
        {
            //Create a WorkspaceName object
            IWorkspaceName workspaceName = new WorkspaceNameClass();
            workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.CadWorkspaceFactory";
            workspaceName.PathName = cadWorkspacePath;
 
            //Create a CadDrawingName object
            IDatasetName cadDatasetName = new CadDrawingNameClass();
            cadDatasetName.Name = cadFileName;
            cadDatasetName.WorkspaceName = workspaceName;
 
            //Open the CAD drawing
            IName name = (IName)cadDatasetName;
            return (ICadDrawingDataset)name.Open();
        }
 
        public IFeatureClass GetFeatureClassFromShapefileOnDisk
                 (string string_ShapefileDirectory, string string_ShapefileName)
        {
            DirectoryInfo directoryInfo_check = new System.IO.DirectoryInfo(string_ShapefileDirectory);
 
            if (directoryInfo_check.Exists)
            {
                //We have a valid directory, proceed
                System.IO.FileInfo fileInfo_check = 
                       new System.IO.FileInfo(string_ShapefileDirectory + "\\" + string_ShapefileName);
 
                if (fileInfo_check.Exists)
                {
                    //We have a valid shapefile, proceed
                    ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory =
                                                                        new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                    ESRI.ArcGIS.Geodatabase.IWorkspace workspace =
                                                                        workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
                    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace =
                                                                        (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
 
                    // Explict Cast
                    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass =
                                                                        featureWorkspace.OpenFeatureClass(string_ShapefileName);
 
                    GeoProcessor IGP = new GeoProcessorClass();
                    ISpatialReference anotherSpatialReference = axMapControlMain.ActiveView.FocusMap.SpatialReference;
                    IProjectedCoordinateSystem IPSystem = anotherSpatialReference as IProjectedCoordinateSystem;
                    GeoProcessor gp = new GeoProcessor();
 
                    return featureClass;
                }
 
                else
 
                    //Not valid shapefile
                    return null;
                }
            }
 
            else
            {
                // Not valid directory
                return null;
            }
        }
    }
}
cs


소스 코드를 추가했을 때, Interop 형식 오류가 생성될 경우가 있습니다.


ArcGIS - Interop 형식 오류[C# ArcGIS Example] dxf viewer source

이 경우, 해당 참조 파일의 속성값 중, "Interop 형식 포함"의 값을 False로 변경하여 해결이 가능합니다.


ArcGIS - Interop 형식 포함 FALSE[GIS 닷넷 윈도우 Library 예제 소스]


4. 프로젝트 실행

1) 초기 실행 후 생성되는 다이얼로그 창에서 데이터 파일을 선택합니다.


ArcGIS - 프로젝트 실행[C# ArcGIS Example] dxf viewer source


2) 정상적으로 파일이 로드되는지 확인합니다.


ArcGIS - 파일 로드 확인[GIS 닷넷 윈도우 Library 예제 소스]


정상적으로 프로그램이 작성되었다면, 왼쪽 메뉴에 로드한 DXF 파일 이름, 오른쪽 창에는 지도가 뜹니다.


DXF 파일 로드하는 C# 프로그램 코드 (ArcGIS 윈도우)

댓글