Skip to content

Commit

Permalink
make layer mandatory for path entities
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Jan 4, 2020
1 parent 82f1f25 commit d628b90
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 31 deletions.
16 changes: 10 additions & 6 deletions trimesh/path/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def __init__(self,
height=None,
vector=None,
normal=None,
align=None):
align=None,
layer=None):
"""
An entity for text labels.
Expand All @@ -275,12 +276,14 @@ def __init__(self,
"""
# where is text placed
self.origin = origin

# what direction is the text pointing
self.vector = vector
# what is the normal of the text plane
self.normal = normal

# how high is the text entity
self.height = height
# what layer is the entity on
self.layer = layer

# None or (2,) str
if align is None:
Expand Down Expand Up @@ -663,9 +666,10 @@ def discrete(self, vertices, scale=1.0, count=None):
discrete : (m, 2) or (m, 3) float
Curve as line segments
"""
discrete = discretize_bezier(vertices[self.points],
count=count,
scale=scale)
discrete = discretize_bezier(
vertices[self.points],
count=count,
scale=scale)
return self._orient(discrete)


Expand Down
23 changes: 12 additions & 11 deletions trimesh/path/exchange/dxf.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def convert_text(e):
raw = raw.decode('utf-8', errors='ignore')

# remove trailing whitespace
raw = util.str(raw).strip()
raw = str(raw).strip()
# without any spaces and in upper case
cleaned = raw.replace(' ', '').strip().upper()

Expand Down Expand Up @@ -478,15 +478,19 @@ def convert_text(e):

def export_dxf(path, layers=None):
"""
Export a 2D path object to a DXF file
Export a 2D path object to a DXF file.
Parameters
----------
path: trimesh.path.path.Path2D
path : trimesh.path.path.Path2D
Input geometry to export
layers : None, set or iterable
If passed only export the layers specified
Returns
----------
export: str, path formatted as a DXF file
export : str
Path formatted as a DXF file
"""

def format_points(points,
Expand Down Expand Up @@ -528,7 +532,7 @@ def format_points(points,
if as_2D:
group = group[:, :2]
three = three[:, :2]

# join into result string
packed = '\n'.join('{:d}\n{:.12f}'.format(g, v)
for g, v in zip(group.reshape(-1),
three.reshape(-1)))
Expand All @@ -552,13 +556,10 @@ def entity_info(entity):
# TODO : convert RGBA entity.color to index
subs = {'COLOR': 255, # default is ByLayer
'LAYER': 0,
'NAME': util.str(id(entity))[:16]}

'NAME': str(id(entity))[:16]}
if hasattr(entity, 'layer'):
# make sure layer name is forced into ASCII
subs['LAYER'] = util.str(entity.layer).encode(
'ascii', errors='ignore').decode('ascii')

subs['LAYER'] = util.to_ascii(entity.layer)
return subs

def convert_line(line, vertices):
Expand Down Expand Up @@ -740,7 +741,7 @@ def convert_generic(entity, vertices):
# run additional self- checks
if tol.strict:
# check that every line pair is (group code, value)
lines = str.splitlines(util.str(blob))
lines = str.splitlines(str(blob))
# should be even number of lines
assert (len(lines) % 2) == 0
# group codes should all be convertible to int and positive
Expand Down
24 changes: 15 additions & 9 deletions trimesh/path/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ def __repr__(self):

def process(self):
"""
Apply basic cleaning functions to the Path object, in- place.
Apply basic cleaning functions to the Path object in- place.
"""
log.debug('Processing drawing')
with self._cache:
for func in self._process_functions():
func()
Expand Down Expand Up @@ -178,6 +177,14 @@ def vertices(self, values):

@property
def entities(self):
"""
The actual entities making up the path.
Returns
-----------
entities : (n,) trimesh.path.entities.Entity
Entities such as Line, Arc, or BSpline curves
"""
return self._entities

@entities.setter
Expand All @@ -190,17 +197,16 @@ def entities(self, values):
@property
def layers(self):
"""
If entities have a layer defined, return it.
Get a list of the layer for every entity.
Returns
---------
layers: (len(entities), ) list of str
layers : (len(entities), ) any
Whatever is stored in each `entity.layer`
"""
layer = ['NONE'] * len(self.entities)
for i, e in enumerate(self.entities):
if hasattr(e, 'layer'):
layer[i] = str(e.layer)
return layer
# layer is a required property for entities
layers = [e.layer for e in self.entities]
return layers

def crc(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion trimesh/resources/dxf.json.template

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions trimesh/resources/helpers/dxf_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def read_files(path):
"""
template = {}
for file_name in os.listdir(path):
# skip emacs buffers
if '~' in file_name:
continue
with open(os.path.join(path, file_name), 'r') as f:
template[file_name] = replace_whitespace(
f.read(), insert=True)
Expand Down
29 changes: 26 additions & 3 deletions trimesh/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
if PY3:
# for type checking
basestring = str
# for converting
str = str
# Python 3
from io import BytesIO, StringIO
else:
Expand All @@ -48,7 +46,7 @@
StringIO.__enter__ = lambda a: a
StringIO.__exit__ = lambda a, b, c, d: a.close()
BytesIO = StringIO
str = unicode

try:
from collections.abc import Mapping
except ImportError:
Expand Down Expand Up @@ -2189,3 +2187,28 @@ def decode_text(text, initial='utf-8'):
# try to decode again, unwrapped in try
text = text.decode(detect['encoding'])
return text


def to_ascii(text):
"""
Force a string or other to ASCII text ignoring errors.
Parameters
-----------
text : any
Input to be converted to ASCII string
Returns
-----------
ascii : str
Input as an ASCII string
"""
if hasattr(text, 'encode'):
# case for existing strings
return text.encode(
'ascii', errors='ignore').decode('ascii')
elif hasattr(text, 'decode'):
# case for bytes
return text.decode('ascii', errors='ignore')
# otherwise just wrap as a string
return str(text)
2 changes: 1 addition & 1 deletion trimesh/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.5.11'
__version__ = '3.5.12'

0 comments on commit d628b90

Please sign in to comment.