Skip to content

Commit e939d9d

Browse files
committed
Refactor elements module for v6.x
1 parent bb1fdb2 commit e939d9d

File tree

1 file changed

+16
-136
lines changed

1 file changed

+16
-136
lines changed

geomdl/elements.py

Lines changed: 16 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
"""
99

1010
import abc
11-
import copy
1211
from .six import add_metaclass
13-
from .base import export, GeomdlTypeSequence, GeomdlError
12+
from .base import export, GeomdlBase, GeomdlTypeSequence, GeomdlError
1413

1514

1615
@add_metaclass(abc.ABCMeta)
17-
class AbstractEntity(object):
16+
class AbstractEntity(GeomdlBase):
1817
""" Abstract base class for all geometric entities. """
18+
__slots__ = ('_data', '_iter_index')
19+
1920
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
2423
self._data = [] # data storage array
2524

25+
def __str__(self):
26+
return self.name + " " + str(self.id) + " " + str(self.data)
27+
2628
def __cmp__(self, other):
2729
return (self.id > other.id) - (self.id < other.id)
2830

@@ -67,137 +69,15 @@ def __getitem__(self, key):
6769
def __reversed__(self):
6870
return reversed(self._data)
6971

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-
19472

19573
@export
19674
class Vertex(AbstractEntity):
19775
""" 3-dimensional Vertex entity with spatial and parametric position. """
76+
__slots__ = '_uv'
77+
19878
def __init__(self, *args, **kwargs):
19979
super(Vertex, self).__init__(*args, **kwargs)
200-
self._name = "vertex"
80+
self._idt['name'] = "vertex" # object name
20181
self.data = [float(arg) for arg in args] if args else [0.0, 0.0, 0.0] # spatial coordinates
20282
self._uv = [0.0, 0.0] # parametric coordinates
20383
self._opt_data['inside'] = False # flag for trimming
@@ -385,7 +265,7 @@ class Triangle(AbstractEntity):
385265
"""
386266
def __init__(self, *args, **kwargs):
387267
super(Triangle, self).__init__(*args, **kwargs)
388-
self._name = "triangle"
268+
self._idt['name'] = "triangle" # object name
389269
self._opt_data['inside'] = False # flag for trimming
390270
if args:
391271
self.add_vertex(*args)
@@ -505,7 +385,7 @@ class Quad(AbstractEntity):
505385

506386
def __init__(self, *args, **kwargs):
507387
super(Quad, self).__init__(*args, **kwargs)
508-
self._name = "quad"
388+
self._idt['name'] = "quad" # object name
509389
if args:
510390
self.data = args
511391

@@ -555,7 +435,7 @@ class Face(AbstractEntity):
555435
""" Representation of Face entity which is composed of triangles or quads. """
556436
def __init__(self, *args, **kwargs):
557437
super(Face, self).__init__(*args, **kwargs)
558-
self._name = "face"
438+
self._idt['name'] = "face" # object name
559439
if args:
560440
self.add_triangle(*args)
561441

@@ -590,7 +470,7 @@ class Body(AbstractEntity):
590470
""" Representation of Body entity which is composed of faces. """
591471
def __init__(self, *args, **kwargs):
592472
super(Body, self).__init__(*args, **kwargs)
593-
self._name = "body"
473+
self._idt['name'] = "body" # object name
594474
if args:
595475
self.add_face(*args)
596476

0 commit comments

Comments
 (0)