如何在 ParaView 3.98.1 中使用 SaveData 写入 ASCII 文件?

How can I use SaveData to write an ASCII file in ParaView 3.98.1?

我正在为一个旧项目编写自动化脚本,我需要来自 Paraview 3.98.1. The function SaveData() in this version does not exist. I found its implementation herepvpython 的一些帮助并将其移至我的代码中。如何将文件另存为 ASCII?像 SaveData(filename, proxy=px, FileType='Ascii') 这样调用它会将我的文件保存为二进制文件(笨拙的行为)。

我需要这个版本,因为我在脚本管道中的一些代码处理非常具体的 vtk 文件。使用最新版本的 SaveData() 功能最终会在我的最终文件中创建不同的元数据,当我处理它们时它最终会破坏我的结果。目前使用旧版本的 Paraview 比修改我的所有代码更容易。

编辑:

该网站现在无法运行,但它是昨天的。也许这是一个内部问题?不管怎样,下面附上代码。

# -----------------------------------------------------------------------------

def SetProperties(proxy=None, **params):
    """Sets one or more properties of the given pipeline object. If an argument
    is not provided, the active source is used. Pass a list of property_name=value
    pairs to this function to set property values. For example::

        SetProperties(Center=[1, 2, 3], Radius=3.5)
    """
    if not proxy:
        proxy = active_objects.source
    properties = proxy.ListProperties()
    for param in params.keys():
        pyproxy = servermanager._getPyProxy(proxy)
        pyproxy.__setattr__(param, params[param])

# -----------------------------------------------------------------------------

def CreateWriter(filename, proxy=None, **extraArgs):
    """Creates a writer that can write the data produced by the source proxy in
        the given file format (identified by the extension). If no source is
        provided, then the active source is used. This doesn't actually write the
        data, it simply creates the writer and returns it."""
    if not filename:
        raise RuntimeError ("filename must be specified")
    session = servermanager.ActiveConnection.Session
    writer_factory = servermanager.vtkSMProxyManager.GetProxyManager().GetWriterFactory()
    if writer_factory.GetNumberOfRegisteredPrototypes() == 0:
        writer_factory.UpdateAvailableWriters()
    if not proxy:
        proxy = GetActiveSource()
    if not proxy:
        raise RuntimeError ("Could not locate source to write")
    writer_proxy = writer_factory.CreateWriter(filename, proxy.SMProxy, proxy.Port)
    writer_proxy.UnRegister(None)
    pyproxy = servermanager._getPyProxy(writer_proxy)
    if pyproxy and extraArgs:
        SetProperties(pyproxy, **extraArgs)
    return pyproxy

# -----------------------------------------------------------------------------

def SaveData(filename, proxy=None, **extraArgs):
    """Save data produced by 'proxy' in a file. If no proxy is specified the
    active source is used. Properties to configure the writer can be passed in
    as keyword arguments. Example usage::

        SaveData("sample.pvtp", source0)
        SaveData("sample.csv", FieldAssociation="Points")
    """
    writer = CreateWriter(filename, proxy, **extraArgs)
    if not writer:
        raise RuntimeError ("Could not create writer for specified file or data type")
    writer.UpdateVTKObjects()
    writer.UpdatePipeline()
    del writer
    
# -----------------------------------------------------------------------------

问题已回答here(也是我的post)。我使用 SaveData() 将二进制文件与我需要的代理一起保存,然后使用 DataSetWriter() 将我的 FileType 更改为 ASCII。这不是一个很好的解决方案,因为 SaveData() 应该这样做,但它确实完成了工作。