I'm looking for a macro in order to reconstruct a piece automatically every 5 seconds.
For now I have a macro that allows me to rebuild a part, but I can't add a loop identical to VBA.
Here is the code to rebuild:
Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Sub main() Set swApp = _ Application.SldWorks Set Part = swApp.ActiveDoc boolstatus = Part.EditRebuild3() End Sub
Here is my loop (VBA type):
Sub time()
Application.OnTime Now + TimeValue("00:00:0"), "main"
Application.OnTime Now + TimeValue("00:00:05"), "time"
End Sub
How can I adapt this loop to Solidworks, or is there another way?
I don't think I'll be the only one to ask myself this question, but what's the point of rebuilding every 5sec?? because it leaves no room / time to work on the piece.
Personally I prefer to use the B key (alt+B by default) and the Ctrl + Q which is more complete.
I use Solidworks as a control screen to remotely control a machine that is not "visible".
I want to know the position and movements of this machine, so I created an equation-driven kinematics that allows me to visualize all the movements. My cutscene updates every 5 seconds, but I have to click rebuild every time to see the movements and I would like it to be automatic.
Ok it's a diversion of the software in a way since SW is not made to be an HMI at the base, but why not as long as you manage to recover the real positions.
By the way , how do you do it, via reason on the machine automaton for example?
Sorry for these questions but it's more to understand the why and how and those who will later appear on your post.
Be careful not to saturate the memory because there is a risk of crashing. When using SW for too long with kinematics and frequent rebuilds, SW ends up crashing and often corrupting the ASM file in the process. You'd better have a duplicate of your ASM. ;-)
Occasionally tell us if it does on your machine what I describe.
To make a loop every 5 seconds I suggest this code:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim oldTime As Date
Dim newTime As Date
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
oldTime = Time
newTime = Time
Do While 1 = 1:
If newTime > oldTime + "0:00:05" Then
boolstatus = Part.EditRebuild3()
oldTime = Time
Else: newTime = Time
End If
Loop
End Sub
However, I don't think it's possible to interact with solidworks when a macro is running in a loop.
On a small file there is this that must be able to work:
Dim swApp As Object
Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim PauseTime, Start
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
If Part Is Nothing Then
MsgBox "Aucun fichier n'est chargé dans SW."
Exit Sub
End If
Do While Part.GetTitle <> ""
PauseTime = 5
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
Set Part = swApp.ActiveDoc
If Part Is Nothing Then
MsgBox "Traitement terminé."
Exit Sub
Else
boolstatus = Part.EditRebuild3()
End If
Loop
End Sub
But I haven't tried on a large file that takes more than 5 seconds to rebuild...