Update
After investigating more closely the CODE 2 sample below (at the end of this post), it does check out spatial analyst (sa) extension, however it does not reflect it in the Tools>Extensions menu option as being checked. However you can still use the sa tools.
--------------Original question---------------
I would like to checkout spatial analyst extension for ArcMap 9.3.1 via this ArcObjects code below, but when I run the code (CODE1) I get these two message box statements and the extension does not checkout under the Tools>Extensions dialog.
"You license has already been initialized please check you implementation"
AND
"SpatialAnalyst license has been checked out for use"
I'm not sure what exactly the first message box means? Any suggestions would be greatly appreciated.
I also found this code (CODE2) to turn on spatial analyst extension, but it does not work either. Maybe I'm missing something obvious with this one?
CODE 1
Private Sub UIButtonControl6_Click()
Dim licenseStatus As esriLicenseStatus
licenseStatus = CheckOutLicense(esriLicenseProductCodeEngine)
If (licenseStatus = esriLicenseNotLicensed) Then
licenseStatus = CheckOutLicense(esriLicenseProductCodeArcView)
If (licenseStatus = esriLicenseNotLicensed) Then
licenseStatus = CheckOutLicense(esriLicenseProductCodeArcEditor)
If (licenseStatus = esriLicenseNotLicensed) Then
licenseStatus = CheckOutLicense(esriLicenseProductCodeArcInfo)
End If
End If
End If
'Take a look at the licenseStatus to see if it failed
'Not licensed
If (licenseStatus = esriLicenseNotLicensed) Then
MsgBox "You are not licensed to run this product"
'The licenses needed are currently in use
ElseIf (licenseStatus = esriLicenseUnavailable) Then
MsgBox "There are insufient licenses to run"
'The licenses unexpected license failure
ElseIf (licenseStatus = esriLicenseFailure) Then
MsgBox "Unexpected license failure please contact you administrator'"
'Already initialized (Initialization can only occur once)
ElseIf (licenseStatus = esriLicenseAlreadyInitialized) Then
MsgBox "You license has already been initialized please check you implementation"
'Everything was checkedout successfully
ElseIf (licenseStatus = esriLicenseCheckedOut) Then
MsgBox "Licenses checked out successfully"
End If
Dim bAlreadyCheckedOut As Boolean
bAlreadyCheckedOut = m_pAoInitialize.IsExtensionCheckedOut(esriLicenseExtensionCodeSpatialAnalyst)
If (Not bAlreadyCheckedOut) Then
licenseStatus = m_pAoInitialize.CheckOutExtension (esriLicenseExtensionCodeSpatialAnalyst)
If (licenseStatus = esriLicenseUnavailable) Then
MsgBox "All SpatialAnalyst licenses are currently in use"
ElseIf (licenseStatus = esriLicenseCheckedOut) Then
licenseStatus = esriLicenseCheckedOut
MsgBox "SpatialAnalyst license has been checked out for use"
Else
MsgBox "Unexpected licensing failure contact you administor"
End If
End If
End Sub
Private Function CheckOutLicense(productCode As esriLicenseProductCode) As esriLicenseStatus
Dim licenseStatus As esriLicenseStatus
Set m_pAoInitialize = New AoInitialize
CheckOutLicense = esriLicenseUnavailable
'Check the productCode
licenseStatus = m_pAoInitialize.IsProductCodeAvailable(productCode)
If (licenseStatus = esriLicenseAvailable) Then
'Check the extensionCode
licenseStatus = m_pAoInitialize.IsExtensionCodeAvailable(productCode, esriLicenseExtensionCodeSpatialAnalyst)
If (licenseStatus = esriLicenseAvailable) Then
'Initialize the license
licenseStatus = m_pAoInitialize.Initialize(productCode)
End If
End If
CheckOutLicense = licenseStatus
End Function
CODE 2
Dim gp As Object
gp = CreateObject("esriGeoprocessing.GPDispatch.1")
If gp.CheckExtension("spatial") = "NotLicensed" Then
MessageBox.Show("NotLicensed")
ElseIf gp.CheckExtension("spatial") = "Unavailable" Then
MessageBox.Show("Unavailable")
ElseIf gp.CheckExtension("spatial") = "NotInitialized" Then
MessageBox.Show("NotInitialized")
ElseIf gp.CheckExtension("spatial") = "Available" Then
gp.CheckOutExtension("spatial")
MessageBox.Show("Checkout")
End If
1 Answer 1
You have to distinguish between product licenses and extension licenses. ArcGIS Desktop as a whole is licensed at three product levels: ArcView, ArcEditor, and ArcInfo.
In addition to the product itself there are various extensions that can be licensed. Spatial Analyst is one such extension.
- If your code is running from within ArcMap there will already be a product license in use of some kind, otherwise ArcMap would not be running. That is why you are getting the first message. Typically you will only run explicit code to check out a product license in an ArcEngine application.
- The second piece of code you have is going through the geoprocessing framework to check out an SA license. Unless you're in a Python GP script, this is a poor way of doing things. In ArcObjects your first code segment is the way to handle licenses.
UPDATE
To programmatically enable an extension in a Desktop application follow the instructions in the How to use extensions help topic. VB.NET code copied directly from the help:
Dim factoryType As Type = Type.GetTypeFromProgID("esriSystem.ExtensionManager")
Dim extensionManager As IExtensionManager = CType(Activator.CreateInstance(factoryType), IExtensionManager)
Dim uid As IUID = New UIDClass()
uid.Value = "esriSpatialAnalystUI.SAExtension"
Dim extension As IExtension = extensionManager.FindExtension(uid)
Dim extensionConfig As IExtensionConfig = CType(extension, IExtensionConfig)
Dim wasEnabled As Boolean = (extensionConfig.State = esriExtensionState.esriESEnabled)
If Not wasEnabled Then
If Not(extensionConfig.State = esriExtensionState.esriESUnavailable) Then
extensionConfig.State = esriExtensionState.esriESEnabled
Else
' Handle the case when the license is not available.
' Provide an error message or exit to avoid running unavailable functionality.
End If
End If
-
Philip, thanks for your reply. After running the code I attempted to use a tool that requires SA extension, however it gave me the Tool Not Licensed msgboxartwork21– artwork212011年03月09日 20:40:34 +00:00Commented Mar 9, 2011 at 20:40
-
I'm programming in VB. It does not recognize "Dim factorytype as Type." Any suggestions for this portion of code?artwork21– artwork212011年03月11日 13:51:51 +00:00Commented Mar 11, 2011 at 13:51
-
The code is VB.NET and Type is a core .NET type. Are you programming in plain VB and not VB.NET? If so, you'll have to translate the code into VB.Philip– Philip2011年03月12日 03:02:49 +00:00Commented Mar 12, 2011 at 3:02
-
I'm programming in plain VB. Do you know of any good translating tools to go backwards?artwork21– artwork212011年03月13日 01:58:34 +00:00Commented Mar 13, 2011 at 1:58