Macro: Rebuild in a loop (every 5 seconds)

Hello

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?

 

Thank you for your help

Hello

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.

3 Likes

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.

Is it normal that line "Application.OnTime Now + TimeValue("00:00:05"), "time" is located between "Sub time()" and "End Sub"?

From the little I practice VBA, I have the impression that the "time" function calls itself.

FUZ3D, coders allow me to retrieve the data. sorry I can't go into more detail, privacy concerns.

Stefbeno, it allows me to tour in a loop

Hello @ f.michaud

I have a little HS

[HS On]

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.

[HS /Off]

Kind regards

Hello 

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.

Hoping that it can help you out.

 

Gauthik

1 Like

Hello

Do you think that with a large file it can replace heating?

Ok I'm going out :-)

2 Likes

Hello

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...

Kind regards

2 Likes

 D.Roger, thank you very much it works perfectly!!

Gauthik67, thanks but when I launch the macro, solidworks crashes instantly.

Zozo_mp, I take note thank you, however my piece is only made up of a single sketch so quite light.

Well done d.roger, it's the "DoEvents" that was missing from my code! I didn't know it existed, we learn every day! :)

2 Likes

Hello

So all that remains is to validate the answer that helped you the most to solve your problem.

Kind regards

1 Like