Skip to content

Commit

Permalink
Fixes importing binary stl + test; cleanup;
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Panasiuk committed Dec 31, 2017
1 parent 8689dec commit d15c97a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,6 +16,7 @@ eggs
.svn
.coverage
.idea
.ropeproject/

.DS_Store*
ehthumbs.db
Expand Down
17 changes: 14 additions & 3 deletions pycam/Importers/STLImporter.py
Expand Up @@ -32,6 +32,10 @@
import pycam.Utils
log = pycam.Utils.log.get_logger()

# The amount of bytes in the header field
HEADER_SIZE = 80
# The amount of bytes in the count field
COUNT_SIZE = 4

vertices = 0
edges = 0
Expand Down Expand Up @@ -66,7 +70,11 @@ def get_facet_count_if_binary_format(source):
"""
# read data (without consuming it)
raw_header_data = source.peek(400)
facet_count = unpack("<I", raw_header_data[80:84])[0]

facet_count = unpack(
"<I", raw_header_data[HEADER_SIZE : HEADER_SIZE + COUNT_SIZE]
)[0]

try:
header_data = raw_header_data.decode("utf-8")
except UnicodeDecodeError:
Expand Down Expand Up @@ -115,6 +123,9 @@ def import_model(filename, use_kdtree=True, callback=None, **kwargs):
p2 = None
p3 = None

# Skip the header and count fields of binary stl file
f.seek(HEADER_SIZE + COUNT_SIZE)

if is_binary:
for i in range(1, facet_count + 1):
if callback and callback():
Expand Down Expand Up @@ -272,8 +283,8 @@ def import_model(filename, use_kdtree=True, callback=None, **kwargs):
if m:
continue

log.info("Imported STL model: %d vertices, %d edges, %d triangles",
vertices, edges, len(model.triangles()))
# TODO display unique vertices and edges count - currently not counted
log.info("Imported STL model: %d triangles", len(model.triangles()))
vertices = 0
edges = 0
kdtree = None
Expand Down
Binary file added pycam/Test/assets/cube_binary.stl
Binary file not shown.
51 changes: 51 additions & 0 deletions pycam/Test/test_stl_loader.py
@@ -0,0 +1,51 @@
"""
Copyright 2018 Ruslan Panasiuk <ruslan.panasiuk@gmail.com>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""

import os

import pycam.Test
from pycam.Importers.STLImporter import import_model

cwd = os.path.dirname(os.path.abspath(__file__))

ASSETS_DIR = 'assets'


def path_to_asset(asset_name: str) -> str:
"""
Returns abs path for given `asset_name`
:param asset_name: file name of the asset from 'Tests/assets'
:returns: str - abs path to asset
"""
return os.path.join(cwd, ASSETS_DIR, asset_name)


# Placeholder
class TestASCIILoader(pycam.Test.PycamTestCase):
pass


class TestBinaryLoader(pycam.Test.PycamTestCase):
"""
Checks ability to load binary .stl files correctly
"""

def test_load_binary_file(self):
model = import_model(path_to_asset('cube_binary.stl'))
self.assertEqual(len(model), 12)

0 comments on commit d15c97a

Please sign in to comment.