# extract programable filter/python annotation values to csv file

**URL:** https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279
**Category:** ParaView Support
**Tags:** python
**Created:** [August 22, 2024, 10:21am UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279 "2024-08-22T10:21:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![NinhGhoster](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ninhghoster/32/15061_2.png) [@NinhGhoster](https://discourse.paraview.org/u/NinhGhoster)
#### Post date: [August 22, 2024, 10:21am UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/1 "2024-08-22T10:21:23Z")

</div>

Hi all,

I wrote the code below using the Programable Filter to extract the volume/area of the object. Not sure if this is a optimised way or not? Please share if there is a better way.

```auto
import numpy as np
import vtk

input0 = inputs[0]

numTriangles = input0.GetNumberOfCells()
volumeArray = np.empty(numTriangles, dtype=np.float64)
areaArray = np.empty(numTriangles, dtype=np.float64)

for i in range(numTriangles):
    cell = input0.GetCell(i)
    p1 = np.array(input0.GetPoint(cell.GetPointId(0)))
    p2 = np.array(input0.GetPoint(cell.GetPointId(1)))
    p3 = np.array(input0.GetPoint(cell.GetPointId(2)))

    # Compute the area of the triangle
    side1 = np.linalg.norm(p2 - p1)
    side2 = np.linalg.norm(p3 - p1)
    side3 = np.linalg.norm(p3 - p2)
    s = 0.5 * (side1 + side2 + side3)
    areaArray[i] = np.sqrt(s * (s - side1) * (s - side2) * (s - side3))

    # Assuming the volume is computed for a tetrahedron with a dummy point p4
    p4 = np.array([0,0,0]) # Use barycenter of surface for better results.
    volumeArray[i] = vtk.vtkTetra.ComputeVolume(p1, p2, p4, p3)

output.CellData.append(volumeArray, "Volume")
output.CellData.append(areaArray, "Area")

```

After that, I added Python Annotation filter to show the Volume/Area of this object

```auto
"Volume in cubic centimeters: %f\nArea in square centimeters: %f" % (1000000 * sum(Volume), 10000 * sum(Area))

```

 ![image](https://discourse.paraview.org/uploads/default/original/2X/5/580b199567588fb69027ad10c01fe6e676513f5a.png)

I wonder is there a way to extract these values to csv file using code instead of option "export spreadsheet?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [August 22, 2024, 12:16pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/2 "2024-08-22T12:16:54Z")

</div>

> [@NinhGhoster](#):
>
> I wonder is there a way to extract these values to csv file using code instead of option "export spreadsheet?

Two other possibilities:

- File → SaveData
- CSV extractor

---

<div class="post-metadata">

### Author: ![NinhGhoster](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ninhghoster/32/15061_2.png) [@NinhGhoster](https://discourse.paraview.org/u/NinhGhoster)
#### Post date: [August 22, 2024, 12:54pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/3 "2024-08-22T12:54:37Z")

</div>

Could you please give me the document about CSV extractor? I’ve never known this way before.  
And is there a way to use code to do this?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [August 22, 2024, 12:55pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/4 "2024-08-22T12:55:22Z")

</div>

[https://docs.paraview.org/en/latest/UsersGuide/savingResults.html#extractors](https://docs.paraview.org/en/latest/UsersGuide/savingResults.html#extractors)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [August 22, 2024, 12:56pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/5 "2024-08-22T12:56:06Z")

</div>

> [@NinhGhoster](#):
>
> And is there a way to use code to do this?

In which kind of code ? pvpython ? programmable filter ? that would be completely different.

---

<div class="post-metadata">

### Author: ![NinhGhoster](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ninhghoster/32/15061_2.png) [@NinhGhoster](https://discourse.paraview.org/u/NinhGhoster)
#### Post date: [August 22, 2024, 1:06pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/6 "2024-08-22T13:06:28Z")

</div>

Either of them would be good, my goal to extract the area, volume, and dimension into the csv file. I’ve already successfully extracted the dimension using Python Shell (refer to this [Bounding Ruler filter: adding an option to show X, Y, Z delta - #8 by NinhGhoster](https://discourse.paraview.org/t/bounding-ruler-filter-adding-an-option-to-show-x-y-z-delta/15274/8)). Hopefully, I can extract these the area and volume into the same csv file by using code since I don’t know a way how to integrate GUI and code together.

Each time step is a unique object. I intend to run the code automated to get these data from all steps. Please let me know if this is not a good idea.

 ![image](https://discourse.paraview.org/uploads/default/original/2X/9/9ad429ec0464399e220de4567c5a5a0b4c0ad0ef.jpeg)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [August 22, 2024, 1:09pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/7 "2024-08-22T13:09:22Z")

</div>

> [@mwestphal](#):
>
> programmable filter

Use `vtkCSVWriter`

> pvpython

Use `SaveData`

> Please let me know if this is not a good idea.

Hard to say without knowing more

---

<div class="post-metadata">

### Author: ![sherin.sugathan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/sherin.sugathan/32/15264_2.png) [@sherin.sugathan](https://discourse.paraview.org/u/sherin.sugathan)
#### Post date: [August 22, 2024, 1:59pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/8 "2024-08-22T13:59:11Z")

</div>

> [@NinhGhoster](#):
>
> Not sure if this is a optimised way or not? Please share if there is a better way.

Just wanted to share a potential way to simplify. Instead of writing the algorithm from scratch, you may consider using the **vtkMassProperties** class in your python script to compute the object properties.

[https://vtk.org/doc/nightly/html/classvtkMassProperties.html](https://vtk.org/doc/nightly/html/classvtkMassProperties.html)

---

<div class="post-metadata">

### Author: ![NinhGhoster](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ninhghoster/32/15061_2.png) [@NinhGhoster](https://discourse.paraview.org/u/NinhGhoster)
#### Post date: [August 22, 2024, 2:14pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/9 "2024-08-22T14:14:59Z")

</div>

Thanks. As you can see in the beginning post, I used vtk to get area and volume of the object. Because it’s 3D object, it’s quite difficult to get dimension. I extracted the bounding box dimension instead.

---

<div class="post-metadata">

### Author: ![NinhGhoster](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ninhghoster/32/15061_2.png) [@NinhGhoster](https://discourse.paraview.org/u/NinhGhoster)
#### Post date: [August 22, 2024, 3:31pm UTC](https://discourse.paraview.org/t/extract-programable-filter-python-annotation-values-to-csv-file/15279/10 "2024-08-22T15:31:12Z")

</div>

@sherin.sugathan @mwestphal I made a short video to explain what I want to achieve, it would be appreciated if you could take look and give feedback for me, thank you

[](https://www.youtube.com/watch?v=T04iBykZkC4)

The reason I am trying to automate it is that I have hundreds of objects that need to be scanned
