프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / MarkusImageCreate / Program.cs @ 98fdb329

이력 | 보기 | 이력해설 | 다운로드 (18.8 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

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

    
27
        static string MarkusConnectionStr;
28
        static string ID2ConnectionStr;
29
        static string ID2ProjectsPath;
30
        static string MarkusImagePath;
31
        static int Resolution;
32
        static string FontPath;
33
        static bool IsOpenGL = false;
34

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

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

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

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

    
56
                }
57

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

    
60
                using (Teigha.Runtime.Services srv = new Teigha.Runtime.Services())
61
                {
62
                    /**********************************************************************/
63
                    /* Display the Product and Version that created the executable        */
64
                    /**********************************************************************/
65
                    Console.WriteLine("\nReadExMgd developed using {0} ver {1}", HostApplicationServices.Current.Product, HostApplicationServices.Current.VersionString);
66

    
67
                    Salaros.Configuration.ConfigParser config;
68

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

    
80
                    ID2ConnectionStr = config.GetValue(CONST_SETTINGS, CONST_ID2Manager_Connect);
81
                    MarkusConnectionStr = config.GetValue(CONST_SETTINGS, CONST_Markus_Connect);
82
                    //ID2ConnectionStr = Encoding.Unicode.GetString(Convert.FromBase64String(config.GetValue(CONST_SETTINGS, CONST_ID2Manager_Connect)));
83
                    //MarkusConnectionStr = Encoding.Unicode.GetString(Convert.FromBase64String(config.GetValue(CONST_SETTINGS, CONST_Markus_Connect)));
84
                    ID2ProjectsPath = config.GetValue(CONST_SETTINGS, CONST_ID2ProjectsPath);
85
                    MarkusImagePath = config.GetValue(CONST_SETTINGS, CONST_MarkusImagePath);
86
                    Resolution = config.GetValue(CONST_SETTINGS, CONST_Resolution, 250);
87
                    FontPath = config.GetValue(CONST_SETTINGS, CONST_FontPath);
88
                    IsOpenGL = config.GetValue(CONST_SETTINGS,CONST_IsOpenGL,false);
89

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

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

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

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

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

    
117
                            Console.Write("Team Code : ");
118
                            ProjectCode = Console.ReadLine();
119

    
120
                            Console.Write("DWG ID : ");
121
                            DocID = Console.ReadLine();
122

    
123
                            ConvertDWG(ProjectCode, DocID);
124

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

    
136
                            ConvertDWG(ProjectCode, DocID);
137
                        }
138
                    }
139
                    else if (args[0].Contains(".dwg"))
140
                    {
141
                        Show(args[0]);
142
                    }
143
                }
144
            }
145
            /********************************************************************/
146
            /* Display the error                                                */
147
            /********************************************************************/
148
            catch (System.Exception e)
149
            {
150
                bSuccess = false;
151
                Console.WriteLine("Teigha?NET for .dwg files Error: " + e.Message);
152
            }
153

    
154
            if (bSuccess)
155
                Console.WriteLine("OdReadExMgd Finished Successfully");
156

    
157
            if (cmd == "DEFAULT")
158
            {
159
                Console.WriteLine("완료");
160
                Console.ReadKey();
161
            }
162
        }
163

    
164
        static string[] ProjectCodes()
165
        {
166
            using (ProjectRepository rep = new ProjectRepository(ID2ConnectionStr))
167
            {
168
                var projects = rep.GetAllProjectList();
169

    
170
                return projects.Select(x => x.Code).ToArray();
171
            }
172
        }
173

    
174
        static void Show(string dwgFile)
175
        {
176
            using (Database pDb = new Database(false, true))
177
            {
178
                pDb.ReadDwgFile(dwgFile, FileShare.Read, true, "");
179
                HostApplicationServices.WorkingDatabase = pDb;
180
                /****************************************************************/
181
                /* Display the File Version                                     */
182
                /****************************************************************/
183
                Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
184
                /****************************************************************/
185
                /* Dump the database                                            */
186
                /****************************************************************/
187
                DbDumper dumper = new DbDumper();
188

    
189
                //dumper.prepareDump(pDb);
190
                dumper.ExplodeAndPurgeNestedBlocks(pDb);
191

    
192
                int width = 9600;
193
                int height = 6787;
194

    
195
                dumper.Show(pDb, width, height);
196
            }
197
        }
198

    
199
        static void InitConvertPDF()
200
        {
201
            foreach (var code in ProjectCodes())
202
            {
203
                ConvertAllProject(code);
204
            }
205
        }
206

    
207
        static bool IsCredential(string Message, string path)
208
        {
209
            bool result = false;
210

    
211
            try
212
            {
213
                Credential credential = new Credential
214
                {
215
                    Type = CredentialType.DomainPassword,
216
                    PersistanceType = PersistanceType.LocalComputer,
217
                    Target = path,
218
                };
219

    
220
                result = credential.Load();
221

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

    
227
                    var dialogResult = prompt.ShowDialog();
228

    
229
                    if (dialogResult == DialogResult.OK)
230
                    {
231
                        Credential genericCredential = new Credential
232
                        {
233
                            Target = path,
234
                            Username = prompt.Username,
235
                            Password = prompt.Password,
236
                            Type = CredentialType.DomainPassword,
237
                            PersistanceType = PersistanceType.LocalComputer
238
                        };
239

    
240
                        genericCredential.Save();
241
                        result = true;
242
                    }
243
                }
244
            }
245
            catch (Exception ex)
246
            {
247
                Console.WriteLine(ex.ToString());
248
            }
249

    
250
            return result;
251
        }
252

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

    
258
            if (!Directory.Exists(targetPath))
259
                Directory.CreateDirectory(targetPath);
260

    
261
            using (MarkusRepository rep = new MarkusRepository(MarkusConnectionStr))
262
            {
263
                var convertItems = rep.GetConvertDoc(0, code);
264

    
265
                foreach (var item in convertItems)
266
                {
267
                    string dwgFile = null;
268

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

    
274

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

    
277
                        using (Database pDb = new Database(false, true))
278
                        {
279
                            pDb.ReadDwgFile(dwgFile, FileShare.Read, true, "");
280
                            HostApplicationServices.WorkingDatabase = pDb;
281
                            /****************************************************************/
282
                            /* Display the File Version                                     */
283
                            /****************************************************************/
284
                            Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
285
                            /****************************************************************/
286
                            /* Dump the database                                            */
287
                            /****************************************************************/
288
                            DbDumper dumper = new DbDumper();
289
                            
290
                            //dumper.prepareDump(pDb);
291
                            dumper.ExplodeAndPurgeNestedBlocks(pDb);
292
                            
293
                            var pngFile = Path.Combine(targetPath, item.DOCUMENT_ID + ".png");
294
                                
295
                            int width = 9600;
296
                            int height = 6787;
297
                            
298
                            if (dumper.ExportPNG(pDb, IsOpenGL, pngFile, width, height))
299
                            {
300
                                File.Delete(dwgFile);
301
                                
302
                                var docInfoID = rep.CreateDocInfo(item.ID, 1);
303

    
304
                                var docPageResult = rep.CreateDocPage(new[]{
305
                                            new DocPage {
306
                                                docinfo_id = docInfoID,
307
                                                page_angle = 0,
308
                                                page_width  = width.ToString(),
309
                                                page_height =  height.ToString(),
310
                                                page_number = 1
311

    
312
                                            } });
313

    
314
                                rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 4, 1, 1, null);
315
                                Console.WriteLine($"Ok. {item.DOCUMENT_ID}");
316
                            }
317
                            else
318
                            {
319
                                rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, "DWG Convert Error");
320
                                Console.WriteLine($"Error. {item.DOCUMENT_ID}");
321
                            }
322
                        }
323
                    }
324
                    catch (Exception ex)
325
                    {
326
                        Console.WriteLine($"Error. {item.DOCUMENT_ID} {ex.ToString()}");
327
                        rep.UpdateStatusAsync(item.SERVICE_ID, item.ID,50, 1, 1, ex.ToString());
328
                    }
329
                    finally
330
                    {
331
                        if(dwgFile != null)
332
                        {
333
                            if(File.Exists(dwgFile))
334
                            {
335
                                File.Delete(dwgFile);
336
                            }
337
                        }
338
                    }
339
                }
340
            }
341
        }
342

    
343
        public static bool ConvertDWG(string code, string DocID)
344
        {
345
            bool result = false;
346

    
347
            try
348
            {
349
                string sourcePath = Path.Combine(Path.Combine(Path.Combine(ID2ProjectsPath, code), "drawings"), "Native");
350
                string targetPath = Path.Combine(MarkusImagePath, code);
351

    
352
                if (!Directory.Exists(targetPath))
353
                    Directory.CreateDirectory(targetPath);
354

    
355
                using (MarkusRepository rep = new MarkusRepository(MarkusConnectionStr))
356
                {
357
                    string SQL = "SELECT * FROM CONVERTER_DOC WHERE DOCUMENT_ID = @DOCUMENT_ID and PROJECT_NO = @PROJECT_NO";
358

    
359
                    var parameters = new DynamicParameters();
360
                    parameters.Add("@PROJECT_NO", code);
361
                    parameters.Add("@DOCUMENT_ID", DocID);
362

    
363
                    var convertItems = rep.Query<ConvertDoc>(SQL, parameters);
364

    
365
                    if (convertItems.Count() > 0)
366
                    {
367
                        var item = convertItems.First();
368
                        
369
                        result = false;
370
                        string dwgFile = null;
371

    
372
                        try
373
                        {
374
                            var orgFile = Path.Combine(sourcePath, item.DOCUMENT_ID + ".dwg");
375
                            dwgFile = System.IO.Path.GetTempFileName().Replace(".tmp", ".dwg");
376

    
377

    
378
                            File.Copy(orgFile, dwgFile, true);
379

    
380
                            using (Database pDb = new Database(false, true))
381
                            {
382
                                pDb.ReadDwgFile(dwgFile, FileShare.Read, true, "");
383
                                HostApplicationServices.WorkingDatabase = pDb;
384
                                /****************************************************************/
385
                                /* Display the File Version                                     */
386
                                /****************************************************************/
387
                                Console.WriteLine("File Version: {0}", pDb.OriginalFileVersion);
388
                                /****************************************************************/
389
                                /* Dump the database                                            */
390
                                /****************************************************************/
391
                                DbDumper dumper = new DbDumper();
392

    
393
                                //dumper.prepareDump(pDb);
394
                                dumper.ExplodeAndPurgeNestedBlocks(pDb);
395

    
396
                                var pngFile = Path.Combine(targetPath, item.DOCUMENT_ID + ".png");
397

    
398
                                int width = 9600;
399
                                int height = 6787;
400

    
401
                                if (dumper.ExportPNG(pDb,IsOpenGL, pngFile, width, height))
402
                                {
403
                                    File.Delete(dwgFile);
404

    
405
                                    var docInfoID = rep.CreateDocInfo(item.ID, 1);
406

    
407
                                    var docPageResult = rep.CreateDocPage(new[]{
408
                                        new DocPage {
409
                                            docinfo_id = docInfoID,
410
                                            page_angle = 0,
411
                                            page_width  = width.ToString(),
412
                                            page_height =  height.ToString(),
413
                                            page_number = 1
414

    
415
                                        } });
416

    
417
                                    rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 4, 1, 1, null);
418
                                    Console.WriteLine($"Ok. {item.DOCUMENT_ID}");
419
                                    result = true;
420
                                }
421
                                else
422
                                {
423
                                    rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, "DWG Convert Error");
424
                                    Console.WriteLine($"Error. {item.DOCUMENT_ID}");
425
                                }
426
                            }
427
                        }
428
                        catch (Exception ex)
429
                        {
430
                            Console.WriteLine($"Error. {item.DOCUMENT_ID} {ex.ToString()}");
431
                            rep.UpdateStatusAsync(item.SERVICE_ID, item.ID, 50, 1, 1, ex.ToString());
432
                        }
433
                        finally
434
                        {
435
                            if (dwgFile != null)
436
                            {
437
                                if (File.Exists(dwgFile))
438
                                {
439
                                    File.Delete(dwgFile);
440
                                }
441
                            }
442
                        }
443
                    }
444
                }
445
            }
446
            catch (Exception ex)
447
            {
448
                Console.WriteLine("Error" + ex.ToString());
449
            }
450

    
451
            return result;
452
        }
453
    }
454
}
클립보드 이미지 추가 (최대 크기: 500 MB)