# Removing and Re-Adding Cells to vtkPolyData:

**URL:** https://discourse.vtk.org/t/removing-and-re-adding-cells-to-vtkpolydata/12084
**Category:** Support
**Created:** [August 3, 2023, 5:41am UTC](https://discourse.vtk.org/t/removing-and-re-adding-cells-to-vtkpolydata/12084 "2023-08-03T05:41:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ramen-newdals](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ramen-newdals/32/7173_2.png) [@ramen-newdals](https://discourse.vtk.org/u/ramen-newdals)
#### Post date: [August 3, 2023, 5:41am UTC](https://discourse.vtk.org/t/removing-and-re-adding-cells-to-vtkpolydata/12084/1 "2023-08-03T05:41:57Z")

</div>

Hello All,

I have a vtkPolyData object where the user will select several input points. Using these points the connected cells will then be deleted from the mesh. I want to make a copy of the removed cells so they can be re-added back to the mesh at a latter point.

I am able to delete the cells using vtkRemovePolyData, but I run into problems when I am trying to copy the cells that are marked for deletion into another vtkPolyData object.

The smallest example I can post of how I am trying to do this is shown bellow where path array is a list of nodes that forms a closed path on the input mesh a sphere in this case.

```auto
   vtkNew<vtkSphereSource> Sphere;
   Sphere->SetCenter(0, 0, 0);
   Sphere->SetRadius(1.10);
	Sphere->Update();
    vtkNew<vtkRemovePolyData> RemovePoints;
	vtkSmartPointer<vtkCellArray> RemovedCells = vtkSmartPointer<vtkCellArray>::New();
	vtkSmartPointer<vtkPoints> RemovedPoints = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkPolyData> RemovedPolyData = vtkSmartPointer<vtkPolyData>::New();
	
	vtkSmartPointer<vtkIdList> ConnectedCellIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> RemovedCellIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> RemovedPointIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> CellPoints = vtkSmartPointer<vtkIdList>::New();

	ConnectedCellIds->Reset();
	Sphere->GetOutput()->BuildLinks();

	for(int i = 0; i<PathARRAY->GetNumberOfValues(); i++){
		Sphere->GetOutput()->GetPointCells(PathARRAY->GetValue(i), ConnectedCellIds);
	}

	RemovePoints->SetInputData(Sphere->GetOutput());
	RemovePoints->SetPointIds(PathARRAY);
	RemovePoints->Update();
	
	// Get vtkIdList of cells that will be removed from the mesh
	for(int i = 0; i<PathARRAY->GetNumberOfValues(); i++){
		ConnectedCellIds->Reset();
		Sphere->GetOutput()->GetPointCells(PathARRAY->GetValue(i), ConnectedCellIds);
		for(int j = 0; j<ConnectedCellIds->GetNumberOfIds(); j++){
			RemovedCellIds->InsertUniqueId(ConnectedCellIds->GetId(j));
		}
	}

	// Get vtkIdList of points that make up the cells that make up the mesh
	vtkIdType PointId;
	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		for(int j = 0; j<Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds()->GetNumberOfIds(); j++){
			PointId = Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds()->GetId(j);
			RemovedPointIds->InsertUniqueId(PointId);
			RemovedPoints->InsertNextPoint(Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPoints()->GetPoint(PointId));
		}
	}

	// Construct the polydata object that makes up the region of the mesh that is being removed
	// Add the points 
	for(int i = 0; i<RemovedPointIds->GetNumberOfIds(); i++){
		RemovedPoints->InsertNextPoint(Sphere->GetOutput()->GetPoint(RemovedPointIds->GetId(i)));
	}
	// Add the cells
	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		RemovedCells->InsertNextCell(3);
		RemovedCells->InsertCellPoint(i*3 + i);
	}

	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		RemovedCells->InsertNextCell(3);
		CellPoints->Reset();
		CellPoints = Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds();
		for(int j = 0; j<CellPoints->GetNumberOfIds(); j++){
			RemovedCells->InsertCellPoint(CellPoints->GetId(j));
		}
	}

```

Any help or suggestions on how to make this functional would be greatly appreciated.

---

<div class="post-metadata">

### Author: ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)
#### Post date: [August 3, 2023, 6:08am UTC](https://discourse.vtk.org/t/removing-and-re-adding-cells-to-vtkpolydata/12084/2 "2023-08-03T06:08:15Z")

</div>

you can take a look at this example: [https://examples.vtk.org/site/Cxx/Meshes/AddCell/](https://examples.vtk.org/site/Cxx/Meshes/AddCell/) that shows exactly how to add triangle to vtkpolydata
