Grasshopper

algorithmic modeling for Rhino

How convert integer to hours, minutes and seconds in VB and problem of time/counter synchrony

I want to convert seconds to hours, minutes and seconds,


for example, 123 seconds

123/3600 = 0.03416 hours
we stayed with the integer part (0h)

and decimal part: 0.03416 * 60 = 2, 05 min
we stayed with the integer part (2 ')

and decimal part: 0.05 * 60 = 3 s
and we stayed with the decimal part (3'')

therefore, 123 seconds is 0h 2 min 3 s

Well, I have a counter and I can not convert the integer in real seconds ...

Maybe I'm doing wrong:
if the timer interval is 0020 seconds, so if the counter reaches 250, it means that it has been 5 seconds (interval timer * counter)

However, it takes longer than 5 seconds to process the script, so I don't get the real time that the counter is active.

How could get the real time (in seconds) maintaining a timer component in 20 ms?

I am new to VB, so any help with this or to pass the integer to hours, minutes and seconds will be very grateful. 

Muchas gracias :)

Views: 6770

Attachments:

Replies to This Discussion

For a quick look.

Private Sub RunScript(ByVal pause As Boolean, ByVal reset As Object, ByVal max As Object, ByVal s As Double, ByRef C As Object)

If reset Then
  int = 0
End If

If pause = False Then
  int = int + 1
End If

C = int

Dim cero As Integer = 0
If int = 0 Then
  C = cero
End If

Dim final As Boolean
  final = int >= max
If final Then
  C = max
End If

Dim stoptimer As Boolean
  Select Case final
    Case 0
    stoptimer = pause
    Case 1
    stoptimer = final
  End Select

'I Need convert C to real hours, minutes and seconds(hms as 0:00:00)
'First, convert C to seconds and here is the problem
Dim hms As Double
hms = C * s
print(hms)


Dim ghdoc As GH_Document = owner.OnPingDocument()
If (ghdoc Is Nothing) Then Return
For Each docobj As IGH_DocumentObject In ghdoc.Objects
  Dim timer As Special.GH_Timer = TryCast(docobj, Special.GH_Timer)
  If (timer Is Nothing) Then Continue For
  If (timer.Targets.Contains(owner.InstanceGuid)) Then
  timer.Locked = stoptimer
  End If
Next

End Sub

'<Custom additional code>


Dim timer As New System.Windows.Forms.Timer
Dim pause As Boolean
Dim max As Integer
Dim int As Integer = 0


'</Custom additional code>
End Class

To pass a number of hours, minutes and seconds in VB, this works:

Private Sub RunScript(ByVal Num As Integer, ByRef A As Object) 

Dim horas As Double
horas = Num / 3600
Dim h As Integer
h = horas
Dim h_dec As Double
h_dec = math.Abs(h - horas)

Dim minutos As Double
minutos = h_dec * 60
Dim m As Integer
m = minutos
Dim m_dec As Double
m_dec = math.Abs(m - minutos)

Dim segundos As Double
segundos = m_dec * 60
Dim s As Integer
s = segundos

Dim hms As String
hms = h & "h " & m & "m " & s & "s"

A = hms

But I still can not analyze the running time counter ...

The quickest way is just to use the .net TimeSpan class

http://msdn.microsoft.com/en-us/library/system.timespan.aspx

you can then use this little piece of code to turn it into seperate time parts:

Dim span As TimeSpan = TimeSpan.FromSeconds(seconds)
days = span.Days
hours = span.Hours
minutes = span.Minutes
second = span.Seconds

I lost your reply at the time Arend, thank you very much.
You have any idea how to sync a counter with real seconds? If I put the timer to 0.020 seconds, 50 iterations supposed to happen in 1 second. But it takes much longer, I guess that is processing the script. So, if I'm not mistaken, by measuring the time it takes to run the script, you could synchronize the time interval and iterations with real-time event. I found information on msdn on how measuring the time of a script, but my level of scripting still does not allow me understand how to do in grasshopper. You have any idea?

Any code that runs at the mercy of an operating system which has full control over time-slicing can not be relied upon to execute any code at a specific time. It'll have to await its turn. You can measure how long a specific operation takes, but to achieve a 100% synchronisation is probably a pipe-dream.

The easiest way to measure how much time elapses between any two lines of code is to use the Stopwatch class in the System.Diagnostics namespace.

--

David Rutten

david@mcneel.com

In his day had reached stopwatch, but I can not instantiate, it gives me error, says that is not defined.
I tried as in the example of msdn and other example of this page, but nothing. This happens to me frequently and I feel confused, because there examples of rhinocommonSDK I'm unable to perform in gh syntax. And grasshopperSDK is very little detailed for me. 
Anyway, I found an alternative solution to my problem.
Thank you

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service