프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 88cb9898

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

1
#region namespaces
2
using System;
3
using System.Collections.Generic;
4
using System.Configuration;
5
using System.Data;
6
using System.Data.Common;
7
using System.Data.SqlClient;
8
using System.IO;
9
using System.Linq;
10
using System.Runtime.InteropServices;
11
using System.Text;
12
using System.Threading;
13
using System.Windows.Forms;
14
using Autodesk.AutoCAD.ApplicationServices;
15
using Autodesk.AutoCAD.DatabaseServices;
16
using Autodesk.AutoCAD.EditorInput;
17
using Autodesk.AutoCAD.Geometry;
18
using Autodesk.AutoCAD.Interop;
19
using Autodesk.AutoCAD.Interop.Common;
20
using Autodesk.AutoCAD.Runtime;
21
using Autodesk.AutoCAD.Windows;
22
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
23
using AVEVA.PID.CustomizationUtility.DB;
24
using AVEVA.PID.CustomizationUtility.Model;
25
using AVEVA.PID.CustomizationUtility.Properties;
26

    
27
#endregion using namespaces
28
namespace AVEVA.PID.CustomizationUtility
29
{
30
    /// <summary>
31
    /// Singleton class used for customization of P&ID application
32
    /// </summary>
33
    public class PIDCustomization
34
    {
35
        private DevExpress.LookAndFeel.DefaultLookAndFeel defaultLookAndFeel = new DevExpress.LookAndFeel.DefaultLookAndFeel();
36

    
37
        public string ProjectName   { get; set; }
38
        public string DrawingID     { get; set; }
39
        public string DrawingName   { get; set; }
40
        public string MajorRevision { get; set; }
41
        public string MinorRevision { get; set; }
42
        public string Description   { get; set; }
43
        public string NameOfUser    { get; set; }
44
        public string ClientDwgNo   { get; set; }
45
        public string RevisedBy     { get; set; }
46
        public string CheckedBy     { get; set; }
47
        public string ApprovedBy    { get; set; }
48
        public string ApprovedBy2   { get; set; }
49
        public string ApprovedBy3   { get; set; }
50
        public string ClientApprovedBy { get; set; }
51
        public byte[] DrawingData   { get; set; }
52

    
53
        private static PIDCustomization _instance;
54
        /// <summary>
55
        /// private Constructor make it singleton
56
        /// </summary>
57
        private PIDCustomization()
58
        {
59
            DrawingData = null;
60
        }
61

    
62
        public static PIDCustomization GetInstance()
63
        {
64
            if (_instance == null)
65
            {
66
                _instance = new PIDCustomization();
67
                _instance.defaultLookAndFeel.LookAndFeel.SkinName = "Office 2016 Colorful";
68
            }
69
            return _instance;           
70
        }
71

    
72
        /// <summary>
73
        /// Public method called on issue drawing in P&ID
74
        /// </summary>
75
        /// <param name="strRecordId">unique database table id for revision</param>
76
        public void ProcessIssueDrawing(string strRecordId)
77
        {            
78
            if (!string.IsNullOrEmpty(strRecordId))
79
            {
80
                ReadDrawingDetails(strRecordId);
81
                SaveBinaryDrawing();
82
            }
83
        }
84

    
85
        /// <summary>
86
        /// public method used for processing block
87
        /// </summary>
88
        /// <param name="BlockId"></param>
89
        public void ProcessBlock(ObjectId BlockId)
90
        {
91
            SetColorForEntity(BlockId,0);// white color
92
        }
93

    
94
        public static void GetProjectInfo()
95
        {
96
            Project_Info _ProjectInfo = Project_Info.GetInstance();
97
            _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
98
            _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
99
            _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
100
            _ProjectInfo.Port = Settings.Default.ProjectPort;
101
            _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
102
            _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
103

    
104
            if (Project_DB.ConnTestAndCreateTable())
105
            {
106
                _ProjectInfo.Enable = true;
107
            }
108
            else
109
            {
110
                _ProjectInfo.Enable = false;
111
                MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
112
            }
113
        }
114
        #region Command
115
        /// <summary>
116
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
117
        /// </summary>
118
        [CommandMethod("APIDConverter")]
119
        public static void APIDConverter()
120
        {
121
            GetInstance();
122
            ConverterRibbonUI.InitUI();
123
            GetProjectInfo();
124
            APIDUtils.SetAvevaExplorer();
125
        }
126
        [CommandMethod("AC")]
127
        public static void AC()
128
        {
129
            GetInstance();
130
            ConverterRibbonUI.InitUI();
131
            GetProjectInfo();
132
            APIDUtils.SetAvevaExplorer();
133
        }
134
        [CommandMethod("ConverterForm")]
135
        public static void ConverterForm()
136
        {
137
            APIDConverter form = new APIDConverter();
138
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
139
            {
140

    
141
            }
142
        }
143
        [CommandMethod("ProjectForm")]
144
        public static void ProjectForm()
145
        {
146
            ProjectForm form = new ProjectForm();
147
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
148
            {
149
                Project_Info _ProjectInfo = Project_Info.GetInstance();
150
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
151
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
152
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
153
                _ProjectInfo.Port = Settings.Default.ProjectPort;
154
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
155
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
156

    
157
                if (Project_DB.ConnTestAndCreateTable())
158
                {
159
                    _ProjectInfo.Enable = true;
160
                    MessageBox.Show("Success project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
161
                }
162
                else
163
                {
164
                    _ProjectInfo.Enable = false;
165
                    MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
166
                }
167
            }
168
        }
169
        [CommandMethod("MappingForm")]
170
        public static void MappingForm()
171
        {
172
            MappingForm form = new MappingForm();
173
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
174
            {
175

    
176
            }
177
        }
178
        #endregion
179
        #region private methods
180
        private string SaveBinaryDrawing()
181
        {
182
            FileStream fs = null;
183
            string strDrawing = string.Empty;
184
            try
185
            {
186
                if (!string.IsNullOrEmpty(this.DrawingName))
187
                {
188
                    string strDrawingname = this.DrawingName;
189
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
190
                    //{
191
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
192
                    //}
193

    
194
                    byte[] drawingData = this.DrawingData;
195
                    if (null != drawingData)
196
                    {
197
                        try
198
                        {                            
199
                            if (File.Exists(strDrawingname) == true)
200
                            {
201
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
202
                            }
203
                        }
204
                        catch (System.Exception )
205
                        {
206
                        }
207

    
208
                        strDrawing = strDrawingname;
209
                        string path = GetDrawingSavePath();                        
210
                        if (!string.IsNullOrEmpty(path))
211
                        {
212
                            strDrawing = path + "\\" + strDrawingname;
213
                        }
214
                        strDrawing += ".dwg";
215

    
216
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
217
                        int ArraySize = new int();
218
                        ArraySize = drawingData.GetUpperBound(0);
219
                        fs.Write(drawingData, 0, ArraySize);
220
                    }
221
                }
222
            }
223
            catch (System.Exception )
224
            { }
225
            finally
226
            {
227
                if (fs != null)
228
                    fs.Close();
229
            }
230
            return strDrawing;
231
        }
232

    
233
        private void ReadDrawingDetails(string strRecordId)
234
        {
235
            SqlConnection connection = null;
236
            try
237
            {
238
                int ID = 0;
239
                int.TryParse(strRecordId, out ID);
240
                if (ID > 0)
241
                {
242
                    connection = new SqlConnection();
243
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
244
                    connection.Open();
245

    
246
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
247
                    SqlCommand cmd = new SqlCommand();
248
                    cmd.Connection = connection;
249
                    cmd.CommandText = strQry;
250
                    cmd.CommandType = CommandType.Text;
251
                    SqlDataReader reader = cmd.ExecuteReader();
252

    
253
                    if (null != reader && reader.HasRows)
254
                    {
255
                        while (reader.Read())
256
                        {
257
                            ProjectName     = reader["ProjectName"].ToString();
258
                            DrawingID       = reader["DrawingId"].ToString();
259
                            DrawingName     = reader["DrawingName"].ToString();
260
                            MajorRevision   = reader["Revision"].ToString();
261
                            MinorRevision   = reader["MinorRevision"].ToString();
262
                            Description     = reader["Description"].ToString();
263
                            NameOfUser      = reader["NameOfUser"].ToString();
264
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
265
                            RevisedBy       = reader["RevisedBy"].ToString();
266
                            CheckedBy       = reader["CheckedBy"].ToString();
267
                            ApprovedBy      = reader["ApprovedBy"].ToString();
268
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
269
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
270
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
271
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
272
                        }
273
                    }
274
                }
275
            }
276
            catch (System.Exception )
277
            { }
278
            finally
279
            {
280
                if (connection != null)
281
                    connection.Close();
282
            }
283
        }
284
        private string GetRecordId()
285
        {
286
            string strRecordId = string.Empty;
287
            SqlConnection connection = null;
288
            try
289
            {
290
                connection = new SqlConnection();
291
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
292
                connection.Open();
293

    
294
                string strQry = "Select Max(ID) from SynchroniseDetails";
295
                SqlCommand cmd = new SqlCommand();
296
                cmd.Connection = connection;
297
                cmd.CommandText = strQry;
298
                cmd.CommandType = CommandType.Text;
299
                SqlDataReader reader = cmd.ExecuteReader();
300
                if (null != reader && reader.HasRows)
301
                {
302
                    strRecordId = reader.GetString(0).ToString();
303
                }
304
            }
305
            catch (System.Exception )
306
            { }
307
            finally
308
            {
309
                if (connection != null)
310
                    connection.Close();
311
            }
312
            return strRecordId;
313
        }
314
        private string GetConnectionString()
315
        {
316
            string strConn = string.Empty;            
317
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
318
            {
319
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
320
            }
321
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
322
            {
323
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
324
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
325
            }           
326

    
327
            return strConn;            
328
        }
329

    
330
        private string GetConnectionString_Reports()
331
        {
332
            string strConn = string.Empty;
333
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
334
            {
335
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
336
            }
337
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
338
            {
339
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
340
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
341
            }
342

    
343
            return strConn;
344
        }
345

    
346
        private string GetConnectionString_Admin()
347
        {
348
            string strConn = string.Empty;
349
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
350
            {
351
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
352
            }
353
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
354
            {
355
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
356
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
357
            }
358

    
359
            return strConn;
360
        }
361

    
362
        private string GetDrawingSavePath()
363
        {
364
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
365
        }
366

    
367
        /// <summary>
368
        /// Sets the color of the entity
369
        /// </summary>
370
        /// <param name="objIdBlockId">block id</param>
371
        /// <param name="iColor">color to be assigned</param>
372
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
373
        {
374
            Transaction trans = null;
375
            try
376
            {
377
                if (objIdBlockId != ObjectId.Null)
378
                {
379
                    Autodesk.AutoCAD.ApplicationServices.Document doc = AcadApp.DocumentManager.MdiActiveDocument;
380
                    Database db = HostApplicationServices.WorkingDatabase;
381
                    trans = doc.TransactionManager.StartTransaction();
382
                    using (doc.LockDocument())
383
                    {
384
                        using (trans)
385
                        {
386
                            if (objIdBlockId.IsErased == false)
387
                            {
388
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
389
                                if (objDb != null)
390
                                {
391
                                    objDb.ColorIndex = iColor;                                    
392
                                }
393
                            }
394
                            trans.Commit();
395
                        }
396
                    }
397
                }
398
            }
399
            catch (System.Exception )
400
            {       
401
            }
402
            finally
403
            {
404
                if (trans != null)
405
                {
406
                    trans.Dispose();
407
                }
408
            }
409
        }
410
        #endregion
411

    
412

    
413

    
414
        #region TEST
415
        [CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
416
        public static void TestSelection()
417
        {
418
            // Get the current document and database, and start a transaction
419
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
420
            Database acCurDb = acDoc.Database;
421
            Editor acDocEd = acDoc.Editor;
422

    
423
            // Request for objects to be selected in the drawing area
424
            PromptSelectionResult acSSPrompt = acDocEd.GetSelection();
425

    
426
            // If the prompt status is OK, objects were selected
427
            if (acSSPrompt.Status == PromptStatus.OK)
428
            {
429
                ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds();
430
                // Get the last selected entity
431

    
432
                if (selectedObjectIds.Length > 0)
433
                {
434
                    ObjectId objIdBlock = selectedObjectIds[0];
435
                    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
436
                    {
437
                        DBObject objDB = acTrans.GetObject(objIdBlock, OpenMode.ForRead, true);
438
                        if (objDB != null)
439
                        {
440
                            // check if the selected entity is of Type BlockReference
441
                            if (objDB.GetType() == typeof(BlockReference))
442
                            {
443
                                BlockReference block = objDB as BlockReference;
444

    
445
                                // open an entity in write mode so that we can modify its color
446
                                //Entity objDb = (Entity)acTrans.GetObject(objIdBlock, OpenMode.ForWrite);
447
                                //if (objDb != null)
448
                                //{
449
                                //    objDb.ColorIndex = 0;// white color
450
                                //}
451
                            }
452
                        }
453
                        acTrans.Commit();
454
                    }
455
                }
456
            }
457
        }
458

    
459

    
460
        #endregion
461
    }
462
}
클립보드 이미지 추가 (최대 크기: 500 MB)