ARCGIS 10, VB.NET, calculate centroids

06-17-2016 02:08 AM
New Contributor


  I have migrated an toolbar initially developped for ArcGis 9,3. This tool is used by non-programmer or cartographers.

  One procedure is called to generate centroids of attributes table and after to launch an calculation.


  The source code used in 9,3 was :


'instantiating new calculator and cursor

  pCalculator = New Calculator

  pCursor = CType(pFeatureClass.Update(Nothing, True), ICursor)

    'Centroid Y

  With pCalculator

  .Cursor = pCursor

.PreExpression = "Dim pGeomShp as IGeometry" & vbNewLine & "Set pGeomShp = [shape]" & vbNewLine & "Dim pArea as IArea" & vbNewLine & "Set pArea = pGeomShp" & vbNewLine

.Expression = "pArea.Centroid.Y"

  .Field = "centroidY"

  End With

    'try & catch *************************************************



  Catch ex As System.Exception

MessageBox.Show("Error: " + ex.Message)

  End Try

  This was working until moving to ArcGis 10 (an upper). A crash was observed while launching pCalculator.Calculate().

  I’ve migrated this code that way (like proposed here )  :


  Dim nCentroidXFieldIndex As Integer = pFeatureClass.FindField("centroidX")

  Dim nCentroidYFieldIndex As Integer = pFeatureClass.FindField("centroidY")

  Dim nBldg_sizeFieldIndex As Integer = pFeatureClass.FindField("Bldg_size")

      If TypeOf pFeatureClass Is IFeatureClass Then

  If TypeOf pFeatureCursor Is IFeatureCursor Then


  Dim pArea As IArea

  Dim pFeature As IFeature = pFeatureCursor.NextFeature()

  Dim myProgressCalc As New FileProgressForm



myProgressCalc.Text = "Veuillez patienter svp"

myProgressCalc.lblProgress.Text = "Calcul des données géographiques"


   pArea = CType(pFeature.Shape, IArea)

   While pFeature IsNot Nothing

pFeature.Value(nCentroidXFieldIndex) = pArea.Centroid.X

pFeature.Value(nCentroidYFieldIndex) = pArea.Centroid.Y

pFeature.Value(nBldg_sizeFieldIndex) = pArea.Area


pFeature = pFeatureCursor.NextFeature


    End While


  End If

  End If

  It’s working but it’s working extremely slowly ! The first code (A) was executed in less than 3 minutes. The new one (B) in 25 minutes !

  In fact, I just want to programmaticaly call a the new calculate geometry tool (when displaying attributes table - )


Screen capture 1

Too bad, can't upload PNG and JPEG files...

or Arc tool => Data Management Tools => Entities => Add geometric attributes ( )

  Screen capture 2

or any other methods !


Both tools are working great in ArcGis Desktop but I can’t find my way to make them work in VB.NET.

Please, any suggestions ? (It’s just driving me crazy).


Thanks by advance for your answer, and sorry for screen capture in french and for poor english speaking


0 Kudos
6 Replies
MVP Esteemed Contributor

One issue with your ICalculate PreExpression script is with ArcGIS 10, VBScript will now crash when try to declare a variable as a type.

Dim pGeomShp as IGeometry

should be replaced with

Dim pGeomShp

0 Kudos
New Contributor


Thank you.

I just made the change you proposed without any success.

The following error is still showing :

"Error -2147467259 HRESULT E_FAIL has been returned from a call to a COM component"

0 Kudos
MVP Esteemed Contributor

Did you also make that change to the Dim pArea line? Also, have you tried running the PreExpression script manually in the Field Calculator?

0 Kudos
New Contributor


Yes, I also made the change for the "Dim pArea" line. With no success.

I tried to execute this code manually in ArcGis desktop. But unfortunately, it did'nt work.  I'm realizing that I should write the good script here before trying to program it.


I made many tries in VB. Tthe only thing that were working correctly (manually, in desktop ) was in Python with that code :


But I just can't make it work in the program.

0 Kudos
MVP Esteemed Contributor

Unfortunately, there's a bug with the ICalculator (#NIM061356) that doesn't allow it to use Python syntax. This has been around since 2010 and while it's classified as High Severity, they still haven't fixed it.

0 Kudos
New Contributor


It's so easy to make it work in Python with 1 line of code and I can't make it with VBScript ...

0 Kudos