Name

search — find and list objects in a BRL-CAD database

Synopsis

search [options...] [path1] [path2...] [search plan...]

DESCRIPTION

search works in a fashion similar to Unix-style find commands (internally it is based on OpenBSD's and NetBSD's find code) but searches objects in the current open database in MGED rather than files in a file system. One or more search paths are specified for searching, and a search plan specifies filters through which objects in the search paths are tested. The names of objects satisfying all of the filters are returned as the command output.

Search Inputs are specified using PATHS and matching of objects is done according to boolean expressions formed using the PRIMITIVES and OPERATORS described below.

OPTIONS

-a

Overrides the hidden flag when assembling path lists, if a generic toplevel path has been specified.

-Q

Exit quietly without printing error messages if the search plan contains errors. Primarily used in situations where programs automatically generate search invocations.

-v

Prints extra information in full path results.

PATHS:

By default, only the "top level" objects in any search are used as starting points in the search. The default mode if no path is explicitly specified returns a list of all objects found matching the criteria using the top level objects as inputs. Specifying paths is done as follows:

Table 1. Path Specifiers

FormResults
/Search using top level objects in database as starting points, return list of matching full paths.
.Search using top level objects in database as starting points, return list of all matching objects.
|Iterate over all objects in the database without doing a tree walk, return list of all matching objects.
|/Search using every object in the database as a starting point, return list of matching full paths.
|.Search using every object in the database as a starting point, return list of all matching objects.
/objnameUse object objname as the starting point, return list of matching full paths.
objnameUse object objname as the starting point, return list of all matching objects.
|/objnameUse every object below objname as starting points, return list of matching full paths.
|objnameUse every object below objname as starting points, return list of all matching objects.


One search command may use multiple paths, each of which can specify its own mode - such a "mixed mode" search will return a list of full paths and objects in the order paths were supplied to search.

The "flat" searches that are specified by using '|' as a modifier to other paths will use every object in the database as starting points for full tree walks and consequently will be significantly more expensive for complex tree hierarchies. The "truly flat" iterative search triggered by the the stand-alone '|' path specifier will be faster, but because it does not walk the tree hierarchy depth-related options (-above, -below, -depth, etc.) will not function.

PRIMITIVES:

-attr attrib[>/</=val]

Returns TRUE if the object has attribute attrib. If a specific value is also supplied for the attribute with an equal condition, TRUE is returned if the object both has the attribute and the attribute is set to value val. In the case of >, <, >=, and <= there are two possibilities - if val is numerical a numerical comparison is performed, otherwise a string comparison is performed. Both attrib and val are treated as patterns under shell pattern matching rules when a string comparison is performed, but in numerical mode only the attribute name is pattern matched.

-bool u/+/-

True if the object in question is being combined into the local hierarchy with the specified boolean operation. Recognized types are Union (u), Intersection (+), and Subtraction (-).

Note that by convention, top level objects are implicitly "unioned" into the database. To see only objects that have been unioned into a comb tree without returning the top level comb object as well, use the -mindepth option to filter for returns deeper in the tree.

-depth [>/</=]depth

True if the depth of the object in the tree is within the range specified.

-iname pattern

Like name except the match is case insensitive.

-iregex pattern

Like regex except the match is case insensitive.

-maxdepth n

True if the depth of the object in the tree is less than or equal to n.

-mindepth n

True if the depth of the object in the tree is greater than or equal to n.

-name pattern

True if the object name (not the full path, just the name) matches pattern. The evaluation is done according to shell pattern matching rules.

-nnodes n

True if the object is a combination and has n nodes. This option also supports supplying <, >, <=, and >= in front of the number to support returning true for objects with node counts less than, greater than, less than or equal to, and greater than or equal to n.

-param param[>/</=val]

Returns TRUE if the object has parameter param. Unlike -attr, which looks only at general attributes stored as metadata on objects, the -param filters based only on the internal geometric object's characteristics. If a specific value is also supplied for the parameter with an equal condition, TRUE is returned if the object both has the parameter and the parameter is set to value val. In the case of >, <, >=, and <= there are two possibilities - if val is numerical a numerical comparison is performed, otherwise a string comparison is performed. Both attrib and val are treated as patterns under shell pattern matching rules when a string comparison is performed, but in numerical mode only the parameter name is pattern matched.

The following table outlines what parameters are visible to the -param option. Because BRL-CAD uses the same internal containers for some types, some parameters will be present even when they do not make sense for the specific subtype. Examples include using arb8's container for arb4 and tgc for rcc. Note: there are also a number of primitives whose parameters are not exposed in a way that that -param can access them: arbn, ars, brep, bspline, dsp, metaball, nmg, pipe, pnts, and poly.

Table 2. Primitive Parameters

PrimitiveAvailable Parameters
arb8 (arb4, arb5, arb6, arb7)V1, V2, V3, V4, V5, V6, V7, V8
botmode, orient, V, F, T
clineV, H, R, T
ebmF, W, N, H, M
ehyV, H, A, r_1, r_2, c
ell (sph)V, A, B, C
epaV, H, A, r_1, r_2
etoV, N, C, r, r_d
extrudeV, H, A, B, S, K
gripV, N, L
halfN, d
hfcfile, dfile, fmt, w, n, shorts, file2mm, v, x, y, xlen, ylen, zscale
hypV, H, A, b, bnr
partV, H, r_v, r_h
revolveV, axis, R, ang, sk_name
rhcV, H, B, r, c
rpcV, H, B, r
sketchV, A, B, VL, SL
submodelfile, treetop, meth
tgc (rcc, rec, tec, trc)V, H, A, B, C, D
torV, H, r_a, r_h
volfile, w, n, d, lo, hi, size, mat


-path pattern

True if the object path matches pattern. The evaluation is done according to shell pattern matching rules.

-print

Evaluates to TRUE - used to print out the path and name of the object.

-regex pattern

True if the object path matches pattern. Pattern evaluation is done using Regular Expression matching.

-stdattr

Returns TRUE if an object has ONLY standard attributes associated with it. Standard attributes in BRL-CAD are:

Table 3. Standard Attributes

GIFTmaterinherit
material_idoshader
region_idrgb


-type pattern

Returns TRUE if the type of the object matches pattern. Pattern evaluation is done using shell pattern matching. Types recognized include:

Table 4. Primitives

arb4arb5arb6arb7arb8arbn
arsbotbrepclinedspebm
ehyellepaetoextrudegrip
halfhfhypjointmetaballnmg
partpipepolyrecrhcrpc
sketchsphsplinesubmodeltgctor
vol     


Table 5. Combinations

TypeAbbreviations Recognized
combinationc, comb, combination
regionr, reg, region


As a convenience, the -type option will also recognize the pattern shape, which will match any object that is not a combination, region, annotation, constraint, grip or joint.

OPERATORS

( expression )

Evaluates to true if the expression inside the parentheses evaluates to true. Used to establish order of operations.

-below[>/</=val] expression

The below operator applies the expression to every object above the current full path to that object, and returns TRUE if one or more of them satisfies the expression. E.g., searching with -below -name s\* on /component/region.r/start_comb.c/prim1.s would return TRUE for /component and /component/region.r as start_comb.c is below both of them, but would return FALSE for any deeper objects (i.e., prim1.s in this example).

Optionally, inequality and equality constraints on depth can be appended to the operator to limit what relative depths are accepted as matches.

expression -and expression (a.k.a. -a)

The and operator operates like the logical AND operator - TRUE only if both expressions are true. AND is the default operator assumed if two expressions are present with no operator explicitly defined.

-above[>/</=val] expression

The above operator applies the expression to every object below the current object in the tree hierarchy. It finds objects that are above a given expression. In effect, it does a "mini-search" up the tree using the current object path as a starting point and returns TRUE if the expression is satisfied by one or more objects in the sub-tree.

Optionally, inequality and equality constraints on depth can be appended to the operator to limit what relative depths are accepted as matches.

-not expression (a.k.a. !)

The logical NOT operator - returns the opposite of the evaluation result of expression.

expression -or expression (a.k.a. -o)

The logical OR operator - true if either expression is true.

A Note on Expressions

All primaries and operands must be expressed as separate arguments to search. In practice, this means that there must be a space between each element in a search expression. E.g. !(-name [a-z] -or -name [0-9]) must be written as:

! ( -name [a-z] -or -name [0-9] )

While this may seem like a rather verbose way of writing the expression, it greatly simplifies the parsing of the expression and is standard for virtually all find type commands. Because [a-z] and [0-9] are atomic arguments as far as search is concerned, they should NOT be expanded in a similar fashion.

EXAMPLES

The following are run from the MGED command prompt:

Example 1. Shell Globbing Based Pattern Match of Object Name

search -name *.s

Find all objects in the database with names ending in ".s".


Example 2. Full Path Instance Reporting of Search Results

search / -name *.s

Find all instances of objects in the database with names ending in ".s".


Example 3. Name Pattern Matching at Depth >= 3

search / -name *.s -mindepth 3

Find all instances of objects in the database with names ending in ".s" that are 3 or more levels deep in their tree.


Example 4. Finding all Objects in a Subtree

search group1 -name *.r

Find all objects within group1 in the database that have names ending in ".r" Note that this will return a list of objects, not the full path information associated with each instance of the objects matched.


Example 5. Finding all Instances of Objects in a Subtree

search /group1 -name *.r

Find all instances of objects within group1 in the database that have names ending in ".r" Note that, unlike the previous example, this will return the full path information associated with each instance of the objects matched.


Example 6. Finding all Instances of a Sub-Path

search / -path \*r245

Find all paths which end with the object r245. Note that all paths generated by objects below r245 will not match this pattern. To find all paths with r245 in them, use the following pattern:

search / -path \*r245\*

If you need to match an object path that is potentially ambiguous (say, r1 when there are lots of regions that match the r1* globbing rule) add the trailing slash to the expression:

search / -path \*r1\/\*


Example 7. Searching by Type

search . -type eto

Find all objects in the database that are elliptical tori. Note that a path of "." produces the same search behavior as the default behavior for an unspecified path.


Example 8. Finding all Instances of Nested Regions

search / -type region -below -type region

Find all instances of objects of type region that contain a region. This searches for regions being used within the subtrees of other regions, which is considered bad practice in BRL-CAD.


Example 9. Finding all Assemblies

search / -type comb ! -below -type region ! -type region

Find all instances of objects of type combination that are not below regions and are not themselves regions. These are regarded as "assemblies" in BRL-CAD.


Example 10. Finding all Combinations below Regions

search / -type comb -below -type region ! -type region

Find all instances of combinations that are not regions and do not contain regions.


Example 11. Finding all Empty Combinations

search -nnodes 0

Find all combinations containing zero nodes.


Example 12. Finding all Combinations With 10 or More Nodes

search -nnodes >=10

Find all combinations with 10 or more nodes.


Example 13. Finding all Intersected Objects

search -bool +

Find all objects that are combined into one or more trees using the Intersect boolean operation somewhere in the .g file. This approach only identifies which objects are intersected somewhere in the tree, not where specifically they are intersected.


Example 14. Finding all Instances of Object Intersection

search / -bool +

Find all boolean operations that use the Intersect boolean operation and report the full paths to those objects. This approach reports all individual cases where an object has been combined into a hierarchy by intersection, not just which objects have been included with the intersection operator.


Example 15. Finding all Unions Within a Comb's Tree

search /all.g -mindepth 1 -bool u

Find all unions within the all.g tree hierarchy, excluding all.g itself.


Example 16. Finding all Non-Subtracted Regions below Regions

search / -type region -below -type region ! -bool -

A more nuanced variation on finding nested regions that adds a check to make sure that the regions aren't being subtracted. While not ideal, using a subtraction does avoid the modeling error of two volumes claiming the three dimensional space and is arguably acceptable - the below command finds nested region instances that are considered errors.


Example 17. Finding all Regions with RGB color 180 180 180

search -type region ( -attr color=180/180/180 -or -attr rgb=180/180/180 )

Prints all regions with their color attribute set to 180/180/180. Some files may use rgb instead of color, so to be comprehensive an -or option is used to check both possibilities.


Example 18. Finding all Plate-Mode BoT Objects

search -type bot -param mode=plate

Prints all BoT primitives using "plate" mode, instead of the closed-mesh-requiring "volume" mode.


Example 19. Using search Results

While the search command as currently implemented in BRL-CAD does not support a find command style exec option, it is possible to collect search results in a Tcl variable and use Tcl scripting to operate on the resulting object set.


  
    mged> set glob_compat_mode 0
    mged> set search_results [search all.g -type region ! -below -bool - -and ! -below -bool +]
    mged> foreach i $search_results {draw $i}
  

The above set of commands finds all regions below the all.g object that contain only unioned objects and draws those regions in the MGED display manager window.

Another common operation to apply to a set of results is to set their colors to random RGB values (here we use random numbers and avoid any value less than 30 to keep objects more visible):


  
    mged> set glob_compat_mode 0
    mged> set search_results [search -type region]
    mged> foreach i $search_results {attr set $i color [expr {int(rand()*225)+30}]/[expr {int(rand()*225)+30}]/[expr {int(rand()*225)+30}]}
  

This approach uses Tcl's random number generator to generate different RGB strings to use for each object in the results list. If we also want to add transparency to the resulting model, an additional foreach line on the same results set can do the job:


  
    mged> foreach i $search_results {attr set $i shader {glass {tr 0.7 ri 1}}}
  

Note

Remember when setting region colors to check for the presence of a regionid_colortable attribute on the _GLOBAL object - if such a table is present (the m35.g sample model has one, for example) you need to remove that table before the colors on the individual regions will "take":


    
      mged> attr rm _GLOBAL regionid_colortable
    

  


DIAGNOSTICS

Errors will be returned if parsing of the arguments fails, or one of the primaries' evaluation functions returns an error.

AUTHOR

Clifford Yapp

COPYRIGHT

This software is Copyright (c) 2008-2016 United States Government as represented by the U.S. Army Research Laboratory. Portions Copyright 1990, 1993, 1994 The Regents of the University of California, per copyright and license information from OpenBSD and NetBSD. For more details see the copyright statements in search.c and search.h.

BUGS

Bugs resulting from incorrect parsing of shell pattern expressions are the result of libbu's fnmatch.

BUG REPORTS

Reports of bugs or problems should be submitted via electronic mail to <devs@brlcad.org>, or via the "cadbug.sh" script.