cad-scale-dxf 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 VisualCraft Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cad-scale-dxf
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Zero-dependency Python library for 2D CAD and DXF geometry: polyline length, shoelace polygon area, DPI-to-mm scale calibration, Ramer-Douglas-Peucker simplification, and ASCII DXF export.
|
|
5
|
+
Project-URL: Homepage, https://imgtocad.com/
|
|
6
|
+
Project-URL: Documentation, https://imgtocad.com/sketch-to-cad
|
|
7
|
+
Project-URL: Repository, https://github.com/visualcraft-lab/dxf-geometry-py
|
|
8
|
+
Author: VisualCraft Lab
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: cad,dxf,geometry,polyline,vector
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# cad-scale-dxf
|
|
21
|
+
|
|
22
|
+
Zero-dependency Python library for 2D CAD and DXF geometry calculations: polyline perimeter, shoelace polygon area, bounding boxes, raster DPI-to-millimeter calibration, 1:N architectural drawing scale conversion, Ramer-Douglas-Peucker polyline simplification, and ASCII DXF `LWPOLYLINE` serialization.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install cad-scale-dxf
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from dxf_geometry import polyline_length, polygon_area, pixels_to_mm, simplify_rdp
|
|
34
|
+
|
|
35
|
+
wall = [(0, 0), (5000, 0), (5000, 4000), (0, 4000)]
|
|
36
|
+
print(polyline_length(wall, closed=True)) # 18000.0
|
|
37
|
+
print(polygon_area(wall)) # 20000000.0
|
|
38
|
+
print(pixels_to_mm(300, 300)) # 25.4
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Online Reference
|
|
42
|
+
|
|
43
|
+
- Interactive image-to-CAD converter: [ImgToCAD](https://imgtocad.com/)
|
|
44
|
+
- Sketch vectorization reference: [Sketch to CAD](https://imgtocad.com/sketch-to-cad)
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# cad-scale-dxf
|
|
2
|
+
|
|
3
|
+
Zero-dependency Python library for 2D CAD and DXF geometry calculations: polyline perimeter, shoelace polygon area, bounding boxes, raster DPI-to-millimeter calibration, 1:N architectural drawing scale conversion, Ramer-Douglas-Peucker polyline simplification, and ASCII DXF `LWPOLYLINE` serialization.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install cad-scale-dxf
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from dxf_geometry import polyline_length, polygon_area, pixels_to_mm, simplify_rdp
|
|
15
|
+
|
|
16
|
+
wall = [(0, 0), (5000, 0), (5000, 4000), (0, 4000)]
|
|
17
|
+
print(polyline_length(wall, closed=True)) # 18000.0
|
|
18
|
+
print(polygon_area(wall)) # 20000000.0
|
|
19
|
+
print(pixels_to_mm(300, 300)) # 25.4
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Online Reference
|
|
23
|
+
|
|
24
|
+
- Interactive image-to-CAD converter: [ImgToCAD](https://imgtocad.com/)
|
|
25
|
+
- Sketch vectorization reference: [Sketch to CAD](https://imgtocad.com/sketch-to-cad)
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cad-scale-dxf"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Zero-dependency Python library for 2D CAD and DXF geometry: polyline length, shoelace polygon area, DPI-to-mm scale calibration, Ramer-Douglas-Peucker simplification, and ASCII DXF export."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "VisualCraft Lab" }]
|
|
13
|
+
keywords = ["cad", "dxf", "geometry", "polyline", "vector"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://imgtocad.com/"
|
|
24
|
+
Documentation = "https://imgtocad.com/sketch-to-cad"
|
|
25
|
+
Repository = "https://github.com/visualcraft-lab/dxf-geometry-py"
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.wheel]
|
|
28
|
+
packages = ["src/dxf_geometry"]
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Zero-dependency 2D CAD and DXF geometry utilities.
|
|
2
|
+
|
|
3
|
+
Online reference: https://imgtocad.com/
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import math
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import List, Optional, Sequence, Tuple
|
|
10
|
+
|
|
11
|
+
Point = Tuple[float, float]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class BBox:
|
|
16
|
+
min_x: float
|
|
17
|
+
min_y: float
|
|
18
|
+
max_x: float
|
|
19
|
+
max_y: float
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def width(self) -> float:
|
|
23
|
+
return self.max_x - self.min_x
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def height(self) -> float:
|
|
27
|
+
return self.max_y - self.min_y
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def distance(a: Point, b: Point) -> float:
|
|
31
|
+
"""Return Euclidean distance between two 2D points."""
|
|
32
|
+
return math.hypot(b[0] - a[0], b[1] - a[1])
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def polyline_length(pts: Sequence[Point], closed: bool = False) -> float:
|
|
36
|
+
"""Return total length of an open or closed 2D polyline."""
|
|
37
|
+
if len(pts) < 2:
|
|
38
|
+
return 0.0
|
|
39
|
+
total = sum(distance(pts[i - 1], pts[i]) for i in range(1, len(pts)))
|
|
40
|
+
if closed and len(pts) > 2:
|
|
41
|
+
total += distance(pts[-1], pts[0])
|
|
42
|
+
return total
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def polygon_area(pts: Sequence[Point]) -> float:
|
|
46
|
+
"""Return unsigned area of a simple polygon using the shoelace formula."""
|
|
47
|
+
n = len(pts)
|
|
48
|
+
if n < 3:
|
|
49
|
+
return 0.0
|
|
50
|
+
acc = sum(
|
|
51
|
+
pts[i][0] * pts[(i + 1) % n][1] - pts[(i + 1) % n][0] * pts[i][1]
|
|
52
|
+
for i in range(n)
|
|
53
|
+
)
|
|
54
|
+
return abs(acc) * 0.5
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def bounding_box(pts: Sequence[Point]) -> Optional[BBox]:
|
|
58
|
+
"""Return axis-aligned bounding box for a point sequence, or None if empty."""
|
|
59
|
+
if not pts:
|
|
60
|
+
return None
|
|
61
|
+
xs = [p[0] for p in pts]
|
|
62
|
+
ys = [p[1] for p in pts]
|
|
63
|
+
return BBox(min(xs), min(ys), max(xs), max(ys))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def pixels_to_mm(pixels: float, dpi: float) -> float:
|
|
67
|
+
"""Convert a raster pixel span at given DPI to millimeters."""
|
|
68
|
+
return 0.0 if dpi <= 0 else (pixels / dpi) * 25.4
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def paper_to_model_scale(paper_length: float, scale_denominator: float) -> float:
|
|
72
|
+
"""Convert paper-space length to real-world model length using 1:N scale."""
|
|
73
|
+
return 0.0 if scale_denominator <= 0 else paper_length * scale_denominator
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _perpendicular_distance(p: Point, a: Point, b: Point) -> float:
|
|
77
|
+
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
78
|
+
if dx == 0.0 and dy == 0.0:
|
|
79
|
+
return distance(p, a)
|
|
80
|
+
num = abs(dy * p[0] - dx * p[1] + b[0] * a[1] - b[1] * a[0])
|
|
81
|
+
return num / math.hypot(dx, dy)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def simplify_rdp(pts: Sequence[Point], epsilon: float) -> List[Point]:
|
|
85
|
+
"""Simplify a 2D polyline using the Ramer-Douglas-Peucker algorithm."""
|
|
86
|
+
if len(pts) <= 2 or epsilon <= 0:
|
|
87
|
+
return list(pts)
|
|
88
|
+
max_dist = -1.0
|
|
89
|
+
index = 0
|
|
90
|
+
for i in range(1, len(pts) - 1):
|
|
91
|
+
d = _perpendicular_distance(pts[i], pts[0], pts[-1])
|
|
92
|
+
if d > max_dist:
|
|
93
|
+
max_dist = d
|
|
94
|
+
index = i
|
|
95
|
+
if max_dist > epsilon:
|
|
96
|
+
left = simplify_rdp(pts[: index + 1], epsilon)
|
|
97
|
+
right = simplify_rdp(pts[index:], epsilon)
|
|
98
|
+
return left[:-1] + right
|
|
99
|
+
return [pts[0], pts[-1]]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def format_dxf_polyline(pts: Sequence[Point], layer: str = "0", closed: bool = False) -> str:
|
|
103
|
+
"""Emit a minimal ASCII DXF LWPOLYLINE entity block."""
|
|
104
|
+
layer_name = layer or "0"
|
|
105
|
+
flag = 1 if closed else 0
|
|
106
|
+
lines = [
|
|
107
|
+
"0", "SECTION", "2", "ENTITIES",
|
|
108
|
+
"0", "LWPOLYLINE",
|
|
109
|
+
"8", layer_name,
|
|
110
|
+
"90", str(len(pts)),
|
|
111
|
+
"70", str(flag),
|
|
112
|
+
]
|
|
113
|
+
for x, y in pts:
|
|
114
|
+
lines.extend(["10", f"{x:.4f}", "20", f"{y:.4f}"])
|
|
115
|
+
lines.extend(["0", "ENDSEC", "0", "EOF"])
|
|
116
|
+
return "\n".join(lines) + "\n"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
__all__ = [
|
|
120
|
+
"BBox",
|
|
121
|
+
"Point",
|
|
122
|
+
"distance",
|
|
123
|
+
"polyline_length",
|
|
124
|
+
"polygon_area",
|
|
125
|
+
"bounding_box",
|
|
126
|
+
"pixels_to_mm",
|
|
127
|
+
"paper_to_model_scale",
|
|
128
|
+
"simplify_rdp",
|
|
129
|
+
"format_dxf_polyline",
|
|
130
|
+
]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from dxf_geometry import (
|
|
3
|
+
bounding_box,
|
|
4
|
+
format_dxf_polyline,
|
|
5
|
+
paper_to_model_scale,
|
|
6
|
+
pixels_to_mm,
|
|
7
|
+
polygon_area,
|
|
8
|
+
polyline_length,
|
|
9
|
+
simplify_rdp,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestDxfGeometry(unittest.TestCase):
|
|
14
|
+
def test_polyline_and_area(self):
|
|
15
|
+
sq = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]
|
|
16
|
+
self.assertAlmostEqual(polyline_length(sq, False), 30.0)
|
|
17
|
+
self.assertAlmostEqual(polyline_length(sq, True), 40.0)
|
|
18
|
+
self.assertAlmostEqual(polygon_area(sq), 100.0)
|
|
19
|
+
|
|
20
|
+
def test_bbox_and_scales(self):
|
|
21
|
+
bbox = bounding_box([(-2.0, 5.0), (8.0, -3.0), (4.0, 12.0)])
|
|
22
|
+
self.assertIsNotNone(bbox)
|
|
23
|
+
self.assertAlmostEqual(bbox.width, 10.0)
|
|
24
|
+
self.assertAlmostEqual(bbox.height, 15.0)
|
|
25
|
+
self.assertAlmostEqual(pixels_to_mm(300.0, 300.0), 25.4)
|
|
26
|
+
self.assertAlmostEqual(paper_to_model_scale(20.0, 50.0), 1000.0)
|
|
27
|
+
|
|
28
|
+
def test_simplify_and_dxf(self):
|
|
29
|
+
pts = [(0.0, 0.0), (5.0, 0.05), (10.0, 0.0), (10.0, 10.0)]
|
|
30
|
+
out = simplify_rdp(pts, 0.2)
|
|
31
|
+
self.assertEqual(len(out), 3)
|
|
32
|
+
dxf = format_dxf_polyline(out, "WALLS", True)
|
|
33
|
+
self.assertIn("LWPOLYLINE", dxf)
|
|
34
|
+
self.assertIn("WALLS", dxf)
|
|
35
|
+
self.assertIn("EOF", dxf)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
if __name__ == "__main__":
|
|
39
|
+
unittest.main()
|