hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 9dab7146
이력 | 보기 | 이력해설 | 다운로드 (10.5 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Configuration; |
4 |
using System.Data; |
5 |
using System.Data.Common; |
6 |
using System.Data.SqlClient; |
7 |
using System.IO; |
8 |
|
9 |
using Autodesk.AutoCAD.ApplicationServices; |
10 |
using Autodesk.AutoCAD.ApplicationServices.Core; |
11 |
using Autodesk.AutoCAD.DatabaseServices; |
12 |
using Autodesk.AutoCAD.EditorInput; |
13 |
using Autodesk.AutoCAD.Geometry; |
14 |
using Autodesk.AutoCAD.Interop; |
15 |
using Autodesk.AutoCAD.Interop.Common; |
16 |
using Autodesk.AutoCAD.Runtime; |
17 |
using Autodesk.AutoCAD.Windows; |
18 |
|
19 |
using AVEVA.PID.Components; |
20 |
using AVEVA.PID.GUI; |
21 |
using AVEVA.PID.Common; |
22 |
using AVEVA.PID.DWGSelector; |
23 |
|
24 |
using AVEVA.PID.CustomizationUtility.DB; |
25 |
using AVEVA.PID.CustomizationUtility.Model; |
26 |
using AVEVA.PID.CustomizationUtility.Properties; |
27 |
|
28 |
namespace AVEVA.PID.CustomizationUtility |
29 |
{ |
30 |
public class AutoModeling |
31 |
{ |
32 |
public static AutoModeling Running { get; set; } |
33 |
Model.Document document; |
34 |
|
35 |
public AutoModeling(Model.Document document) |
36 |
{ |
37 |
this.document = document; |
38 |
} |
39 |
|
40 |
public void CreateDrawing() |
41 |
{ |
42 |
string outPath = Project_DB.GetDirectiveValue("DRGPTH"); |
43 |
string tempPath = Path.GetTempPath(); |
44 |
string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT"); |
45 |
|
46 |
DWTSelector.strDrawingNumber = document.AvevaDrawingNumber; |
47 |
DWTSelector.strSheetNumber = document.AvevaSheetNumber; |
48 |
DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber; |
49 |
DWTSelector.strCadFilePath = outPath; |
50 |
|
51 |
try |
52 |
{ |
53 |
string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false); |
54 |
Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication; |
55 |
Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text); |
56 |
|
57 |
Commands.addLatestPidvesrionToDictionary(); |
58 |
AVVPropCmd.ActivateAcad(); |
59 |
|
60 |
if (acadDocument != null) |
61 |
{ |
62 |
Running = this; |
63 |
acadApplication.ActiveDocument = acadDocument; |
64 |
acadDocument.SendCommand("QSAVE "); |
65 |
acadDocument.SendCommand("APPIDRun "); |
66 |
acadDocument.SendCommand("QSAVE "); |
67 |
Running = null; |
68 |
} |
69 |
} |
70 |
catch (System.Exception ex) |
71 |
{ |
72 |
|
73 |
} |
74 |
|
75 |
GC.Collect(); |
76 |
} |
77 |
public void Run() |
78 |
{ |
79 |
RunLineModeling(); |
80 |
} |
81 |
|
82 |
#region Run Method |
83 |
private void RunLineModeling() |
84 |
{ |
85 |
foreach (var item in document.LINES) |
86 |
{ |
87 |
LineModeling(item); |
88 |
|
89 |
} |
90 |
} |
91 |
#endregion |
92 |
|
93 |
#region Modeling Method |
94 |
private void LineModeling(Model.Line line) |
95 |
{ |
96 |
List<string> points = new List<string>(); |
97 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
98 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
99 |
|
100 |
if (line.Aveva.Type == Model.Type.Pipe) |
101 |
line.Aveva.Handle = DrawPipe(line.Aveva.Name, points); |
102 |
else if (line.Aveva.Type == Model.Type.Signal) |
103 |
line.Aveva.Handle = DrawSignal(line.Aveva.Name, points); |
104 |
} |
105 |
#endregion |
106 |
|
107 |
#region Drawing Method |
108 |
private long DrawPipe(string style, List<string> coordinates) |
109 |
{ |
110 |
List<long> prevHandles = GetAllGroupHandles(); |
111 |
|
112 |
GUIUtils.SetPipeStyle(style); |
113 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
114 |
Editor editor = acDoc.Editor; |
115 |
List<object> commandParam = new List<object>(); |
116 |
//command name |
117 |
commandParam.Add("DRPIPE"); |
118 |
//coordinate |
119 |
foreach (var item in coordinates) |
120 |
commandParam.Add(item); |
121 |
//enter |
122 |
commandParam.Add(null); |
123 |
//enter |
124 |
commandParam.Add(null); |
125 |
|
126 |
editor.Command(commandParam.ToArray()); |
127 |
|
128 |
List<long> newHandles = GetAllGroupHandles(); |
129 |
List<long> otherHandles = new List<long>(); |
130 |
foreach (var item in newHandles) |
131 |
if (!prevHandles.Contains(item)) |
132 |
otherHandles.Add(item); |
133 |
|
134 |
if (otherHandles.Count == 1) |
135 |
return otherHandles[0]; |
136 |
else |
137 |
return 0; |
138 |
} |
139 |
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
140 |
{ |
141 |
List<long> prevHandles = GetAllGroupHandles(); |
142 |
GUIUtils.SetPipeStyle(style); |
143 |
|
144 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
145 |
Editor editor = acDoc.Editor; |
146 |
List<object> commandParam = new List<object>(); |
147 |
//command name |
148 |
commandParam.Add("DRPIPE"); |
149 |
//coordinate |
150 |
foreach (var item in coordinates) |
151 |
commandParam.Add(item); |
152 |
//enter |
153 |
commandParam.Add(null); |
154 |
|
155 |
//Input Object ID |
156 |
commandParam.Add(objectId); |
157 |
|
158 |
//enter |
159 |
commandParam.Add(null); |
160 |
|
161 |
editor.Command(commandParam.ToArray()); |
162 |
|
163 |
List<long> newHandles = GetAllGroupHandles(); |
164 |
List<long> otherHandles = new List<long>(); |
165 |
foreach (var item in newHandles) |
166 |
if (!prevHandles.Contains(item)) |
167 |
otherHandles.Add(item); |
168 |
|
169 |
if (otherHandles.Count == 1) |
170 |
return otherHandles[0]; |
171 |
else |
172 |
return 0; |
173 |
} |
174 |
|
175 |
private long DrawSignal(string style, List<string> coordinates) |
176 |
{ |
177 |
List<long> prevHandles = GetAllGroupHandles(); |
178 |
|
179 |
GUIUtils.SetPipeStyle(style); |
180 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
181 |
Editor editor = acDoc.Editor; |
182 |
List<object> commandParam = new List<object>(); |
183 |
//command name |
184 |
commandParam.Add("VPESIGNAL"); |
185 |
//coordinate |
186 |
foreach (var item in coordinates) |
187 |
commandParam.Add(item); |
188 |
//enter |
189 |
commandParam.Add(null); |
190 |
|
191 |
editor.Command(commandParam.ToArray()); |
192 |
|
193 |
List<long> newHandles = GetAllGroupHandles(); |
194 |
List<long> otherHandles = new List<long>(); |
195 |
foreach (var item in newHandles) |
196 |
if (!prevHandles.Contains(item)) |
197 |
otherHandles.Add(item); |
198 |
|
199 |
if (otherHandles.Count == 1) |
200 |
return otherHandles[0]; |
201 |
else |
202 |
return 0; |
203 |
} |
204 |
private void InsertSymbol(string insertSymbolName, double x, double y) |
205 |
{ |
206 |
try |
207 |
{ |
208 |
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
209 |
drawingData.InsertSymbolName = insertSymbolName; |
210 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
211 |
Editor editor = acDoc.Editor; |
212 |
List<object> commandParam = new List<object>(); |
213 |
commandParam.Add("INSSYM"); |
214 |
Point3d point = new Point3d(x, y, 0); |
215 |
commandParam.Add(point); |
216 |
commandParam.Add(null); |
217 |
|
218 |
editor.Command(commandParam.ToArray()); |
219 |
} |
220 |
catch (System.Exception ex) |
221 |
{ |
222 |
|
223 |
} |
224 |
} |
225 |
#endregion |
226 |
|
227 |
private List<long> GetAllGroupHandles() |
228 |
{ |
229 |
List<long> handles = new List<long>(); |
230 |
|
231 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
232 |
Database acCurDb = acDoc.Database; |
233 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
234 |
{ |
235 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
236 |
foreach (var entry in gd) |
237 |
handles.Add(entry.Value.Handle.Value); |
238 |
acTrans.Commit(); |
239 |
} |
240 |
return handles; |
241 |
} |
242 |
|
243 |
private ObjectId GetFirstPolyLine(long groupHandle) |
244 |
{ |
245 |
ObjectId result = ObjectId.Null; |
246 |
|
247 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
248 |
Database acCurDb = acDoc.Database; |
249 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
250 |
{ |
251 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
252 |
foreach (var entry in gd) |
253 |
{ |
254 |
if (entry.Value.Handle.Value == groupHandle) |
255 |
{ |
256 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
257 |
foreach (var item in group.GetAllEntityIds()) |
258 |
{ |
259 |
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
260 |
{ |
261 |
result = item; |
262 |
break; |
263 |
} |
264 |
} |
265 |
break; |
266 |
} |
267 |
} |
268 |
acTrans.Commit(); |
269 |
} |
270 |
|
271 |
return result; |
272 |
} |
273 |
|
274 |
#region Test Source |
275 |
public void test() |
276 |
{ |
277 |
//InitGUI(); |
278 |
//DrawSignal("SONIC", new List<string>() { "2,100", "100,100" }); |
279 |
|
280 |
DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }); |
281 |
|
282 |
//DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" }); |
283 |
|
284 |
//DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" }); |
285 |
} |
286 |
public static void TESTStatic() |
287 |
{ |
288 |
AutoModeling auto = new AutoModeling(null); |
289 |
auto.test(); |
290 |
} |
291 |
#endregion |
292 |
} |
293 |
} |