Skip to content

Commit 7b52c65

Browse files
committed
check for elements with a given name
1 parent 24970e8 commit 7b52c65

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Added `Model.has_element_with_name`.
13+
* Added `Model.find_element_with_name`.
14+
1215
### Changed
1316

1417
### Removed

src/compas_model/models/model.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,38 @@ def has_element(self, element: Element) -> bool:
290290
guid = str(element.guid)
291291
return guid in self._elements
292292

293+
def has_element_with_name(self, name: str) -> bool:
294+
"""Returns True if the model contains an element with the given name.
295+
296+
Parameters
297+
----------
298+
name : str
299+
The name to check.
300+
301+
Returns
302+
-------
303+
bool
304+
305+
"""
306+
return any(element.name == name for element in self.elements())
307+
308+
def find_element_with_name(self, name: str) -> Optional[Element]:
309+
"""Returns True if the model contains an element with the given name.
310+
311+
Parameters
312+
----------
313+
name : str
314+
The name to check.
315+
316+
Returns
317+
-------
318+
:class:`Element` or None
319+
320+
"""
321+
for element in self.elements():
322+
if element.name == name:
323+
return element
324+
293325
# =============================================================================
294326
# Groups
295327
# =============================================================================

0 commit comments

Comments
 (0)