General error executing calculator

2993
10
11-02-2010 05:19 PM
JamesWhisenhunt
New Contributor III
Hello all,

I am trying to update the area field on a shapefile; however, I am getting the following error:

"Error HRESULT_FAIL has returned from a call to a COM component"

This was originally written in 9.3.1 and worked without problems.  Any ideas why I am having problems in ArcMap 10?

As always thanks for all help,

James

Private Sub UpdateArea(ByVal pFlayer As IFeatureLayer)
[INDENT]
Try

[INDENT]Dim pfClass As IFeatureClass
pfClass = pFlayer.FeatureClass

Dim pCalc As ICalculator
pCalc = New Calculator
Dim pCursor As ICursor
pCursor = pfClass.Update(Nothing, True)
With pCalc
[INDENT].Cursor = pCursor
.PreExpression = "Dim dblArea as double" & vbNewLine & _
[INDENT][INDENT][INDENT]"Dim pArea As IArea" & vbNewLine & _
"Set pArea = [Shape]" & vbNewLine & _
"dblArea = pArea.Area"[/INDENT][/INDENT][/INDENT]
.Expression = "dblArea"

.Field = "Area"[/INDENT]
End With

pCalc.Calculate()

pCursor = Nothing
[/INDENT]
Catch ex As Exception
[INDENT]MsgBox(ex.Message.ToString)[/INDENT]
End Try
[/INDENT]
    End Sub
0 Kudos
10 Replies
RichardFairhurst
MVP Honored Contributor
ArcGIS 10 does not support VBA.  It supports VB Script, which cannot be used to do geometry based calculations.  Your only option with the field calculator is to rewrite the calculation in Python.  I am not that good with Python, so I will leave it to others to help you.
0 Kudos
JamesWhisenhunt
New Contributor III
This is not VBA this is from a VB.Net application I put together in 9.3.1.  I am trying to upgrade to 10.

Again thanks for your help,
James
0 Kudos
AlexanderGray
Occasional Contributor III
The PreExpression is VBA.  So even if this is vb.net, you are asking the calculator to do VBA.  Same as if you put the expression in the calculate in the table view.

If it was me I would ditch the calculator and just loop through your update cursor, get the area by casting the feature shape to IArea and set the area field manually.  That way I don't have to mix and match programming languages.  Depending on the number of rows, performance might be a consideration though.
0 Kudos
JamesWhisenhunt
New Contributor III
Thank you,

I did originally write this in VBA to test.  I then moved it to VB.Net.  The reason I am trying to use the calculator is I have about 40,000 polygons I have to loop through.  I have several calculations and I thought performance would be a problem.  I could probably do them in one pass.  I will try a loop to see how fast it is. 

Again thank you for the help.

James
0 Kudos
F_Pfaefflin
New Contributor
Hello all,

can somebody please help me get going with Python Expressions/PreExpressions in the ICalculator from within my .net-code. I can successfully run Python and VBScript from the Calculator in the Desktop User Interface. I can also successfully run the simple

VBScript Expression
Dim pCalc As GeoDatabaseUI.ICalculator = New GeoDatabaseUI.Calculator()
pCalc.Expression = "[FID] * 2"


from within my code using the Calculator. However, if I try the

Python-Expression
Dim pCalc As GeoDatabaseUI.ICalculator = New GeoDatabaseUI.Calculator()
pCalc.Expression = "!FID! * 2"


I get the "General error executing calculator" and an Exception "Error HRESULT E_FAIL has been returned from a call to a COM component". However, as I said, this Python-Expression runs fine in the Calculator User Interface.

My suspicion is that maybe the parser doesn't notice that it is receiving a Python-Expression. Do I have to - and if yes, where - programmatically switch the parser of the ICalculator to Python? What am I missing?

Florian.
0 Kudos
DuncanHornby
MVP Notable Contributor
I agree with Florian, if you look at the ICalculator interface it does not say that the expression should be Python or even VB, so what is it ESRI?
0 Kudos
KenBuja
MVP Esteemed Contributor
There is a bug (NIM061356) on using Python with ICalculator: ICalculator or ICalculatorUI2 do not offer a property where the parser (expression type) can be specified; VBScript is the default, so Python expressions error out.

Their suggested workaround is as follows:


Below is a workaround that compensates for the lack of being able to specify the parser in ICalculator.  It takes advantage of IGeoProcessor and the CalculateField_management geoprocessing tool.  The parser or expression_type argument of that tool can be used to specify Python as the parser.

'(To test the below VBA code, create a UIButton in VBA and add the code inside the Click event handler of your new UIButton.  The second procedure, ReturnMessages(), is called into action by the first procedure towards the very end, but may not be necessary if you do not want to read the messages of the geoprocessing results.  Change the first parameter as needed to reflect the correct location of your input table.)

Private Sub UIButtonControl1_Click()

  'Create the Geoprocessor
  Dim pGp As IGeoProcessor
  Set pGp = New GeoProcessor

  'Set Overwriteoutput to True
  pGp.OverwriteOutput = True

  'Add the custom toolbox containing the model tool
  'pGp.AddToolbox "C:\CustomTools\custom.tbx"

  'Create the Parameter array
  Dim pParamArray As IVariantArray
  Set pParamArray = New VarArray

'Populate array of parameters

  'First Parameter:  in_table (e.g. File GDB feature class)
  pParamArray.Add "C:\Incidents\845875\Converted_GDB931\downgradedFGDB.gdb\Main_931"

  'Second Parameter:  field on which to calculate
  pParamArray.Add "LastField"

  'Third Parameter:  expression
  pParamArray.Add "math.log1p(!SumOfConcentrations!)"

  'Fourth Parameter:  expression_type (Optional)  !!!  !!!  !!!  !!!  !!!
  pParamArray.Add "PYTHON_9.3"

  Dim pResult As IGeoProcessorResult

  'Execute the Model tool
  Set pResult = pGp.Execute("CalculateField_management", pParamArray, Nothing)

  'Get the returned tool messages
  ReturnMessages pResult

End Sub

Public Sub ReturnMessages(ByVal messages As IGeoProcessorResult)
  Dim i As Long
  Dim message As String

  For i = 0 To messages.MessageCount - 1
     message = messages.GetMessage(i)
     Debug.Print message
  Next

End Sub

0 Kudos
F_Pfaefflin
New Contributor
Thank you for this information!
With the bug number NIM061356 I also found the other thread covering the issue 11589-Use-Python-expression-in-field-calculator I can only agree that (as it is not getting fixed due to low priority) it would be very nice of ESRI to put some information on this bug at least in the online help.

Florian.
0 Kudos
KenBuja
MVP Esteemed Contributor
If you have support with Esri, I would suggest calling them and adding your name onto the bug. If more people let them know this is a problem, they may give it a higher priority.
0 Kudos