-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMergePolyData.cxx
164 lines (147 loc) · 5.58 KB
/
MergePolyData.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <fstream>
#include <iostream>
#include <vtkAppendFilter.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDataSetMapper.h>
#include <vtkIntArray.h>
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkUnstructuredGridWriter.h>
#include "include/vtkCleanUnstructuredGrid.h"
#define MERGE_TOL 1E-12
bool ValidateInputFiles(std::vector<std::string> &inputFiles,
std::string &outFile) {
// Validate input file paths
for (int f = 0; f < inputFiles.size(); f++) {
ifstream file(inputFiles[f]);
if (!file.good()) { // file couldn't be opened
std::cout << "Error:" << inputFiles[f] << " could not be opened"
<< std::endl;
return false;
}
}
return true;
}
void AddAttributeToCells(vtkSmartPointer<vtkUnstructuredGrid> pGrid,
vtkSmartPointer<vtkIdList> pCellCounts) {
// Setup index array
vtkSmartPointer<vtkIntArray> indexArray = vtkSmartPointer<vtkIntArray>::New();
indexArray->SetNumberOfComponents(1);
indexArray->SetName("PartID");
int partId = 0;
for (vtkIdType i = 0; i < pGrid->GetNumberOfCells(); i++) {
if (i < pCellCounts->GetId(partId)) {
indexArray->InsertNextValue(partId);
} else {
indexArray->InsertNextValue(++partId);
}
}
pGrid->GetCellData()->SetScalars(indexArray);
}
void ExportAsAbaqusFile(vtkSmartPointer<vtkUnstructuredGrid> ipGrid,
vtkSmartPointer<vtkIdList> pCellCounts,
std::string filePath) {
std::ofstream file;
file.open(filePath);
file << "*************************************\n";
file << "*HEADING\n";
file << "FemTech\n";
file << "*************************************\n";
file << "*NODE, NSET=All\n";
for (int i = 0; i < ipGrid->GetNumberOfPoints(); i++) {
double pnt[3];
ipGrid->GetPoint(i, pnt);
file << i + 1 << ", " << pnt[0] << ", " << pnt[1] << ", " << pnt[2]
<< "\n";
}
int currentCell = 0;
int nSet = pCellCounts->GetNumberOfIds();
for (int set = 0; set < nSet; set++) {
file << "*ELEMENT,TYPE=C3D8,ELSET=PART_" << set + 1 << std::endl;
int ncell = pCellCounts->GetId(set);
for (int i = currentCell; i < pCellCounts->GetId(set); i++) {
vtkSmartPointer<vtkIdList> cellPointIds =
vtkSmartPointer<vtkIdList>::New();
ipGrid->GetCellPoints(i, cellPointIds);
// Only for Element with 8 nodes
assert(cellPointIds->GetNumberOfIds() == 8);
file << i + 1 << ", " << cellPointIds->GetId(0) + 1 << ", "
<< cellPointIds->GetId(1) + 1 << ", " << cellPointIds->GetId(2) + 1
<< ", " << cellPointIds->GetId(3) + 1 << ", "
<< cellPointIds->GetId(4) + 1 << ", " << cellPointIds->GetId(5) + 1
<< ", " << cellPointIds->GetId(6) + 1 << ", "
<< cellPointIds->GetId(7) + 1 << std::endl;
}
currentCell = pCellCounts->GetId(set);
}
file.close();
}
int main(int argc, char *argv[]) {
std::vector<std::string> inputFiles;
std::string outFile;
bool isInputs = false;
bool isOutput = false;
bool writeinp = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-in") == 0) {
isInputs = true;
isOutput = false;
} else if (strcmp(argv[i], "-out") == 0) {
isInputs = false;
isOutput = true;
} else if (strcmp(argv[i], "-abaqus") == 0) {
writeinp = true;
} else {
if (isInputs) {
inputFiles.push_back(argv[i]);
} else if (isOutput) {
outFile = argv[i];
isInputs = false;
isOutput = false;
}
}
}
if (!ValidateInputFiles(inputFiles, outFile))
return 0;
int partNumber = 0;
vtkSmartPointer<vtkIdList> pCellCounts = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkAppendFilter> pAppend =
vtkSmartPointer<vtkAppendFilter>::New();
std::cout << "Number of input files " << inputFiles.size() << "\n";
for (int f = 0; f < inputFiles.size(); f++) {
cout << "Reading file " << inputFiles[f] << endl;
vtkSmartPointer<vtkUnstructuredGridReader> reader =
vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName(inputFiles[f].c_str());
reader->Update();
vtkSmartPointer<vtkUnstructuredGrid> pUnstructedGrid =
vtkSmartPointer<vtkUnstructuredGrid>::New();
pUnstructedGrid->DeepCopy(reader->GetOutput());
pAppend->AddInputData(pUnstructedGrid);
int nCells = pUnstructedGrid->GetNumberOfCells();
int nPrevCount = f > 0 ? pCellCounts->GetId(f - 1) : 0;
pCellCounts->InsertNextId(nCells + nPrevCount);
}
pAppend->Update();
vtkSmartPointer<vtkCleanUnstructuredGrid> pGridCleaner =
vtkSmartPointer<vtkCleanUnstructuredGrid>::New();
pGridCleaner->AddInputData(pAppend->GetOutput());
pGridCleaner->Update();
vtkSmartPointer<vtkUnstructuredGrid> pMergedGrid = pGridCleaner->GetOutput();
AddAttributeToCells(pMergedGrid, pCellCounts);
vtkSmartPointer<vtkUnstructuredGridWriter> writter =
vtkSmartPointer<vtkUnstructuredGridWriter>::New();
writter->SetInputData(pMergedGrid);
writter->SetFileName(outFile.c_str());
writter->Write();
std::cout << "Written to : " << outFile << std::endl;
if (writeinp) {
std::string abaqusFilename =
outFile.substr(0, outFile.find_last_of('.')) + ".inp";
ExportAsAbaqusFile(pMergedGrid, pCellCounts, abaqusFilename);
std::cout << "Written Abaqus inp file : " << abaqusFilename << std::endl;
}
return 0;
}