66from compas .geometry import Transformation
77from compas_model .datastructures import KDTree
88from compas_model .elements import Element
9+ from compas_model .elements import Group
910from compas_model .interactions import Modifier
1011from compas_model .materials import Material
1112
@@ -252,26 +253,26 @@ def has_material(self, material: Material) -> bool:
252253 def add_element (
253254 self ,
254255 element : Element ,
255- parent : Optional [ElementNode ] = None ,
256+ parent : Optional [Element ] = None ,
256257 material : Optional [Material ] = None ,
257- ) -> ElementNode :
258+ ) -> Element :
258259 """Add an element to the model.
259260
260261 Parameters
261262 ----------
262263 element : :class:`Element`
263264 The element to add.
264- parent : :class:`ElementNode `, optional
265- The parent group node of the element.
266- If ``None``, the element will be added directly under the root node .
265+ parent : :class:`Element `, optional
266+ The parent element of the element.
267+ If ``None``, the element will be added directly under the root element .
267268 material : :class:`Material`, optional
268269 A material to assign to the element.
269270 Note that the material should have already been added to the model before it can be assigned.
270271
271272 Returns
272273 -------
273- :class:`Elementnode `
274- The tree node containing the element in the hierarchy .
274+ :class:`Element `
275+ The element added to the model .
275276
276277 Raises
277278 ------
@@ -289,23 +290,20 @@ def add_element(
289290
290291 element .graphnode = self .graph .add_node (element = element )
291292
292- if not parent :
293- parent = self ._tree .root
294-
295- if isinstance ( parent , Element ):
296- if parent . treenode is None :
293+ if parent is None :
294+ parent_node = self ._tree .root
295+ elif isinstance ( parent , Element ):
296+ parent_node = parent . treenode
297+ if parent_node is None :
297298 raise ValueError ("The parent element is not part of this model." )
298-
299- parent = parent .treenode
300-
301- if not isinstance (parent , ElementNode ):
302- raise ValueError ("Parent should be an Element or ElementNode of the current model." )
299+ else :
300+ raise ValueError ("Parent should be an Element or None" )
303301
304302 if material and not self .has_material (material ):
305303 raise ValueError ("The material is not part of the model: {}" .format (material ))
306304
307305 element_node = ElementNode (element = element )
308- parent .add (element_node )
306+ parent_node .add (element_node )
309307
310308 if material :
311309 self .assign_material (material = material , element = element )
@@ -317,28 +315,45 @@ def add_element(
317315 # and perhaps all resets should be collected in a reset decorator
318316 self ._bvh = None
319317
320- return element_node
318+ return element
319+
320+ def add_group (self , name : str = None ) -> Group :
321+ """Add a group to the model.
322+
323+ Parameters
324+ ----------
325+ name : str
326+ The name of the group.
327+
328+ Returns
329+ -------
330+ :class:`Group`
331+ The group added to the model.
332+
333+ """
334+ group = Group (name = name )
335+ return self .add_element (group )
321336
322- def add_elements (self , elements : list [Element ], parent : Optional [ElementNode ] = None ) -> list [ElementNode ]:
337+ def add_elements (self , elements : list [Element ], parent : Optional [Element ] = None ) -> list [Element ]:
323338 """Add multiple elements to the model.
324339
325340 Parameters
326341 ----------
327342 elements : list[:class:`Element`]
328343 The model elements.
329- parent : :class:`GroupNode `, optional
330- The parent group node of the elements.
331- If ``None``, the elements will be added directly under the root node .
344+ parent : :class:`Group `, optional
345+ The parent group of the elements.
346+ If ``None``, the elements will be added directly under the root element .
332347
333348 Returns
334349 -------
335- list[:class:`ElementNode `]
350+ list[:class:`Element `]
336351
337352 """
338- nodes = []
353+ added_elements = []
339354 for element in elements :
340- nodes .append (self .add_element (element , parent = parent ))
341- return nodes
355+ added_elements .append (self .add_element (element , parent = parent ))
356+ return added_elements
342357
343358 def add_material (self , material : Material ) -> None :
344359 """Add a material to the model.
0 commit comments