Skip to content

Commit

Permalink
Integrated the separate PNG export plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwright committed Sep 18, 2024
1 parent 0b878a9 commit 1ecdf7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
35 changes: 33 additions & 2 deletions nimble_build_system/cad/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
Rendering and documentation generation is also supported for the shelves.
"""

# pylint: disable=unused-import

import os
import posixpath
import warnings

import yaml
from cadorchestrator.components import AssembledComponent, GeneratedMechanicalComponent
import cadquery as cq
import cadquery_png_plugin.plugin # This activates the PNG plugin for CadQuery
import cadscript
from cq_warehouse.fastener import ButtonHeadScrew
from cq_annotate.views import explode_assembly
Expand Down Expand Up @@ -110,6 +114,7 @@ class Shelf():
_hole_locations = None # List of hole locations for the device
_fasteners = [] # List of screw positions for the device
_unit_width = 6 # 6 or 10 inch rack
_render_options = None

# Hole location parameters
_screw_dist_x = None
Expand Down Expand Up @@ -299,6 +304,22 @@ def hole_locations(self, value):
self._hole_locations = value


@property
def render_options(self):
"""
Return the options for rendering the shelf.
"""
return self._render_options


@render_options.setter
def render_options(self, value):
"""
Set the options for rendering the shelf.
"""
self._render_options = value


def generate_device_model(self):
"""
Generates the device model only.
Expand Down Expand Up @@ -412,7 +433,12 @@ def generate_assembly_model(self, explode=False):
return self._shelf_assembly_model


def get_render(self, model, camera_pos, annotate=False, image_format="png", image_path=None):
def get_render(self,
model,
camera_position=None,
annotate=False,
image_format="png",
file_path=None):
"""
Generates a render of the assembly.
Expand All @@ -434,9 +460,10 @@ def get_render(self, model, camera_pos, annotate=False, image_format="png", imag

# Check to see if we are dealing with a single part or an assembly
if isinstance(model, cq.Assembly):
print("We have an assembly")
model.exportPNG(options=self.render_options, file_path=file_path)
else:
print("We have a part")
# TODO - Implement PNG rendering of a single part

return NotImplemented

Expand Down Expand Up @@ -764,6 +791,10 @@ def __init__(self,
axis="-Z",
length=6)
]
self.render_options = {
"view": "back-top-left",
"zoom": 1.25,
}

super().__init__(device,
assembly_key,
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
python_requires='>=3.10',
install_requires=[
'numpy~=1.26',
'cadquery>=2',
# 'cadquery>=2',
'cadquery @ git+https://github.com/CadQuery/cadquery.git',
'cadquery-png-plugin @ git+https://github.com/jmwright/cadquery-png-plugin.git',
'cadscript>=0.5.2',
'exsource-tools',
'cq-cli @ git+https://github.com/CadQuery/cq-cli.git',
Expand Down
13 changes: 7 additions & 6 deletions tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def test_assembly_png_rendering():
# Test the generated CAD assembly
shelf_assy = rpi_shelf.generate_assembly_model()

rpi_shelf.get_render(shelf_assy, (0, 0, 0))
# Set up a temporary path to export the image to
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, "test_model.png")

# temp_dir = tempfile.gettempdir()

# Export the model to a PNG image
# cq.exporters.export(shelf_assy, os.path.join(temp_dir, "test_model.png"))
# Do a sample render of the shelf assembly
rpi_shelf.get_render(shelf_assy,
file_path=temp_path)

assert True
assert os.path.isfile(temp_path)


def test_annotated_assembly_png_rendering():
Expand Down

0 comments on commit 1ecdf7e

Please sign in to comment.