프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 5598104a

이력 | 보기 | 이력해설 | 다운로드 (13 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
#endregion using namespaces
24
namespace AVEVA.PID.CustomizationUtility
25
{
26
    /// <summary>
27
    /// Singleton class used for customization of P&ID application
28
    /// </summary>
29
    public class PIDCustomization
30
    {
31
        private DevExpress.LookAndFeel.DefaultLookAndFeel defaultLookAndFeel = new DevExpress.LookAndFeel.DefaultLookAndFeel();
32

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

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

    
58
        public static PIDCustomization GetInstance()
59
        {
60
            if (_instance == null)
61
            {
62
                _instance = new PIDCustomization();
63
                _instance.defaultLookAndFeel.LookAndFeel.SkinName = "Office 2016 Colorful";
64
            }
65
            return _instance;           
66
        }
67

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

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

    
90
        #region Command
91
        /// <summary>
92
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
93
        /// </summary>
94
        [CommandMethod("APIDConverter")]
95
        public static void APIDConverter()
96
        {
97
            ConverterRibbonUI.InitUI();
98
        }
99
        [CommandMethod("AC")]
100
        public static void AC()
101
        {
102
            GetInstance();
103
            ConverterRibbonUI.InitUI();
104
        }
105
        [CommandMethod("ConverterForm")]
106
        public static void ConverterForm()
107
        {
108
            APIDConverter form = new APIDConverter();
109
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
110
            {
111

    
112
            }
113
        }
114
        [CommandMethod("ProjectForm")]
115
        public static void ProjectForm()
116
        {
117
            ProjectForm form = new ProjectForm();
118
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
119
            {
120

    
121
            }
122
        }
123
        [CommandMethod("MappingForm")]
124
        public static void MappingForm()
125
        {
126
            MappingForm form = new MappingForm();
127
            if (AcadApp.ShowModalDialog(form) == DialogResult.OK)
128
            {
129

    
130
            }
131
        }
132
        #endregion
133
        #region private methods
134
        private string SaveBinaryDrawing()
135
        {
136
            FileStream fs = null;
137
            string strDrawing = string.Empty;
138
            try
139
            {
140
                if (!string.IsNullOrEmpty(this.DrawingName))
141
                {
142
                    string strDrawingname = this.DrawingName;
143
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
144
                    //{
145
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
146
                    //}
147

    
148
                    byte[] drawingData = this.DrawingData;
149
                    if (null != drawingData)
150
                    {
151
                        try
152
                        {                            
153
                            if (File.Exists(strDrawingname) == true)
154
                            {
155
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
156
                            }
157
                        }
158
                        catch (System.Exception )
159
                        {
160
                        }
161

    
162
                        strDrawing = strDrawingname;
163
                        string path = GetDrawingSavePath();                        
164
                        if (!string.IsNullOrEmpty(path))
165
                        {
166
                            strDrawing = path + "\\" + strDrawingname;
167
                        }
168
                        strDrawing += ".dwg";
169

    
170
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
171
                        int ArraySize = new int();
172
                        ArraySize = drawingData.GetUpperBound(0);
173
                        fs.Write(drawingData, 0, ArraySize);
174
                    }
175
                }
176
            }
177
            catch (System.Exception )
178
            { }
179
            finally
180
            {
181
                if (fs != null)
182
                    fs.Close();
183
            }
184
            return strDrawing;
185
        }
186

    
187
        private void ReadDrawingDetails(string strRecordId)
188
        {
189
            SqlConnection connection = null;
190
            try
191
            {
192
                int ID = 0;
193
                int.TryParse(strRecordId, out ID);
194
                if (ID > 0)
195
                {
196
                    connection = new SqlConnection();
197
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
198
                    connection.Open();
199

    
200
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
201
                    SqlCommand cmd = new SqlCommand();
202
                    cmd.Connection = connection;
203
                    cmd.CommandText = strQry;
204
                    cmd.CommandType = CommandType.Text;
205
                    SqlDataReader reader = cmd.ExecuteReader();
206

    
207
                    if (null != reader && reader.HasRows)
208
                    {
209
                        while (reader.Read())
210
                        {
211
                            ProjectName     = reader["ProjectName"].ToString();
212
                            DrawingID       = reader["DrawingId"].ToString();
213
                            DrawingName     = reader["DrawingName"].ToString();
214
                            MajorRevision   = reader["Revision"].ToString();
215
                            MinorRevision   = reader["MinorRevision"].ToString();
216
                            Description     = reader["Description"].ToString();
217
                            NameOfUser      = reader["NameOfUser"].ToString();
218
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
219
                            RevisedBy       = reader["RevisedBy"].ToString();
220
                            CheckedBy       = reader["CheckedBy"].ToString();
221
                            ApprovedBy      = reader["ApprovedBy"].ToString();
222
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
223
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
224
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
225
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
226
                        }
227
                    }
228
                }
229
            }
230
            catch (System.Exception )
231
            { }
232
            finally
233
            {
234
                if (connection != null)
235
                    connection.Close();
236
            }
237
        }
238
        private string GetRecordId()
239
        {
240
            string strRecordId = string.Empty;
241
            SqlConnection connection = null;
242
            try
243
            {
244
                connection = new SqlConnection();
245
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
246
                connection.Open();
247

    
248
                string strQry = "Select Max(ID) from SynchroniseDetails";
249
                SqlCommand cmd = new SqlCommand();
250
                cmd.Connection = connection;
251
                cmd.CommandText = strQry;
252
                cmd.CommandType = CommandType.Text;
253
                SqlDataReader reader = cmd.ExecuteReader();
254
                if (null != reader && reader.HasRows)
255
                {
256
                    strRecordId = reader.GetString(0).ToString();
257
                }
258
            }
259
            catch (System.Exception )
260
            { }
261
            finally
262
            {
263
                if (connection != null)
264
                    connection.Close();
265
            }
266
            return strRecordId;
267
        }
268
        private string GetConnectionString()
269
        {
270
            string strConn = string.Empty;            
271
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
272
            {
273
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
274
            }
275
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
276
            {
277
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
278
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
279
            }           
280

    
281
            return strConn;            
282
        }
283

    
284
        private string GetConnectionString_Reports()
285
        {
286
            string strConn = string.Empty;
287
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
288
            {
289
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
290
            }
291
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
292
            {
293
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
294
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
295
            }
296

    
297
            return strConn;
298
        }
299

    
300
        private string GetDrawingSavePath()
301
        {
302
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
303
        }
304

    
305
        /// <summary>
306
        /// Sets the color of the entity
307
        /// </summary>
308
        /// <param name="objIdBlockId">block id</param>
309
        /// <param name="iColor">color to be assigned</param>
310
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
311
        {
312
            Transaction trans = null;
313
            try
314
            {
315
                if (objIdBlockId != ObjectId.Null)
316
                {
317
                    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
318
                    Database db = HostApplicationServices.WorkingDatabase;
319
                    trans = doc.TransactionManager.StartTransaction();
320
                    using (doc.LockDocument())
321
                    {
322
                        using (trans)
323
                        {
324
                            if (objIdBlockId.IsErased == false)
325
                            {
326
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
327
                                if (objDb != null)
328
                                {
329
                                    objDb.ColorIndex = iColor;                                    
330
                                }
331
                            }
332
                            trans.Commit();
333
                        }
334
                    }
335
                }
336
            }
337
            catch (System.Exception )
338
            {       
339
            }
340
            finally
341
            {
342
                if (trans != null)
343
                {
344
                    trans.Dispose();
345
                }
346
            }
347
        }
348
        #endregion
349
    }
350
}
클립보드 이미지 추가 (최대 크기: 500 MB)