Imports MediaCy.Addins.Scripting
Imports MediaCy.Addins.Measurements
Imports MediaCy.Addins.ThresholdTool.Gadgets
Imports MediaCy.IQL.ObjectManager
Imports MediaCy.IQL.Engine
Imports System

Imports MediaCy.Addins.SCalibration
Imports MediaCy.Addins.SCalibration.Commands
Imports MediaCy.IQL.Calibrations


Public Module OpenImageTestModule
    Public Load_ImageName As String
    Public FirstLoad As Boolean = True
    Public IsStop As Boolean = False
    Public AllImageName() As String
    Public AllImageNumber As Integer
    Public CalibrationSelected As String
    Public SelectNumber As Integer
    Public CalibrationSelectTrue As Boolean
    Public CalibrationName() As String
    Public zero_cali As Boolean


    Public visible_OG As McImage
    Public Imp_image As McImage

    Public Function Applyimage() As SimpleScript

        Applyimage = New SimpleScript
        Dim docList1 = New List(1)

        If FirstLoad = True Or IsStop = True Then
            Exit Function
        End If

        With Application.DocumentCommands.Open(Applyimage)
            .Visible =False
            .Filenames = New String() {Load_ImageName}
            .Run(docList1)
        End With

        visible_OG=docList1(0).data

        ApplyCalibration
        _2D_Filters

        visible_OG.Clear


        With Application.WindowCommands.CloseAll(Applyimage)
            .Run()
        End With



        With Application.DocumentCommands.Active(Applyimage)
            .Run(Nothing)
        End With

    End Function

'     Public Sub GarbageCollect()
'        System.GC.Collect()
'        System.GC.WaitForPendingFinalizers()
'        System.GC.Collect()
'        System.GC.WaitForPendingFinalizers()
'    End Sub

    Public Function GetCalibrationName() As SimpleScript

        GetCalibrationName = New SimpleScript
        Dim im As McImage=ThisApplication.ActiveImage
        Dim CalibrationCount As Integer = 0

        Try
            ReDim CalibrationName(ThisApplication.SpatialCalibs.Count-1)
        Catch
            MsgBox("Please add calibration in IP-10",,"Error")
            zero_cali=True

            Exit Function
        End Try

        zero_cali= False
        For Each GetCalibration As IMcSpatialCalib In ThisApplication.SpatialCalibs
            CalibrationName(CalibrationCount) = GetCalibration.Name
            CalibrationCount += 1
        Next

    End Function

    Public Function ApplyCalibration() As SimpleScript
            '
        ApplyCalibration = New SimpleScript

        Dim var1 = CalibrationSelected, spcal1

        If visible_OG Is Nothing And ThisApplication.ActiveImage Is Nothing Then'
            Exit Function
        End If

        If CalibrationSelectTrue =False Then
            Exit Function
        End If

            With Measure.Calibration.SpatialCommands.Define(ApplyCalibration)
                .Run(var1, spcal1)
            End With


            With Measure.Calibration.SpatialCommands.Apply(ApplyCalibration)'this Code Pile up memory
                .Run(visible_OG, spcal1)
            End With

    End Function



	Public Function _2D_Filters() As SimpleScript
		_2D_Filters = New SimpleScript


        With Adjust.ImageCommands.Duplicate(_2D_Filters)
            .Visible =False
            .Name = "scratch_imp"
            .Run(visible_OG, Imp_image)
        End With

		With Process.Filter.MorphologicalCommands.Close(_2D_Filters)
			.Passes = 3
			.Shape = MediaCy.IQL.Filters.mcMorphoShape.mcms2x2Square
            .Run(Imp_image, Imp_image)
        End With

        Imp_image.Clear
        Imp_image.Close

	End Function

End Module
