File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
src/compas/datastructures/assembly
tests/compas/datastructures Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -201,3 +201,22 @@ def find(self, guid):
201201
202202 return self .graph .node_attribute (key , "part" )
203203
204+ def find_by_key (self , key ):
205+ """Find a part in the assembly by its key.
206+
207+ Parameters
208+ ----------
209+ key : int | str, optional
210+ The identifier of the part in the assembly.
211+
212+ Returns
213+ -------
214+ :class:`~compas.datastructures.Part` | None
215+ The identified part,
216+ or None if the part can't be found.
217+
218+ """
219+ if key not in self .graph .node :
220+ return None
221+
222+ return self .graph .node_attribute (key , "part" )
Original file line number Diff line number Diff line change @@ -41,3 +41,16 @@ def test_find():
4141
4242 assembly .add_part (part )
4343 assert assembly .find (part .guid ) == part
44+
45+
46+ def test_find_by_key ():
47+ assembly = Assembly ()
48+ part = Part ()
49+ assembly .add_part (part , key = 2 )
50+ assert assembly .find_by_key (2 ) == part
51+
52+ part = Part ()
53+ assembly .add_part (part , key = "6" )
54+ assert assembly .find_by_key ("6" ) == part
55+
56+ assert assembly .find_by_key ("100" ) is None
You can’t perform that action at this time.
0 commit comments