SEARCH(nged)

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 a search plan built using boolean expressions constructed using the PRIMITIVES and OPERATORS described below. Note that OPTIONS are not used to form search plans - unlike the search plan they are specified before the paths and are used to control settings that apply globally to search's behavior and reporting during the execution of the search.

The results of the search command will be sorted according to David Koelle’s "alphanum" algorithm, which is basically a human-intuitive ordering for strings with letters and numbers. For example, a result returning d1.r, d10.r and d2.r will be sorted as:

d1.r
d2.r
d10.r

instead of the default ASCII character ordering, which would look like:

d1.r
d10.r
d2.r

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
Form Results

/

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.

/objname

Use object objname as the starting point, return list of matching full paths.

objname

Use object objname as the starting point, return list of all matching objects.

|/objname

Use every object below objname as starting points, return list of matching full paths.

|objname

Use 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.

-exec expression arg1 arg2 …​ ";"

True if the Tcl expression returns true. Any pair of curly braces ('{}') in an arguemt is replaced by the name or full path of the object. The semicolon terminates the argument list and must not be omitted. Remember to quote the parameters properly. The semicolon must stand on its own and must be quoted.

When using multiple -exec filters, keep in mind that filters are evaluated lazily, so if you have two -execs and the first one returns false for one node, the second one will not run at all.

-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
Primitive Available Parameters

arb8 (arb4, arb5, arb6, arb7)

V1, V2, V3, V4, V5, V6, V7, V8

bot

mode[surf,volume,plate,plate_nocos], orient[no,lh,rh], V, F, T

cline

V, H, R, T

ebm

F, W, N, H, M

ehy

V, H, A, r_1, r_2, c

ell (sph)

V, A, B, C

epa

V, H, A, r_1, r_2

eto

V, N, C, r, r_d

extrude

V, H, A, B, S, K

grip

V, N, L

half

N, d

hf

cfile, dfile, fmt, w, n, shorts, file2mm, v, x, y, xlen, ylen, zscale

hyp

V, H, A, b, bnr

part

V, H, r_v, r_h

revolve

V, axis, R, ang, sk_name

rhc

V, H, B, r, c

rpc

V, H, B, r

sketch

V, A, B, VL, SL

submodel

file, treetop, meth

tgc (rcc, rec, tec, trc)

V, H, A, B, C, D

tor

V, H, r_a, r_h

vol

file, 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.

-size [>/</=]size

True if the size of the object is within the range specified. NOTE: for combs, extrudes, and other composite objects this filter considers only the object definition itself, not its related elements.

-stdattr

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

Table 3. Standard Attributes

GIFTmater

inherit

material_id

oshader

region_id

rgb

-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

arb4

arb5

arb6

arb7

arb8

arbn

annot

ars

binunif

bot

brep

bspline

cline

constrnt

datum

dsp

ebm

ehy

ell

epa

eto

extrude

grip

half

hf

hrt

hyp

joint

metaball

nmg

part

pipe

pnts

poly

rec

rhc

rpc

script

sketch

sph

submodel

superell

tgc

tor

vol

Table 5. Combinations
Type Abbreviations Recognized

combination

c, comb, combination

region

r, 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.

In order to identify shapes defined as implicit solids using the "plate mode" methodology, the pattern plate will match any object whose internal definition indiciates it is of that type. Currently such objects are possible with "bot" and "brep" objects.

The the pattern volume can be used as a mirror of plate, in that it will report objects that clearly define a non-implicit solid volume. Its determination is local - for example, combs aren’t matched because a comb may be made up of non-volumetric objects. In particular, plate mode objects will not match because their volume is implicit, and (for example) surface mode BoTs will not match because they don’t define a volume.

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 below the matching object in the tree hierarchy. E.g. searching with -below -name start\* on /component/region.r/start_comb.c/prim1.s would return prim1.s, as the object below start_comb.c

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 above the current full path to that object, and returns TRUE if one or more of them satisfies the expression. E.g., searching with -above -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.

-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. Find All Combs Containing At Least One of Two Objects (dbfind)

search |. ( -depth 0 -above=1 -name obj1 ) -or ( -depth 0 -above=1 -name obj2 )

Prints all combs immediately above either obj1 or obj2. This more or less mimics the results of the dbfind command, although it will be somewhat slower (noticably so on large databases.)

Example 19. Finding all regions containing an intersection of obj2

search |/ -maxdepth 0 -type region -above ( -name obj2 -bool + )

Prints all regions immediately above obj2 where obj2 is included using an intersection operation.

Example 20. 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 21. Verbose Reporting of Search Information

mged> search -v /all.g ! -type comb

/u all.g(c)/u platform.r(r)/u platform.s(arb8)
/u all.g(c)/u box.r(r)/u box.s(arb8)
/u all.g(c)/u cone.r(r)/u cone.s(tgc)
/u all.g(c)/u ellipse.r(r)/u ellipse.s(ell)
/u all.g(c)/u tor.r(r)/u tor(tor)
/u all.g(c)/u light.r(r)/u LIGHT(ell)

Report all paths ending in an object that is not a comb using "verbose" output style. The verbose form of path reporting reports not just the path, but the boolean operator used at each setp along the path and the primitive type of each object along the path.

Example 22. Operating on Sets of search Results

There are two available methods for working with sets of search results. The first is the exec option, which is used in the following example to search for all regions below subtractions or intersections in the m35.g example model in BRL-CAD and draw the objects' wireframes in the display manager:

mged>search all.g -type region -and ( -below -bool - -or -below -bool + ) -exec draw "{}" ";"
mged>autoview

ANOTHER possibility, especially useful for doing multiple sequential operations which would be awkward to write out in a single search line, is to assign the results of a search command to a Tcl variable. To illustrate, let’s randomize the colors of all regions in the "cab" assembly of the m35.g example model and then assign a glass shader with transparency as a follow-on step.

First, we collect the set of search results into a Tcl variable:

mged> set glob_compat_mode 0
mged> set search_results [search cab -type region]

Next we randomize the colors using Tcl’s functionality (here we use random numbers and avoid any value less than 30 to keep objects more visible):

mged> foreach i $search_results {attr set $i color [expr {int(rand()*225)+30}]/[expr {int(rand()*225)+30}]/[expr {int(rand()*225)+30}]}

Finally, we add transparency to the same regions:

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

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 (and for MGED wireframes, re-open the database) 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

This software is Copyright (c) 2008-2021 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.