Skip to content

Commit

Permalink
Added support for defining parts using STL format (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
openvmp authored Jan 7, 2024
1 parent daa3161 commit 8789587
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.step binary
*.png binary
*.stl binary
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,20 @@ parts:
Store the model in "bold.step"
</td>
<td><img src="https://github.com/openvmp/partcad/blob/main/examples/part_step/bolt.png?raw=true"></td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/STL_(file_format)">STL</a></td>
<td>
<code># partcad.yaml
parts:
cube:
type: stl</code>
<br/>
Store the model in "cube.stl"
</td>
<td><img src="https://github.com/openvmp/partcad/blob/main/examples/part_stl/cube.png?raw=true"></td>
</tr>
<tr>
<td><a href="https://github.com/CadQuery/cadquery">CadQuery</a></td>
<td>
Expand All @@ -179,7 +192,7 @@ parts:
Place the CadQuery script in "cube.py"
</td>
<td><img src="https://github.com/openvmp/partcad/blob/main/examples/part_cadquery_primitive/cube.png?raw=true"></td>
<tr>
</tr>
https://github.com/openvmp/partcad/blob/main/examples/assembly_logo/logo.png?raw=true
<tr>
<td><a href="https://github.com/gumyr/build123d">build123d</a></td>
Expand All @@ -193,7 +206,7 @@ parts:
Place the build123d script in "cube.py"
</td>
<td><img src="https://github.com/openvmp/partcad/blob/main/examples/part_cadquery_primitive/cube.png?raw=true"></td>
<tr>
</tr>
</table>
Other methods to define parts are coming in soon (e.g. SCAD).
Expand Down
3 changes: 1 addition & 2 deletions examples/part_step/partcad.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
desc: PartCAD example project to demonstrate parts implemented using STEP files
parts:
bolt:
desc: A sample bolt
desc: M8x30-screw
type: step
description: M8x30-screw
render:
png:
prefix: ./
Expand Down
Binary file added examples/part_stl/cube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/part_stl/cube.stl
Binary file not shown.
11 changes: 11 additions & 0 deletions examples/part_stl/partcad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
desc: PartCAD example project to demonstrate parts implemented using STL files
parts:
cube:
desc: A cube defined in STL
type: stl
render:
png:
prefix: ./
width: 128
height: 128
markdown: README.md
23 changes: 23 additions & 0 deletions src/partcad/part_factory_stl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# OpenVMP, 2024
#
# Author: Roman Kuzmenko
# Created: 2024-01-06
#
# Licensed under Apache License, Version 2.0.
#

import build123d as b3d
from . import part_factory as pf
from . import part as p


class PartFactoryStl(pf.PartFactory):
def __init__(self, ctx, project, part_config):
super().__init__(ctx, project, part_config, extension=".stl")
# Complement the config object here if necessary
self._create(part_config)

def instantiate(self, part):
shape = b3d.Mesher().read(part.path)[0].wrapped
part.set_shape(shape)
4 changes: 4 additions & 0 deletions src/partcad/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from . import project_config
from . import part_factory_step as pfs
from . import part_factory_stl as pfstl
from . import part_factory_cadquery as pfc
from . import part_factory_build123d as pfb
from . import assembly_factory_assy as afa
Expand Down Expand Up @@ -88,6 +89,9 @@ def init_parts(self):
elif part_config["type"] == "step":
logging.info("Initializing STEP part: %s..." % part_name)
pfs.PartFactoryStep(self.ctx, self, part_config)
elif part_config["type"] == "stl":
logging.info("Initializing STL part: %s..." % part_name)
pfstl.PartFactoryStl(self.ctx, self, part_config)
else:
logging.error(
"Invalid part type encountered: %s: %s" % (part_name, part_config)
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/test_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@
}


def test_part_get_1():
"""Load part from a project by the part name"""
def test_part_get_step_1():
"""Load a STEP part from a project by the part name"""
ctx = pc.Context("examples/part_step")
repo1 = ctx.get_project("this")
bolt = repo1.get_part("bolt")
assert bolt is not None
assert bolt.get_wrapped() is not None


def test_part_get_2():
"""Load part from the context by the project and part names"""
def test_part_get_step_2():
"""Load a STEP part from the context by the project and part names"""
ctx = pc.Context("examples/part_step")
bolt = ctx.get_part("bolt", "this")
assert bolt is not None
assert bolt.get_wrapped() is not None


def test_part_get_stl():
"""Load a STL part"""
ctx = pc.Context("examples/part_stl")
part = ctx.get_part("cube", "this")
assert part is not None
assert part.get_wrapped() is not None


def test_part_get_3():
"""Instantiate a project by a local import config and load a part"""
ctx = pc.Context() # Empty config
Expand Down

0 comments on commit 8789587

Please sign in to comment.