Skip to content

Commit

Permalink
More rendering params (openvmp#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandhakumar19dev committed Jan 23, 2024
1 parent b887910 commit a1fd146
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions examples/produce_assembly_assy/partcad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ assemblies:
render:
svg:
prefix: ./
stl: ./
markdown: README.md
4 changes: 4 additions & 0 deletions examples/produce_part_cadquery_primitive/partcad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ parts:
path: cylinder.py
desc: This is a cylinder from examples
render:
extra_opts:
focus: 25
svg:
prefix: ./
png:
prefix: ./
markdown: README.md
8 changes: 8 additions & 0 deletions examples/produce_part_step/partcad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ parts:
type: step
desc: M8x30-screw
render:
extra_opts:
marginTop: 100
marginLeft: 100
strokeWidth: 0.5
height: 300
width: 300
svg:
prefix: ./
png:
prefix: ./
markdown: README.md
45 changes: 25 additions & 20 deletions partcad/src/partcad/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ def _finalize_real(self, show_object):
if not show_object is None:
self.show(show_object)

def render_svg_somewhere(self, project=None, filepath=None):
def render_svg_somewhere(self, project=None, filepath=None, extra_opts={}):
"""Renders an SVG file somewhere and ignore the project settings"""
if filepath is None:
filepath = tempfile.mktemp(".svg")

cq_obj = self.get_cadquery()
cq_obj = cq_obj.rotate((0, 0, 0), (1, -1, 0.75), 180)
cq.exporters.export(cq_obj, filepath, opt=DEFAULT_RENDER_SVG_OPTS)

cq.exporters.export(cq_obj, filepath, opt={**DEFAULT_RENDER_SVG_OPTS, **extra_opts})
print(f"{filepath=:}")
self.svg_path = filepath

def _get_svg_path(self, project):
def _get_svg_path(self, project, extra_opts={}):
if self.svg_path is None:
self.render_svg_somewhere(project, None)
self.render_svg_somewhere(project, None, extra_opts=extra_opts)
return self.svg_path

def render_getopts(
self,
kind,
extension,
project=None,
filepath=None,
filepath=None, include_extra_opts=False
):
if not project is None:
render_opts = project.config_obj["render"]
Expand Down Expand Up @@ -126,15 +126,20 @@ def render_getopts(

logging.info("Rendering: %s" % filepath)

extra_opts = render_opts.get("extra_opts", {})

if include_extra_opts:
return opts, filepath, extra_opts

return opts, filepath

def render_svg(
self,
project=None,
filepath=None,
):
_, filepath = self.render_getopts("svg", ".svg", project, filepath)
self.render_svg_somewhere(project, filepath)
_, filepath, extra_opts = self.render_getopts("svg", ".svg", project, filepath, include_extra_opts=True)
self.render_svg_somewhere(project, filepath, extra_opts=extra_opts)

def render_png(
self,
Expand All @@ -147,21 +152,21 @@ def render_png(
logging.error("Export to PNG is not supported")
return

png_opts, filepath = self.render_getopts("png", ".png", project, filepath)
png_opts, filepath, extra_opts = self.render_getopts("png", ".png", project, filepath, include_extra_opts=True)

if width is None:
if "width" in png_opts and not png_opts["width"] is None:
width = png_opts["width"]
else:
width = DEFAULT_RENDER_WIDTH
if height is None:
if "height" in png_opts and not png_opts["height"] is None:
height = png_opts["height"]
else:
height = DEFAULT_RENDER_HEIGHT
# if width is None:
# if "width" in png_opts and not png_opts["width"] is None:
# width = png_opts["width"]
# else:
# width = DEFAULT_RENDER_WIDTH
# if height is None:
# if "height" in png_opts and not png_opts["height"] is None:
# height = png_opts["height"]
# else:
# height = DEFAULT_RENDER_HEIGHT

# Render the vector image
svg_path = self._get_svg_path(project)
svg_path = self._get_svg_path(project, extra_opts=extra_opts)

plugins.export_png.export(project, svg_path, width, height, filepath)

Expand Down
2 changes: 2 additions & 0 deletions partcad/tests/unit/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_render_svg_part_1():
assert cube is not None
try:
cube.render_svg(project=prj)
# cube.render_png(project=prj)
except Exception as e:
assert False, "Valid render request caused an exception: %s" % e

Expand All @@ -45,6 +46,7 @@ def test_render_svg_assy_2():
assert assy is not None
try:
assy.render_svg(project=prj)
# assy.render_stl(project=prj)
except Exception as e:
assert False, "Valid render request caused an exception: %s" % e

Expand Down

0 comments on commit a1fd146

Please sign in to comment.