# Example for extracting a single cell from a polydata into a new dataset

**URL:** https://discourse.vtk.org/t/example-for-extracting-a-single-cell-from-a-polydata-into-a-new-dataset/6858
**Category:** Web
**Created:** [October 13, 2021, 2:37pm UTC](https://discourse.vtk.org/t/example-for-extracting-a-single-cell-from-a-polydata-into-a-new-dataset/6858 "2021-10-13T14:37:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![raphael-bresson](https://discourse.vtk.org/user_avatar/discourse.vtk.org/raphael-bresson/32/3996_2.png) [@raphael-bresson](https://discourse.vtk.org/u/raphael-bresson)
#### Post date: [October 13, 2021, 2:37pm UTC](https://discourse.vtk.org/t/example-for-extracting-a-single-cell-from-a-polydata-into-a-new-dataset/6858/1 "2021-10-13T14:37:16Z")

</div>

Hi,

could anyone provide me with an example for doing the following operation:  
Let’s say I have a polydata.  
I want to extract one cell (knowing its ID) into a new dataset (which I will then display as a wireframe to highlight that cell).  
@Sebastien_Jourdain suggested doing shallow copy of my polydata but I’m not sure how to do that and then only having a single cell in that copy ?  
Is it because the copy only deals with points and I then have to manually build my single cell ?  
Thanks for your help

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [October 13, 2021, 2:51pm UTC](https://discourse.vtk.org/t/example-for-extracting-a-single-cell-from-a-polydata-into-a-new-dataset/6858/2 "2021-10-13T14:51:26Z")

</div>

```auto
const selection = vtkPolyData.newInstance();
const selection.shallowCopy(originalDS);
// selection.getCellData().clear();
const cell = [];
const targetCellId = 234;
let cellOffset = 0;
let currentCellId = 0;
const originalCells = originalDS.getPolys().getData(); // assuming only polys

// find cell
while (currentCellId < targetCellId) {
  cellOffset += originalCells[cellOffset] + 1;
  currentCellId++;
}

// extract a copy of the found one
for (let i = 0; i <= originalCells[cellOffset]; i++) {
  cell.push(originalCells[cellOffset+i]
}

// override the original cells with the one we created
selection.getPolys().setData(Uint16Array.from(cell))

```

---

<div class="post-metadata">

### Author: ![raphael-bresson](https://discourse.vtk.org/user_avatar/discourse.vtk.org/raphael-bresson/32/3996_2.png) [@raphael-bresson](https://discourse.vtk.org/u/raphael-bresson)
#### Post date: [October 13, 2021, 8:48pm UTC](https://discourse.vtk.org/t/example-for-extracting-a-single-cell-from-a-polydata-into-a-new-dataset/6858/3 "2021-10-13T20:48:34Z")

</div>

Great thanks a lot !
