|
| 1 | +from __future__ import print_function |
| 2 | +from __future__ import absolute_import |
| 3 | +from __future__ import division |
| 4 | + |
| 5 | +from ..datastructure import Datastructure |
| 6 | +from ..graph import Graph |
| 7 | +from .exceptions import AssemblyError |
| 8 | + |
| 9 | + |
| 10 | +class Assembly(Datastructure): |
| 11 | + """A data structure for managing the connections between different parts of an assembly. |
| 12 | +
|
| 13 | + Attributes |
| 14 | + ---------- |
| 15 | + attributes: dict |
| 16 | + General attributes of the assembly that will be included in the data dict. |
| 17 | + graph: :class:`compas.datastructures.Graph` |
| 18 | + The graph that is used under the hood to store the parts and their connections. |
| 19 | + """ |
| 20 | + |
| 21 | + @property |
| 22 | + def DATASCHEMA(self): |
| 23 | + import schema |
| 24 | + return schema.Schema({ |
| 25 | + "attributes": dict, |
| 26 | + "graph": Graph, |
| 27 | + }) |
| 28 | + |
| 29 | + @property |
| 30 | + def JSONSCHEMANAME(self): |
| 31 | + return 'assembly' |
| 32 | + |
| 33 | + def __init__(self, name=None, **kwargs): |
| 34 | + super(Assembly, self).__init__() |
| 35 | + self.attributes = {'name': name or 'Assembly'} |
| 36 | + self.attributes.update(kwargs) |
| 37 | + self.graph = Graph() |
| 38 | + self._parts = {} |
| 39 | + |
| 40 | + def __str__(self): |
| 41 | + tpl = "<Assembly with {} parts and {} connections>" |
| 42 | + return tpl.format(self.graph.number_of_nodes(), self.graph.number_of_edges()) |
| 43 | + |
| 44 | + @property |
| 45 | + def name(self): |
| 46 | + """str : The name of the assembly.""" |
| 47 | + return self.attributes.get('name') or self.__class__.__name__ |
| 48 | + |
| 49 | + @name.setter |
| 50 | + def name(self, value): |
| 51 | + self.attributes['name'] = value |
| 52 | + |
| 53 | + @property |
| 54 | + def data(self): |
| 55 | + """dict : A data dict representing the assembly data structure for serialization. |
| 56 | + """ |
| 57 | + data = { |
| 58 | + 'attributes': self.attributes, |
| 59 | + 'graph': self.graph.data, |
| 60 | + } |
| 61 | + return data |
| 62 | + |
| 63 | + @data.setter |
| 64 | + def data(self, data): |
| 65 | + self.attributes.update(data['attributes'] or {}) |
| 66 | + self.graph.data = data['graph'] |
| 67 | + |
| 68 | + def add_part(self, part, key=None, **kwargs): |
| 69 | + """Add a part to the assembly. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + part: :class:`compas.datastructures.Part` |
| 74 | + The part to add. |
| 75 | + key: int or str, optional |
| 76 | + The identifier of the part in the assembly. |
| 77 | + Note that the key is unique only in the context of the current assembly. |
| 78 | + Nested assemblies may have the same ``key`` value for one of their parts. |
| 79 | + Default is ``None`` in which case the key will be automatically assigned integer value. |
| 80 | + kwargs: dict |
| 81 | + Additional named parameters collected in a dict. |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + int or str |
| 86 | + The identifier of the part in the current assembly graph. |
| 87 | +
|
| 88 | + """ |
| 89 | + if part.guid in self._parts: |
| 90 | + raise AssemblyError('Part already added to the assembly') |
| 91 | + key = self.graph.add_node(key=key, part=part, **kwargs) |
| 92 | + part.key = key |
| 93 | + self._parts[part.guid] = part |
| 94 | + return key |
| 95 | + |
| 96 | + def add_connection(self, a, b, **kwargs): |
| 97 | + """Add a connection between two parts. |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + a: :class:`compas.datastructures.Part` |
| 102 | + The "from" part. |
| 103 | + b: :class:`compas.datastructures.Part` |
| 104 | + The "to" part. |
| 105 | + kwargs: dict |
| 106 | + Additional named parameters collected in a dict. |
| 107 | +
|
| 108 | + Returns |
| 109 | + ------- |
| 110 | + tuple of str or int |
| 111 | + The tuple of node identifiers that identifies the connection. |
| 112 | +
|
| 113 | + Raises |
| 114 | + ------ |
| 115 | + :class:`AssemblyError` |
| 116 | + If ``a`` and/or ``b`` are not in the assembly. |
| 117 | + """ |
| 118 | + if a.key is None or b.key is None: |
| 119 | + raise AssemblyError('Both parts have to be added to the assembly before a connection can be created.') |
| 120 | + if not self.graph.has_node(a.key) or not self.graph.has_node(b.key): |
| 121 | + raise AssemblyError('Both parts have to be added to the assembly before a connection can be created.') |
| 122 | + return self.graph.add_edge(a.key, b.key, **kwargs) |
| 123 | + |
| 124 | + def parts(self): |
| 125 | + """The parts of the assembly. |
| 126 | +
|
| 127 | + Yields |
| 128 | + ------ |
| 129 | + :class:`compas.datastructures.Part` |
| 130 | + The individual parts of the assembly. |
| 131 | + """ |
| 132 | + for node in self.graph.nodes(): |
| 133 | + yield self.graph.node_attribute(node, 'part') |
| 134 | + |
| 135 | + def connections(self, data=False): |
| 136 | + """Iterate over the connections between the parts. |
| 137 | +
|
| 138 | + Parameters |
| 139 | + ---------- |
| 140 | + data : bool, optional |
| 141 | + If ``True``, yield both the identifier and the attributes of each connection. |
| 142 | +
|
| 143 | + Yields |
| 144 | + ------ |
| 145 | + tuple |
| 146 | + The next connection identifier (u, v), if ``data`` is ``False``. |
| 147 | + Otherwise, the next connector identifier and its attributes as a ((u, v), attr) tuple. |
| 148 | + """ |
| 149 | + return self.graph.edges(data) |
| 150 | + |
| 151 | + def find(self, guid): |
| 152 | + """Find a part in the assembly by its GUID. |
| 153 | +
|
| 154 | + Parameters |
| 155 | + ---------- |
| 156 | + guid: str |
| 157 | + A globally unique identifier. |
| 158 | + This identifier is automatically assigned when parts are created. |
| 159 | +
|
| 160 | + Returns |
| 161 | + ------- |
| 162 | + :class:`compas.datastructures.Part` or None |
| 163 | + The identified part, if any. |
| 164 | +
|
| 165 | + """ |
| 166 | + return self._parts.get(guid) |
0 commit comments