# How to ReplaceCell to a vtkEmptyCell in vtkUnstructuredGrid ?

**URL:** https://discourse.vtk.org/t/how-to-replacecell-to-a-vtkemptycell-in-vtkunstructuredgrid/8303
**Category:** Support
**Created:** [April 15, 2022, 1:36am UTC](https://discourse.vtk.org/t/how-to-replacecell-to-a-vtkemptycell-in-vtkunstructuredgrid/8303 "2022-04-15T01:36:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jacques.chen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jacques.chen](https://discourse.vtk.org/u/Jacques.chen)
#### Post date: [April 15, 2022, 1:36am UTC](https://discourse.vtk.org/t/how-to-replacecell-to-a-vtkemptycell-in-vtkunstructuredgrid/8303/1 "2022-04-15T01:36:05Z")

</div>

I use vtkUnstructuredGrid to store cell dates , and I want replace a cell to a vtkEmptyCell . But fail.

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [April 15, 2022, 1:08pm UTC](https://discourse.vtk.org/t/how-to-replacecell-to-a-vtkemptycell-in-vtkunstructuredgrid/8303/2 "2022-04-15T13:08:41Z")

</div>

I’m pretty confident that just replacing the `Types` array at id `cellId` to `VTK_EMPTY_CELL` should work, if the cell is already allocated. This way you can just shallow-copy the unstructured grid and save memory. Let me write an example:

```auto
vtkSmartPointer<vtkUnstructuredGrid> EditUnstructuredGrid(
    vtkUnstructuredGrid* input, vtkIdList* cellIds)
{
  vtkSmartPointer<vtkUnstructuredGrid> output = vtkSmartPointer<vtkUnstructuredGrid>::New();
  output->ShallowCopy(input);

  // We need a new types array with a deep copy so we don't edit the input.
  vtkNew<vtkUnsignedCharArray> types;
  types->DeepCopy(input->GetCellTypesArray());

  // We reset the cells in the output with the new types array
  output->SetCells(types, output->GetCells(), output->GetFaceLocations(), output->GetFaces());

  // We can now edit the types
  for (vtkIdType i = 0; i < cellIds->GetNumberOfIds(); ++i)
  {
    types->SetValue(cellIds->GetId(i), VTK_EMPTY_CELL);
  }

  // WARNING This only works because an empty cell always has less points than
  // any other cell. If you replace a VTK_LINE with a VTK_TRIANGLE,
  // you'll be messing up the cell array.

  return output;
}

```

---

<div class="post-metadata">

### Author: ![Jacques.chen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jacques.chen](https://discourse.vtk.org/u/Jacques.chen)
#### Post date: [April 18, 2022, 1:29am UTC](https://discourse.vtk.org/t/how-to-replacecell-to-a-vtkemptycell-in-vtkunstructuredgrid/8303/3 "2022-04-18T01:29:44Z")

</div>

thank you sir! your example is work well in my software.
