@@ -30,7 +30,7 @@ class Assembly(Datastructure):
3030
3131 def __init__ (self , name = None , ** kwargs ):
3232 super (Assembly , self ).__init__ ()
33- self .attributes = {' name' : name or ' Assembly' }
33+ self .attributes = {" name" : name or " Assembly" }
3434 self .attributes .update (kwargs )
3535 self .graph = Graph ()
3636 self ._parts = {}
@@ -42,39 +42,43 @@ def __init__(self, name=None, **kwargs):
4242 @property
4343 def DATASCHEMA (self ):
4444 import schema
45- return schema .Schema ({
46- "attributes" : dict ,
47- "graph" : Graph ,
48- })
45+
46+ return schema .Schema (
47+ {
48+ "attributes" : dict ,
49+ "graph" : Graph ,
50+ }
51+ )
4952
5053 @property
5154 def JSONSCHEMANAME (self ):
52- return ' assembly'
55+ return " assembly"
5356
5457 @property
5558 def data (self ):
5659 data = {
57- ' attributes' : self .attributes ,
58- ' graph' : self .graph .data ,
60+ " attributes" : self .attributes ,
61+ " graph" : self .graph .data ,
5962 }
6063 return data
6164
6265 @data .setter
6366 def data (self , data ):
64- self .attributes .update (data ['attributes' ] or {})
65- self .graph .data = data ['graph' ]
67+ self .attributes .update (data ["attributes" ] or {})
68+ self .graph .data = data ["graph" ]
69+ self ._parts = {part .guid : part .key for part in self .parts ()}
6670
6771 # ==========================================================================
6872 # properties
6973 # ==========================================================================
7074
7175 @property
7276 def name (self ):
73- return self .attributes .get (' name' ) or self .__class__ .__name__
77+ return self .attributes .get (" name" ) or self .__class__ .__name__
7478
7579 @name .setter
7680 def name (self , value ):
77- self .attributes [' name' ] = value
81+ self .attributes [" name" ] = value
7882
7983 # ==========================================================================
8084 # customization
@@ -114,10 +118,10 @@ def add_part(self, part, key=None, **kwargs):
114118
115119 """
116120 if part .guid in self ._parts :
117- raise AssemblyError (' Part already added to the assembly' )
121+ raise AssemblyError (" Part already added to the assembly" )
118122 key = self .graph .add_node (key = key , part = part , ** kwargs )
119123 part .key = key
120- self ._parts [part .guid ] = part
124+ self ._parts [part .guid ] = part . key
121125 return key
122126
123127 def add_connection (self , a , b , ** kwargs ):
@@ -143,10 +147,11 @@ def add_connection(self, a, b, **kwargs):
143147 If `a` and/or `b` are not in the assembly.
144148
145149 """
150+ error_msg = "Both parts have to be added to the assembly before a connection can be created."
146151 if a .key is None or b .key is None :
147- raise AssemblyError ('Both parts have to be added to the assembly before a connection can be created.' )
152+ raise AssemblyError (error_msg )
148153 if not self .graph .has_node (a .key ) or not self .graph .has_node (b .key ):
149- raise AssemblyError ('Both parts have to be added to the assembly before a connection can be created.' )
154+ raise AssemblyError (error_msg )
150155 return self .graph .add_edge (a .key , b .key , ** kwargs )
151156
152157 def parts (self ):
@@ -159,7 +164,7 @@ def parts(self):
159164
160165 """
161166 for node in self .graph .nodes ():
162- yield self .graph .node_attribute (node , ' part' )
167+ yield self .graph .node_attribute (node , " part" )
163168
164169 def connections (self , data = False ):
165170 """Iterate over the connections between the parts.
@@ -194,4 +199,29 @@ def find(self, guid):
194199 or None if the part can't be found.
195200
196201 """
197- return self ._parts .get (guid )
202+ key = self ._parts .get (guid )
203+
204+ if key is None :
205+ return None
206+
207+ return self .graph .node_attribute (key , "part" )
208+
209+ def find_by_key (self , key ):
210+ """Find a part in the assembly by its key.
211+
212+ Parameters
213+ ----------
214+ key : int | str, optional
215+ The identifier of the part in the assembly.
216+
217+ Returns
218+ -------
219+ :class:`~compas.datastructures.Part` | None
220+ The identified part,
221+ or None if the part can't be found.
222+
223+ """
224+ if key not in self .graph .node :
225+ return None
226+
227+ return self .graph .node_attribute (key , "part" )
0 commit comments