/
CivilApplication.cs
326 lines (277 loc) · 13 KB
/
CivilApplication.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2016 Autodesk, Inc. All rights reserved.
// Author: paolo.serra@autodesk.com
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
using Autodesk.AECC.Interop.UiRoadway;
using Autodesk.AutoCAD.Interop;
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace CivilConnection
{
/// <summary>
/// CivilApplication object type.
/// </summary>
public class CivilApplication
{
/// <summary>
/// The documents in Civil 3D.
/// </summary>
private IList<CivilDocument> Documents;
/// <summary>
/// The land XML path.
/// </summary>
public string LandXMLPath;
/// <summary>
/// The active document
/// </summary>
AcadDocument ActiveDocument;
/// <summary>
/// The active application
/// </summary>
AeccRoadwayApplication mApp;
/// <summary>
/// Gets the internal element.
/// </summary>
/// <value>
/// The internal element.
/// </value>
internal object InternalElement { get { return this.mApp; } }
/// <summary>
/// Gets the application.
/// </summary>
/// <returns></returns>
internal AeccRoadwayApplication GetApplication()
{
Utils.Log(string.Format("GetApplication started...", ""));
string m_sAcadProdID = "AutoCAD.Application";
string[] progids = new string[] {
"AeccXUiRoadway.AeccRoadwayApplication.13.2", // 2020
"AeccXUiRoadway.AeccRoadwayApplication.13.0", // 2019
"AeccXUiRoadway.AeccRoadwayApplication.12.0", // 2018
"AeccXUiRoadway.AeccRoadwayApplication.11.0", // 2017
"AeccXUiRoadway.AeccRoadwayApplication.10.5" // 2016
};
#if C2021
progids = new string[] {"AeccXUiRoadway.AeccRoadwayApplication.13.3"}; // 2021
#elif C2022
progids = new string[] {"AeccXUiRoadway.AeccRoadwayApplication.13.4"}; // 2022
#elif C2023
progids = new string[] {"AeccXUiRoadway.AeccRoadwayApplication.13.5"}; // 2023
#endif
AcadApplication m_oAcadApp = null;
try
{
m_oAcadApp = System.Runtime.InteropServices.Marshal.GetActiveObject(m_sAcadProdID) as AcadApplication;
}
catch (COMException ex)
{
if (!ex.ToString().Contains("0x800401E3"))
{
throw new Exception("Civil 3D Communication Error");
}
}
// Roadway application
dynamic output = null;
foreach (string r_sAeccAppProgId in progids)
{
try
{
output = m_oAcadApp.GetInterfaceObject(r_sAeccAppProgId);
if (output != null)
{
break;
}
}
catch (COMException ex)
{
if (!ex.ToString().Contains("0x800401E3"))
{
throw new Exception("Civil 3D Communication Error");
}
}
catch (Exception)
{
}
}
//return m_oAcadApp.GetInterfaceObject(r_sAeccAppProgId);
Utils.Log(string.Format("GetApplication completed.", ""));
return output;
}
/// <summary>
/// Creates the connection with the running session of Civil 3D.
/// </summary>
///
public CivilApplication()
{
Utils.InitializeLog();
Utils.Log(string.Format("CivilApplication.CivilApplication started...", ""));
try
{
this.mApp = this.GetApplication();
}
catch (Exception ex)
{
Utils.Log($"EXCEPTION: {ex.Message}");
}
if (this.mApp == null)
{
Utils.Log($"ERROR: Cannot connect to the Civil 3D Application");
return;
}
IList<CivilDocument> documents = new List<CivilDocument>();
foreach (var doc in this.mApp.Documents)
{
documents.Add(new CivilDocument(doc as AeccRoadwayDocument));
}
this.Documents = documents;
this.ActiveDocument = mApp.ActiveDocument;
var revitDoc = RevitServices.Persistence.DocumentManager.Instance.CurrentDBDocument;
RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(revitDoc);
Autodesk.Revit.DB.Units units = revitDoc.GetUnits(); // new Autodesk.Revit.DB.Units(Autodesk.Revit.DB.UnitSystem.Metric);
var du = this.Documents.First()._document.Settings.DrawingSettings.UnitZoneSettings.DrawingUnits;
Utils.Log(string.Format("CivilApplication.Units started...", ""));
#if (C2023 || C2022 || C2021)
// 6.0.0 Change Revit Document units to match the Civil 3D Units
if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitMeters)
{
Utils.Log(string.Format("Civil Document in meters", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Meters));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitDecimeters)
{
Utils.Log(string.Format("Civil Document in decimeters", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Centimeters));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitCentimeters)
{
Utils.Log(string.Format("Civil Document in centimeters", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Centimeters));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitMillimeters)
{
Utils.Log(string.Format("Civil Document in millimeters", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Millimeters));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitFeet)
{
Utils.Log(string.Format("Civil Document in feet", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Feet));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitInches)
{
Utils.Log(string.Format("Civil Document in inches", ""));
units.SetFormatOptions(Autodesk.Revit.DB.SpecTypeId.Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.UnitTypeId.Inches));
}
else
{
throw new Exception("UNITS ERROR\nThe Civil 3D units of the Active Document are not supported in Revit.\nChange the Civil 3D Units to continue");
}
#else
// 1.1.0 Change Revit Document units to match the Civil 3D Units
if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitMeters)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_METERS));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitDecimeters)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_DECIMETERS));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitCentimeters)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_CENTIMETERS));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitMillimeters)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_MILLIMETERS));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitFeet)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_DECIMAL_FEET));
}
else if (du == Autodesk.AECC.Interop.Land.AeccDrawingUnitType.aeccDrawingUnitInches)
{
units.SetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length, new Autodesk.Revit.DB.FormatOptions(Autodesk.Revit.DB.DisplayUnitType.DUT_DECIMAL_INCHES));
}
else
{
throw new Exception("UNITS ERROR\nThe Civil 3D units of the Active Document are not supported in Revit.\nChange the Civil 3D Units to continue");
}
#endif
revitDoc.SetUnits(units);
RevitServices.Transactions.TransactionManager.Instance.TransactionTaskDone();
Utils.Log(string.Format("CivilApplication.Units completed.", ""));
SessionVariables.LandXMLPath = System.IO.Path.GetTempPath();
SessionVariables.IsLandXMLExported = false;
SessionVariables.CivilApplication = this;
SessionVariables.ParametersCreated = false;
SessionVariables.DocumentTotalTransform = null;
RevitUtils.DocumentTotalTransform();
}
/// <summary>
/// Returns the list of Civil Documents opened in Civil 3D.
/// </summary>
/// <returns></returns>
public IList<CivilDocument> GetDocuments()
{
//foreach (CivilDocument doc in this.Documents)
//{
// //Utils.DumpLandXML(doc._document);
//}
return this.Documents;
}
/// <summary>
/// Returns the Civil Documents opened in Civil 3D with the same name.
/// </summary>
/// <param name="name">The Document name</param>
/// <returns></returns>
public CivilDocument GetDocumentByName(string name)
{
return this.Documents.First(x => x.Name == name);
}
/// <summary>
/// Enables the Run Periodically mode and updates the connection with Civil 3D.
/// </summary>
/// <returns></returns>
[CanUpdatePeriodicallyAttribute(true)]
public CivilApplication UpdatePeriodically()
{
Utils.Log(string.Format("CivilApplication.UpdatePeriodically started...", ""));
return new CivilApplication();
}
/// <summary>
/// Writes a message to the log file
/// </summary>
/// <param name="data">The data that is passed through</param>
/// <param name="message">An optional message to write to the log.</param>
/// <returns></returns>
public static object WriteToLog(object data, string message = "")
{
Utils.Log(string.Format("{0}{1}", message.Length > 0 ? message + " " : "", data));
return data;
}
/// <summary>
/// Public textual representation of the Dynamo node preview.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return string.Format("CivilApplication(ActiveDocument = {0})", this.ActiveDocument.Name);
}
}
}