Import an SVG sketch with SVGShape

The SVGShape component in the foundation library allows you to integrate SVG graphics into your CAD projects seamlessly. This guide provides an overview of its functionalities, supported SVG features, and best practices for creating compatible SVG files.

Overview

SVGShape is designed to convert SVG paths and text into 3D models that can be used for laser cutting, 3D printing, or other manufacturing processes. It parses the SVG data, extracts geometric information, and creates corresponding sketch geometry. Here is the signature of the SVGShape class: SVGShape(sketch: Sketch, svg: str, xshift: float = 0, yshift: float = 0, angle:float=0., scale: float = 1) Note that the angle is in radians.

Supported SVG Features

1. Path Elements

  • Description: SVG <path> elements define complex shapes using a series of commands (e.g., M for move, L for line, C for cubic Bezier curves).
  • Usage: Ideal for intricate designs, logos, and custom shapes.
  • Example:
Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
# Example SVG Path
from cadbuildr.foundation import *
svg = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path d="M10 10 H 90 V 90 H 10 L 10 10" fill="none" stroke="black"/>
</svg>
"""
def get_extruded_svg():
  # initialize the component
  cube = Part()
  # Create a Sketch from XY Plane
  s = Sketch(cube.xy())
  square = SVGShape(s, svg)
  e = Extrusion(square, 20)
  cube.add_operation(e)
  return cube
show(get_extruded_svg())

The string M10 10 H 90 V 90 H 10 L 10 10 is an SVG path definition. It describes a series of drawing commands that create a shape. Let’s break it down step by step:

  1. M10 10: “Move” to the point (10, 10). This is the starting point.
  2. H 90: Draw a horizontal line to x-coordinate 90 (y remains 10).
  3. V 90: Draw a vertical line to y-coordinate 90 (x remains 90).
  4. H 10: Draw a horizontal line back to x-coordinate 10 (y remains 90).
  5. L 10 10: Draw a line to the point (10, 10), which is where we started.

In simpler terms, this path draws a square with its top-left corner at (10, 10) and its bottom-right corner at (90, 90). The square is drawn in a clockwise direction.

The resulting shape would look like this:

(10,10) +-----------------+ (90,10)
        |                 |
        |                 |
        |                 |
        |                 |
        |                 |
(10,90) +-----------------+ (90,90)

However the viewbox is set to (0, 0) to (100, 100), and by default the SVGShape will bring the center of the viewbox to the origin of the sketch. So the square will be centered at the origin and have sides of 80 mm.

2. Text Elements

  • Description: SVG <text> elements allow you to include textual content with specified fonts and sizes.
  • Usage: Perfect for adding labels, names, or any textual information to your designs.
  • Example:
Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
# Example SVG Text
from cadbuildr.foundation import *
svg = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 50">
<text x="0" y="40" font-family="Roboto" font-size="40">
  CADbuildr
</text>
</svg>
"""
def get_extruded_svg():
  # initialize the component
  cube = Part()
  # Create a Sketch from XY Plane
  s = Sketch(cube.xy())
  square = SVGShape(s, svg)
  e = Extrusion(square, 20)
  cube.add_operation(e)
  return cube
show(get_extruded_svg())

3. Other shapes : Circle, Ellipse, Polygon, Rect

SVGShape supports various other SVG shape elements, allowing you to create complex designs with ease. Here’s an example demonstrating Circle, Ellipse, Line, Polyline, and Polygon shapes:

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
# Example SVG Shapes
from cadbuildr.foundation import *
svg_rect = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<rect x="-40" y="-40" width="80" height="80" fill="red" stroke="black" stroke-width="2"/>
</svg>
"""
svg_circle = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 100">
<circle cx="50" cy="50" r="40" />
</svg>
"""
svg_ellipse = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 100">
<ellipse cx="150" cy="50" rx="40" ry="25" />
</svg>
"""
svg_polygon = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 100">
<polygon points="420,20 480,20 460,80 440,50" />
</svg>
"""
def get_extruded_svg():
  # initialize the component
  part = Part()
  # Create a Sketch from XY Plane
  s = Sketch(part.xy())
  square = SVGShape(s, svg_rect, scale=0.2)
  circle = SVGShape(s, svg_circle, xshift=20, scale=0.2)
  ellipse = SVGShape(s, svg_ellipse, xshift=-20, scale=0.2)
  polygon = SVGShape(s, svg_polygon, xshift=40, scale=0.2)
  extrusions = [Extrusion(shape, 10) for shape in [circle, ellipse, polygon, square]]
  for e in extrusions:
      part.add_operation(e)
  return part
show(get_extruded_svg())
Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
# %%
# Mountain Logo
from cadbuildr.foundation import *
import math

svg = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">
    <path fill="#333"
        d="M213.26 203.23l.01-.05.02.02zm-1.83-17.74a5.8 5.8 0 0 0-.3-.29c1.83-2.72 3.66-5.45 5.5-8.16.27-.39.6-.98.95-1.03 3.8-.4 5.43-3.28 7.22-6.61 1.6 2.34 3.12 4.25 4.28 6.36 1.18 2.14 2.85 3.32 5.17 3.82 1.08.23 2.16.47 2.8 1.61 5.16 9.16 12.26 16.17 23.05 19.57-2.79.7-13.23-3-15.8-5.3-.7 2.13-1.38 4.2-2.06 6.26-.11-.02-.22-.04-.33-.06-.37-2.58-.71-5.16-1.1-7.74-.14-.88.1-2.11-1.34-2.14-1.21-.03-2.56-.57-3.43.93-.51.87-1.16 1.65-1.12 3.14-2.43-3.31-.59-6.42-.77-9.72-3.27-.61-6.28-3.09-10.25-1.46.65-.93 1.3-1.87 1.95-2.8 2.31-3.27 2.31-3.27-.23-6.87-2.84 1.98-6.67 2.82-7.43 7.07-.23 1.29-1.5 1.5-2.34 1.88-.91-.17.05-1.25-1.07-1.22-1.33.52-1.97 2.11-3.37 2.75zm3.87-2.61c.69-.25 1.1-.52.52-1.28-.23.38-.82.44-.52 1.28zm-90.59 22.5c-.09-.11-.18-.2-.28-.3 3.2-3.25 6.41-6.49 9.6-9.74 1.14-1.17 2.71-.91 4.09-1.1 1.47-.21 2.49-.8 3.43-1.9 1.76-2.05 3.63-4.02 5.52-6.1 2.87 2.28 5.08 5.08 8.2 7.22 1.6-1.9 3.26-3.71 4.74-5.66 1.65-2.18 3.65-3.79 6.33-4.49 1.32-.34 2.19-1.13 2.86-2.34 1.38-2.52 2.98-4.93 4.38-7.45 1.12-2 2.2-3.94 5.24-4.07-1.73-3.29.7-4.81 2.37-6.64 2.2-2.4 4.43-4.78 6.77-7.31 1.33 2.68 2.64 5.07 3.7 7.56 1.36 3.15 3.42 5.56 6.52 7.07 3.37 1.65 5.67 4.19 7.2 7.65 1.52 3.41 3.48 6.63 5.22 9.94.35.66.81 1.2 1.48 1.53 4.37 2.16 7.43 5.72 10.38 9.44 2.84 3.57 5.8 7.03 8.72 10.54-.2 0-.42 0-.63 0-.26 0-.53 0-.79-.01-1.4-1.12-2.81-2.22-4.06-3.48-2.31-2.32-4.79-3.72-8.35-2.25-.2.79-.46 1.73.06 2.89.27.6.32 1.32.18 2.13-.32-.04-.64-.07-.96-.11-1.4-3.18-1.13-6.88-3.67-9.59-.38 1.66.68 2.96.3 4.37-2.36-1.99-4.36-4.37-7.09-5.99-1.4-.83-2.67-1.85-4.29-2.3-5.82-1.59-10.7-5.06-15.86-7.96-1.17-.65-1.71-1.92-2.29-3.03-.92-1.77-2.22-2.53-4.23-2.35-1.23.11-2.49-.09-4.26-.17 3.08-3.24 5.68-6.17 8.5-8.86 1.53-1.47 3.85-.62 5.8-1.22.8-3.33-.59-5.81-3.12-8.02-1.74 2.56-4.66 4.16-5.61 7.32-.04.13-.2.28-.33.32-4.88 1.7-6.15 6.48-8.64 10.17-1.74 2.58-3.04 5.47-7.46 5.86 8.29 4.56 16.41 8.31 20.9 15.97-1.12-.22-2.24-.43-3.37-.63-3.55-2.6-7.43-4.7-11.61-6.17-.48-.17-1.1-.02-1.65.08-.79.14-1.55.44-2.34.56-2.9.4-5.63 1.07-7.75 2.77l-3.36-.08c-1.25 0-2.48.03-3.64.07-.47.01-.94.04-1.41.06 1.3-1.09 2.55-2.29 3.4-3.97-1.78-1.42-3.62-2.89-5.66-4.51-.81 2.39-.34 4.46.83 7.01-3.4-1.05-5.28.41-6.96 2.23-1.34.19-2.64.4-3.89.65-.09-.3-.17-.6-.26-.94-1.12.6-2.18 1.17-3.24 1.74-.56.15-1.12.3-1.66.46-.1-.68-.17-1.31-.29-2.19-2.68 1.84-5.2 3.57-7.72 5.31zm70.93-25.39c5.28 6.74 11.56 11.96 19.82 14.87.1-.92-.28-1.13-.62-1.3-4.15-2.04-6.7-5.56-9.02-9.36-1.98-3.24-6.15-4.88-10.18-4.21zm63.69 25.31c-1.18 1.12-2.43 2.14-3.75 3.04-2.65 1.8-5.53 3.18-8.54 4.14-6.02 1.93-12.43 2.3-18.73 1.79-3.16-.25-6.3-.74-9.4-1.39-1.55-.33-3.08-.72-4.58-1.15l-4.65-1.45-2.38-.75c-.8-.25-1.6-.45-2.4-.68-1.61-.44-3.21-.9-4.82-1.32-1.62-.41-3.24-.79-4.86-1.18-1.62-.39-3.26-.7-4.89-1.06-13.08-2.64-26.56-3.9-39.97-2.89-6.69.54-13.36 1.63-19.8 3.54-3.2.97-6.37 2.1-9.39 3.52-1.52.72-3 1.48-4.44 2.32-1.42.86-2.83 1.76-4.09 2.85 1.46-.78 2.97-1.43 4.5-2 1.53-.58 3.08-1.09 4.64-1.54 3.13-.91 6.3-1.62 9.49-2.16 6.39-1.09 12.84-1.57 19.3-1.62 12.92-.07 25.8 1.55 38.5 4.1 6.36 1.24 12.68 2.81 18.94 4.51l4.83 1.31c1.68.42 3.36.74 5.05 1.01 3.37.56 6.78.84 10.2.89 3.41.02 6.85-.21 10.22-.83 3.38-.61 6.71-1.57 9.84-3.01 3.11-1.43 6.03-3.3 8.49-5.63 1.24-1.16 2.37-2.41 3.37-3.74 1-1.35 1.9-2.75 2.64-4.22-1.04 1.27-2.13 2.49-3.33 3.6z" />

</svg>
"""


class MountainLogo(Part):
    def __init__(self):
        # background extrusion
        s = Sketch(self.xy())
        p1 = Point(s, 0, 0)
        circle = Circle(p1, 150)
        e = Extrusion(circle, 2)
        self.add_operation(e)

        self.extrude_logo()

    def extrude_logo(self):
        # Create a Sketch from XY Plane
        s = Sketch(self.xy())
        square = SVGShape(s, svg, scale=1.5, xshift=12, yshift=-10, angle=math.pi / 8)
        e = Extrusion(square, 2, cut=True)
        self.add_operation(e)


show(MountainLogo())

# %%

Limitations

While SVGShape is a powerful tool, it’s essential to be aware of its current limitations to ensure smooth integration:

1. Simplistic SVGs Only

  • Explanation: Complex SVGs with multiple nested groups, intricate filters, or advanced styling may not be fully supported.
  • Recommendation: Use simple SVGs with clear path and text elements. Avoid unnecessary layers or complex transformations.

2. Font Limitations

  • Supported Fonts: Only a subset of fonts are supported on the Playground environment. Ensure that the font used in your SVG is available.

Example: Creating a Custom Sign

Refer to the Creating a Customizable Laser-Cut Office Sign tutorial for a comprehensive example of using SVGShape with both path and text elements to create a 3D sign model.

Conclusion

SVGShape is a versatile component for integrating SVG graphics into Replicad projects. By adhering to the supported features and best practices outlined in this guide, you can create detailed and customizable 3D models suitable for various manufacturing processes.