Skip to content

Commit

Permalink
Added support to define parts using 3MF (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
openvmp authored Jan 7, 2024
1 parent c689a8e commit 30f4728
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ Store the model in "cube.stl"
<td><img src="https://github.com/openvmp/partcad/blob/main/examples/part_stl/cube.png?raw=true"></td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/3D_Manufacturing_Format">3MF</a></td>
<td>
<code># partcad.yaml
parts:
cube:
type: 3mf</code>
<br/>
Store the model in "cube.3mf"
</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>
<code># partcad.yaml
Expand All @@ -193,7 +206,6 @@ 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>
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>
<td>
Expand Down
Binary file added examples/part_3mf/cube.3mf
Binary file not shown.
Binary file added examples/part_3mf/cube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/part_3mf/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 3MF files
parts:
cube:
type: 3mf
desc: A cube defined in 3MF
render:
png:
prefix: ./
width: 128
height: 128
markdown: README.md
2 changes: 1 addition & 1 deletion examples/part_step/partcad.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
desc: PartCAD example project to demonstrate parts implemented using STEP files
parts:
bolt:
desc: M8x30-screw
type: step
desc: M8x30-screw
render:
png:
prefix: ./
Expand Down
2 changes: 1 addition & 1 deletion examples/part_stl/partcad.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
desc: PartCAD example project to demonstrate parts implemented using STL files
parts:
cube:
desc: A cube defined in STL
type: stl
desc: A cube defined in STL
render:
png:
prefix: ./
Expand Down
23 changes: 23 additions & 0 deletions src/partcad/part_factory_3mf.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 PartFactory3mf(pf.PartFactory):
def __init__(self, ctx, project, part_config):
super().__init__(ctx, project, part_config, extension=".3mf")
# 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 @@ -12,6 +12,7 @@
from . import project_config
from . import part_factory_step as pfs
from . import part_factory_stl as pfstl
from . import part_factory_3mf as pf3
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 @@ -92,6 +93,9 @@ def init_parts(self):
elif part_config["type"] == "stl":
logging.info("Initializing STL part: %s..." % part_name)
pfstl.PartFactoryStl(self.ctx, self, part_config)
elif part_config["type"] == "3mf":
logging.info("Initializing 3mf part: %s..." % part_name)
pf3.PartFactory3mf(self.ctx, self, part_config)
else:
logging.error(
"Invalid part type encountered: %s: %s" % (part_name, part_config)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def test_part_get_stl():
assert part.get_wrapped() is not None


def test_part_get_3mf():
"""Load a 3MF part"""
ctx = pc.Context("examples/part_3mf")
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 30f4728

Please sign in to comment.