프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ ef90c19c

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

1 6348c496 gaqhf
#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 8562d7dc gaqhf
using AVEVA.PID.CustomizationUtility.DB;
24
using AVEVA.PID.CustomizationUtility.Model;
25
using AVEVA.PID.CustomizationUtility.Properties;
26
27 6348c496 gaqhf
#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 d327a608 gaqhf
        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 e3ced1c9 gaqhf
        #region Command
112 6348c496 gaqhf
        /// <summary>
113
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
114
        /// </summary>
115 74a0c9d6 gaqhf
        [CommandMethod("APIDConverter")]
116
        public static void APIDConverter()
117 6348c496 gaqhf
        {
118 d327a608 gaqhf
            GetInstance();
119
            GetProjectInfo();
120 88cb9898 gaqhf
            APIDUtils.SetAvevaExplorer();
121 aabc2b2d gaqhf
        }
122 dc270d65 gaqhf
        [CommandMethod("AC")]
123
        public static void AC()
124
        {
125 e3ced1c9 gaqhf
            GetInstance();
126 d327a608 gaqhf
            GetProjectInfo();
127 88cb9898 gaqhf
            APIDUtils.SetAvevaExplorer();
128 dc270d65 gaqhf
        }
129 1c5bd296 gaqhf
        [CommandMethod("APPIDRun")]
130
        public static void APIDRun()
131
        {
132
            if (AutoModeling.Running != null)
133
            {
134 932933ed gaqhf
                GUIUtils.SetAutoLabelCheck(false);
135 1c5bd296 gaqhf
                AutoModeling.Running.Run();
136
            }
137
        }
138 d03dde83 gaqhf
        [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 a6d63eda gaqhf
                    {
168
                        if (AvevaThread.IsRunning(ThreadType.GetAttributeInformationFromForm))
169
                        {
170
                            AvevaThread.Stop(ThreadType.GetAttributeInformationFromForm);
171
                            MessageBox.Show("Fail save attribute!\r\nneed admin setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
172
                        }
173
                        else
174
                            MessageBox.Show("Success Save Attribute!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
175
                    }
176 d03dde83 gaqhf
                }
177
            }
178
        }
179 e3ced1c9 gaqhf
        #endregion
180 6348c496 gaqhf
        #region private methods
181
        private string SaveBinaryDrawing()
182
        {
183
            FileStream fs = null;
184
            string strDrawing = string.Empty;
185
            try
186
            {
187
                if (!string.IsNullOrEmpty(this.DrawingName))
188
                {
189
                    string strDrawingname = this.DrawingName;
190
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
191
                    //{
192
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
193
                    //}
194
195
                    byte[] drawingData = this.DrawingData;
196
                    if (null != drawingData)
197
                    {
198
                        try
199
                        {                            
200
                            if (File.Exists(strDrawingname) == true)
201
                            {
202
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
203
                            }
204
                        }
205
                        catch (System.Exception )
206
                        {
207
                        }
208
209
                        strDrawing = strDrawingname;
210
                        string path = GetDrawingSavePath();                        
211
                        if (!string.IsNullOrEmpty(path))
212
                        {
213
                            strDrawing = path + "\\" + strDrawingname;
214
                        }
215
                        strDrawing += ".dwg";
216
217
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
218
                        int ArraySize = new int();
219
                        ArraySize = drawingData.GetUpperBound(0);
220
                        fs.Write(drawingData, 0, ArraySize);
221
                    }
222
                }
223
            }
224
            catch (System.Exception )
225
            { }
226
            finally
227
            {
228
                if (fs != null)
229
                    fs.Close();
230
            }
231
            return strDrawing;
232
        }
233
234
        private void ReadDrawingDetails(string strRecordId)
235
        {
236
            SqlConnection connection = null;
237
            try
238
            {
239
                int ID = 0;
240
                int.TryParse(strRecordId, out ID);
241
                if (ID > 0)
242
                {
243
                    connection = new SqlConnection();
244
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
245
                    connection.Open();
246
247
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
248
                    SqlCommand cmd = new SqlCommand();
249
                    cmd.Connection = connection;
250
                    cmd.CommandText = strQry;
251
                    cmd.CommandType = CommandType.Text;
252
                    SqlDataReader reader = cmd.ExecuteReader();
253
254
                    if (null != reader && reader.HasRows)
255
                    {
256
                        while (reader.Read())
257
                        {
258
                            ProjectName     = reader["ProjectName"].ToString();
259
                            DrawingID       = reader["DrawingId"].ToString();
260
                            DrawingName     = reader["DrawingName"].ToString();
261
                            MajorRevision   = reader["Revision"].ToString();
262
                            MinorRevision   = reader["MinorRevision"].ToString();
263
                            Description     = reader["Description"].ToString();
264
                            NameOfUser      = reader["NameOfUser"].ToString();
265
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
266
                            RevisedBy       = reader["RevisedBy"].ToString();
267
                            CheckedBy       = reader["CheckedBy"].ToString();
268
                            ApprovedBy      = reader["ApprovedBy"].ToString();
269
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
270
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
271
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
272
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
273
                        }
274
                    }
275
                }
276
            }
277
            catch (System.Exception )
278
            { }
279
            finally
280
            {
281
                if (connection != null)
282
                    connection.Close();
283
            }
284
        }
285
        private string GetRecordId()
286
        {
287
            string strRecordId = string.Empty;
288
            SqlConnection connection = null;
289
            try
290
            {
291
                connection = new SqlConnection();
292
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
293
                connection.Open();
294
295
                string strQry = "Select Max(ID) from SynchroniseDetails";
296
                SqlCommand cmd = new SqlCommand();
297
                cmd.Connection = connection;
298
                cmd.CommandText = strQry;
299
                cmd.CommandType = CommandType.Text;
300
                SqlDataReader reader = cmd.ExecuteReader();
301
                if (null != reader && reader.HasRows)
302
                {
303
                    strRecordId = reader.GetString(0).ToString();
304
                }
305
            }
306
            catch (System.Exception )
307
            { }
308
            finally
309
            {
310
                if (connection != null)
311
                    connection.Close();
312
            }
313
            return strRecordId;
314
        }
315
        private string GetConnectionString()
316
        {
317
            string strConn = string.Empty;            
318
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
319
            {
320
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
321
            }
322
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
323
            {
324
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
325
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
326
            }           
327
328
            return strConn;            
329
        }
330
331
        private string GetConnectionString_Reports()
332
        {
333
            string strConn = string.Empty;
334
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
335
            {
336
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
337
            }
338
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
339
            {
340
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
341
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
342
            }
343
344
            return strConn;
345
        }
346
347 e9ed6b7b gaqhf
        private string GetConnectionString_Admin()
348
        {
349
            string strConn = string.Empty;
350
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
351
            {
352
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
353
            }
354
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
355
            {
356
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
357
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
358
            }
359
360
            return strConn;
361
        }
362
363 6348c496 gaqhf
        private string GetDrawingSavePath()
364
        {
365
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
366
        }
367
368
        /// <summary>
369
        /// Sets the color of the entity
370
        /// </summary>
371
        /// <param name="objIdBlockId">block id</param>
372
        /// <param name="iColor">color to be assigned</param>
373
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
374
        {
375
            Transaction trans = null;
376
            try
377
            {
378
                if (objIdBlockId != ObjectId.Null)
379
                {
380 8562d7dc gaqhf
                    Autodesk.AutoCAD.ApplicationServices.Document doc = AcadApp.DocumentManager.MdiActiveDocument;
381 6348c496 gaqhf
                    Database db = HostApplicationServices.WorkingDatabase;
382
                    trans = doc.TransactionManager.StartTransaction();
383
                    using (doc.LockDocument())
384
                    {
385
                        using (trans)
386
                        {
387
                            if (objIdBlockId.IsErased == false)
388
                            {
389
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
390
                                if (objDb != null)
391
                                {
392
                                    objDb.ColorIndex = iColor;                                    
393
                                }
394
                            }
395
                            trans.Commit();
396
                        }
397
                    }
398
                }
399
            }
400
            catch (System.Exception )
401
            {       
402
            }
403
            finally
404
            {
405
                if (trans != null)
406
                {
407
                    trans.Dispose();
408
                }
409
            }
410
        }
411
        #endregion
412 6b3cb476 gaqhf
413
414
415
        #region TEST
416
        [CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
417
        public static void TestSelection()
418
        {
419 e7770ee1 gaqhf
            Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
420
            AcadUtility acutil = (AcadUtility)acadApplication.ActiveDocument.Utility as AcadUtility;
421
            acutil.Prompt("HI");
422
            //Thread tt = new Thread(AvevaThread.Test);
423
            //tt.IsBackground = true;
424
            //tt.Start();
425 c6df982b gaqhf
            
426 e7770ee1 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
427
            Editor editor = acDoc.Editor;
428
            editor.PromptingForPoint += Editor_PromptingForPoint;
429
        }
430
431
        private static void Editor_PromptingForPoint(object sender, PromptPointOptionsEventArgs e)
432
        {
433
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
434
            Editor editor = acDoc.Editor;
435
            editor.PromptingForPoint -= Editor_PromptingForPoint;
436
437
            Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
438
            AcadUtility acutil = (AcadUtility)acadApplication.ActiveDocument.Utility as AcadUtility;
439 c6df982b gaqhf
            string aa = AVEVA.PID.Components.DesignatorAIValue.strHandle;
440
            int dd = acadApplication.ActiveDocument.SelectionSets.Count;
441
442
443
            //acadApplication.ActiveDocument.SendCommand(" 100,100 ");
444 9dab7146 gaqhf
        }
445
446
        [CommandMethod("PicTest", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
447
        public static void PicTest()
448
        {
449 c94252bf gaqhf
            // Get the current document and database, and start a transaction
450
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
451
            Database acCurDb = acDoc.Database;
452
            Editor acDocEd = acDoc.Editor;
453
454
            // Request for objects to be selected in the drawing area
455
            PromptSelectionResult acSSPrompt = acDocEd.GetSelection();
456 6b3cb476 gaqhf
457
458 c94252bf gaqhf
            // If the prompt status is OK, objects were selected
459
            if (acSSPrompt.Status == PromptStatus.OK)
460
            {
461
                ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds();
462
                // Get the last selected entity
463
464
                if (selectedObjectIds.Length > 0)
465
                {
466 c6df982b gaqhf
                    ObjectId objectId = selectedObjectIds[0];
467
                    using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
468
                    {
469
                        DBObject dBObject = tr.GetObject(objectId, OpenMode.ForWrite);
470
                        if (dBObject.GetType() == typeof(Polyline))
471
                        {
472
                            Polyline line = dBObject as Polyline;
473
                            line.SetPointAt(line.NumberOfVertices - 1, new Point2d(line.EndPoint.X + 3, line.EndPoint.Y + 3));
474
                        }
475
476
                        //if (objectId.ObjectClass.DxfName == "TEXT")
477
                        //{
478
                        //    DBText text = (DBText)tr.GetObject(objectId, OpenMode.ForWrite);
479
                        //    text.TextString += " DIS";
480
                        //}
481
                        //else
482
                        //{
483
                        //    MText mtext = (MText)tr.GetObject(objectId, OpenMode.ForWrite);
484
                        //    mtext.Contents += " DIS";
485
                        //}
486
                        tr.Commit();
487
                    }
488 c94252bf gaqhf
                }
489
            }
490
        }
491 6b3cb476 gaqhf
        #endregion
492 6348c496 gaqhf
    }
493
}
클립보드 이미지 추가 (최대 크기: 500 MB)