'!TITLE "Robot program"

Function CamAPI_ResolveControllerHandle(ByVal name as String) as Object

	Dim ctrlHandle As Object
	CamAPI_ResolveControllerHandle = Cao.Controllers(name)

end function

'### Get the last request error from camera (see error code in camera doc)
Function CamAPI_LastProviderError(ByRef ctrlHandle as object) as string

	CamAPI_LastProviderError = ctrlHandle.Variables("@LASTERRORDLL").Value

end function

'### Get the last request error from camera (see error code in camera doc)
Function CamAPI_LastCamError(ByRef ctrlHandle as object) as integer

	CamAPI_LastCamError = ctrlHandle.Variables("@LASTERRORCAM").Value

end function

'### Get the last request error from camera by name (see error code in camera doc)
Function CamAPI_LastCamErrorByName(ByVal name as String) as integer

	Dim ctrlHandle As Object
	ctrlHandle = CamAPI_ResolveControllerHandle(name)
	CamAPI_LastCamErrorByName = ctrlHandle.Variables("@LASTERRORCAM").Value

end function

'### Get se last request Position 
' (After 'Run.Locate, [JOBS]) the first found pos)
' (After 'Run.Locate, [JOB], [MATCH} the position of given match)
Function CamAPI_CurrentPosition(ByRef ctrlHandle as object) as Position

	CamAPI_CurrentPosition = P(ctrlHandle.Variables("@X").Value, ctrlHandle.Variables("@Y").Value, ctrlHandle.Variables("@Z").Value, ctrlHandle.Variables("@RX").Value, ctrlHandle.Variables("@RY"). Value, ctrlHandle.Variables("@RZ").Value)

end function

'### Get se last request Position (by name)
' (After 'Run.Locate, [JOBS]) the first found pos)
' (After 'Run.Locate, [JOB], [MATCH} the position of given match)
Function CamAPI_CurrentPositionByName(ByVal name as string) as Position

	Dim ctrlHandle As Object
	ctrlHandle = CamAPI_ResolveControllerHandle(name)
	CamAPI_CurrentPositionByName = CamAPI_CurrentPosition(ctrlHandle)

end function

'### Get next position
Sub CamAPI_Next(ByRef ctrlHandle as object)

	Call ctrlHandle.Execute("Next","") 

end Sub

Sub CamAPI_NextByName(ByVal name as string)

	Dim ctrlHandle As Object
	ctrlHandle = CamAPI_ResolveControllerHandle(name)
	Call CamAPI_Next(ctrlHandle)
end Sub

'### Get next position
Sub CamAPI_Prev(ByRef ctrlHandle as object)

	Call ctrlHandle.Execute("Prev","") 

end Sub

Sub CamAPI_PrevByName(ByVal name as string)

	Dim ctrlHandle As Object
	ctrlHandle = CamAPI_ResolveControllerHandle(name)
	Call CamAPI_Prev(ctrlHandle)
end Sub

'### Init Camera
'# Args: Name for controller instabce,  Ip address (hostname), port of device, response timeout) 
'# Return: Controller handle
Function CamAPI_Init(ByVal name as String, ByVal ip as String, ByVal port as integer, byVal timeout as integer) as object

	Dim ctrlHandle As Object
	Dim connString as string

	connString = "@EventDisable=false,Device=Inspector,IP=" & ip & ",Port=" & port & ",Timeout=" & timeout & ""
	S[2] = connString
	ctrlHandle = Cao.AddController(name, "CaoProv.SickAG.PLOC2D", "", connString) '"@EventDisable=false")

	CAll ctrlHandle.AddVariable("@X", "")
	CAll ctrlHandle.AddVariable("@Y", "")
	CAll ctrlHandle.AddVariable("@Z", "")
	CAll ctrlHandle.AddVariable("@RY", "")
	CAll ctrlHandle.AddVariable("@RX", "")
	CAll ctrlHandle.AddVariable("@RZ", "")
	CAll ctrlHandle.AddVariable("@JOB", "")
	CAll ctrlHandle.AddVariable("@MATCH", "")
	CAll ctrlHandle.AddVariable("@MATCHES", "")
	CAll ctrlHandle.AddVariable("@LASTERRORCAM", "")
	CAll ctrlHandle.AddVariable("@LASTERRORDLL", "")
	CamAPI_Init = ctrlHandle

end function

'### Destroys Camera with the givven 'handle'
'# Args: Controller Handle
Sub CamAPI_Destroy(ByRef ctrlHandle as object)

	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@X").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@Y").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@Z").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@RY").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@RX").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@RZ").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@JOB").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@MATCH").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@MATCHES").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@LASTERRORCAM").Index)
	call ctrlHandle.Variables.Remove(ctrlHandle.Variables("@LASTERRORDLL").Index)
	cao.Controllers.Remove ctrlHandle.Index

end sub

'### Destroys Camera with the given controller-'name' instance to
'# Args: Controller instance name
Sub CamAPI_DestroyByName(ByVal name as String)

	Dim ctrlHandle As Object
	ctrlHandle = 	Cao.Controllers(name)
	Call CamAPI_Destroy(ctrlHandle)

end sub

'### Locate all Targets with the given controller-'name' instance
'# Args: Controller instance handle, Job id
'# Return : Count of matches
Function CamAPI_LocateAllByName(ByVal name as String, ByVal jobIndex as Integer) as integer

	Dim locateResult as Variant 
	Dim ctrl As Object
	ctrl = 	Cao.Controllers(name)
	CamAPI_LocateAllByName = CamAPI_LocateAll(ctrl, jobIndex)

end Function

'### Locate all Targets with the given controller-'handle' instance
'# Args: Controller instance name, Job id
'# Return : Count of matches
Function CamAPI_LocateAll(ByRef ctrl as object, ByVal jobIndex as Integer) as integer

	Dim locateResult as Variant 	
	Dim parameter as String

	parameter = "" & jobIndex & ""
	locateResult = ctrl.Execute("LocateTargets", parameter)
	CamAPI_LocateAll = ctrl.Variables("@MATCHES").Value

end Function

'### Locate target by index with the given controller-'name' instance
'# Args: Controller instance name, Job id, match index
'# Return : Position at index
Function CamAPI_LocateAtByName(ByVal name as String, ByVal jobIndex as Integer, byVal index as integer) as position

	Dim locateResult as Variant 
	Dim ctrl As Object
	ctrl = 	Cao.Controllers(name)
	CamAPI_LocateAtByName = CamAPI_LocateAt(ctrl, jobIndex, index)

end Function

'### Locate target by index with the given controller-'handle' instance
'# Args: Controller instance name, Job id, match index
'# Return : Position at index
Function CamAPI_LocateAt(ByRef ctrl as object, ByVal jobIndex as Integer, byVal index as integer) as position

	Dim locateResult as Variant 	
	Dim parameter as String
	
	parameter = "" & jobIndex & "," & index & ""
	locateResult = ctrl.Execute("SelectTarget", parameter) 
	CamAPI_LocateAt = CamAPI_CurrentPosition(ctrl)

end Function
