Skip to content

Commit

Permalink
Workflow commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MadScrewdriver committed Aug 19, 2023
1 parent 65ea56a commit 09c0238
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ _build
*/__pycache__/
__pycache__/*
tests/__pycache__/*
qsketchmetric/__pycache__/*
qsketchmetric/__pycache__/*
/qodana.yaml
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
55 changes: 34 additions & 21 deletions qsketchmetric/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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.
Expand All @@ -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:
"""
Expand All @@ -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()

Expand All @@ -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())
Expand All @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -10,6 +10,7 @@
version=VERSION,
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
url="https://github.com/MadScrewdriver/qsketchmetric",
author="Franciszek Łajszczak",
author_email="[email protected]",
license='MIT',
Expand All @@ -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",
]
)

0 comments on commit 09c0238

Please sign in to comment.