Generating txt.file on trigger

Hi guys,
I was wondering if its possible to generate txt file on trigger using a signal
For example if I’m having a part on conveyor passing through conveyor signal, how can I generate a txt file when the signal is true.

Thanks in advance :slight_smile:

You select the Signal (Modelling Tab) and in the Field “Listeners” you add your PythonScript.
Then you need to extend the function OnSignal which is always executed if the SignalValue changes state.

from __future__ import print_function
from vcScript import *
import os


def OnSignal( signal ):
  home = os.path.expanduser("~")
  file = os.path.join(home, "Desktop/test.txt")
  with open(file, "w") as fd:
    fd.write("Hello World")
    fd.write("\n")
    text = str(signal.Value)
    fd.write(text)
    fd.write("\n")

There is one possible problem. If your HDD is in SleepMode and you get this signal, then the whole simulation should block until the OnSignal function were handled (data were written). This function does also not allow time taking movements of a servo controller.

Thanks a lot. You sir are a hero.
One last thing…is there a way I can read the product ID when it passes through the signal

Thanks :slight_smile:

There are different types of Signals:

  • BoolSignal
  • ComponentSignal
  • IntegerSignal
  • MatrixSignal
  • RealSignal
  • StringSignal

You need to get the ComponentSignal, where the Value is the component.

I attached an example and a video.

belt_with_productid.vcmx (776.3 KB)
2020-03-31 15-23-35.mkv (94.9 MB)

If someone else has better ideas how to solve it, please tell us.

As you can see, I never used the RangeSignal. Is it always sampled even if there is nothing on the path?
What I totally dislike is how to detect types. Is there not a better way?

1 Like

Hello @deadeye

This does the same thing as you did but coding is simpler.

def OnSignal(signal):
  if signal.Name == 'ComponentSignal' and signal.Value != None:
    part = signal.Value
    print 'ProductID: ', part.PruductID  ,' Name: ' , part.Name
    print 'Product material ',part.Material.Name
1 Like