2222"""Provides for managing components."""
2323
2424from enum import Enum , unique
25+ from functools import cached_property
2526from typing import TYPE_CHECKING , Any , Optional , Union
2627import uuid
2728
@@ -239,6 +240,11 @@ def __init__(
239240
240241 self ._master_component .occurrences .append (self )
241242
243+ def _clear_cached_bodies (self ) -> None :
244+ """Clear the cached bodies."""
245+ if "bodies" in self .__dict__ :
246+ del self .__dict__ ["bodies" ]
247+
242248 @property
243249 def id (self ) -> str :
244250 """ID of the component."""
@@ -259,7 +265,7 @@ def components(self) -> list["Component"]:
259265 """List of ``Component`` objects inside of the component."""
260266 return self ._components
261267
262- @property
268+ @cached_property
263269 def bodies (self ) -> list [Body ]:
264270 """List of ``Body`` objects inside of the component."""
265271 bodies = []
@@ -509,6 +515,7 @@ def extrude_sketch(
509515 response = self ._bodies_stub .CreateExtrudedBody (request )
510516 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = False )
511517 self ._master_component .part .bodies .append (tb )
518+ self ._clear_cached_bodies ()
512519 return Body (response .id , response .name , self , tb )
513520
514521 @min_backend_version (24 , 2 , 0 )
@@ -558,6 +565,7 @@ def sweep_sketch(
558565 response = self ._bodies_stub .CreateSweepingProfile (request )
559566 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = False )
560567 self ._master_component .part .bodies .append (tb )
568+ self ._clear_cached_bodies ()
561569 return Body (response .id , response .name , self , tb )
562570
563571 @min_backend_version (24 , 2 , 0 )
@@ -605,6 +613,7 @@ def sweep_chain(
605613 response = self ._bodies_stub .CreateSweepingChain (request )
606614 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = True )
607615 self ._master_component .part .bodies .append (tb )
616+ self ._clear_cached_bodies ()
608617 return Body (response .id , response .name , self , tb )
609618
610619 @min_backend_version (24 , 2 , 0 )
@@ -720,6 +729,7 @@ def extrude_face(
720729
721730 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = False )
722731 self ._master_component .part .bodies .append (tb )
732+ self ._clear_cached_bodies ()
723733 return Body (response .id , response .name , self , tb )
724734
725735 @protect_grpc
@@ -753,6 +763,7 @@ def create_sphere(self, name: str, center: Point3D, radius: Distance) -> Body:
753763 response = self ._bodies_stub .CreateSphereBody (request )
754764 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = False )
755765 self ._master_component .part .bodies .append (tb )
766+ self ._clear_cached_bodies ()
756767 return Body (response .id , response .name , self , tb )
757768
758769 @protect_grpc
@@ -819,6 +830,7 @@ def create_body_from_loft_profile(
819830 response = self ._bodies_stub .CreateExtrudedBodyFromLoftProfiles (request )
820831 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = False )
821832 self ._master_component .part .bodies .append (tb )
833+ self ._clear_cached_bodies ()
822834 return Body (response .id , response .name , self , tb )
823835
824836 @protect_grpc
@@ -856,6 +868,7 @@ def create_surface(self, name: str, sketch: Sketch) -> Body:
856868
857869 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = True )
858870 self ._master_component .part .bodies .append (tb )
871+ self ._clear_cached_bodies ()
859872 return Body (response .id , response .name , self , tb )
860873
861874 @protect_grpc
@@ -896,6 +909,7 @@ def create_surface_from_face(self, name: str, face: Face) -> Body:
896909
897910 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = True )
898911 self ._master_component .part .bodies .append (tb )
912+ self ._clear_cached_bodies ()
899913 return Body (response .id , response .name , self , tb )
900914
901915 @protect_grpc
@@ -936,6 +950,7 @@ def create_body_from_surface(self, name: str, trimmed_surface: TrimmedSurface) -
936950
937951 tb = MasterBody (response .master_id , name , self ._grpc_client , is_surface = response .is_surface )
938952 self ._master_component .part .bodies .append (tb )
953+ self ._clear_cached_bodies ()
939954 return Body (response .id , response .name , self , tb )
940955
941956 @check_input_types
@@ -1136,6 +1151,7 @@ def delete_body(self, body: Body | str) -> None:
11361151 # on the client side
11371152 body_requested ._is_alive = False
11381153 self ._grpc_client .log .debug (f"Body { body_requested .id } has been deleted." )
1154+ self ._clear_cached_bodies ()
11391155 else :
11401156 self ._grpc_client .log .warning (
11411157 f"Body { id } is not found in this component (or subcomponents)."
0 commit comments