프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 0169b3c7

이력 | 보기 | 이력해설 | 다운로드 (15.1 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 e3ced1c9 gaqhf
        private DevExpress.LookAndFeel.DefaultLookAndFeel defaultLookAndFeel = new DevExpress.LookAndFeel.DefaultLookAndFeel();
36
37 6348c496 gaqhf
        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 e3ced1c9 gaqhf
                _instance.defaultLookAndFeel.LookAndFeel.SkinName = "Office 2016 Colorful";
68 6348c496 gaqhf
            }
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 d327a608 gaqhf
        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 e3ced1c9 gaqhf
        #region Command
115 6348c496 gaqhf
        /// <summary>
116
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
117
        /// </summary>
118 74a0c9d6 gaqhf
        [CommandMethod("APIDConverter")]
119
        public static void APIDConverter()
120 6348c496 gaqhf
        {
121 d327a608 gaqhf
            GetInstance();
122 aabc2b2d gaqhf
            ConverterRibbonUI.InitUI();
123 d327a608 gaqhf
            GetProjectInfo();
124 aabc2b2d gaqhf
        }
125 dc270d65 gaqhf
        [CommandMethod("AC")]
126
        public static void AC()
127
        {
128 e3ced1c9 gaqhf
            GetInstance();
129 dc270d65 gaqhf
            ConverterRibbonUI.InitUI();
130 d327a608 gaqhf
            GetProjectInfo();
131 dc270d65 gaqhf
        }
132 aabc2b2d gaqhf
        [CommandMethod("ConverterForm")]
133 74a0c9d6 gaqhf
        public static void ConverterForm()
134 aabc2b2d gaqhf
        {
135 dc270d65 gaqhf
            APIDConverter form = new APIDConverter();
136
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
137
            {
138
139
            }
140
        }
141
        [CommandMethod("ProjectForm")]
142
        public static void ProjectForm()
143
        {
144
            ProjectForm form = new ProjectForm();
145
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
146
            {
147 8562d7dc gaqhf
                Project_Info _ProjectInfo = Project_Info.GetInstance();
148
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
149
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
150
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
151
                _ProjectInfo.Port = Settings.Default.ProjectPort;
152
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
153
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
154 dc270d65 gaqhf
155 8562d7dc gaqhf
                if (Project_DB.ConnTestAndCreateTable())
156
                {
157
                    _ProjectInfo.Enable = true;
158
                    MessageBox.Show("Success project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
159
                }
160
                else
161
                {
162
                    _ProjectInfo.Enable = false;
163
                    MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
164
                }
165 dc270d65 gaqhf
            }
166
        }
167
        [CommandMethod("MappingForm")]
168
        public static void MappingForm()
169
        {
170
            MappingForm form = new MappingForm();
171
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
172 74a0c9d6 gaqhf
            {
173
174
            }
175 6348c496 gaqhf
        }
176 e3ced1c9 gaqhf
        #endregion
177 6348c496 gaqhf
        #region private methods
178
        private string SaveBinaryDrawing()
179
        {
180
            FileStream fs = null;
181
            string strDrawing = string.Empty;
182
            try
183
            {
184
                if (!string.IsNullOrEmpty(this.DrawingName))
185
                {
186
                    string strDrawingname = this.DrawingName;
187
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
188
                    //{
189
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
190
                    //}
191
192
                    byte[] drawingData = this.DrawingData;
193
                    if (null != drawingData)
194
                    {
195
                        try
196
                        {                            
197
                            if (File.Exists(strDrawingname) == true)
198
                            {
199
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
200
                            }
201
                        }
202
                        catch (System.Exception )
203
                        {
204
                        }
205
206
                        strDrawing = strDrawingname;
207
                        string path = GetDrawingSavePath();                        
208
                        if (!string.IsNullOrEmpty(path))
209
                        {
210
                            strDrawing = path + "\\" + strDrawingname;
211
                        }
212
                        strDrawing += ".dwg";
213
214
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
215
                        int ArraySize = new int();
216
                        ArraySize = drawingData.GetUpperBound(0);
217
                        fs.Write(drawingData, 0, ArraySize);
218
                    }
219
                }
220
            }
221
            catch (System.Exception )
222
            { }
223
            finally
224
            {
225
                if (fs != null)
226
                    fs.Close();
227
            }
228
            return strDrawing;
229
        }
230
231
        private void ReadDrawingDetails(string strRecordId)
232
        {
233
            SqlConnection connection = null;
234
            try
235
            {
236
                int ID = 0;
237
                int.TryParse(strRecordId, out ID);
238
                if (ID > 0)
239
                {
240
                    connection = new SqlConnection();
241
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
242
                    connection.Open();
243
244
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
245
                    SqlCommand cmd = new SqlCommand();
246
                    cmd.Connection = connection;
247
                    cmd.CommandText = strQry;
248
                    cmd.CommandType = CommandType.Text;
249
                    SqlDataReader reader = cmd.ExecuteReader();
250
251
                    if (null != reader && reader.HasRows)
252
                    {
253
                        while (reader.Read())
254
                        {
255
                            ProjectName     = reader["ProjectName"].ToString();
256
                            DrawingID       = reader["DrawingId"].ToString();
257
                            DrawingName     = reader["DrawingName"].ToString();
258
                            MajorRevision   = reader["Revision"].ToString();
259
                            MinorRevision   = reader["MinorRevision"].ToString();
260
                            Description     = reader["Description"].ToString();
261
                            NameOfUser      = reader["NameOfUser"].ToString();
262
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
263
                            RevisedBy       = reader["RevisedBy"].ToString();
264
                            CheckedBy       = reader["CheckedBy"].ToString();
265
                            ApprovedBy      = reader["ApprovedBy"].ToString();
266
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
267
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
268
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
269
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
270
                        }
271
                    }
272
                }
273
            }
274
            catch (System.Exception )
275
            { }
276
            finally
277
            {
278
                if (connection != null)
279
                    connection.Close();
280
            }
281
        }
282
        private string GetRecordId()
283
        {
284
            string strRecordId = string.Empty;
285
            SqlConnection connection = null;
286
            try
287
            {
288
                connection = new SqlConnection();
289
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
290
                connection.Open();
291
292
                string strQry = "Select Max(ID) from SynchroniseDetails";
293
                SqlCommand cmd = new SqlCommand();
294
                cmd.Connection = connection;
295
                cmd.CommandText = strQry;
296
                cmd.CommandType = CommandType.Text;
297
                SqlDataReader reader = cmd.ExecuteReader();
298
                if (null != reader && reader.HasRows)
299
                {
300
                    strRecordId = reader.GetString(0).ToString();
301
                }
302
            }
303
            catch (System.Exception )
304
            { }
305
            finally
306
            {
307
                if (connection != null)
308
                    connection.Close();
309
            }
310
            return strRecordId;
311
        }
312
        private string GetConnectionString()
313
        {
314
            string strConn = string.Empty;            
315
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
316
            {
317
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
318
            }
319
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
320
            {
321
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
322
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
323
            }           
324
325
            return strConn;            
326
        }
327
328
        private string GetConnectionString_Reports()
329
        {
330
            string strConn = string.Empty;
331
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
332
            {
333
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
334
            }
335
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
336
            {
337
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
338
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
339
            }
340
341
            return strConn;
342
        }
343
344
        private string GetDrawingSavePath()
345
        {
346
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
347
        }
348
349
        /// <summary>
350
        /// Sets the color of the entity
351
        /// </summary>
352
        /// <param name="objIdBlockId">block id</param>
353
        /// <param name="iColor">color to be assigned</param>
354
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
355
        {
356
            Transaction trans = null;
357
            try
358
            {
359
                if (objIdBlockId != ObjectId.Null)
360
                {
361 8562d7dc gaqhf
                    Autodesk.AutoCAD.ApplicationServices.Document doc = AcadApp.DocumentManager.MdiActiveDocument;
362 6348c496 gaqhf
                    Database db = HostApplicationServices.WorkingDatabase;
363
                    trans = doc.TransactionManager.StartTransaction();
364
                    using (doc.LockDocument())
365
                    {
366
                        using (trans)
367
                        {
368
                            if (objIdBlockId.IsErased == false)
369
                            {
370
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
371
                                if (objDb != null)
372
                                {
373
                                    objDb.ColorIndex = iColor;                                    
374
                                }
375
                            }
376
                            trans.Commit();
377
                        }
378
                    }
379
                }
380
            }
381
            catch (System.Exception )
382
            {       
383
            }
384
            finally
385
            {
386
                if (trans != null)
387
                {
388
                    trans.Dispose();
389
                }
390
            }
391
        }
392
        #endregion
393
    }
394
}
클립보드 이미지 추가 (최대 크기: 500 MB)