添加循环

在宏中添加一个循环,用于重命名已创建的线探针

要添加到宏的附加代码用通俗易懂的语言描述如下:

  • 设置最小 X 坐标 XMIN。
  • 设置最大 X 坐标 XMAX。
  • 设置线探针之间所需的 X 距离 XDIST。
  • 初始化当前 X 位置 XLOC = XMIN + XDIST。
  • WHILE(XLOC 小于 XMAX)
    • 在 XLOC 处创建线探针
    • 使用标题中的 XLOC 重命名新的线探针
    • XLOC = XLOC + XDIST
  • END WHILE

为避免在宏中直接设置 XMIN 和 XMAX,使用表征类的 getRegionExtents 方法来设置适当的值。Java API 文档中表征类的方法显示它需要要获取范围的区域对象。

在编辑器窗口中,将之前考虑的代码段替换为后面的修改后的代码段。原始代码以纯文本显示,新代码或修改后的代码以粗体显示。完全按照显示输入新代码,因为 Java 是一种区分大小写的编程语言。

// Start of execute0 function
 
Simulation simulation_0 =
  getActiveSimulation();
 
Units units_1 =
  simulation_0
  .getUnitsManager()
  .getPreferredUnits(
    Dimension.Builder().length(1).build())
    )
;
 Scene scene_0 =
  simulation_0
  .getSceneManager()
  .getScene("Scalar Scene 1");
 
   
FvRepresentation fvRepresentation_0 =
  ((FvRepresentation) simulation_0
  .getRepresentationManager()
  .getObject("Volume Mesh"));
  
Region region_1 =
  simulation_0
  .getRegionManager()
  .getRegion("Fluid");

PartDisplayer partDisplayer_0 =
    scene_0.getDisplayerManager().createPartDisplayer("Probe Surface", -1, 1);
 
// 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_1);
 
// 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_1 = 
      simulation_0.getPartManager().createLinePart(new ArrayList<>(Collections.
<NamedObject>emptyList()),
new DoubleVector(new double[] {0.0, 0.0, 0.0}),
new DoubleVector(new double[] {1.0, 0.0, 0.0}) 
20, null);

LabCoordinateSystem labCoordinateSystem_0 =
  simulation_0.getCoordinateSystemManager().
  getLabCoordinateSystem();


linePart_1.getPoint1Coordinate()
.setCoordinateSystem(labCoordinateSystem_0);
 

linePart_1.getPoint1Coordinate()
.setUnits0(units_1);
linePart_1.getPoint1Coordinate()
.setUnits1(units_1);
linePart_1.getPoint1Coordinate()
.setUnits2(units_1);
linePart_1.getPoint1Coordinate()
.setDefinition("");


linePart_1.getPoint1Coordinate()
.setValue(
  new DoubleVector(
    new double[] {dXLOC, 0.0, 0.0}
  )
);

linePart_1.getPoint1Coordinate
.setCoordinate(
  units_1,
  units_1,
  units_1,
  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.
 
linePart_1.getPoint2Coordinate()
.setUnits0(units_1);
linePart_1.getPoint2Coordinate()
.setUnits1(units_1);
linePart_1.getPoint2Coordinate()
.setUnits2(units_1);
linePart_1.getPoint2Coordinate()
.setDefinition("");


linePart_1.getPoint2Coordinate()
.setValue(
  new DoubleVector(
    new double[] {dXLOC, 0.0762, 0.0}
  )
);

linePart_1.getPoint2Coordinate
.setCoordinate(
  units_1,
  units_1,
  units_1,
  new DoubleVector(
    new double[] {dXLOC, 0.0762, 0.0}
  )
);
 
 
// 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.
 

PartDisplayer_0.getVisibleParts().addPart(linePart_1);
partDisplayer_0.getHiddenParts().addParts();

String strPartName =
  String.format("X = %6.4f",dXLOC);
 
linePart_1.setPresentationName(strPartName);
 
// Update dXLOC and end the loop with a closing brace
dXLOC = dXLOC + dXDIST;
} // End of Loop
 

这样就完成了创建线探针的主要部分。