# vtkAppendPolyData java problem

**URL:** https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169
**Category:** Support
**Created:** [June 18, 2019, 9:28pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169 "2019-06-18T21:28:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![naffrancois](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@naffrancois](https://discourse.vtk.org/u/naffrancois)
#### Post date: [June 18, 2019, 9:28pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/1 "2019-06-18T21:28:01Z")

</div>

Dear all,

I am new to using vtk, please accept my apologies for any silly question. I am using java on windows 10 64b platform and I am currently trying to append two vtk files having the same structure: unstructured grid with equal number of cells and cell data arrays. Here is the piece of code which fails:

vtkAppendPolyData append1 = new vtkAppendPolyData();

vtkUnstructuredGridReader reader1 = new vtkUnstructuredGridReader();  
reader1.SetFileName(filename1);  
reader1.ReadAllScalarsOn();  
reader1.Update();

vtkUnstructuredGridReader reader2 = new vtkUnstructuredGridReader();  
reader2.SetFileName(filename2);  
reader2.ReadAllScalarsOn();  
reader2.Update();

append1.AddInputData(reader1.GetOutput());  
append1.AddInputData(reader2.GetOutput());  
append1.Update();

System.out.println(append1.GetOutput().GetCellData().GetNumberOfArrays());  
System.out.println(reader1.GetOutput().GetCellData().GetNumberOfArrays());  
System.out.println(reader2.GetOutput().GetCellData().GetNumberOfArrays());

These last calls return:  
0  
8  
8

This seems to me that append fails. Note that if I only add one of the two files, the correct number of arrays is returned. I could not find other threads on this, at least they did not allow me to solve the issue so far.

Many thanks

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 18, 2019, 9:34pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/2 "2019-06-18T21:34:15Z")

</div>

Do the two unstructured grids loaded in have the same cell array names?

---

<div class="post-metadata">

### Author: ![naffrancois](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@naffrancois](https://discourse.vtk.org/u/naffrancois)
#### Post date: [June 19, 2019, 8:04am UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/3 "2019-06-19T08:04:51Z")

</div>

Thank you for your answer. The two unstructured grids have same number of cell data arrays of same size but with different names. Under Paraview the appendattribute filter works well, this is what I would like to do in my java program.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 19, 2019, 1:54pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/4 "2019-06-19T13:54:15Z")

</div>

> [@naffrancois](#):
>
> Under Paraview the appendattribute filter works well, this is what I would like to do in my java program.

Ah! ParaView uses a custom class (not available in VTK) called `vtkMergeArrays`. This combines cell arrays with different names into the output dataset. `vtkAppendPolyData`, on the other hand, assumes that the inputs have arrays with the same names - if they do not, the arrays are not copied to the output. Moreover, you would want to use `vtkAppendDataSet` - `vtkAppendPolyData` only handles polydata inputs while your readers are producing `vtkUnstructuredGrid`.

For your application, you’ll need to combine the cell data arrays manually. Something like

```java
vtkUnstructuredGridReader reader1 = new vtkUnstructuredGridReader();
reader1.SetFileName(filename1);
reader1.ReadAllScalarsOn();
reader1.Update();

vtkUnstructuredGridReader reader2 = new vtkUnstructuredGridReader();
reader2.SetFileName(filename2);
reader2.ReadAllScalarsOn();
reader2.Update();

vtkUnstructuredGrid combined = new vtkUnstructuedGrid();
combined.DeepCopy(reader1.GetOutput());
vktCellData cd = combined.GetCellData();
vtkUnstructuredGrid ug2 = reader2.GetOutput());
vtkCellData ug2cd = ug2.GetCellData();
for (int i = 0; i < ug2cd.GetNumberOfArrays(); ++i)
{
  cd.AddArray(ug2cd.GetArray(i));
}

```

Then you can use `combined` later on in your pipeline. I haven’t tested the code, but it should be pretty close to what you need.

---

<div class="post-metadata">

### Author: ![naffrancois](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@naffrancois](https://discourse.vtk.org/u/naffrancois)
#### Post date: [June 19, 2019, 2:23pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/5 "2019-06-19T14:23:05Z")

</div>

Wow ! Thanks ! I will test this later on today but I have a pretty good feeling 🙂

---

<div class="post-metadata">

### Author: ![naffrancois](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@naffrancois](https://discourse.vtk.org/u/naffrancois)
#### Post date: [June 19, 2019, 8:17pm UTC](https://discourse.vtk.org/t/vtkappendpolydata-java-problem/1169/6 "2019-06-19T20:17:13Z")

</div>

Works like a charm ! Thank you so much for your help 🙂
