from vcScript import *
import csv

app = getApplication()
comp = getComponent()
sensor_signal = comp.findBehaviour('SensorSignal')
product_id, material, pass_through_time = None, None, None
uri = r''
unicode_uri = unicode(uri)

def OnStart():
  with open(unicode_uri, 'wb') as csv_file:
    csv_writer = csv.writer(csv_file, delimiter = ',')
    csv_writer.writerow(['ProductID', 'Material', 'PassThroughTime'])

def OnSignal( signal ):
  global product_id,material, pass_through_time
  if signal.Name == "SensorSignal" and sensor_signal.Value:
    part = sensor_signal.Value
    if part.getProperty("SimulationTime") and part.getProperty("ProductID"):
      sim_time1 = part.getProperty("SimulationTime").Value
      sim_time2 = app.Simulation.SimTime
      pass_through_time = sim_time2 - sim_time1
      product_id = part.getProperty("ProductID").Value
      material = part.getProperty("Material").Value.Name
      with open(unicode_uri, 'a') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter = ',',lineterminator='\n')
        csv_writer.writerow([product_id, material, pass_through_time])