# Creating a new vtkDataSet from an existing vtkDataSet

**URL:** https://discourse.vtk.org/t/creating-a-new-vtkdataset-from-an-existing-vtkdataset/5870
**Category:** Support
**Created:** [May 26, 2021, 5:43pm UTC](https://discourse.vtk.org/t/creating-a-new-vtkdataset-from-an-existing-vtkdataset/5870 "2021-05-26T17:43:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [May 26, 2021, 5:43pm UTC](https://discourse.vtk.org/t/creating-a-new-vtkdataset-from-an-existing-vtkdataset/5870/1 "2021-05-26T17:43:32Z")

</div>

Hello,

I just met a question about creating a new vtkDataSet from an existing vtkDataSet. I have an original dataset A

`vtkSmartPointer<vtkDataSet> A = CreateDataSetA();`

And now I want to create a deepcopy of the DataSet A with all the PointData and CellData. I tried different ways and print the new Dataset B:

```
// ***Not working in the compiling***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>::New();
B->DeepCopy(A);

// ***Working but probably Shallow copy***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>(A);

// ***Has coordinates of A but no Point Data***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>(A);
B->DeepCopy(A);

// ***Working***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>::Newinstance(A);
B->DeepCopy(A);

// ***Print function***
std::filebuf file_Buff;
file_Buff.open("./test.txt", std::ios::out);
std::ostream os(&file_Buff);
B->Print(os);
file_Buff.close();

```

The last one is working and I guess that is a Deep Copy. But I wonder why thoese weird things happen.

I would be appreciate if someone could explain that.

---

<div class="post-metadata">

### Author: ![Joachim\_P](https://discourse.vtk.org/user_avatar/discourse.vtk.org/joachim_p/32/3272_2.png) [@Joachim\_P](https://discourse.vtk.org/u/Joachim_P)
#### Post date: [May 27, 2021, 5:48am UTC](https://discourse.vtk.org/t/creating-a-new-vtkdataset-from-an-existing-vtkdataset/5870/2 "2021-05-27T05:48:18Z")

</div>

The last one is indeed correct. If you want to copy object A to object B, you first have to create object B with same type than object A. The `NewInstance()` does that. Then, the `DeepCopy()` will deep copy (meaning allocate new memory, not sharing pointers) all data from object A into object B.  
HTH.

---

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [September 29, 2021, 9:01am UTC](https://discourse.vtk.org/t/creating-a-new-vtkdataset-from-an-existing-vtkdataset/5870/3 "2021-09-29T09:01:51Z")

</div>

Thanks so much. I realize that after a while and the data structure in VTK is kind of different from the basic data type.
