-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolidWorksIntegration.vb
334 lines (258 loc) · 10.9 KB
/
SolidWorksIntegration.vb
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
327
328
329
330
331
332
333
334
Imports DriveWorks.Applications
Imports DriveWorks.SolidWorks
Imports System.IO
Imports SldWorks
Imports DriveWorks.Reporting
Imports SwConst
Public Class SolidWorksInteraction
Private mSwApp As SldWorks.SldWorks
Private mSwModel As SldWorks.ModelDoc2
Private mSwCustpropManager As CustomPropertyManager
Private mSolidWorksService As ISolidWorksService
Private mGroupSettings As Generation.GenerationSettings
Private mEventLog As IApplicationEventService
Private Const DEFAULT_MACRO_METHOD_NAME As String = "Main"
Public Event LogFileEvent(logText As String)
Public Sub New(SolidWorksService As ISolidWorksService, groupSettings As Generation.GenerationSettings, ByVal eventLog As IApplicationEventService)
mGroupSettings = groupSettings
mEventLog = eventLog
If SolidWorksService.SolidWorks Is Nothing Then
mSwApp = CType(CreateObject("SldWorks.Application"), SldWorks.SldWorks)
Else
mSwApp = SolidWorksService.SolidWorks
End If
mSwApp.Visible = True
'Attach to the current doc
mSwModel = mSwApp.ActiveDoc
' get the custom property manager
If mSwModel IsNot Nothing Then
mSwCustpropManager = mSwModel.Extension.CustomPropertyManager("")
End If
End Sub
Public Sub Dispose()
mSwModel = Nothing
mSwApp = Nothing
End Sub
#Region " SolidWorks Traversal "
Public Function GetModelPath() As String
If Not mSwModel Is Nothing Then
Return mSwModel.GetPathName
Else
Return ""
End If
End Function
Public ReadOnly Property FilePath As String
Get
If Not mSwModel Is Nothing Then
Return IO.Path.GetDirectoryName(mSwModel.GetPathName)
Else
Return ""
End If
End Get
End Property
Public ReadOnly Property FileName As String
Get
If Not mSwModel Is Nothing Then
Return IO.Path.GetFileName(mSwModel.GetPathName)
Else
Return ""
End If
End Get
End Property
Public ReadOnly Property IsPart As Boolean
Get
Return mSwModel.GetType() = 1
End Get
End Property
Public ReadOnly Property FileNameWithoutExtension As String
Get
If Not mSwModel Is Nothing Then
Return IO.Path.GetFileNameWithoutExtension(mSwModel.GetPathName)
Else
Return ""
End If
End Get
End Property
Private Function GetCustomProperty(propertyName As String) As String
If mSwCustpropManager Is Nothing Then Return String.Empty
Return mSwCustpropManager.Get(propertyName)
End Function
Public ReadOnly Property CreateCAMData() As Boolean
Get
'the default is to run the cam data, that way if the custom property doesn't exist, it will run by default
' the only value that will stop the property from returning true is FALSE
Dim createCAMDataValue As String = GetCustomProperty("DWCAMWorks")
If String.IsNullOrEmpty(createCAMDataValue) Then
Return True
Else
Select Case createCAMDataValue.ToLower
Case "false"
Return False
Case Else
Return True
End Select
End If
End Get
End Property
Public ReadOnly Property PerformExtractMachineFeatures() As Boolean
Get
'the default is to run the cam data, that way if the custom property doesn't exist, it will run by default
' the only value that will stop the property from returning true is FALSE
Dim createCAMDataValue As String = GetCustomProperty("DWCAMWorksEMF")
If String.IsNullOrEmpty(createCAMDataValue) Then
Return True
Else
Select Case createCAMDataValue.ToLower
Case "false"
Return False
Case Else
Return True
End Select
End If
End Get
End Property
Public ReadOnly Property GenerateOperationPlanOption() As Integer
Get
' GOP preference are
' 1 : RETAIN
' 2 : REGENERATE
' 3 : CANCEL
' 4 : QUERY PREFERENCES
'We default to 2
'the default is to run the cam data, that way if the custom property doesn't exist, it will run by default
Dim CAMDataValue As String = GetCustomProperty("DWCAMWorksGOP")
If String.IsNullOrEmpty(CAMDataValue) Then
' 2 : REGENERATE
Return 2
Else
Select Case CAMDataValue.ToLower
Case "1"
Return 1
Case "2"
Return 2
Case "3"
Return 3
Case "4"
Return 4
Case Else
Return 2
End Select
End If
End Get
End Property
Public ReadOnly Property PerformGenerateToolPath() As Boolean
Get
'the default is to run the cam data, that way if the custom property doesn't exist, it will run by default
' the only value that will stop the property from returning true is FALSE
Dim createCAMDataValue As String = GetCustomProperty("DWCAMWorksGTP")
If String.IsNullOrEmpty(createCAMDataValue) Then
Return True
Else
Select Case createCAMDataValue.ToLower
Case "false"
Return False
Case Else
Return True
End Select
End If
End Get
End Property
Public ReadOnly Property PostProcessPath() As String
Get
Dim CAMDataValue As String
' Get the custom property called DWCAMWorksPostFilePath
CAMDataValue = GetCustomProperty("DWCAMWorksPostFilePath")
' if we don't find the custom property - return an empty string
If String.IsNullOrEmpty(CAMDataValue) Then
Return String.Empty
Else
Return CAMDataValue
End If
End Get
End Property
#End Region
#Region " Macros "
''' <summary>
''' Runs a specific macro for a model.
''' </summary>
''' <param name="fullFilePath">The full path to the macro to run.</param>
''' <param name="macroName">The name of the method in the DriveWorks module in the macro to run (defaults to Main).</param>
''' <remarks></remarks>
Friend Sub RunModelMacro(ByVal fullFilePath As String, Optional ByVal macroName As String = DEFAULT_MACRO_METHOD_NAME)
RunMacroCore(fullFilePath, macroName)
End Sub
''' <summary>
''' Runs a specific macro for a model.
''' </summary>
''' <param name="fileName">The name and extension of the macro without any path information.</param>
''' <param name="macroName">The name of the method in the DriveWorks module in the macro to run (defaults to Main).</param>
''' <remarks></remarks>
Friend Sub RunGroupContentMacro(ByVal fileName As String, Optional ByVal macroName As String = DEFAULT_MACRO_METHOD_NAME)
' Make sure the group content folder exists before doing anything else
Static Dim mGcfExists As Boolean = Directory.Exists(mGroupSettings.GroupContentFolder)
If Not mGcfExists Then
Return
End If
' Run the macro
Dim fullPath = IO.Path.Combine(mGroupSettings.GroupContentFolder, "Macros", fileName)
RunMacroCore(fullPath, macroName)
End Sub
''' <summary>
''' Runs a specific macro for a model.
''' </summary>
''' <param name="fileName">The name and extension of the macro without any path information.</param>
''' <param name="macroName">The name of the method in the DriveWorks module in the macro to run (defaults to Main).</param>
''' <remarks></remarks>
Friend Sub RunSharedContentMacro(ByVal fileName As String, Optional ByVal macroName As String = DEFAULT_MACRO_METHOD_NAME)
' Make sure the shared content folder exists before doing anything else
Static Dim mScfExists As Boolean = Directory.Exists(mGroupSettings.SharedContentFolder)
If Not mScfExists Then
Return
End If
' Run the macro
Dim fullPath = IO.Path.Combine(mGroupSettings.SharedContentFolder, "Macros", fileName)
RunMacroCore(fullPath, macroName)
End Sub
Private Sub RunMacroCore(ByVal fullFilePath As String, ByVal macroName As String)
If Not File.Exists(fullFilePath) Then
Return
End If
RunMacro(mEventLog, fullFilePath, macroName)
End Sub
''' <summary>
''' Runs a macro in a module called DriveWorks in the given macro file.
''' </summary>
''' <param name="report">The report to which to log problems/success (optional).</param>
''' <param name="macroFilePath">The path to the macro file.</param>
''' <param name="macroName">The name of the macro to run.</param>
''' <returns>True if the macro was successfully executed, otherwise false.</returns>
''' <remarks></remarks>
Private Function RunMacro(ByVal report As IReportWriter, ByVal macroFilePath As String, ByVal macroName As String) As Boolean
Dim result As Integer ' ByRef
Dim success = mSwApp.RunMacro2(macroFilePath, "DriveWorks", macroName, swRunMacroOption_e.swRunMacroDefault, result)
Dim resultNative = DirectCast(result, swRunMacroError_e)
' If the method doesn't exist, don't report anything
If Not success AndAlso resultNative = swRunMacroError_e.swRunMacroError_InvalidProcname Then
Return False
End If
If success Then
'Report that the macro ran successfully
report.WriteEntry(ReportingLevel.Normal,
ReportEntryType.Information,
"Running SolidWorks Macro",
String.Empty,
String.Format("The SolidWorks Macro '{0}' successfully ran", macroFilePath),
Nothing)
Else
'Report that the macro failed to run
report.WriteEntry(ReportingLevel.Normal,
ReportEntryType.Error,
"Running SolidWorks Macro",
String.Empty,
String.Format("The SolidWorks Macro '{0}' failed to run", macroFilePath, resultNative.ToString),
Nothing)
End If
Return success
End Function
#End Region
End Class