예제 #1
0
def dbsample(document: Document):
    """
    Show statistics on the current geometries in JSON format.
    """
    global debug_data

    data: Dict[str, Any] = {}
    if document.is_empty():
        data["count"] = 0
    else:
        data["count"] = sum(len(lc) for lc in document.layers.values())
        data["layer_count"] = len(document.layers)
        data["length"] = document.length()
        data["pen_up_length"] = document.pen_up_length()
        data["bounds"] = document.bounds()
        data["layers"] = {
            layer_id: [as_vector(line).tolist() for line in layer]
            for layer_id, layer in document.layers.items()
        }

    debug_data.append(data)
    return document
예제 #2
0
def write(
    document: vp.Document,
    cmd_string: Optional[str],
    output,
    file_format: str,
    page_size: str,
    landscape: bool,
    center: bool,
    layer_label: str,
    pen_up: bool,
    color_mode: str,
    device: Optional[str],
    velocity: Optional[int],
    quiet: bool,
    single_path: bool,
):
    """Write command."""

    if document.is_empty():
        logging.warning("no geometry to save, no file created")
        return document

    if file_format is None:
        # infer format
        _, ext = os.path.splitext(output.name)
        file_format = ext.lstrip(".").lower()

    if file_format == "svg":
        page_size_px = None
        if page_size:
            page_size_px = vp.convert_page_size(page_size)
            if landscape:
                page_size_px = page_size_px[::-1]

        vp.write_svg(
            output=output,
            document=document,
            page_size=page_size_px,
            center=center,
            source_string=cmd_string if cmd_string is not None else "",
            layer_label_format=layer_label,
            show_pen_up=pen_up,
            color_mode=color_mode,
            single_path=single_path,
        )
    elif file_format == "hpgl":
        if not page_size:
            config = vp.config_manager.get_plotter_config(device)
            if config is not None:
                paper_config = config.paper_config_from_size(
                    document.page_size)
            else:
                paper_config = None

            if paper_config and document.page_size is not None:
                page_size = paper_config.name
                landscape = document.page_size[0] > document.page_size[1]
            else:
                logging.error(
                    "write: the plotter page size could not be inferred from the current page "
                    "size (use the `--page-size SIZE` option)")
                return document

        vp.write_hpgl(
            output=output,
            document=document,
            landscape=landscape,
            center=center,
            device=device,
            page_size=page_size,
            velocity=velocity,
            quiet=quiet,
        )
    else:
        logging.warning(
            f"write: format could not be inferred or format unknown '{file_format}', "
            "no file created (use the `--format` option)")

    return document