From 09c0238c8b71485e5de3b76870d01bfe4ca2ce58 Mon Sep 17 00:00:00 2001 From: Franciszek Lajszczak Date: Sat, 19 Aug 2023 20:37:13 +0200 Subject: [PATCH] Workflow commit --- .gitignore | 3 ++- README.md | 4 +++ qsketchmetric/renderer.py | 55 ++++++++++++++++++++++++--------------- setup.py | 11 +++++--- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index b74ac7b..35519ee 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ _build */__pycache__/ __pycache__/* tests/__pycache__/* -qsketchmetric/__pycache__/* \ No newline at end of file +qsketchmetric/__pycache__/* +/qodana.yaml diff --git a/README.md b/README.md index e76e99d..1027db0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ [![CI](https://github.com/MadScrewdriver/qsketchmetric/actions/workflows/tests.yml/badge.svg)](https://github.com/MadScrewdriver/qsketchmetric/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/MadScrewdriver/qsketchmetric/graph/badge.svg?token=OBMRQRRHUQ)](https://codecov.io/gh/MadScrewdriver/qsketchmetric) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) +[![PyPI](https://img.shields.io/pypi/v/qsketchmetric)](https://pypi.org/project/qsketchmetric/) +[![Python 3](https://img.shields.io/badge/python-3.9_|_3.10_|_3.11-blue.svg)](https://www.python.org/downloads/release/python-3114/) +[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) # qsketchmetric Parametric 2D Python dxf renderer diff --git a/qsketchmetric/renderer.py b/qsketchmetric/renderer.py index 048b525..23f5ef5 100644 --- a/qsketchmetric/renderer.py +++ b/qsketchmetric/renderer.py @@ -3,11 +3,14 @@ from copy import deepcopy from pathlib import Path from random import choice +from typing import Optional, Dict import ezdxf from ezdxf import units, bbox +from ezdxf.entities import DXFGraphic +from ezdxf.layouts import Modelspace from ezdxf.math import Vec3 -from py_expression_eval import Parser +from py_expression_eval import Parser # type: ignore from ezdxf.document import Drawing @@ -16,8 +19,10 @@ class Renderer: Class for rendering parametric 2D dxf drawings """ - def __init__(self, input_parametric_path: Path, output_rendered_object: Drawing, extra_variables: dict = None, - offset_drawing: tuple = (0, 0)): + def __init__(self, input_parametric_path: Path, + output_rendered_object: Drawing, + extra_variables: Optional[dict[str, float]] = None, + offset_drawing: tuple[int, int] = (0, 0)): """ Initializes a new instance of the class. @@ -36,24 +41,24 @@ def __init__(self, input_parametric_path: Path, output_rendered_object: Drawing, if extra_variables is None: extra_variables = dict() - self.new_points = {} - self.input_parametric_path = input_parametric_path + self.new_points: Dict[Vec3, tuple[int, int]] = {} + self.input_parametric_path: Path = input_parametric_path - self.input_dxf = ezdxf.readfile(self.input_parametric_path) - self.input_msp = self.input_dxf.modelspace() + self.input_dxf: Drawing = ezdxf.readfile(self.input_parametric_path) + self.input_msp: Modelspace = self.input_dxf.modelspace() - self.output_dxf = output_rendered_object - self.output_msp = self.output_dxf.modelspace() + self.output_dxf: Drawing = output_rendered_object + self.output_msp: Modelspace = self.output_dxf.modelspace() - self.offset_drawing_x = offset_drawing[0] - self.offset_drawing_y = offset_drawing[1] + self.offset_drawing_x: float = offset_drawing[0] + self.offset_drawing_y: float = offset_drawing[1] - self.variables = {} | extra_variables + self.variables: Dict[str, float] = {} | extra_variables - self.graph = {} - self.visited_graph = {} - self.points = {} - self.new_entities = [] + self.graph: Dict[Vec3, list[tuple[str, Vec3, float, dict]]] = {} + self.visited_graph: Dict[Vec3, list[tuple[str, Vec3]]] = {} + self.points: Dict[str, Vec3] = {} + self.new_entities: list[DXFGraphic] = [] def render(self) -> dict: """ @@ -73,10 +78,15 @@ def render(self) -> dict: """ self.input_dxf.units = units.MM + extracted_texts: filter = filter(None, self.input_dxf.query( + "MTEXT")[0].text.split("----- custom -----")[-1].split("\P")) - self.variables |= { - v.split(":")[0].strip(): float(Parser().parse(v.split(":")[1].strip()).evaluate(self.variables)) for v in - filter(None, self.input_dxf.query("MTEXT")[0].text.split("----- custom -----")[-1].split("\P"))} + extracted_variables: Dict[str, float] = { + v.split(":")[0].strip(): float(Parser().parse(v.split(":")[1].strip()).evaluate(self.variables)) + for v in extracted_texts + } + + self.variables |= extracted_variables self._prepare_graph() @@ -98,7 +108,9 @@ def get_bounding_box(self) -> tuple[float, float]: Returns: A tuple containing the width and height of the bounding box. The width is calculated as the difference - between the x-coordinates of the top-right and bottom-left vertices of the bounding box. The height is calculated as the difference between the y-coordinates of the top-right and bottom-left vertices of the bounding box. + between the x-coordinates of the top-right and bottom-left vertices of the bounding box. The height is + calculated as the difference between the y-coordinates of the top-right and bottom-left vertices of the + bounding box. """ bounding_box = bbox.extents(self.output_msp, cache=bbox.Cache()) @@ -110,7 +122,8 @@ def _prepare_graph(self): """ Prepares the graph by iterating through the entities in the input_msp entity space. For each entity, it extracts relevant information from the xdata and creates a new_length value. - If the entity is a LINE, it calculates the start and end points, adds the line type to the output_dxf linetypes, + If the entity is a LINE, it calculates the start and end points, + adds the line type to the output_dxf linetypes, and updates the graph and visited_graph dictionaries accordingly. If the entity is a CIRCLE, it calculates the center point and updates the graph dictionary. If the entity is an ARC, it calculates the center point and updates the graph dictionary. diff --git a/setup.py b/setup.py index 18a6852..b3d97e3 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup, find_packages +from setuptools import setup, find_packages # type: ignore VERSION = '1.6.6' DESCRIPTION = 'Parametric 2D CAD' @@ -10,6 +10,7 @@ version=VERSION, description=DESCRIPTION, long_description=LONG_DESCRIPTION, + url="https://github.com/MadScrewdriver/qsketchmetric", author="Franciszek Ɓajszczak", author_email="franciszek@lajszczak.dev", license='MIT', @@ -18,10 +19,14 @@ keywords='CAD, QCAD, 2D, parametric, drawing, renderer, python renderer, python CAD, python 2d CAD, p' 'python 2d drawing, python parametric drawing, python parametric CAD, python QCAD, QCAD python, ' 'parametric QCAD python, parametric QCAD, QCAD parametric, QCAD python parametric, QCAD python 2d,', + python_requires=">=3.9", classifiers=[ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", - 'License :: OSI Approved :: MIT License', - "Programming Language :: Python :: 3", + "Topic :: Software Development :: Libraries", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.9", ] )