Component Index¶
Any Component
class can be added to the
cqparts
search index, allowing it to be found by other modules with
concise code
This includes anything we’ve made in Make your own Part and Make your own Assembly.
Registering, and searching is all done by the cqparts.search
module.
Registering¶
Adding a Component
to the search index is done
in one of 2 ways.
Decorator¶
You can use the register()
method as a class decorator.
>>> import cqparts
>>> from cqparts.search import register
>>> from cqparts.params import *
>>> @register(a='one', b='two')
... class SomeThing(cqparts.Part):
... length = Float(10)
>>> @register(a='one', b='three')
... class AnotherThing(cqparts.Assembly):
... height = Int(100)
This has added thse two components to the cqparts
search index.
Note that:
SomeThing
is aPart
, andAnotherThing
is anAssembly
. It actually doesn’t matter what they are, they will be added to the index, but they should be aComponent
of some sort.- Both
SomeThing
andAnotherThing
have a common criteriaa='one'
Tip
Make your component’s criteria unique.
If it is not unique you run the risk of making yours, or other’s components fail in searches.
Possibly employ common_criteria()
to clean up your code.
Direct Register¶
Components can also be registered by the register()
method without
using it as a decorator.
>>> import cqparts
>>> from cqparts.search import register
>>> from cqparts.params import *
>>> class _SomeThing(cqparts.Part):
... length = Float(10)
>>> SomeThing = register(a='one', b='two')(_SomeThing)
>>> class _AnotherThing(cqparts.Assembly):
... height = Int(100)
>>> AnotherThing = register(a='one', b='three')(_AnotherThing)
This and the decorated approach are functionally identical, which method you use is dependent on your library’s design, and which best suits you.
Searching & Finding¶
The 2 parts registered above can be found again with search()
and/or find()
:
Search¶
search()
returns a set
of all components that match the given
search criteria:
>>> from cqparts.search import search
>>> search(a='one')
{__main__.SomeThing, __main__.AnotherThing}
>>> search(b='two', a='one')
{__main__.SomeThing}
Find¶
But most of the time you’ll only be expecting one part to bounce back, because mostly this is intended as an indexing tool, not a searching tool.
To index a unique part, or fail (throw a
SearchError
exception), use find()
instead:
>>> from cqparts.search import find
>>> find(a='one', b='two')
__main__.SomeThing
>>> # You can use this to build a component straight away
>>> find(a='one', b='two')(length=50) # creates an instance
<SomeThing: length=50.0>
This is great for finding a component class with a registered part number.
Using find()
with criteria that is not unique will raise a
SearchMultipleFoundError
exception:
>>> find(a='one')
SearchMultipleFoundError: 2 results found
Similarly, calling find()
with criteria that is not indexed will raise a
SearchNoneFoundError
exception:
>>> find(b='infinity')
SearchNoneFoundError: 0 results found