def _draw(self): # Prepare border border_params = self.DEFAULT_PARAMS.copy() border_params.update(self.attribs) border_params[ 'fill'] = '#fff' # For border stroke background should be white border = shapes.Rect((self.x, self.y), (self.width, self.height), **border_params) res = [border] # Prepare background stroke_width = border_params.get('stroke-width') value, unit = parse_measure_units(stroke_width) bg_params = self.DEFAULT_PARAMS.copy() bg_params.update(self.attribs) del bg_params['stroke-width'] del bg_params['stroke'] # Hatching and filling if hasattr(self, "hatch") and self.hatch: bg_params['style'] = "fill: url(#{})".format(self._hatching_id) res.append(self.hatch) if hasattr(self, "filling"): bg_params['fill'] = self.filling else: if 'fill' not in bg_params: bg_params['fill'] = "#fff" background = shapes.Rect( (self.x + float(value) / 2, self.y + float(value) / 2), (self.width - value, self.height - value), **bg_params) res.append(background) return res
def add_hatching(self, angle=45, distance=3, width=1, color="#000"): """ Add hatching to the walls. angle - angle of hatches in deg distance - distance between hatches width - stroke-width **Replaces all previously added hatchings or fillings.** """ if hasattr(self, "filling"): del self.filling angle = math.radians(angle) style = "stroke: {color}; stroke-width: {width}".format(color=color, width=width) pattern_width = distance / math.sin(angle) pattern_height = pattern_width * math.tan(angle) self.hatch = pattern.Pattern((0, 0), (pattern_width, pattern_height), id=self._hatching_id, patternUnits="userSpaceOnUse") self.hatch.add( shapes.Rect((0, 0), (pattern_width, pattern_height), fill="#fff")) self.hatch.add( shapes.Line((0, 0), (pattern_width, pattern_height), style=style)) self.hatch.add( shapes.Line((-1, (pattern_height - 1)), (1, (pattern_height + 1)), style=style)) self.hatch.add( shapes.Line(((pattern_width - 1), -1), ((pattern_width + 1), 1), style=style)) return self.hatch
def generate_circles_svg(xlist, ylist, rlist, width=792, height=600, colors=['white']): # colorhexes=["ff"]): if len(colors) < len(xlist): colors = colors * len(xlist) assert len(xlist) == len(ylist) == len(rlist) == len(colors) dwg = svgwrite.Drawing('temp.svg', profile='tiny', size=("%dpx" % width, "%dpx" % height)) # , style="background-color:black") dwg.add( shapes.Rect( insert=('-50%', '-50%'), size=('200%', '200%'), fill='black', )) for x, y, r, fill in enumerate(xlist, ylist, rlist, colors): dwg.add( shapes.Circle((x, y), r, fill=fill, stroke_width=0, stroke="red", shape_rendering="crispEdges")) return dwg.tostring()
def _draw(self): attribs = {"stroke": "#000", "stroke-width": "2", "fill": "#fff"} attribs.update(self.attribs) # vertical (x coordinates equal) if self.wall_start_point[0] == self.wall_end_point[0]: width = self.wall_width height = self.width # horizontal else: width = self.width height = self.wall_width return shapes.Rect((self.start_point[0], self.start_point[1]), (width, height), **attribs)
def _draw(self): res = [] rect_params = self.attribs.copy() if hasattr(self, "hatch") and self.hatch: rect_params['style'] = "fill: url(#{})".format(self._hatching_id) res.append(self.hatch) if hasattr(self, "filling"): rect_params['fill'] = self.filling else: if 'fill' not in self.attribs: rect_params['fill'] = "#fff" rect = shapes.Rect(self.corner, self.size, **rect_params) res.append(rect) return res
def add_background(svg): p = ET.ElementTree(ET.fromstring(svg)) root = p.getroot() root.insert( 0, ET.fromstring( shapes.Rect( insert=('-50%', '-50%'), size=('200%', '200%'), fill='black', ).tostring())) # root.set('style', "background-color:black") #this works in browser, but not for all renderers, eg cairo # root.set('viewport-fill', 'black') #this works in browser, but not for all renderers, eg cairo return ET.tostring(root)
def create_svg_document_with_light(elements, size, viewbox=None, background_color="white", background_opacity=1.0): """Create the full SVG document, with a lighting filter. Resources: - https://www.w3.org/TR/SVG11/filters.html#LightSourceDefinitions - https://svgwrite.readthedocs.io/en/master/classes/filters.html - http://www.svgbasics.com/filters2.html - https://css-tricks.com/look-svg-light-source-filters/ :param viewbox: (minx, miny, width, height) """ # TODO work in progress # TODO have a look at how threejs is converted to SVG: # https://github.com/mrdoob/three.js/blob/master/examples/jsm/renderers/SVGRenderer.js dwg = Drawing("ase.svg", profile="full", size=size) light_filter = dwg.defs.add(Filter(size=("100%", "100%"))) diffuse_lighting = light_filter.feDiffuseLighting(size=size, surfaceScale=10, diffuseConstant=1, kernelUnitLength=1, color="white") diffuse_lighting.fePointLight(source=(size[0], 0, 1000)) light_filter.feComposite(operator="arithmetic", k1=1) root = Group(id="root", filter=light_filter.get_funciri()) dwg.add(root) # if Color(background_color).web != "white": # apparently the best way, see: https://stackoverflow.com/a/11293812/5033292 root.add( shapes.Rect(size=size, fill=background_color, fill_opacity=background_opacity)) for element in elements: root.add(element) if viewbox: dwg.viewbox(*viewbox) return dwg
def generate_circle_svg(x=60, y=60, r=30, width=792, height=600, fill="white"): dwg = svgwrite.Drawing('temp.svg', profile='tiny', size=("%dpx" % width, "%dpx" % height)) # , style="background-color:black") dwg.add( shapes.Rect( insert=('-50%', '-50%'), size=('200%', '200%'), fill='black', )) dwg.add( shapes.Circle((x, y), r, fill=fill, stroke_width=0, stroke="red", shape_rendering="crispEdges")) return dwg.tostring()
def _draw(self): res = [] # Borders res.append(self._get_borders()) # Title table title_insert_point = (self.width - 10 - 185, self.height - 10 - 55) res.append( shapes.Rect(title_insert_point, (185, 55), **self.LINE_ATTRIBS)) res.append(self._get_table_line((0, 5), (65, 5))) res.append(self._get_table_line((0, 10), (65, 10))) res.append(self._get_table_line((0, 15), (185, 15))) res.append(self._get_table_line((0, 20), (65, 20))) res.append(self._get_table_line((0, 25), (65, 25))) res.append(self._get_table_line((0, 30), (65, 30))) res.append(self._get_table_line((0, 35), (65, 35))) res.append(self._get_table_line((0, 40), (185, 40))) res.append(self._get_table_line((0, 45), (65, 45))) res.append(self._get_table_line((0, 50), (65, 50))) res.append(self._get_table_line((7, 0), (7, 25))) res.append(self._get_table_line((17, 0), (17, 55))) res.append(self._get_table_line((40, 0), (40, 55))) res.append(self._get_table_line((55, 0), (55, 55))) res.append(self._get_table_line((65, 0), (65, 55))) res.append(self._get_table_line((135, 15), (135, 55))) res.append(self._get_table_line((135, 20), (185, 20))) res.append(self._get_table_line((135, 35), (185, 35))) res.append(self._get_table_line((140, 20), (140, 35))) res.append(self._get_table_line((145, 20), (145, 35))) res.append(self._get_table_line((150, 15), (150, 35))) res.append(self._get_table_line((167, 15), (167, 35))) res.append(self._get_table_line((155, 35), (155, 40))) # Text res.append( text.Text( self.title, (title_insert_point[0] + 100, title_insert_point[1] + 30), **self.DEFAULT_LABEL_ATTRIBS)) return res
def _draw(self): rect_params = self.DEFAULT_PARAMS.copy() rect_params.update(self.attribs) res = [] # Hatching and filling if hasattr(self, "hatch") and self.hatch: rect_params['style'] = "fill: url(#{})".format(self._hatching_id) res.append(self.hatch) if hasattr(self, "filling"): rect_params['fill'] = self.filling else: if 'fill' not in rect_params: rect_params['fill'] = "#fff" # Create outer and inner rects del rect_params['stroke-width'] del rect_params['stroke'] top_rect = shapes.Rect(self.corner, (self.size[0], self.wall_width), **rect_params) left_rect = shapes.Rect(self.corner, (self.wall_width, self.size[1]), **rect_params) right_rect = shapes.Rect( (self.corner[0] + self.size[0] - self.wall_width, self.corner[1]), (self.wall_width, self.size[1]), **rect_params) bottom_rect = shapes.Rect( (self.corner[0], self.corner[1] + self.size[1] - self.wall_width), (self.size[0], self.wall_width), **rect_params) inner_params = self.DEFAULT_PARAMS.copy() inner_params.update(self.attribs) inner_params['fill-opacity'] = "0" rect = shapes.Rect(self.corner, self.size, **inner_params) inner_rect = shapes.Rect(self.inner_corner, self.inner_size, **inner_params) res.extend((top_rect, left_rect, right_rect, bottom_rect, rect, inner_rect)) # Apertures if self.apertures: for aperture in self.apertures: res.append(aperture._draw()) # Bulkheads borders = [] backgrounds = [] if self.bulkheads: for bulkhead in self.bulkheads: border, *background = bulkhead._draw() borders.append(border) backgrounds.extend(background) res.extend(borders) res.extend(backgrounds) return res
def create_svg_document(elements, size, viewbox=None, background_color="white", background_opacity=1.0): """Create the full SVG document. :param viewbox: (minx, miny, width, height) """ dwg = Drawing("ase.svg", profile="tiny", size=size) root = Group(id="root") dwg.add(root) # if Color(background_color).web != "white": # apparently the best way, see: https://stackoverflow.com/a/11293812/5033292 root.add( shapes.Rect(size=size, fill=background_color, fill_opacity=background_opacity)) for element in elements: root.add(element) if viewbox: dwg.viewbox(*viewbox) return dwg