da-crivelli / freecad-parametric-fea

A flexible parametric FEA library based on FreeCAD
GNU Lesser General Public License v2.1
33 stars 5 forks source link

Package .vtk files in a single scene #12

Open da-crivelli opened 1 year ago

da-crivelli commented 1 year ago

The idea is to be able to open a single ParaVIEW collection that includes all of the result meshes.

See the following code suggested by Lufthschraube on the FreeCAD forums:

PVUFile = open(ExportOutputPath + "/" + FileName + ".pvd","w")
    PVUFile.write("<VTKFile type=\"Collection\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n\t<Collection>\n")

    n = 0
    for ExportObject in sorted(FEMResultObjects):
        n = n + 1

        #Read and format time stamp (e.g., 12.37 s = 0001237)
        FreeCADResultLabelParts = ExportObject.split("_")
        PreDecimal = FreeCADResultLabelParts[-3]
        PostDecimal = FreeCADResultLabelParts[-2]

        if len(PostDecimal) == 1:
            PostDecimal = PostDecimal + str(0)
        TimeValue = float(PreDecimal) + float(PostDecimal)/100
        TimeString = str(int(round(TimeValue*100,0))).zfill(7)

        #For some reason, the VTK exporter expects a list (although only one object is processed at once...) - so build a list with the current object
        ExportObjectList = []
        ExportObjectList.append(FreeCAD.ActiveDocument.getObject(ExportObject))
        FileNameOUT = FileName + "_" + TimeString + ".vtu"
        FilePath = ExportOutputPath + "/" + FileNameOUT
        FreeCAD.Console.PrintMessage(FilePath + " is to be created\n")
        feminout.importVTKResults.export(ExportObjectList,FilePath)

        PVUFile.write("\t\t<DataSet timestep=\"" + str(TimeValue) + "\" file=\"" + FileNameOUT + "\"/>\n")

    #Close the .pvu file
    PVUFile.write("\t</Collection>\n</VTKFile>")
    PVUFile.close()