프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / MarkusImageCreate / Program.cs @ 1620ca9c

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

1
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
using Teigha.DatabaseServices;
10
using OdReadExMgd;
11
using CredentialManagement;
12
using Dapper;
13
using Teigha.Runtime;
14

    
15
namespace MarkusImageCreate
16
{
17
    public class Program
18
    {
19
        const string CONST_SETTINGS = "SETTINGS";
20
        const string CONST_ID2Manager_Connect = "ID2Manager";
21
        const string CONST_Markus_Connect = "MARKUS";
22
        const string CONST_ID2ProjectsPath = "ID2ProjectsPath";
23
        const string CONST_MarkusImagePath = "MarkusImagePath";
24
        const string CONST_Resolution = "Resolution";
25
        const string CONST_FontPath  = "FontPath";
26
        const string CONST_IsOpenGL = "IsOpenGL";
27

    
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
        static bool IsOpenGL = false;
35

    
36
        public static void Main(string[] args)
37
        {
38

    
39
            bool bSuccess = true;
40
            string cmd = "DEFAULT";
41

    
42
            if(args.Length > 0)
43
            {
44
                cmd = args[0];
45
            }
46

    
47
            try
48
            {
49
                try
50
                {
51
                    Teigha.Runtime.Services.odActivate(ActivationData.userInfo, ActivationData.userSignature);
52
                }
53
                catch (System.Exception ex)
54
                {
55
                    Console.WriteLine(ex.ToString());
56

    
57
                }
58

    
59
                Console.WriteLine("Activate Teigha");
60

    
61
                using (Teigha.Runtime.Services srv = new Teigha.Runtime.Services())
62
                {
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
                    Salaros.Configuration.ConfigParser config;
69

    
70
                    try
71
                    {
72
                        config = new Salaros.Configuration.ConfigParser(Path.Combine(AppContext.BaseDirectory, "MarkusImageCreate.ini"));
73
                    }
74
                    catch (System.Exception)
75
                    {
76
                        Console.WriteLine($"Config File Path Not Found : {Path.Combine(AppContext.BaseDirectory, "MarkusImageCreate.ini")}");
77
                        Console.ReadKey();
78
                        throw;
79
                    }
80

    
81
                    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
                    IsOpenGL = config.GetValue(CONST_SETTINGS,CONST_IsOpenGL,false);
90

    
91
                    while (!Directory.Exists(ID2ProjectsPath))
92
                    {
93
                        string sourceComputer = ID2ProjectsPath.Split('\\').FirstOrDefault(x => !string.IsNullOrEmpty(x));
94
                        IsCredential($"\\\\{sourceComputer}에 대한 인증이 필요합니다.", sourceComputer);
95
                    }
96

    
97
                    while (!Directory.Exists(MarkusImagePath))
98
                    {
99
                        string sourceComputer = MarkusImagePath.Split('\\').FirstOrDefault(x => !string.IsNullOrEmpty(x));
100
                        IsCredential($"\\\\{sourceComputer}에 대한 인증이 필요합니다.", sourceComputer);
101
                    }
102

    
103
                    if (cmd == "DEFAULT")
104
                    {
105
                        InitConvertPDF();
106
                    }
107
                    else if (cmd == "INPUT")
108
                    {
109
                        bool isBreak = false;
110

    
111
                        while (!isBreak)
112
                        {
113
                            string ProjectCode = null;
114
                            string DocID = null;
115

    
116
                            Console.WriteLine("# 생성이 필요한 DWG ID 입력 #");
117

    
118
                            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
                            Console.WriteLine("계속 진행은 아무키나 입력,종료는 CTRL+BREAK");
127
                            Console.ReadKey();
128
                        }
129
                    }
130
                    else if (args[0] == "RUN")
131
                    {
132
                        if (args.Count() == 3)
133
                        {
134
                            string ProjectCode = args[1];
135
                            string DocID = args[2];
136

    
137
                            ConvertDWG(ProjectCode, DocID);
138
                        }
139
                    }
140
                    else if (args[0].Contains(".dwg"))
141
                    {
142
                        Show(args[0]);
143
                    }
144
                }
145
            }
146
            /********************************************************************/
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

    
158
            if (cmd == "DEFAULT")
159
            {
160
                Console.WriteLine("완료");
161
                Console.ReadKey();
162
            }
163
        }
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
        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
        static void InitConvertPDF()
201
        {
202
            foreach (var code in ProjectCodes())
203
            {
204
                ConvertAllProject(code);
205
            }
206
        }
207

    
208
        static bool IsCredential(string Message, string path)
209
        {
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
                result = credential.Load();
222

    
223
                if (!result)
224
                {
225
                    VistaPrompt prompt = new VistaPrompt();
226
                    prompt.Title = $"{Message}";
227

    
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
            catch (System.Exception ex)
247
            {
248
                Console.WriteLine(ex.ToString());
249
            }
250

    
251
            return result;
252
        }
253

    
254
        static void ConvertAllProject(string code)
255
        {
256
            string sourcePath = Path.Combine(Path.Combine(Path.Combine(ID2ProjectsPath, code), "drawings"),"Native");
257
            string targetPath = Path.Combine(MarkusImagePath, code);
258

    
259
            if (!Directory.Exists(targetPath))
260
                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
                    string dwgFile = null;
269

    
270
                    try
271
                    {
272
                        var orgFile = Path.Combine(sourcePath, item.DOCUMENT_ID + ".dwg");
273
                        dwgFile = System.IO.Path.GetTempFileName().Replace(".tmp",".dwg");
274

    
275

    
276
                        File.Copy(orgFile, dwgFile,true);
277

    
278
                        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
                            
294
                            var pngFile = Path.Combine(targetPath, item.DOCUMENT_ID + ".png");
295
                                
296
                            int width = 9600;
297
                            int height = 6787;
298
                            
299
                            if (dumper.ExportPNG(pDb, IsOpenGL, pngFile, width, height))
300
                            {
301
                                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
                                Console.WriteLine($"Ok. {item.DOCUMENT_ID}");
317
                            }
318
                            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
                        }
324
                    }
325
                    catch (System.Exception ex)
326
                    {
327
                        Console.WriteLine($"Error. {item.DOCUMENT_ID} {ex.ToString()}");
328
                        rep.UpdateStatusAsync(item.SERVICE_ID, item.ID,50, 1, 1, ex.ToString());
329
                    }
330
                    finally
331
                    {
332
                        
333
                        if(dwgFile != null)
334
                        {
335
                            if(File.Exists(dwgFile))
336
                            {
337
                                File.Delete(dwgFile);
338
                            }
339
                        }
340

    
341
                        GC.Collect(2);
342
                        GC.Collect(2);
343
                    }
344
                }
345
            }
346
        }
347

    
348
        public static bool ConvertDWG(string code, string DocID)
349
        {
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
                    string SQL = "SELECT * FROM CONVERTER_DOC WHERE DOCUMENT_ID = @DOCUMENT_ID and PROJECT_NO = @PROJECT_NO";
363

    
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
                        var item = convertItems.First();
373
                        
374
                        result = false;
375
                        string dwgFile = null;
376
                        string recoverPath = null;
377

    
378
                        try
379
                        {
380
                            var orgFile = Path.Combine(sourcePath, item.DOCUMENT_ID + ".dwg");
381
                            dwgFile = System.IO.Path.GetTempFileName().Replace(".tmp", ".dwg");
382
                            recoverPath = dwgFile.Replace(".dwg", "Recover.Dwg");
383

    
384
                            File.Copy(orgFile, dwgFile, true);
385
                           
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

    
399
                            using (Database pDb = new Database(false, true))
400
                            {
401
                                pDb.ReadDwgFile(recoverPath, FileShare.Read, true, "");
402
                                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
                                {
422
                                    //File.Delete(dwgFile);
423

    
424
                                    var docInfoID = rep.CreateDocInfo(item.ID, 1);
425

    
426
                                    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

    
434
                                        } });
435

    
436
                                    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
                                }
445
                            }
446
                        }
447
                        catch (System.Exception ex)
448
                        {
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
                            {
456
                                if (File.Exists(dwgFile))
457
                                {
458
                                    File.Delete(dwgFile);
459
                                }
460
                            }
461

    
462
                            if(recoverPath != null)
463
                            {
464
                                if (File.Exists(recoverPath))
465
                                {
466
                                    File.Delete(recoverPath);
467
                                }
468
                            }
469
                        }
470
                    }
471
                }
472
            }
473
            catch (System.Exception ex)
474
            {
475
                Console.WriteLine("Error" + ex.ToString());
476
            }
477

    
478
            return result;
479
        }
480
    }
481
}
클립보드 이미지 추가 (최대 크기: 500 MB)