-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCAMModelContext.vb
215 lines (145 loc) · 10 KB
/
CAMModelContext.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
Imports DriveWorks.SolidWorks.Generation
Imports DriveWorks.SolidWorks
Imports DriveWorks.Applications
Imports System.Windows.Forms
Imports System.IO
Public Class CAMModelContext
Private WithEvents mModelGenerationContext As IModelGenerationContext
Private mSolidWorksService As ISolidWorksService
Private mEventLog As IApplicationEventService
Private mEventSourceName As String
Private masterModelName As String
Private WithEvents mSolidWorksIntegration As SolidWorksInteraction
Private mGroupSettings As Generation.GenerationSettings
' CAM Works Objects
Private mCamWorks As CAMWORKSLib.CWApp
Private mCamWorksDoc As CAMWORKSLib.CWDoc
Private mCamWorksPart As CAMWORKSLib.CWPartDoc
Public Sub New(ByVal ModelGenerationContext As IModelGenerationContext, ByVal SolidWorksService As ISolidWorksService, ByVal eventLog As IApplicationEventService, ByVal eventSourceName As String, groupSettings As Generation.GenerationSettings)
' set the member variables based on the incoming arguments
mModelGenerationContext = ModelGenerationContext
mSolidWorksService = SolidWorksService
mEventLog = eventLog
mEventSourceName = eventSourceName
mGroupSettings = groupSettings
' create a new camworks object and make sure it runs without messages or dialogs
mCamWorks = New CAMWORKSLib.CWApp
mCamWorks.SilentMode = True
' get the master model name of the clone being generated by DriveWorks
masterModelName = ModelGenerationContext.Model.MasterPath
End Sub
Private Sub mModelGenerationContext_Finished(sender As Object, e As EventArgs) Handles mModelGenerationContext.Finished
' use this event if you need to take an action when the file has been closed.
End Sub
Private Sub mModelGenerationContext_Saving(sender As Object, e As EventArgs) Handles mModelGenerationContext.Saving
' this event is fired by DriveWorks AFTER driving is complete but BEFORE the model is saved.
' masterModel Macro is the name of the a macro file to run for a specific master model. the macro must be in the same directory as the master model and have the same name (with CAMWorks.swp at the end)
Dim masterModelMacro As String = IO.Path.Combine(IO.Path.GetDirectoryName(masterModelName), IO.Path.GetFileNameWithoutExtension(masterModelName) & "CAMWorks.swp")
' GroupContentmacro is the name of the macro file to be run on All Models
Dim GroupContentMacro As String = "DriveWorks-CamWorks.swp"
' make sure we are still silent
mCamWorks.SilentMode = True
' lets not break anything, or stop DriveWorks from running if we do
Try
'Attach to our SolidWorks wrapper
mSolidWorksIntegration = New SolidWorksInteraction(mSolidWorksService, mGroupSettings, mEventLog)
Dim targetName As String = mSolidWorksIntegration.FileNameWithoutExtension
' first job, lets see if its a part (We don't support assemblies yet)
If Not mSolidWorksIntegration.IsPart Then
' its not a part, report on it and quit
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "File is not a SolidWorks Part. Operation aborted", Nothing, targetName, Nothing)
Return
End If
'Get Camworks Active Doc
mCamWorksDoc = mCamWorks.IGetActiveDoc
If mCamWorksDoc IsNot Nothing Then
' cast the active doc object to a part doc object
mCamWorksPart = DirectCast(mCamWorksDoc, CAMWORKSLib.CWPartDoc)
Else
' no valid doc at all, report and quit
mEventLog.AddEvent(ApplicationEventType.Error, mEventSourceName, "Export CAMWorks Data", "File cannot be converted to a CAMWorks part. Operation aborted", Nothing, targetName, Nothing)
Return
End If
' Add CAMWorks Code here, if we need to run (We are going to check for a custom property (DWCAMWORKS) in the model, which itself can be driven by DriveWorks)
' if it doesn't exist, or is set to something other than False, then we will continue
If Not mSolidWorksIntegration.CreateCAMData Then
' we are not going to run
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Custom property 'DWCAMWORKS' set to false, no further action required", Nothing, targetName, Nothing)
Return
End If
' run the Step1 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step1")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step1")
'Extract Machine Features ?
If mSolidWorksIntegration.PerformExtractMachineFeatures Then
mCamWorks.ActiveDocEMF()
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Extract Machine Features run", Nothing, targetName, Nothing)
Else
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Custom property 'DWCAMWorksEMF' set to false, Extract Machine Features not run", Nothing, targetName, Nothing)
End If
' run the Step2 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step2")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step2")
' Peform Generate Operation Plan
' GOP preference are
' 1 : RETAIN
' 2 : REGENERATE
' 3 : CANCEL
' 4 : QUERY PREFERENCES
Dim operationPlanOption As Integer = mSolidWorksIntegration.GenerateOperationPlanOption
mCamWorks.ActiveDocGOP(operationPlanOption)
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Operation Plan generated with a code of " & operationPlanOption, Nothing, targetName, Nothing)
' run the Step3 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step3")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step3")
' Generate Tool Path
If mSolidWorksIntegration.PerformGenerateToolPath Then
mCamWorks.ActiveDocGTP()
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Generate Tool Path run", Nothing, targetName, Nothing)
Else
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "Custom property 'DWCAMWorksGTP' set to false, Generate Tool Path not run", Nothing, targetName, Nothing)
End If
' run the Step4 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step4")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step4")
' create the POST file
Dim postFile As String = mSolidWorksIntegration.PostProcessPath
If String.IsNullOrEmpty(postFile) Then
mEventLog.AddEvent(ApplicationEventType.Information, mEventSourceName, "Export CAMWorks Data", "POST file not found in custom property 'DWCAMWorksPostFilePath'", Nothing, targetName, Nothing)
Else
' lets check to see if we have a cam works doc
If mCamWorksPart Is Nothing Then
mEventLog.AddEvent(ApplicationEventType.Error, mEventSourceName, "Export CAMWorks Data", "Error creating CAM Data: CAM Document not found", Nothing, targetName, Nothing)
Else
' if the incoming path isn't rooted, make sure we put the file in the same place as the target part
If Not IO.Path.IsPathRooted(postFile) Then
postFile = IO.Path.Combine(mSolidWorksIntegration.FilePath, postFile)
End If
' just incase the folder doesn't exist, lets make sure it does exist
Dim fileDirectory As String = IO.Path.GetDirectoryName(postFile)
If Not IO.Directory.Exists(fileDirectory) Then
IO.Directory.CreateDirectory(fileDirectory)
End If
If IO.Directory.Exists(fileDirectory) Then
' create the post
mCamWorksPart.PostProcess(postFile)
mEventLog.AddEvent(ApplicationEventType.Success, mEventSourceName, "Export CAMWorks Data", "POST file created at: " & postFile, Nothing, targetName, Nothing)
Else
mEventLog.AddEvent(ApplicationEventType.Error, mEventSourceName, "Export CAMWorks Data", "POST file creation aborted, unable to create folder: " & fileDirectory, Nothing, targetName, Nothing)
End If
End If
End If
' run the Step5 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step5")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step5")
' we got this far. No matter what we did, we need to try and save the cam file
mCamWorks.SaveActiveDocument()
' run the Step6 proceedure (Sub) in the gcf macro and the file specific macro
mSolidWorksIntegration.RunGroupContentMacro(GroupContentMacro, "Step6")
mSolidWorksIntegration.RunModelMacro(masterModelMacro, "Step6")
Catch ex As Exception
' disaster. Report
mEventLog.AddEvent(ApplicationEventType.Error, mEventSourceName, "Export CAMWorks Data", ex.ToString, Nothing, "Error creating CAM Data", Nothing)
End Try
End Sub
End Class