| 390 | } |
| 391 | |
| 392 | void vtkCellGridWriter::WriteData() |
| 393 | { |
| 394 | if (!this->FileName || !this->FileName[0]) |
| 395 | { |
| 396 | vtkErrorMacro("No filename set."); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | auto* grid = this->GetInput(); |
| 401 | if (!grid) |
| 402 | { |
| 403 | vtkErrorMacro("No input dataset to write to \"" << this->FileName << "\""); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | std::ofstream output(this->FileName); |
| 408 | if (!output.good()) |
| 409 | { |
| 410 | vtkErrorMacro("Could not open \"" << this->FileName << "\" for writing."); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | nlohmann::json data; |
| 415 | if (this->ToJSON(data, grid)) |
| 416 | { |
| 417 | switch (this->FileFormat) |
| 418 | { |
| 419 | default: |
| 420 | case Format::PlainText: |
| 421 | output << data; |
| 422 | break; |
| 423 | case Format::MessagePack: |
| 424 | { |
| 425 | output << "vtkCellGrid\nMessagePack\nv1\n"; |
| 426 | std::vector<char> mpack; |
| 427 | nlohmann::json::to_msgpack(data, mpack); |
| 428 | auto writeSize = mpack.size() / sizeof(std::istream::char_type) + |
| 429 | (mpack.size() % sizeof(std::istream::char_type) ? 1 : 0); |
| 430 | output.write(reinterpret_cast<std::ostream::char_type*>(mpack.data()), writeSize); |
| 431 | } |
| 432 | break; |
| 433 | } |
| 434 | output.close(); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | vtkErrorMacro("Could not write JSON to \"" << this->FileName << "\"."); |
| 439 | return; /* 0 */ |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | VTK_ABI_NAMESPACE_END |