프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 20dd244c

이력 | 보기 | 이력해설 | 다운로드 (17.2 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
        public string ProjectName   { get; set; }
36
        public string DrawingID     { get; set; }
37
        public string DrawingName   { get; set; }
38
        public string MajorRevision { get; set; }
39
        public string MinorRevision { get; set; }
40
        public string Description   { get; set; }
41
        public string NameOfUser    { get; set; }
42
        public string ClientDwgNo   { get; set; }
43
        public string RevisedBy     { get; set; }
44
        public string CheckedBy     { get; set; }
45
        public string ApprovedBy    { get; set; }
46
        public string ApprovedBy2   { get; set; }
47
        public string ApprovedBy3   { get; set; }
48
        public string ClientApprovedBy { get; set; }
49
        public byte[] DrawingData   { get; set; }
50

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

    
60
        public static PIDCustomization GetInstance()
61
        {
62
            if (_instance == null)
63
            {
64
                _instance = new PIDCustomization();
65
            }
66
            return _instance;           
67
        }
68

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

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

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

    
101
            if (Project_DB.ConnTestAndCreateTable())
102
            {
103
                _ProjectInfo.Enable = true;
104
            }
105
            else
106
            {
107
                _ProjectInfo.Enable = false;
108
                MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
109
            }
110
        }
111
        #region Command
112
        /// <summary>
113
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
114
        /// </summary>
115
        [CommandMethod("APIDConverter")]
116
        public static void APIDConverter()
117
        {
118
            GetInstance();
119
            GetProjectInfo();
120
            APIDUtils.SetAvevaExplorer();
121
        }
122
        [CommandMethod("AC")]
123
        public static void AC()
124
        {
125
            GetInstance();
126
            GetProjectInfo();
127
            APIDUtils.SetAvevaExplorer();
128
        }
129
        [CommandMethod("APPIDRun")]
130
        public static void APIDRun()
131
        {
132
            if (AutoModeling.Running != null)
133
            {
134
                GUIUtils.SetAutoLabelCheck(false);
135
                AutoModeling.Running.Run();
136
            }
137
        }
138
        [CommandMethod("GIFF")]
139
        public static void GetAttributeInformationFromForm()
140
        {
141
            // Get the current document and database, and start a transaction
142
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
143
            Database acCurDb = acDoc.Database;
144
            Editor acDocEd = acDoc.Editor;
145

    
146
            // Request for objects to be selected in the drawing area
147
            PromptSelectionResult acSSPrompt = acDocEd.GetSelection();
148
            
149
            // If the prompt status is OK, objects were selected
150
            if (acSSPrompt.Status == PromptStatus.OK)
151
            {
152
                ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds();
153

    
154
                if (selectedObjectIds.Length > 0)
155
                {
156
                    ObjectId objIdBlock = selectedObjectIds[0];
157
                    Entity entity = APIDUtils.GetEntityByHandle(objIdBlock.Handle.Value);
158

    
159
                    GUI.ComponentPropertiesHelper componentPropertiesHelper = new GUI.ComponentPropertiesHelper();
160
                    AvevaThread.Run(ThreadType.GetAttributeInformationFromForm, null);
161
                    if (!componentPropertiesHelper.DisplayComponentProperties(entity))
162
                    {
163
                        AvevaThread.Stop(ThreadType.GetAttributeInformationFromForm);
164
                        MessageBox.Show("Fail Save Attribute!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
165
                    }
166
                    else
167
                        MessageBox.Show("Success Save Attribute!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
168
                }
169
            }
170
        }
171
        #endregion
172
        #region private methods
173
        private string SaveBinaryDrawing()
174
        {
175
            FileStream fs = null;
176
            string strDrawing = string.Empty;
177
            try
178
            {
179
                if (!string.IsNullOrEmpty(this.DrawingName))
180
                {
181
                    string strDrawingname = this.DrawingName;
182
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
183
                    //{
184
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
185
                    //}
186

    
187
                    byte[] drawingData = this.DrawingData;
188
                    if (null != drawingData)
189
                    {
190
                        try
191
                        {                            
192
                            if (File.Exists(strDrawingname) == true)
193
                            {
194
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
195
                            }
196
                        }
197
                        catch (System.Exception )
198
                        {
199
                        }
200

    
201
                        strDrawing = strDrawingname;
202
                        string path = GetDrawingSavePath();                        
203
                        if (!string.IsNullOrEmpty(path))
204
                        {
205
                            strDrawing = path + "\\" + strDrawingname;
206
                        }
207
                        strDrawing += ".dwg";
208

    
209
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
210
                        int ArraySize = new int();
211
                        ArraySize = drawingData.GetUpperBound(0);
212
                        fs.Write(drawingData, 0, ArraySize);
213
                    }
214
                }
215
            }
216
            catch (System.Exception )
217
            { }
218
            finally
219
            {
220
                if (fs != null)
221
                    fs.Close();
222
            }
223
            return strDrawing;
224
        }
225

    
226
        private void ReadDrawingDetails(string strRecordId)
227
        {
228
            SqlConnection connection = null;
229
            try
230
            {
231
                int ID = 0;
232
                int.TryParse(strRecordId, out ID);
233
                if (ID > 0)
234
                {
235
                    connection = new SqlConnection();
236
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
237
                    connection.Open();
238

    
239
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
240
                    SqlCommand cmd = new SqlCommand();
241
                    cmd.Connection = connection;
242
                    cmd.CommandText = strQry;
243
                    cmd.CommandType = CommandType.Text;
244
                    SqlDataReader reader = cmd.ExecuteReader();
245

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

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

    
320
            return strConn;            
321
        }
322

    
323
        private string GetConnectionString_Reports()
324
        {
325
            string strConn = string.Empty;
326
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
327
            {
328
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
329
            }
330
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
331
            {
332
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
333
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
334
            }
335

    
336
            return strConn;
337
        }
338

    
339
        private string GetConnectionString_Admin()
340
        {
341
            string strConn = string.Empty;
342
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
343
            {
344
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
345
            }
346
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
347
            {
348
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
349
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
350
            }
351

    
352
            return strConn;
353
        }
354

    
355
        private string GetDrawingSavePath()
356
        {
357
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
358
        }
359

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

    
405

    
406

    
407
        #region TEST
408
        [CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
409
        public static void TestSelection()
410
        {
411
        }
412

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

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

    
424

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

    
431
                if (selectedObjectIds.Length > 0)
432
                {
433
                    ObjectId objIdBlock = selectedObjectIds[0];
434
                    AutoModeling.TESTStatic(objIdBlock.Handle.Value);
435
                }
436
            }
437
        }
438
        #endregion
439
    }
440
}
클립보드 이미지 추가 (최대 크기: 500 MB)