|
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import abc |
11 | | -import copy |
12 | 11 | from .six import add_metaclass |
13 | | -from .base import export, GeomdlTypeSequence, GeomdlError |
| 12 | +from .base import export, GeomdlBase, GeomdlTypeSequence, GeomdlError |
14 | 13 |
|
15 | 14 |
|
16 | 15 | @add_metaclass(abc.ABCMeta) |
17 | | -class AbstractEntity(object): |
| 16 | +class AbstractEntity(GeomdlBase): |
18 | 17 | """ Abstract base class for all geometric entities. """ |
| 18 | + __slots__ = ('_data', '_iter_index') |
| 19 | + |
19 | 20 | def __init__(self, *args, **kwargs): |
20 | | - self._name = "entity" # object name |
21 | | - self._id = int(kwargs.get('id', 0)) # object ID |
22 | | - self._opt_data = dict() # custom data dict |
23 | | - self._cache = {} # cache dict |
| 21 | + super(AbstractEntity, self).__init__(*args, **kwargs) |
| 22 | + self._idt['name'] = "entity" # object name |
24 | 23 | self._data = [] # data storage array |
25 | 24 |
|
| 25 | + def __str__(self): |
| 26 | + return self.name + " " + str(self.id) + " " + str(self.data) |
| 27 | + |
26 | 28 | def __cmp__(self, other): |
27 | 29 | return (self.id > other.id) - (self.id < other.id) |
28 | 30 |
|
@@ -67,137 +69,15 @@ def __getitem__(self, key): |
67 | 69 | def __reversed__(self): |
68 | 70 | return reversed(self._data) |
69 | 71 |
|
70 | | - def __copy__(self): |
71 | | - cls = self.__class__ |
72 | | - result = cls.__new__(cls) |
73 | | - result.__dict__.update(self.__dict__) |
74 | | - return result |
75 | | - |
76 | | - def __deepcopy__(self, memo): |
77 | | - # Don't copy self reference |
78 | | - cls = self.__class__ |
79 | | - result = cls.__new__(cls) |
80 | | - memo[id(self)] = result |
81 | | - # Don't copy the cache |
82 | | - memo[id(self._cache)] = self._cache.__new__(dict) |
83 | | - # Copy all other attributes |
84 | | - for k, v in self.__dict__.items(): |
85 | | - setattr(result, k, copy.deepcopy(v, memo)) |
86 | | - return result |
87 | | - |
88 | | - def __str__(self): |
89 | | - return self.name + " " + str(self.id) + " " + str(self.data) |
90 | | - |
91 | | - __repr__ = __str__ |
92 | | - |
93 | | - @property |
94 | | - def id(self): |
95 | | - """ Object ID (as an integer). |
96 | | -
|
97 | | - Please refer to the `wiki <https://github.com/orbingol/NURBS-Python/wiki/Using-Python-Properties>`_ for details |
98 | | - on using this class member. |
99 | | -
|
100 | | - :getter: Gets the object ID |
101 | | - :setter: Sets the object ID |
102 | | - :type: int |
103 | | - """ |
104 | | - return self._id |
105 | | - |
106 | | - @id.setter |
107 | | - def id(self, value): |
108 | | - self._id = int(value) |
109 | | - |
110 | | - @id.deleter |
111 | | - def id(self): |
112 | | - self._id = 0 |
113 | | - |
114 | | - @property |
115 | | - def name(self): |
116 | | - """ Object name (as a string) |
117 | | -
|
118 | | - Please refer to the `wiki <https://github.com/orbingol/NURBS-Python/wiki/Using-Python-Properties>`_ for details |
119 | | - on using this class member. |
120 | | -
|
121 | | - :getter: Gets the object name |
122 | | - :setter: Sets the object name |
123 | | - :type: str |
124 | | - """ |
125 | | - return self._name |
126 | | - |
127 | | - @name.setter |
128 | | - def name(self, value): |
129 | | - self._name = str(value) |
130 | | - |
131 | | - @name.deleter |
132 | | - def name(self): |
133 | | - self._name = "" |
134 | | - |
135 | | - @property |
136 | | - def opt(self): |
137 | | - """ Dictionary for storing custom data in the current geometry object. |
138 | | -
|
139 | | - ``opt`` is a wrapper to a dict in *key => value* format, where *key* is string, *value* is any Python object. |
140 | | - You can use ``opt`` property to store custom data inside the geometry object. For instance: |
141 | | -
|
142 | | - .. code-block:: python |
143 | | -
|
144 | | - geom.opt = ["face_id", 4] # creates "face_id" key and sets its value to an integer |
145 | | - geom.opt = ["contents", "data values"] # creates "face_id" key and sets its value to a string |
146 | | - print(geom.opt) # will print: {'face_id': 4, 'contents': 'data values'} |
147 | | -
|
148 | | - del geom.opt # deletes the contents of the hash map |
149 | | - print(geom.opt) # will print: {} |
150 | | -
|
151 | | - geom.opt = ["body_id", 1] # creates "body_id" key and sets its value to 1 |
152 | | - geom.opt = ["body_id", 12] # changes the value of "body_id" to 12 |
153 | | - print(geom.opt) # will print: {'body_id': 12} |
154 | | -
|
155 | | - geom.opt = ["body_id", None] # deletes "body_id" |
156 | | - print(geom.opt) # will print: {} |
157 | | -
|
158 | | - :getter: Gets the dict |
159 | | - :setter: Adds key and value pair to the dict |
160 | | - :deleter: Deletes the contents of the dict |
161 | | - """ |
162 | | - return self._opt_data |
163 | | - |
164 | | - @opt.setter |
165 | | - def opt(self, key_value): |
166 | | - if not isinstance(key_value, GeomdlTypeSequence): |
167 | | - raise GeomdlError("opt input must be a list or a tuple") |
168 | | - if len(key_value) != 2: |
169 | | - raise GeomdlError("opt input must have a size of 2, corresponding to [0:key] => [1:value]") |
170 | | - if not isinstance(key_value[0], str): |
171 | | - raise GeomdlError("key must be string") |
172 | | - |
173 | | - if key_value[1] is None: |
174 | | - self._opt_data.pop(*key_value) |
175 | | - else: |
176 | | - self._opt_data[key_value[0]] = key_value[1] |
177 | | - |
178 | | - @opt.deleter |
179 | | - def opt(self): |
180 | | - self._opt_data = dict() |
181 | | - |
182 | | - def opt_get(self, value): |
183 | | - """ Safely query for the value from the :py:attr:`opt` property. |
184 | | -
|
185 | | - :param value: a key in the :py:attr:`opt` property |
186 | | - :type value: str |
187 | | - :return: the corresponding value, if the key exists. ``None``, otherwise. |
188 | | - """ |
189 | | - try: |
190 | | - return self._opt_data[value] |
191 | | - except KeyError: |
192 | | - return None |
193 | | - |
194 | 72 |
|
195 | 73 | @export |
196 | 74 | class Vertex(AbstractEntity): |
197 | 75 | """ 3-dimensional Vertex entity with spatial and parametric position. """ |
| 76 | + __slots__ = '_uv' |
| 77 | + |
198 | 78 | def __init__(self, *args, **kwargs): |
199 | 79 | super(Vertex, self).__init__(*args, **kwargs) |
200 | | - self._name = "vertex" |
| 80 | + self._idt['name'] = "vertex" # object name |
201 | 81 | self.data = [float(arg) for arg in args] if args else [0.0, 0.0, 0.0] # spatial coordinates |
202 | 82 | self._uv = [0.0, 0.0] # parametric coordinates |
203 | 83 | self._opt_data['inside'] = False # flag for trimming |
@@ -385,7 +265,7 @@ class Triangle(AbstractEntity): |
385 | 265 | """ |
386 | 266 | def __init__(self, *args, **kwargs): |
387 | 267 | super(Triangle, self).__init__(*args, **kwargs) |
388 | | - self._name = "triangle" |
| 268 | + self._idt['name'] = "triangle" # object name |
389 | 269 | self._opt_data['inside'] = False # flag for trimming |
390 | 270 | if args: |
391 | 271 | self.add_vertex(*args) |
@@ -505,7 +385,7 @@ class Quad(AbstractEntity): |
505 | 385 |
|
506 | 386 | def __init__(self, *args, **kwargs): |
507 | 387 | super(Quad, self).__init__(*args, **kwargs) |
508 | | - self._name = "quad" |
| 388 | + self._idt['name'] = "quad" # object name |
509 | 389 | if args: |
510 | 390 | self.data = args |
511 | 391 |
|
@@ -555,7 +435,7 @@ class Face(AbstractEntity): |
555 | 435 | """ Representation of Face entity which is composed of triangles or quads. """ |
556 | 436 | def __init__(self, *args, **kwargs): |
557 | 437 | super(Face, self).__init__(*args, **kwargs) |
558 | | - self._name = "face" |
| 438 | + self._idt['name'] = "face" # object name |
559 | 439 | if args: |
560 | 440 | self.add_triangle(*args) |
561 | 441 |
|
@@ -590,7 +470,7 @@ class Body(AbstractEntity): |
590 | 470 | """ Representation of Body entity which is composed of faces. """ |
591 | 471 | def __init__(self, *args, **kwargs): |
592 | 472 | super(Body, self).__init__(*args, **kwargs) |
593 | | - self._name = "body" |
| 473 | + self._idt['name'] = "body" # object name |
594 | 474 | if args: |
595 | 475 | self.add_face(*args) |
596 | 476 |
|
|
0 commit comments