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 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 Applyimage()


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


        visible_OG=ThisApplication.Images.Open(Load_ImageName,mcImageCreateFlags.mcicfNotVisible)

        ApplyCalibration
        _2D_Filters


        visible_OG.Close 'close image
        visible_OG=Nothing


    End Function




    Public Function ApplyCalibration()


        Dim var1 = CalibrationSelected, spcal1


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


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

        visible_OG.SpatialCalibration=spcal1


    End Function


    Public Function _2D_Filters()



        Imp_image=visible_OG 'Avoid [Duplicate] step

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

        Imp_image.Close
        Imp_image=Nothing


	End Function

End Module
