BIM World
A Professional BIM Learning Platform


BIM Q&A | Four Effective Ways to Calculate Delay Using CATIA VBA Secondary Development

Source: CATIA Little Ant

BIM Q&A | CATIA Secondary Development: Four Feasible Methods for Obtaining Delay in CATIA VBA

When working on CATIA secondary development, there are times when you need to pause the program execution and let CATIA idle for a moment.

Here are four effective methods to implement delays in CATIA VBA:

1. Basic Delay Using Timer Function (Timing unit: seconds; 1 = 1 second)

If you want to limit the execution speed of an application interface and add a delay within a loop, the Timer function is a simple and convenient choice. While it’s not extremely precise, it’s suitable for delays of a few seconds. Timer is a built-in VBA function and is widely recommended in VB manuals. The general structure is as follows:

Sub delay(T As Single)
Dim time1 As Single
time1 = Timer
Do While Timer - time1 < T
  DoEvents 'Allows the OS to process other events
Loop
Debug.Print "Run completed, total time taken: " & Timer - time1 & " seconds"
End Sub

You can call it like this:

Sub calculate1_time()
delay (1.5)
End Sub

BIM Q&A | CATIA Secondary Development: Four Feasible Methods for Obtaining Delay in CATIA VBA

2. Precise Delay Using Sleep Function (Timing unit: milliseconds; 1000 = 1 second)

For more precise delays, you can use the Sleep function, a Windows API call that pauses the current thread for a specified number of milliseconds. You must declare it before use:

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

The Sleep function suspends the current thread without consuming CPU resources, unlike DoEvents which yields control to other processes but still keeps the CPU fully busy. For example, Sleep 1000 delays execution by 1 second.

Here is a sample implementation:

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub calculate2_time()
Dim d As Date
d = Time()
Sleep 3000 'Pause for 3 seconds
Debug.Print "End of run, total time: " & DateDiff("s", d, Time()) & " seconds"
End Sub

3. Accurate Delay Using timeGetTime Function (Timing unit: milliseconds; 1000 = 1 second)

A more reliable method involves the timeGetTime Windows API function, which returns the number of milliseconds since system start. It supports millisecond-level precision and won’t roll over for about 49.7 days.

You must declare it before use:

Private Declare Function timeGetTime Lib "winmm.dll" () As Long

This delay function works similarly to the first method but uses timeGetTime instead of Timer. The DoEvents statement ensures the system remains responsive:

Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
Sub delay(T As Long)
Dim time1 As Long
time1 = timeGetTime
Do
  DoEvents 'Allows OS to handle other events
Loop While timeGetTime - time1 < T
End Sub

Calling example:

Sub ce_time()
Dim d As Date
d = Time()
Call delay(1000) 'Delay for 1 second
Debug.Print "Run completed, total time taken: " & DateDiff("s", d, Time()) & " seconds"
End Sub

BIM Q&A | CATIA Secondary Development: Four Feasible Methods for Obtaining Delay in CATIA VBA

4. Smart Use of VBA’s Built-in DateAdd Function (Timing unit: seconds; 1 = 1 second)

You can also create a delay by comparing the current time with a target end time using the DateAdd function:

Public Sub BKWait(HowManySecs As Integer)
  'Pause execution for HowManySecs seconds
  Dim EndWait As Date
  EndWait = DateAdd("s", HowManySecs, Now)
  While Now < EndWait
    'Do nothing during the wait
  Wend
End Sub

Example usage:

Sub calculate4()
BKWait(5) 'Pause for 5 seconds
End Sub

xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » BIM Q&A | Four Effective Ways to Calculate Delay Using CATIA VBA Secondary Development

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up