프로젝트

일반

사용자정보

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

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 74a0c9d6

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

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

    
56
        public static PIDCustomization GetInstance()
57
        {
58
            if (_instance == null)
59
            {
60
                _instance = new PIDCustomization();
61
            }
62
            return _instance;           
63
        }
64

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

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

    
87
        /// <summary>
88
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
89
        /// </summary>
90
        [CommandMethod("APIDConverter")]
91
        public static void APIDConverter()
92
        {
93
            ConverterRibbonUI.InitUI();
94
        }
95
        [CommandMethod("ConverterForm")]
96
        public static void ConverterForm()
97
        {
98
            APIDConverter converter = new APIDConverter();
99
            if (AcadApp.ShowModalDialog(converter) == DialogResult.OK)
100
            {
101

    
102
            }
103
        }
104
        #region private methods
105
        private string SaveBinaryDrawing()
106
        {
107
            FileStream fs = null;
108
            string strDrawing = string.Empty;
109
            try
110
            {
111
                if (!string.IsNullOrEmpty(this.DrawingName))
112
                {
113
                    string strDrawingname = this.DrawingName;
114
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
115
                    //{
116
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
117
                    //}
118

    
119
                    byte[] drawingData = this.DrawingData;
120
                    if (null != drawingData)
121
                    {
122
                        try
123
                        {                            
124
                            if (File.Exists(strDrawingname) == true)
125
                            {
126
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
127
                            }
128
                        }
129
                        catch (System.Exception )
130
                        {
131
                        }
132

    
133
                        strDrawing = strDrawingname;
134
                        string path = GetDrawingSavePath();                        
135
                        if (!string.IsNullOrEmpty(path))
136
                        {
137
                            strDrawing = path + "\\" + strDrawingname;
138
                        }
139
                        strDrawing += ".dwg";
140

    
141
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
142
                        int ArraySize = new int();
143
                        ArraySize = drawingData.GetUpperBound(0);
144
                        fs.Write(drawingData, 0, ArraySize);
145
                    }
146
                }
147
            }
148
            catch (System.Exception )
149
            { }
150
            finally
151
            {
152
                if (fs != null)
153
                    fs.Close();
154
            }
155
            return strDrawing;
156
        }
157

    
158
        private void ReadDrawingDetails(string strRecordId)
159
        {
160
            SqlConnection connection = null;
161
            try
162
            {
163
                int ID = 0;
164
                int.TryParse(strRecordId, out ID);
165
                if (ID > 0)
166
                {
167
                    connection = new SqlConnection();
168
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
169
                    connection.Open();
170

    
171
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
172
                    SqlCommand cmd = new SqlCommand();
173
                    cmd.Connection = connection;
174
                    cmd.CommandText = strQry;
175
                    cmd.CommandType = CommandType.Text;
176
                    SqlDataReader reader = cmd.ExecuteReader();
177

    
178
                    if (null != reader && reader.HasRows)
179
                    {
180
                        while (reader.Read())
181
                        {
182
                            ProjectName     = reader["ProjectName"].ToString();
183
                            DrawingID       = reader["DrawingId"].ToString();
184
                            DrawingName     = reader["DrawingName"].ToString();
185
                            MajorRevision   = reader["Revision"].ToString();
186
                            MinorRevision   = reader["MinorRevision"].ToString();
187
                            Description     = reader["Description"].ToString();
188
                            NameOfUser      = reader["NameOfUser"].ToString();
189
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
190
                            RevisedBy       = reader["RevisedBy"].ToString();
191
                            CheckedBy       = reader["CheckedBy"].ToString();
192
                            ApprovedBy      = reader["ApprovedBy"].ToString();
193
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
194
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
195
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
196
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
197
                        }
198
                    }
199
                }
200
            }
201
            catch (System.Exception )
202
            { }
203
            finally
204
            {
205
                if (connection != null)
206
                    connection.Close();
207
            }
208
        }
209
        private string GetRecordId()
210
        {
211
            string strRecordId = string.Empty;
212
            SqlConnection connection = null;
213
            try
214
            {
215
                connection = new SqlConnection();
216
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
217
                connection.Open();
218

    
219
                string strQry = "Select Max(ID) from SynchroniseDetails";
220
                SqlCommand cmd = new SqlCommand();
221
                cmd.Connection = connection;
222
                cmd.CommandText = strQry;
223
                cmd.CommandType = CommandType.Text;
224
                SqlDataReader reader = cmd.ExecuteReader();
225
                if (null != reader && reader.HasRows)
226
                {
227
                    strRecordId = reader.GetString(0).ToString();
228
                }
229
            }
230
            catch (System.Exception )
231
            { }
232
            finally
233
            {
234
                if (connection != null)
235
                    connection.Close();
236
            }
237
            return strRecordId;
238
        }
239
        private string GetConnectionString()
240
        {
241
            string strConn = string.Empty;            
242
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
243
            {
244
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
245
            }
246
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
247
            {
248
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
249
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
250
            }           
251

    
252
            return strConn;            
253
        }
254

    
255
        private string GetConnectionString_Reports()
256
        {
257
            string strConn = string.Empty;
258
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
259
            {
260
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
261
            }
262
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
263
            {
264
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
265
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
266
            }
267

    
268
            return strConn;
269
        }
270

    
271
        private string GetDrawingSavePath()
272
        {
273
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
274
        }
275

    
276
        /// <summary>
277
        /// Sets the color of the entity
278
        /// </summary>
279
        /// <param name="objIdBlockId">block id</param>
280
        /// <param name="iColor">color to be assigned</param>
281
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
282
        {
283
            Transaction trans = null;
284
            try
285
            {
286
                if (objIdBlockId != ObjectId.Null)
287
                {
288
                    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
289
                    Database db = HostApplicationServices.WorkingDatabase;
290
                    trans = doc.TransactionManager.StartTransaction();
291
                    using (doc.LockDocument())
292
                    {
293
                        using (trans)
294
                        {
295
                            if (objIdBlockId.IsErased == false)
296
                            {
297
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
298
                                if (objDb != null)
299
                                {
300
                                    objDb.ColorIndex = iColor;                                    
301
                                }
302
                            }
303
                            trans.Commit();
304
                        }
305
                    }
306
                }
307
            }
308
            catch (System.Exception )
309
            {       
310
            }
311
            finally
312
            {
313
                if (trans != null)
314
                {
315
                    trans.Dispose();
316
                }
317
            }
318
        }
319
        #endregion
320
    }
321
}