프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / MarkusImageCreate / Program.cs @ dfb9a03c

이력 | 보기 | 이력해설 | 다운로드 (20.1 KB)

1 d1d8c5ea taeseongkim
using ID2.Manager.Dapper.Repository;
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.IO;
8
using ID2.Manager.Dapper.Entities;
9 c7f3eb42 taeseongkim
using Teigha.DatabaseServices;
10
using OdReadExMgd;
11 b0bfc1d3 taeseongkim
using CredentialManagement;
12 7842224c taeseongkim
using Dapper;
13 50f9e1d9 taeseongkim
using Teigha.Runtime;
14 d1d8c5ea taeseongkim
15
namespace MarkusImageCreate
16
{
17 0d8516d2 taeseongkim
    public class Program
18 d1d8c5ea taeseongkim
    {
19
        const string CONST_SETTINGS = "SETTINGS";
20
        const string CONST_ID2Manager_Connect = "ID2Manager";
21 13a36357 humkyung
        const string CONST_Markus_Connect = "MARKUS";
22 d1d8c5ea taeseongkim
        const string CONST_ID2ProjectsPath = "ID2ProjectsPath";
23
        const string CONST_MarkusImagePath = "MarkusImagePath";
24
        const string CONST_Resolution = "Resolution";
25
        const string CONST_FontPath  = "FontPath";
26 0d8516d2 taeseongkim
        const string CONST_IsOpenGL = "IsOpenGL";
27 d1d8c5ea taeseongkim
28
        static string MarkusConnectionStr;
29
        static string ID2ConnectionStr;
30
        static string ID2ProjectsPath;
31
        static string MarkusImagePath;
32
        static int Resolution;
33
        static string FontPath;
34 0d8516d2 taeseongkim
        static bool IsOpenGL = false;
35 d1d8c5ea taeseongkim
36 0d8516d2 taeseongkim
        public static void Main(string[] args)
37 d1d8c5ea taeseongkim
        {
38 0d8516d2 taeseongkim
39 c7f3eb42 taeseongkim
            bool bSuccess = true;
40 0d8516d2 taeseongkim
            string cmd = "DEFAULT";
41
42
            if(args.Length > 0)
43
            {
44
                cmd = args[0];
45
            }
46 429bc8fa taeseongkim
47
            try
48 c7f3eb42 taeseongkim
            {
49 0d8516d2 taeseongkim
                try
50
                {
51
                    Teigha.Runtime.Services.odActivate(ActivationData.userInfo, ActivationData.userSignature);
52
                }
53 50f9e1d9 taeseongkim
                catch (System.Exception ex)
54 0d8516d2 taeseongkim
                {
55
                    Console.WriteLine(ex.ToString());
56
57
                }
58
59
                Console.WriteLine("Activate Teigha");
60
61 429bc8fa taeseongkim
                using (Teigha.Runtime.Services srv = new Teigha.Runtime.Services())
62 c7f3eb42 taeseongkim
                {
63
                    /**********************************************************************/
64
                    /* Display the Product and Version that created the executable        */
65
                    /**********************************************************************/
66
                    Console.WriteLine("\nReadExMgd developed using {0} ver {1}", HostApplicationServices.Current.Product, HostApplicationServices.Current.VersionString);
67
68 0d8516d2 taeseongkim
                    Salaros.Configuration.ConfigParser config;
69
70
                    try
71
                    {
72
                        config = new Salaros.Configuration.ConfigParser(Path.Combine(AppContext.BaseDirectory, "MarkusImageCreate.ini"));
73
                    }
74 50f9e1d9 taeseongkim
                    catch (System.Exception)
75 0d8516d2 taeseongkim
                    {
76
                        Console.WriteLine($"Config File Path Not Found : {Path.Combine(AppContext.BaseDirectory, "MarkusImageCreate.ini")}");
77
                        Console.ReadKey();
78
                        throw;
79
                    }
80
81 c7f3eb42 taeseongkim
                    ID2ConnectionStr = config.GetValue(CONST_SETTINGS, CONST_ID2Manager_Connect);
82
                    MarkusConnectionStr = config.GetValue(CONST_SETTINGS, CONST_Markus_Connect);
83
                    //ID2ConnectionStr = Encoding.Unicode.GetString(Convert.FromBase64String(config.GetValue(CONST_SETTINGS, CONST_ID2Manager_Connect)));
84
                    //MarkusConnectionStr = Encoding.Unicode.GetString(Convert.FromBase64String(config.GetValue(CONST_SETTINGS, CONST_Markus_Connect)));
85
                    ID2ProjectsPath = config.GetValue(CONST_SETTINGS, CONST_ID2ProjectsPath);
86
                    MarkusImagePath = config.GetValue(CONST_SETTINGS, CONST_MarkusImagePath);
87
                    Resolution = config.GetValue(CONST_SETTINGS, CONST_Resolution, 250);
88
                    FontPath = config.GetValue(CONST_SETTINGS, CONST_FontPath);
89 0d8516d2 taeseongkim
                    IsOpenGL = config.GetValue(CONST_SETTINGS,CONST_IsOpenGL,false);
90 c7f3eb42 taeseongkim
91 b0bfc1d3 taeseongkim
                    while (!Directory.Exists(ID2ProjectsPath))
92
                    {
93
                        string sourceComputer = ID2ProjectsPath.Split('\\').FirstOrDefault(x => !string.IsNullOrEmpty(x));
94 0d8516d2 taeseongkim
                        IsCredential($"\\\\{sourceComputer}에 대한 인증이 필요합니다.", sourceComputer);
95 429bc8fa taeseongkim
                    }
96
97
                    while (!Directory.Exists(MarkusImagePath))
98
                    {
99
                        string sourceComputer = MarkusImagePath.Split('\\').FirstOrDefault(x => !string.IsNullOrEmpty(x));
100 0d8516d2 taeseongkim
                        IsCredential($"\\\\{sourceComputer}에 대한 인증이 필요합니다.", sourceComputer);
101 b0bfc1d3 taeseongkim
                    }
102
103 0d8516d2 taeseongkim
                    if (cmd == "DEFAULT")
104
                    {
105
                        InitConvertPDF();
106
                    }
107
                    else if (cmd == "INPUT")
108 c7f3eb42 taeseongkim
                    {
109 0d8516d2 taeseongkim
                        bool isBreak = false;
110
111
                        while (!isBreak)
112 7842224c taeseongkim
                        {
113
                            string ProjectCode = null;
114
                            string DocID = null;
115
116
                            Console.WriteLine("# 생성이 필요한 DWG ID 입력 #");
117 0d8516d2 taeseongkim
118 7842224c taeseongkim
                            Console.Write("Team Code : ");
119
                            ProjectCode = Console.ReadLine();
120
121
                            Console.Write("DWG ID : ");
122
                            DocID = Console.ReadLine();
123
124
                            ConvertDWG(ProjectCode, DocID);
125
126 0d8516d2 taeseongkim
                            Console.WriteLine("계속 진행은 아무키나 입력,종료는 CTRL+BREAK");
127
                            Console.ReadKey();
128 7842224c taeseongkim
                        }
129 0d8516d2 taeseongkim
                    }
130
                    else if (args[0] == "RUN")
131
                    {
132
                        if (args.Count() == 3)
133 c7f3eb42 taeseongkim
                        {
134 0d8516d2 taeseongkim
                            string ProjectCode = args[1];
135
                            string DocID = args[2];
136
137
                            ConvertDWG(ProjectCode, DocID);
138 c7f3eb42 taeseongkim
                        }
139
                    }
140 0d8516d2 taeseongkim
                    else if (args[0].Contains(".dwg"))
141 c7f3eb42 taeseongkim
                    {
142 0d8516d2 taeseongkim
                        Show(args[0]);
143 c7f3eb42 taeseongkim
                    }
144
                }
145
            }
146 429bc8fa taeseongkim
            /********************************************************************/
147
            /* Display the error                                                */
148
            /********************************************************************/
149
            catch (System.Exception e)
150
            {
151
                bSuccess = false;
152
                Console.WriteLine("Teigha?NET for .dwg files Error: " + e.Message);
153
            }
154
155
            if (bSuccess)
156
                Console.WriteLine("OdReadExMgd Finished Successfully");
157 0d8516d2 taeseongkim
158
            if (cmd == "DEFAULT")
159
            {
160
                Console.WriteLine("완료");
161
                Console.ReadKey();
162
            }
163 d1d8c5ea taeseongkim
        }
164
165
        static string[] ProjectCodes()
166
        {
167
            using (ProjectRepository rep = new ProjectRepository(ID2ConnectionStr))
168
            {
169
                var projects = rep.GetAllProjectList();
170
171
                return projects.Select(x => x.Code).ToArray();
172
            }
173
        }
174
175 c7f3eb42 taeseongkim
        static void Show(string dwgFile)
176
        {
177
            using (Database pDb = new Database(false, true))
178
            {
179
                pDb.ReadDwgFile(dwgFile, FileShare.Read, true, "");
180
                HostApplicationServices.WorkingDatabase = pDb;
181
                /****************************************************************/
182
                /* Display the File Version                                     */
183
                /****************************************************************/
184
                Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
185
                /****************************************************************/
186
                /* Dump the database                                            */
187
                /****************************************************************/
188
                DbDumper dumper = new DbDumper();
189
190
                //dumper.prepareDump(pDb);
191
                dumper.ExplodeAndPurgeNestedBlocks(pDb);
192
193
                int width = 9600;
194
                int height = 6787;
195
196
                dumper.Show(pDb, width, height);
197
            }
198
        }
199
200 d1d8c5ea taeseongkim
        static void InitConvertPDF()
201
        {
202
            foreach (var code in ProjectCodes())
203
            {
204 7842224c taeseongkim
                ConvertAllProject(code);
205 d1d8c5ea taeseongkim
            }
206
        }
207
208 429bc8fa taeseongkim
        static bool IsCredential(string Message, string path)
209 b0bfc1d3 taeseongkim
        {
210
            bool result = false;
211
212
            try
213
            {
214
                Credential credential = new Credential
215
                {
216
                    Type = CredentialType.DomainPassword,
217
                    PersistanceType = PersistanceType.LocalComputer,
218
                    Target = path,
219
                };
220
221 7842224c taeseongkim
                result = credential.Load();
222
223
                if (!result)
224 b0bfc1d3 taeseongkim
                {
225
                    VistaPrompt prompt = new VistaPrompt();
226 429bc8fa taeseongkim
                    prompt.Title = $"{Message}";
227 b0bfc1d3 taeseongkim
228
                    var dialogResult = prompt.ShowDialog();
229
230
                    if (dialogResult == DialogResult.OK)
231
                    {
232
                        Credential genericCredential = new Credential
233
                        {
234
                            Target = path,
235
                            Username = prompt.Username,
236
                            Password = prompt.Password,
237
                            Type = CredentialType.DomainPassword,
238
                            PersistanceType = PersistanceType.LocalComputer
239
                        };
240
241
                        genericCredential.Save();
242
                        result = true;
243
                    }
244
                }
245
            }
246 50f9e1d9 taeseongkim
            catch (System.Exception ex)
247 b0bfc1d3 taeseongkim
            {
248
                Console.WriteLine(ex.ToString());
249
            }
250
251
            return result;
252
        }
253
254 7842224c taeseongkim
        static void ConvertAllProject(string code)
255 d1d8c5ea taeseongkim
        {
256 c7f3eb42 taeseongkim
            string sourcePath = Path.Combine(Path.Combine(Path.Combine(ID2ProjectsPath, code), "drawings"),"Native");
257 d1d8c5ea taeseongkim
            string targetPath = Path.Combine(MarkusImagePath, code);
258
259 c7f3eb42 taeseongkim
            if (!Directory.Exists(targetPath))
260 d1d8c5ea taeseongkim
                Directory.CreateDirectory(targetPath);
261
262
            using (MarkusRepository rep = new MarkusRepository(MarkusConnectionStr))
263
            {
264
                var convertItems = rep.GetConvertDoc(0, code);
265
266
                foreach (var item in convertItems)
267
                {
268 7459b663 taeseongkim
                    string dwgFile = null;
269
270 d1d8c5ea taeseongkim
                    try
271
                    {
272 c7f3eb42 taeseongkim
                        var orgFile = Path.Combine(sourcePath, item.DOCUMENT_ID + ".dwg");
273 7459b663 taeseongkim
                        dwgFile = System.IO.Path.GetTempFileName().Replace(".tmp",".dwg");
274 d1d8c5ea taeseongkim
275 b0bfc1d3 taeseongkim
276 c7f3eb42 taeseongkim
                        File.Copy(orgFile, dwgFile,true);
277 d1d8c5ea taeseongkim
278 c7f3eb42 taeseongkim
                        using (Database pDb = new Database(false, true))
279
                        {
280
                            pDb.ReadDwgFile(dwgFile, FileShare.Read, true, "");
281
                            HostApplicationServices.WorkingDatabase = pDb;
282
                            /****************************************************************/
283
                            /* Display the File Version                                     */
284
                            /****************************************************************/
285
                            Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
286
                            /****************************************************************/
287
                            /* Dump the database                                            */
288
                            /****************************************************************/
289
                            DbDumper dumper = new DbDumper();
290
                            
291
                            //dumper.prepareDump(pDb);
292
                            dumper.ExplodeAndPurgeNestedBlocks(pDb);
293 0d8516d2 taeseongkim
                            
294 c7f3eb42 taeseongkim
                            var pngFile = Path.Combine(targetPath, item.DOCUMENT_ID + ".png");
295
                                
296
                            int width = 9600;
297
                            int height = 6787;
298
                            
299 0d8516d2 taeseongkim
                            if (dumper.ExportPNG(pDb, IsOpenGL, pngFile, width, height))
300 d1d8c5ea taeseongkim
                            {
301 c7f3eb42 taeseongkim
                                File.Delete(dwgFile);
302
                                
303
                                var docInfoID = rep.CreateDocInfo(item.ID, 1);
304
305
                                var docPageResult = rep.CreateDocPage(new[]{
306
                                            new DocPage {
307
                                                docinfo_id = docInfoID,
308
                                                page_angle = 0,
309
                                                page_width  = width.ToString(),
310
                                                page_height =  height.ToString(),
311
                                                page_number = 1
312
313
                                            } });
314
315
                                rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 4, 1, 1, null);
316 d1d8c5ea taeseongkim
                                Console.WriteLine($"Ok. {item.DOCUMENT_ID}");
317
                            }
318 c7f3eb42 taeseongkim
                            else
319
                            {
320
                                rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, "DWG Convert Error");
321
                                Console.WriteLine($"Error. {item.DOCUMENT_ID}");
322
                            }
323 d1d8c5ea taeseongkim
                        }
324
                    }
325 50f9e1d9 taeseongkim
                    catch (System.Exception ex)
326 d1d8c5ea taeseongkim
                    {
327
                        Console.WriteLine($"Error. {item.DOCUMENT_ID} {ex.ToString()}");
328 c7f3eb42 taeseongkim
                        rep.UpdateStatusAsync(item.SERVICE_ID, item.ID,50, 1, 1, ex.ToString());
329 d1d8c5ea taeseongkim
                    }
330 7459b663 taeseongkim
                    finally
331
                    {
332 50f9e1d9 taeseongkim
                        
333 7459b663 taeseongkim
                        if(dwgFile != null)
334
                        {
335
                            if(File.Exists(dwgFile))
336
                            {
337
                                File.Delete(dwgFile);
338
                            }
339
                        }
340 50f9e1d9 taeseongkim
341
                        GC.Collect(2);
342
                        GC.Collect(2);
343 7459b663 taeseongkim
                    }
344 d1d8c5ea taeseongkim
                }
345
            }
346
        }
347 7842224c taeseongkim
348 0d8516d2 taeseongkim
        public static bool ConvertDWG(string code, string DocID)
349 7842224c taeseongkim
        {
350
            bool result = false;
351
352
            try
353
            {
354
                string sourcePath = Path.Combine(Path.Combine(Path.Combine(ID2ProjectsPath, code), "drawings"), "Native");
355
                string targetPath = Path.Combine(MarkusImagePath, code);
356
357
                if (!Directory.Exists(targetPath))
358
                    Directory.CreateDirectory(targetPath);
359
360
                using (MarkusRepository rep = new MarkusRepository(MarkusConnectionStr))
361
                {
362 0d8516d2 taeseongkim
                    string SQL = "SELECT * FROM CONVERTER_DOC WHERE DOCUMENT_ID = @DOCUMENT_ID and PROJECT_NO = @PROJECT_NO";
363 7842224c taeseongkim
364
                    var parameters = new DynamicParameters();
365
                    parameters.Add("@PROJECT_NO", code);
366
                    parameters.Add("@DOCUMENT_ID", DocID);
367
368
                    var convertItems = rep.Query<ConvertDoc>(SQL, parameters);
369
370
                    if (convertItems.Count() > 0)
371
                    {
372 0d8516d2 taeseongkim
                        var item = convertItems.First();
373
                        
374
                        result = false;
375
                        string dwgFile = null;
376 50f9e1d9 taeseongkim
                        string recoverPath = null;
377 7842224c taeseongkim
378 0d8516d2 taeseongkim
                        try
379
                        {
380
                            var orgFile = Path.Combine(sourcePath, item.DOCUMENT_ID + ".dwg");
381
                            dwgFile = System.IO.Path.GetTempFileName().Replace(".tmp", ".dwg");
382 50f9e1d9 taeseongkim
                            recoverPath = dwgFile.Replace(".dwg", "Recover.Dwg");
383 7842224c taeseongkim
384 0d8516d2 taeseongkim
                            File.Copy(orgFile, dwgFile, true);
385 50f9e1d9 taeseongkim
                           
386
                            using (AuditInfo auditInfo = new AuditInfo())
387
                            {
388
                                auditInfo.FixErrors = true;
389
                                using (Teigha.Runtime.FileStreamBuf streamBuf = new FileStreamBuf(dwgFile))
390
                                {
391
                                    using (Database rDb = HostApplicationServices.Current.recoverFile(streamBuf, auditInfo, ""))
392
                                    {
393
                                        int count = auditInfo.NumErrors;
394
                                        rDb.SaveAs(recoverPath, rDb.OriginalFileVersion);
395
                                    }
396
                                }
397
                            }
398 7842224c taeseongkim
399 0d8516d2 taeseongkim
                            using (Database pDb = new Database(false, true))
400
                            {
401 50f9e1d9 taeseongkim
                                pDb.ReadDwgFile(recoverPath, FileShare.Read, true, "");
402 0d8516d2 taeseongkim
                                HostApplicationServices.WorkingDatabase = pDb;
403
                                /****************************************************************/
404
                                /* Display the File Version                                     */
405
                                /****************************************************************/
406
                                Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
407
                                /****************************************************************/
408
                                /* Dump the database                                            */
409
                                /****************************************************************/
410
                                DbDumper dumper = new DbDumper();
411
412
                                //dumper.prepareDump(pDb);
413
                                dumper.ExplodeAndPurgeNestedBlocks(pDb);
414
415
                                var pngFile = Path.Combine(targetPath, item.DOCUMENT_ID + ".png");
416
417
                                int width = 9600;
418
                                int height = 6787;
419
420
                                if (dumper.ExportPNG(pDb,IsOpenGL, pngFile, width, height))
421 7842224c taeseongkim
                                {
422 50f9e1d9 taeseongkim
                                    //File.Delete(dwgFile);
423 7842224c taeseongkim
424 0d8516d2 taeseongkim
                                    var docInfoID = rep.CreateDocInfo(item.ID, 1);
425 7842224c taeseongkim
426 0d8516d2 taeseongkim
                                    var docPageResult = rep.CreateDocPage(new[]{
427
                                        new DocPage {
428
                                            docinfo_id = docInfoID,
429
                                            page_angle = 0,
430
                                            page_width  = width.ToString(),
431
                                            page_height =  height.ToString(),
432
                                            page_number = 1
433 7842224c taeseongkim
434 0d8516d2 taeseongkim
                                        } });
435 7842224c taeseongkim
436 0d8516d2 taeseongkim
                                    rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 4, 1, 1, null);
437
                                    Console.WriteLine($"Ok. {item.DOCUMENT_ID}");
438
                                    result = true;
439
                                }
440
                                else
441
                                {
442
                                    rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, "DWG Convert Error");
443
                                    Console.WriteLine($"Error. {item.DOCUMENT_ID}");
444 7842224c taeseongkim
                                }
445
                            }
446 0d8516d2 taeseongkim
                        }
447 50f9e1d9 taeseongkim
                        catch (System.Exception ex)
448 0d8516d2 taeseongkim
                        {
449
                            Console.WriteLine($"Error. {item.DOCUMENT_ID} {ex.ToString()}");
450
                            rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, ex.ToString());
451
                        }
452
                        finally
453
                        {
454
                            if (dwgFile != null)
455 7842224c taeseongkim
                            {
456 0d8516d2 taeseongkim
                                if (File.Exists(dwgFile))
457 7842224c taeseongkim
                                {
458 0d8516d2 taeseongkim
                                    File.Delete(dwgFile);
459 7842224c taeseongkim
                                }
460
                            }
461 50f9e1d9 taeseongkim
462
                            if(recoverPath != null)
463
                            {
464
                                if (File.Exists(recoverPath))
465
                                {
466
                                    File.Delete(recoverPath);
467
                                }
468
                            }
469 7842224c taeseongkim
                        }
470
                    }
471
                }
472
            }
473 50f9e1d9 taeseongkim
            catch (System.Exception ex)
474 7842224c taeseongkim
            {
475
                Console.WriteLine("Error" + ex.ToString());
476
            }
477
478
            return result;
479
        }
480 d1d8c5ea taeseongkim
    }
481
}
클립보드 이미지 추가 (최대 크기: 500 MB)