Adding the Loop
Add a loop to the macro that renames the line probes that are created.
The additional code to be added to the macro is described in plain language as follows:
- Set the minimum X coordinate, XMIN.
- Set the maximum X coordinate, XMAX.
- Set the required X distance between line probes, XDIST.
- Initialize the current X location, XLOC = XMIN + XDIST.
- WHILE (XLOC is less than XMAX)
- Create a line probe at XLOC
- Rename the new line probe with XLOC in the title
- XLOC = XLOC + XDIST
- END WHILE
To avoid having to set XMIN and XMAX directly in the macro, use the
getRegionExtents
method of the
Representation
class to set appropriate values. The method in the Java API documentation for the
Representation
class, shows that it requires the
Region
object for which extents are to be obtained.
In your editor window, replace the section of code considered previously with the modified code that now follows. Original code is shown in plain text and new or modified code is shown in bold. Enter the new code exactly as shown, as Java is a case-sensitive programming language.
// Start of execute0 function
Simulation simulation_0 =
getActiveSimulation();
Units units_0 =
simulation_0
.getUnitsManager()
.getPreferredUnits(
new IntVector(
new int[]
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
)
);
Scene scene_0 =
simulation_0
.getSceneManager()
.getScene("Scalar Scene 1");
PartDisplayer partDisplayer_1 =
((PartDisplayer) scene_0
.getCreatorDisplayer());
FvRepresentation fvRepresentation_0 =
((FvRepresentation) simulation_0
.getRepresentationManager()
.getObject("Volume Mesh"));
partDisplayer_1
.setRepresentation(fvRepresentation_0);
Region region_0 =
simulation_0
.getRegionManager()
.getRegion("Fluid");
// New code begins here
// Define preliminary variables
double dXDIST = 0.1; // Distance between probes
// Use the FvRepresentation to find the extents of region_0
// (which was retrieved from simulation_0 earlier).
// FvRepresentation is derived from the Representation
// class and provides it's own version of the
// getRegionExtents method specifically for volume
// regions.
// getRegionExtents returns an array of six doubles:
// [xmin,xmax,ymin,ymax,zmin,zmax]
double[] dExtents =
fvRepresentation_0
.getRegionExtents(region_0);
// Retrieve xmin and xmax from dExtents
double dXMIN = dExtents[0];
double dXMAX = dExtents[1];
// Set the initial X location
double dXLOC = dXMIN + dXDIST;
// Start the loop
while(dXLOC < dXMAX) {
// Comes the section from the recorded macro
// Indent it further by two spaces and change
// all references to 0.04 with the variable dXLOC
LinePart linePart_0 =
simulation_0
.getPartManager()
.createLinePart(
new NeoObjectVector(
new Object[] {region_0}
),
new DoubleVector(
new double[] {dXLOC, 0.0, 0.0}
),
new DoubleVector(
new double[] {dXLOC, 0.0762, 0.0}
),
20
);
LabCoordinateSystem labCoordinateSystem_0 =
simulation_0
.getCoordinateSystemManager()
.getLabCoordinateSystem();
linePart_0
.setCoordinateSystem(labCoordinateSystem_0);
Coordinate coordinate_0 =
linePart_0.getPoint1Coordinate();
coordinate_0
.setCoordinate(
units_0,
units_0,
units_0,
new DoubleVector(
new double[] {dXLOC, 0.0, 0.0}
)
);
// The next two sections set the units on each axis,
// and the position, of the second Point of linePart_0.
Coordinate coordinate_1 =
linePart_0
.getPoint2Coordinate();
coordinate_1
.setCoordinate(
units_0,
units_0,
units_0,
new DoubleVector(
new double[] {dXLOC, 0.0762, 0.0}
)
);
// Redundant sections removed from here
// Change the name of the linePart.
// The new name is created using the current X
// location. The String.format method ensures
// a consistent appearance of the number. See
// the Java documentation for details.
String strPartName =
String.format("X = %6.4f",dXLOC);
linePart_0.setPresentationName(strPartName);
// Update dXLOC and end the loop with a closing brace
dXLOC = dXLOC + dXDIST;
} // End of Loop
This completes the main section for creating line probes.