Inserting a Waveform Group Into a Data Set Example for VB.NET

'Inserting a Waveform Group Into a Data Set Example

'*****************************************************************'
'* This is a comprehensive sample, which shows how to insert a *'
'* waveform group with one ECG channel into a Data Set. *'
'* The main function is InsertECGWaveform; the rest of the *'
'* functions are helping functions. *'
'*****************************************************************'

' The main function that creates the waveform group and adds to the Data Set
Function InsertECGWaveform(ByRef objDataSet As LTDICLib.LEADDicomDS, ByRef Samples() As Short) As Boolean
   InsertECGWaveform = False

   ' Our new waveform group
   Dim objECGWaveformGroup As New LTDICLib.DicomWaveformGroup

   objECGWaveformGroup.EnableMethodErrors = False

   ' Reset the waveform group, we don't really need to call this!
   objECGWaveformGroup.Reset()

   ' Set the Waveform Sample Interpretation
   objECGWaveformGroup.SampleInterpretation = LTDICLib.DicomWaveformSampleInterpretation.WAVEFORM_SAMPLE_INTERPRETATION_SS

   Dim lNumberOfSamples As Integer lNumberOfSamples = UBound(Samples) - LBound(Samples) + 1

   ' Set the Number of Waveform Samples. You can obtain this number by accessing
   ' the NumberOfSamplesPerChannel property.
   If objECGWaveformGroup.SetNumberOfSamplesPerChannel(lNumberOfSamples) <> LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      Exit Function
   End If

   ' Set the Sampling Frequency
   objECGWaveformGroup.SamplingFrequency = 240.0#

   ' No Multiplex Group Time Offset. When the value is defined, it can be set using the
   ' MultiplexGroupTimeOffset property.
   objECGWaveformGroup.ValueDefined(LTDICLib.DicomWaveformGroupValues.MULTIPLEX_GROUP_TIME_OFFSET) = False

   ' No Trigger Time Offset. When the value is defined, it can be set using the
   ' TriggerTimeOffset property.
   objECGWaveformGroup.ValueDefined(LTDICLib.DicomWaveformGroupValues.TRIGGER_TIME_OFFSET) = False

   ' No Trigger Sample Position. When the value is defined, it can be set using the
   ' TriggerSamplePosition property.
   objECGWaveformGroup.ValueDefined(LTDICLib.DicomWaveformGroupValues.TRIGGER_SAMPLE_POSITION) = False

   ' Waveform Originality is ORIGINAL
   objECGWaveformGroup.WaveformOriginality = LTDICLib.DicomWaveformOriginality.WAVEFORM_ORIGINALITY_ORIGINAL

   ' Set the Multiplex Group Label
   objECGWaveformGroup.ValueDefined(LTDICLib.DicomWaveformGroupValues.MULTIPLEX_GROUP_LABEL) = True
   objECGWaveformGroup.MultiplexGroupLabel = "SCPECG Waveform"

   ' Set the Waveform Padding Value
   objECGWaveformGroup.ValueDefined(LTDICLib.DicomWaveformGroupValues.WAVEFORM_PADDING_VALUE) = True
   objECGWaveformGroup.WaveformPaddingValue = 32768

   If Not InsertECGChannel(objDataSet, objECGWaveformGroup, Samples) Then
      Exit Function
   End If

   ' Delete any waveform groups that already exist in the Data Set
   Do While objDataSet.GetWaveformGroupCount() > 0
      objDataSet.DeleteWaveformGroup(0)
   Loop

   ' Insert the new waveform group into the Data Set
   If objDataSet.AddWaveformGroup(objECGWaveformGroup) <> LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      Exit Function
   End If

   InsertECGWaveform = True
End Function

 

' Adds an ECG channel to the group
Function InsertECGChannel(ByRef objDataSet As LTDICLib.LEADDicomDS, ByRef objWaveformGroup As LTDICLib.DicomWaveformGroup, ByRef Samples() As Short) As Boolean
   InsertECGChannel = False

   Dim objECGWaveformChannel As LTDICLib.DicomWaveformChannel

   ' Add a channel to the group
   On Error Resume Next
   objECGWaveformChannel = objWaveformGroup.Channels.Add ()
   If Err.Number <> 0 Then
      Exit Function
   End If
   On Error GoTo 0

   ' Set the samples of the channel
   If objECGWaveformChannel.SetChannelSamples(Samples) < 0 Then
      Exit Function
   End If

   ' Set the Channel Source and Sensitivity
   If Not SetChannelSourceAndSensitivity(objDataSet, objECGWaveformChannel) Then
      Exit Function
   End If

   ' Set the Channel Status
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_STATUS) = True
   objECGWaveformChannel.ChannelStatus = LTDICLib.DicomWaveformChannelStatus.CHANNEL_STATUS_OK

   ' Set the Channel Time Skew
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_TIME_SKEW) = True
   objECGWaveformChannel.ChannelTimeSkew = 0.0#

   ' Set the Waveform Channel Number
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.WAVEFORM_CHANNEL_NUMBER) = True
   objECGWaveformChannel.WaveformChannelNumber = 0

   ' Set the Channel Label
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_LABEL) = True
   objECGWaveformChannel.ChannelLabel = "First Channel"

   ' No Channel Offset. When the value is defined, it can be set using the ChannelOffset
   ' property objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_OFFSET) = False

   ' Set the Filter Low Frequency
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.FILTER_LOW_FREQUENCY) = True
   objECGWaveformChannel.FilterLowFrequency = 0.05

   ' Set Filter High Frequency
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.FILTER_HIGH_FREQUENCY) = True
   objECGWaveformChannel.FilterHighFrequency = 100.0#

   ' Set the Channel Minimum Value
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_MINIMUM_VALUE) = True
   objECGWaveformChannel.ChannelMinimumValue = -386

   ' Set the Channel Maximum Value
   objECGWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_MAXIMUM_VALUE) = True
   objECGWaveformChannel.ChannelMaximumValue = 1264

   ' When the Notch Filter Frequency and Bandwidth are defined, they can be set using the
   ' NotchFilterFrequency and NotchFilterBandwidth properties.

   ' Last, but not least, set the channel annotations!
   InsertECGChannel = SetChannelAnnotations(objECGWaveformChannel)
End Function

' Sets the Channel Source and Sensitivity
Function SetChannelSourceAndSensitivity(ByRef objDataSet As LTDICLib.LEADDicomDS, ByRef objWaveformChannel As LTDICLib.DicomWaveformChannel) As Boolean
   SetChannelSourceAndSensitivity = False

   ' We will use the Context Group Table

   ' -------------------- Channel Source --------------------

   Const CID_3001 As String = "CID 3001"

   ' Load the ECG Leads Context Group
   objDataSet.LoadContextGroup(CID_3001)
   If Not objDataSet.FindContextGroup(CID_3001) Then
      Exit Function
   End If

   ' 5.6.3-9-1 is Lead I (Einthoven)
   If Not objDataSet.FindCodedConcept("SCPECG", "5.6.3-9-1") Then
      Exit Function
   End If

   ' Set the Channel Source
   With objWaveformChannel.ChannelSource
      .CodingSchemeDesignator = objDataSet.CurrentCodedConcept.CodingSchemeDesignator
      .CodingSchemeVersion = objDataSet.CurrentCodedConcept.CodingSchemeVersion
      .CodeValue = objDataSet.CurrentCodedConcept.CodeValue
      .CodeMeaning = objDataSet.CurrentCodedConcept.CodeMeaning
   End With

   ' -------------------- Channel Sensitivity --------------------

   objWaveformChannel.ValueDefined(LTDICLib.DicomWaveformChannelValues.CHANNEL_SENSITIVITY) = True

   ' The Channel Sensitivity
   objWaveformChannel.ChannelSensitivity = 0.00122

   Const CID_3082 As String = "CID 3082"

   ' Load the Cardiology Units of Measurement Context Group
   objDataSet.LoadContextGroup(CID_3082)
   If Not objDataSet.FindContextGroup(CID_3082) Then
      Exit Function
   End If

   If Not objDataSet.FindCodedConcept("UCUM", "mV") Then
      Exit Function
   End If

   ' The Channel Sensitivity Units
   With objWaveformChannel.ChannelSensitivityUnits
      .CodingSchemeDesignator = objDataSet.CurrentCodedConcept.CodingSchemeDesignator
      .CodingSchemeVersion = objDataSet.CurrentCodedConcept.CodingSchemeVersion
      .CodeValue = objDataSet.CurrentCodedConcept.CodeValue
      .CodeMeaning = objDataSet.CurrentCodedConcept.CodeMeaning
   End With

   ' The Channel Sensitivity Correction Factor
   objWaveformChannel.ChannelSensitivityCF = 1.0#

   ' The Channel Baseline
   objWaveformChannel.ChannelBaseline = 0.0#

   SetChannelSourceAndSensitivity = True
End Function

Function SetChannelAnnotations(ByRef objWaveformChannel As LTDICLib.DicomWaveformChannel) As Boolean
   SetChannelAnnotations = False

   ' Delete any existing channel annotations
   Do While objWaveformChannel.Annotations.Count > 0
      objWaveformChannel.Annotations.Remove(0)
   Loop

   Dim objECGWaveformAnnotation As LTDICLib.DicomWaveformAnnotation

   ' Add an annotation object

On Error Resume Next

   objECGWaveformAnnotation = objWaveformChannel.Annotations.Add()
   If Err.Number <> 0 Then
      Exit Function
   End If

On Error GoTo 0

   ' Our annotation is defined by a Coded Name/Numeric Measurement pair
   objECGWaveformAnnotation.ValueType = LTDICLib.DicomWaveformAnnValueType.TYPE_CODED_NAME_AND_NUMERIC_VALUE

   ' The Coded Name. Note: Instead of setting these values directly, you can also use
   ' the Context Group Table; the Context Groups defined by the DICOM Content Mapping
   ' Resource (DCMR) can be loaded into this table. Take a look at the function
   ' SetChannelSourceAndSensitivity above for an example.
   With objECGWaveformAnnotation.CodedName
      .CodingSchemeDesignator = "LN"
      .CodingSchemeVersion = "19971101"
      .CodeValue = "8867-4"
      .CodeMeaning = "Heart rate"
   End With

   ' The Numeric Value

On Error Resume Next

   objECGWaveformAnnotation.NumericValueCount = 1
   If Err.Number <> 0 Then
      Exit Function
   End If

On Error GoTo 0

   objECGWaveformAnnotation.NumericValue(0) = 69.0#

   ' The Measurement Units. Refer to the note given when the Coded Name was set.
   objECGWaveformAnnotation.ValueDefined(LTDICLib.DicomWaveformAnnValues.MEASUREMENT_UNITS) = True
   With objECGWaveformAnnotation.MeasurementUnits
      .CodingSchemeDesignator = "UCUM"
      .CodingSchemeVersion = "1.4"
      .CodeValue = "{H.B.}/min"
      .CodeMeaning = "Heart beat per minute"
   End With

   SetChannelAnnotations = True
End Function