Example of FindItemsByQueryWithMax2 (Python)¶
Example of FindItemsByQueryWithMax2 (Python)¶
1# This example finds database items using a specified query and displays the
2# references of any items found in the output toolbox. The number of items found is capped at 2.
3
4# Create a local variable for the output toolbox
5ot = impact.gui.output_toolbox
6
7# Clear the output toolbox
8ot.clear()
9
10# Create a local variable for the active database
11db = impact.active_database
12
13# Create a local variable for the name of the query
14queryName = "Contains a"
15
16# Get the query to find items by
17query = db.find_master_tool_setting( queryName, ipMasterSettingType.mstDBQuery )
18
19# Check the query exists
20if query.isNone():
21
22 # Add a message to the output toolbox
23 ot.add("Unable to find query '" + queryName + "'")
24
25else:
26
27 # Find the database items using the specified query, but limit to 2 items
28 items = db.find_items_by_query_with_max2( query, 2 )
29
30 # check that some items were returned
31 if isinstance(items):
32
33 # iterate all the items
34 for i in range(0, len(items) - 1 + 1):
35
36 # Create a local variable for the database item
37 dbItem = None
38 try:
39 dbItem = items(i)
40 except Exception as exc:
41 pass
42 impact.gui.output_toolbox.add(f"Failed to create object via items(): {exc}")
43
44 # display the reference of the database item in the output toolbox
45 ot.add(dbItem.reference)
46 else:
47
48 # Add a message to the output toolbox
49 ot.add("No items found using query '" + queryName + "'")