<div>Howdy.</div><div><br></div>I have been looking through the Internet to find a solution for how to have the Plot over line <div>executed automatically through a Python script. I was able to find some sources here on the </div>
<div>Paraview mailing list but nobody reporting a fully functional script (at list for my case).</div><div>So I will share what I wrote to help those people that will surely come up with the same problems</div><div>I had. What does the script do:</div>
<div><br></div><div>- execute a PlotOverLine filter </div><div>- export the results in a CSV file</div><div>- execute a subsequent second filter directly after the first</div><div>- export this to a separate file</div><div>
<br></div><div>I know it may sound an easy job and I know it is not by any means a "perfect" solution.... but</div><div>the code is heavily commented so I think it can surely work well as the base for further </div>
<div>development for other people. </div><div><br></div><div>Good work.</div><div><br></div><div>Luca</div><div><br></div><div>-----------------------------------------------------------------------------</div><div>-----------------------------------------------------------------------------</div>
<div><br></div><div><div># First of all, I created this script because I needed to execute the probing with</div><div># the Plot Over Line filter on 9 points for each time step in a long simulation with</div><div># the OpenFOAM CFD tool; that kind of thing that you cannot do by hand.</div>
<div># This is such a dumb script that you can of course modify it as you like to suit your needs! </div><div># That's Open Source...!</div><div>#</div><div># @@ IMPORTANT NOTICE @@</div><div># </div><div># If you are running Paraview using the binaries provided by the Ubuntu repos, it is</div>
<div># very likely that you don't have full python support due to the lack of some functions</div><div># here and there in the provided installation.</div><div># THUS, THIS SCRIPT WON'T RUN!! </div><div># The obvious work around is to uninstall the Ubuntu version and build from source the</div>
<div># last stable. This was tested using Ubuntu 11.04 and Paraview 3.8.0 32bit.</div><div>#</div><div># Luca Giannelli</div><div><br></div><div><br></div><div>import os</div><div>try: paraview.simple</div><div>except: from paraview.simple import *</div>
<div>paraview.simple._DisableFirstRenderCameraReset()</div><div><br></div><div># If you want to create a set of following operations and save the</div><div># results in different locations, you can manage the folders with</div>
<div># this code section. </div><div># Just write the path to your folder without forgetting the quotes "".</div><div><br></div><div>if not os.path.exists("YOUR_WORK_DIR_GOES_HERE"):</div><div> os.makedirs("YOUR_WORK_DIR_GOES_HERE")</div>
<div><br></div><div># you can add all the folders that you like in this way by copying this</div><div># check code and inputting the new names. </div><div><br></div><div># -------------------------------------------------------------------------------</div>
<div># Moving to the core of the script instuctions for the execution of PlotOverLine</div><div># -------------------------------------------------------------------------------</div><div><br></div><div># Let's first identify the source to which we want to execute the P-O-L on:</div>
<div># This "fotobioreattore_foam" is just the name that I used and you can change</div><div># it to whatever you like. In the same way, you must provide as a variable in</div><div># the FindSource, the name of the first module in the stack of your very own </div>
<div># case for the script to work. For example:</div><div>#</div><div># my_case = FindSource("THE_NAME_OF_YOUR_CASE_GOES_HERE")<span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div>fotobioreattore_foam = FindSource("fotobioreattore.foam")</div><div><br></div><div># Now set the source you just defined, as the active one so that the P-O-L filter</div><div># will be executed on that without need of any other definition. Example:</div>
<div>#</div><div># SetActiveSource(my_case) </div><div><br></div><div>SetActiveSource(fotobioreattore_foam)</div><div> </div><div># Create the first P-O-L, set the extremes of the line and then commit to render </div><div>
PlotOverLine2 = PlotOverLine( Source="High Resolution Line Source" )</div><div>DataRepresentation7 = Show()</div><div><br></div><div>PlotOverLine2.Source.Point1 = [0.0, 0.0, 0.0]</div><div>PlotOverLine2.Source.Point2 = [0.0, 0.0, 0.43]</div>
<div><br></div><div># This will calculate all the values for all the fileds along the line.</div><div># I was not able to identify how to show only the needed parameters.</div><div># However, it is trivial to run all the files through an "awk" command</div>
<div># for creating a file with just the needed parameters so I do not really</div><div># felt to spend time on something like this.</div><div># Sorry.</div><div><br></div><div>Render()</div><div><br></div><div># The last part of the first batch unit is the definition of a writer for</div>
<div># the actual CSV file creation.</div><div># The source should be your freshly created PlotOverLine. In my case it is called PlotOverLine2</div><div>source = PlotOverLine2</div><div>writer = CreateWriter("YOUR_FILE_NAME_GOES_HERE.csv", source)</div>
<div>writer.FieldAssociation = "Points" # or "Cells"</div><div>writer.UpdatePipeline()</div><div>del writer</div><div><br></div><div>del PlotOverLine2</div><div>del fotobioreattore_foam</div><div>del DataRepresentation7</div>
<div><br></div><div># ---------------------------------------------------------------------------------------------------</div><div># I am not really sure if this is needed or not but, you know... better to safe than sorry. So, with </div>
<div># this line, the first batch operation is concluded. You will have your CSV file in your folder</div><div># and you will end up with the PlotOverLine filter selected in the GUI. For this reason, if you want</div><div>
# to proceed in the creation of a 2nd P-O-L in the same original source, you will need to specify</div><div># the active source part again here.</div><div># ---------------------------------------------------------------------------------------------------</div>
<div><br></div><div># Second batch section. You can use whatever filter you prefer here.... I just needed to go with another </div><div># P-O-L in another place.</div><div><br></div><div>fotobioreattore_foam = FindSource("fotobioreattore.foam")</div>
<div>SetActiveSource(fotobioreattore_foam)</div><div><br></div><div><br></div><div>PlotOverLine2 = PlotOverLine( Source="High Resolution Line Source" )</div><div><br></div><div>DataRepresentation7 = Show()</div>
<div><br></div><div>PlotOverLine2.Source.Point1 = [-0.0345, 0.0, 0.019]</div><div>PlotOverLine2.Source.Point2 = [-0.0345, 0.0, 0.43]</div><div><br></div><div>Render()</div><div><br></div><div># The writer again</div><div>
source = PlotOverLine2</div><div>writer = CreateWriter("SECOND_FILE_NAME.csv", source)</div><div>writer.FieldAssociation = "Points" # or "Cells"</div><div>writer.UpdatePipeline()</div><div>del writer</div>
<div><br></div><div>del PlotOverLine2</div><div>del fotobioreattore_foam</div><div>del DataRepresentation7</div><div><br></div><div># Please continue adding all the filters that you like and exporting the</div><div># results to all the CSV files that you want.</div>
<div>#</div><div># Enjoy!!!!</div></div>