File: vtkSMReaderReloadHelper.cxx

package info (click to toggle)
paraview 5.4.1%2Bdfsg4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 218,616 kB
  • sloc: cpp: 2,331,508; ansic: 322,365; python: 111,051; xml: 79,203; tcl: 47,013; yacc: 4,877; java: 4,438; perl: 3,238; sh: 2,920; lex: 1,908; f90: 748; makefile: 273; pascal: 228; objc: 83; fortran: 31
file content (221 lines) | stat: -rw-r--r-- 6,756 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*=========================================================================

  Program:   ParaView
  Module:    vtkSMReaderReloadHelper.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkSMReaderReloadHelper.h"

#include "vtkCollection.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPVFileInformation.h"
#include "vtkPVXMLElement.h"
#include "vtkSMCoreUtilities.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMStringVectorProperty.h"
#include "vtkSMTrace.h"
#include "vtkSmartPointer.h"

#include <cassert>
#include <vtksys/SystemTools.hxx>

namespace
{
vtkPVFileInformation* vtkFindFileGroupFor(vtkPVFileInformation* info, const std::string& unixFname)
{
  if (info == NULL)
  {
    return NULL;
  }
  if (info->GetType() != vtkPVFileInformation::FILE_GROUP &&
    info->GetType() != vtkPVFileInformation::DIRECTORY)
  {
    return NULL;
  }

  for (int cc = 0, max = info->GetContents()->GetNumberOfItems(); cc < max; ++cc)
  {
    vtkPVFileInformation* item =
      vtkPVFileInformation::SafeDownCast(info->GetContents()->GetItemAsObject(cc));
    if (item == NULL)
    {
      continue;
    }
    if (item->GetType() == vtkPVFileInformation::FILE_GROUP)
    {
      if (vtkPVFileInformation* found = vtkFindFileGroupFor(item, unixFname))
      {
        return found;
      }
    }
    else if (item->GetType() == vtkPVFileInformation::SINGLE_FILE &&
      info->GetType() == vtkPVFileInformation::FILE_GROUP && item->GetFullPath() != NULL)
    {
      std::string unixFullPath(item->GetFullPath());
      vtksys::SystemTools::ConvertToUnixSlashes(unixFullPath);
      if (unixFname == unixFullPath)
      {
        return info;
      }
    }
  }
  return NULL;
}

// Returns empty to indicate nothing found or don't bother updating.
std::vector<std::string> vtkGetFilesInSeries(vtkSMSessionProxyManager* pxm, const char* fname)
{
  std::string dir = vtksys::SystemTools::GetFilenamePath(fname);

  // We use unix-slashes so we can consistently compare filenames in
  // vtkFindFileGroupFor().
  std::string unixFname = fname;
  vtksys::SystemTools::ConvertToUnixSlashes(unixFname);

  vtkSmartPointer<vtkSMProxy> helper;
  helper.TakeReference(pxm->NewProxy("misc", "FileInformationHelper"));
  vtkSMPropertyHelper(helper, "WorkingDirectory").Set(dir.c_str());
  vtkSMPropertyHelper(helper, "Path").Set(dir.c_str());
  vtkSMPropertyHelper(helper, "SpecialDirectories").Set(0);
  vtkSMPropertyHelper(helper, "DirectoryListing").Set(1);
  helper->UpdateVTKObjects();

  vtkNew<vtkPVFileInformation> info;
  helper->GatherInformation(info.Get());

  if (vtkPVFileInformation* group = vtkFindFileGroupFor(info.Get(), unixFname))
  {
    std::vector<std::string> retval(group->GetContents()->GetNumberOfItems());
    for (int cc = 0, max = group->GetContents()->GetNumberOfItems(); cc < max; ++cc)
    {
      vtkPVFileInformation* item =
        vtkPVFileInformation::SafeDownCast(group->GetContents()->GetItemAsObject(cc));
      retval[cc] = item->GetFullPath();
    }
    return retval;
  }

  return std::vector<std::string>();
}
}

vtkStandardNewMacro(vtkSMReaderReloadHelper);
//----------------------------------------------------------------------------
vtkSMReaderReloadHelper::vtkSMReaderReloadHelper()
{
}

//----------------------------------------------------------------------------
vtkSMReaderReloadHelper::~vtkSMReaderReloadHelper()
{
}

//----------------------------------------------------------------------------
bool vtkSMReaderReloadHelper::SupportsReload(vtkSMSourceProxy* proxy)
{
  if (vtkPVXMLElement* hints = proxy ? proxy->GetHints() : NULL)
  {
    if (vtkPVXMLElement* rfhints = hints->FindNestedElementByName("ReloadFiles"))
    {
      if (proxy->GetProperty(rfhints->GetAttributeOrEmpty("property")))
      {
        return true;
      }
    }
    return (hints->FindNestedElementByName("ReaderFactory") != NULL &&
      vtkSMCoreUtilities::GetFileNameProperty(proxy) != NULL);
  }

  return false;
}

//----------------------------------------------------------------------------
bool vtkSMReaderReloadHelper::SupportsFileSeries(vtkSMSourceProxy* proxy)
{
  if (this->SupportsReload(proxy))
  {
    const char* pname = vtkSMCoreUtilities::GetFileNameProperty(proxy);
    vtkSMStringVectorProperty* svp =
      vtkSMStringVectorProperty::SafeDownCast(proxy->GetProperty(pname));
    return svp && svp->GetRepeatable();
  }
  return false;
}

//----------------------------------------------------------------------------
bool vtkSMReaderReloadHelper::ReloadFiles(vtkSMSourceProxy* proxy)
{
  if (!this->SupportsReload(proxy))
  {
    return false;
  }

  assert(proxy);

  SM_SCOPED_TRACE(CallFunction).arg("ReloadFiles").arg(proxy);

  vtkPVXMLElement* hints = proxy->GetHints();
  vtkPVXMLElement* rfhints = hints ? hints->FindNestedElementByName("ReloadFiles") : NULL;
  if (rfhints && proxy->GetProperty(rfhints->GetAttributeOrEmpty("property")))
  {
    proxy->InvokeCommand(rfhints->GetAttributeOrEmpty("property"));
  }
  else
  {
    proxy->RecreateVTKObjects();
  }
  proxy->UpdatePipelineInformation();
  return true;
}

//----------------------------------------------------------------------------
bool vtkSMReaderReloadHelper::ExtendFileSeries(vtkSMSourceProxy* proxy)
{
  if (!this->SupportsFileSeries(proxy))
  {
    return false;
  }

  SM_SCOPED_TRACE(CallFunction).arg("ExtendFileSeries").arg(proxy);

  const char* pname = vtkSMCoreUtilities::GetFileNameProperty(proxy);
  assert(pname);

  vtkSMStringVectorProperty* svp =
    vtkSMStringVectorProperty::SafeDownCast(proxy->GetProperty(pname));
  assert(svp && svp->GetRepeatable());
  if (svp->GetNumberOfElements() < 1)
  {
    vtkErrorMacro("Reader has not files specified. At least 1 file must be specified "
                  "to be able to add more to the series.");
    return false;
  }

  std::vector<std::string> files =
    vtkGetFilesInSeries(proxy->GetSessionProxyManager(), svp->GetElement(0));
  if (files.size() > 0)
  {
    svp->SetElements(files);
    proxy->UpdateVTKObjects();
    proxy->UpdatePipelineInformation();
    return true;
  }
  return false;
}

//----------------------------------------------------------------------------
void vtkSMReaderReloadHelper::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}