diff --git a/graphic.py b/graphic.py index 3ed795d..eabe2e8 100644 --- a/graphic.py +++ b/graphic.py @@ -1,20 +1,39 @@ import copy import math import ezdxf +from ezdxf.math import UCS + from type3d import Vector3D import numpy as np import transformation from typing import List, Union, Tuple +def draw_text(msp, text, text_point): + text_entity = msp.add_text( + text=text, + dxfattribs={ + # text rotation angle in degrees in OCS + "thickness": 0.333, + "color": 1, + "style": "TXT", + "height": 2, + }, + ) + # set text position in OCS + text_entity.set_pos(text_point, align="MIDDLE_CENTER") + + class Line: def __init__( self, + msp, start_p: Vector3D, end_p: Vector3D, tension_k: float, n_point: int, ): + self._msp = msp self._start_p: Vector3D = start_p self._end_p: Vector3D = end_p self._tension_k: float = tension_k @@ -64,12 +83,11 @@ class Line: p_points, ) self._points = points + msp = self._msp + msp.add_polyline3d(points) return points def sag(self): - # msp.add_polyline3d( - # [(start_p.x, start_p.y, start_p.z), (end_p.x, end_p.y, end_p.z)] - # ) start_p = self._start_p end_p = self._end_p span = self._span @@ -79,26 +97,31 @@ class Line: n_point = self._n_point middle_z = points[int(n_point / 2)][2] rotation = self._rotation - sag_points = rotation.rotate( + p_sag_points = [ + [start_p.x, start_p.y, start_p.z], [ - [start_p.x, start_p.y, start_p.z], - [ - middle_span + start_p.x, - start_p.y, - start_p.z + middle_span * height / span, - ], - [middle_span + start_p.x, start_p.y, middle_z], - [ - middle_span + start_p.x, - start_p.y, - start_p.z + middle_span * height / span, - ], - [start_p.x + span, start_p.y, end_p.z], - ] - ) + middle_span + start_p.x, + start_p.y, + start_p.z + middle_span * height / span, + ], + [middle_span + start_p.x, start_p.y, middle_z], + [ + middle_span + start_p.x, + start_p.y, + start_p.z + middle_span * height / span, + ], + [start_p.x + span, start_p.y, end_p.z], + ] # 旋转前的值 + sag_points = rotation.rotate(p_sag_points) + sag_value = start_p.z + middle_span * height / span - middle_z + msp = self._msp + msp.add_polyline3d(sag_points) + draw_text(msp, f'sag {sag_value}', sag_points[1]) return sag_points def swing(self, angel): + if not self._points: + self.curve() points = self._points start_p = self._start_p end_p = self._end_p @@ -108,24 +131,50 @@ class Line: [end_p.x, end_p.y, end_p.z], ) swing_point = swing_rotation.rotate(points) + msp = self._msp + msp.add_polyline3d(swing_point) return swing_point class Canvas: def __init__(self): self._doc = None - self._msp = None + self.msp = None def init_canvas(self): doc = ezdxf.new() self._doc = doc msp = doc.modelspace() - self._msp = msp + self.msp = msp + doc.styles.new("TXT", dxfattribs={"font": "romans.shx"}) def draw(self, points): - msp = self._msp + msp = self.msp msp.add_polyline3d(points) def save(self, file_path): doc = self._doc doc.saveas(file_path) + + +class Ruler: + def __init__(self, curve_points1: List, curve_points2: List): + self._curve_points1 = curve_points1 + self._curve_points2 = curve_points2 + + def closest_distance(self): + curve_points1 = self._curve_points1 + curve_points2 = self._curve_points2 + closest = 999999999999 + closest_poc1 = 0 + closest_poc2 = 0 + for foo in range(len(curve_points1)): + poc1 = curve_points1[foo] # point of curve 1 + for bar in range(len(curve_points2)): + poc2 = curve_points2[bar] + distance = np.sum(np.power((np.array(poc1) - np.array(poc2)), 2)) ** 0.5 + if distance < closest: + closest_poc1 = poc1 + closest_poc2 = poc2 + closest = distance + return closest, closest_poc1, closest_poc2 diff --git a/main.py b/main.py index 90c8f84..1b081fd 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,22 @@ import math -from graphic import Canvas, Line +from graphic import Canvas, Line, Ruler, draw_text from type3d import Vector3D if __name__ == "__main__": canvas = Canvas() canvas.init_canvas() - line = Line(Vector3D(20, 30, 0), Vector3D(400, 100, 1), 0.3 * 1e-3, 400) - canvas.draw(line.curve()) - canvas.draw(line.sag()) - canvas.draw(line.swing(80/180*math.pi)) + line = Line(canvas.msp, Vector3D(20, 30, 0), Vector3D(400, 100, 1), 0.3 * 1e-3, 800) + line.curve() + line.sag() + # canvas.draw(line.swing(80 / 180 * math.pi)) + line111 = Line( + canvas.msp, Vector3D(50, 30, 0), Vector3D(430, 190, 1), 0.35 * 1e-3, 800 + ) + line111_swing = line111.swing(30 / 180 * math.pi) + ruler = Ruler(line.curve(), line111_swing) + closest_distance, poc1, poc2 = ruler.closest_distance() + canvas.draw([poc1, poc2]) + draw_text(canvas.msp, f"closest distance: {closest_distance}", poc1) + print(closest_distance) canvas.save("lwpolyline1.dxf") diff --git a/transformation.py b/transformation.py index ff1c3f5..7cdd34a 100644 --- a/transformation.py +++ b/transformation.py @@ -54,7 +54,7 @@ def rotation( angel: float, axis_start: np.ndarray, axis_end: np.ndarray, points: np.ndarray ) -> np.ndarray: _rotation = Rotation(angel, axis_start.tolist(), axis_end.tolist()) - return _rotation.rotate(points.tolist()) + return np.array(_rotation.rotate(points.tolist())) class Rotation: diff --git a/type3d.py b/type3d.py index 098a94e..707104f 100644 --- a/type3d.py +++ b/type3d.py @@ -20,3 +20,5 @@ class Vector3D: / abs(self) / abs(other) ) + +