Simcenter STAR-CCM+ How to filter objects of a Collection in Java, based on their name?

2023-11-02T04:14:32.000-0400
Simcenter STAR-CCM+ Simcenter STAR-CCM+ Virtual Reality Teamcenter Share Simcenter STAR-CCM+ Viewer Simcenter STAR-CCM+ Application Specific Solutions

Summary

A Collection is a Java class that is used to group elements (objects). In Simcenter STAR-CCM+, collections are used to group objects such as boundaries, regions, or derived parts.


Details

A Collection is a Java class that is used to group elements (objects). In Simcenter STAR-CCM+, collections are used to group objects such as boundaries, regions, or derived parts.

Filter elements with a name containing a certain string of characters
When using, “Collection myRegionColl = sim.getRegionManager().getObjects(); you get all objects from the region manager. If only regions with a name containing, for example, “solid” are required, use the following code:
Collection <Region> myRegionColl = sim.getRegionManager().getObjects();
Collection <Region> myFilteredRegionColl = new ArrayList();
for(Region reg: myRegionColl){
    if(reg.getPresentationName().toLowerCase().contains("solid”)){
        myFilteredRegionColl.add(reg);
    }
}
This way, the collection “myFilteredRegionColl” contains only regions with names containing “solid” without taking the case in consideration, which means a region that is named “Carter_soLid_18” is in the collection.
 
Filter elements with a name starting with a certain string of characters:
Collection <Region> myRegionColl = sim.getRegionManager().getObjects();
Collection <Region> myFilteredRegionColl = new ArrayList();
for(Region reg: myRegionColl){
    if(reg.getPresentationName().toLowerCase().startsWith(“solid”)){
        myFilteredRegionColl.add(reg);
    }
}
This way, the collection “myFilteredRegionColl” contains only regions with a name starting with “Solid” without taking the case in consideration, which means a region that is named “soLid_18” is in the collection.
 
Filter elements with a name ending with a certain string of characters:
Collection <Region> myRegionColl = sim.getRegionManager().getObjects();
Collection <Region> myFilteredRegionColl = new ArrayList();
for(Region reg: myRegionColl){
    if(reg.getPresentationName().toLowerCase().endsWith(“solid”)){
        myFilteredRegionColl.add(reg);
    }
}
This way, the collection “myFilteredRegionColl” contains only regions with a name ending with “solid” without taking the case in consideration, which means a region that is named “Carter_soLid” is in the collection.
 
Filter elements with a name starting with a figure:
Harder but useful, this time, what is called “regex” in Java is used:
 
Collection <Region> myRegionColl = sim.getRegionManager().getObjects();
Collection <Region> myFilteredRegionColl = new ArrayList();
for(Region reg: myRegionColl){
    if(reg.getPresentationName().toLowerCase().matches(“^\\d.*”)){
        myFilteredRegionColl.add(reg);
    }
}

 
  • "^" means: starts with,
  • "\\d" means: a figure and
  • ".*" means: whatever follows.
This way, the collection “myFilteredRegionColl” contains only regions with a name starting with a figure: 0, 1, 2, 3,..., 9, which means a region that is named “7_Carter_soLid” is in the collection.
 
Filter elements with a name not starting with a figure:
Use the same syntax as the previous one, but replace “^\\d.*” by “^\\D.*
 
Other examples:
Starts with a letter: “^[a-zA-Z].*
Does not start with a letter: “^[^a-zA-Z].*
Starts with a white space: “^\\s.*
Does not start with a white space: “^\\S.*
Starts with a figure from 1 through 5: “^[1-5].*
 
They are infinite possibilities; to find out more, look for Java regex (regular expression) in your search engine.

Note: Be careful, copy/paste the code above could use bad quotes. If that's the case, just delete the quotes characters and type them again in your text editor.

KB Article ID# KB000034900_EN_US

Contents

SummaryDetails

Associated Components

Design Manager Electronics Cooling In-Cylinder (STAR-ICE) Job Manager Simcenter STAR-CCM+