重构代码,优化逻辑。
This commit is contained in:
parent
ef683999b4
commit
50b88b4e26
93
graphic.py
93
graphic.py
|
|
@ -1,20 +1,39 @@
|
||||||
import copy
|
import copy
|
||||||
import math
|
import math
|
||||||
import ezdxf
|
import ezdxf
|
||||||
|
from ezdxf.math import UCS
|
||||||
|
|
||||||
from type3d import Vector3D
|
from type3d import Vector3D
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import transformation
|
import transformation
|
||||||
from typing import List, Union, Tuple
|
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:
|
class Line:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
msp,
|
||||||
start_p: Vector3D,
|
start_p: Vector3D,
|
||||||
end_p: Vector3D,
|
end_p: Vector3D,
|
||||||
tension_k: float,
|
tension_k: float,
|
||||||
n_point: int,
|
n_point: int,
|
||||||
):
|
):
|
||||||
|
self._msp = msp
|
||||||
self._start_p: Vector3D = start_p
|
self._start_p: Vector3D = start_p
|
||||||
self._end_p: Vector3D = end_p
|
self._end_p: Vector3D = end_p
|
||||||
self._tension_k: float = tension_k
|
self._tension_k: float = tension_k
|
||||||
|
|
@ -64,12 +83,11 @@ class Line:
|
||||||
p_points,
|
p_points,
|
||||||
)
|
)
|
||||||
self._points = points
|
self._points = points
|
||||||
|
msp = self._msp
|
||||||
|
msp.add_polyline3d(points)
|
||||||
return points
|
return points
|
||||||
|
|
||||||
def sag(self):
|
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
|
start_p = self._start_p
|
||||||
end_p = self._end_p
|
end_p = self._end_p
|
||||||
span = self._span
|
span = self._span
|
||||||
|
|
@ -79,26 +97,31 @@ class Line:
|
||||||
n_point = self._n_point
|
n_point = self._n_point
|
||||||
middle_z = points[int(n_point / 2)][2]
|
middle_z = points[int(n_point / 2)][2]
|
||||||
rotation = self._rotation
|
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,
|
||||||
middle_span + start_p.x,
|
start_p.z + middle_span * height / span,
|
||||||
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, middle_z],
|
middle_span + start_p.x,
|
||||||
[
|
start_p.y,
|
||||||
middle_span + start_p.x,
|
start_p.z + middle_span * height / span,
|
||||||
start_p.y,
|
],
|
||||||
start_p.z + middle_span * height / span,
|
[start_p.x + span, start_p.y, end_p.z],
|
||||||
],
|
] # 旋转前的值
|
||||||
[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
|
return sag_points
|
||||||
|
|
||||||
def swing(self, angel):
|
def swing(self, angel):
|
||||||
|
if not self._points:
|
||||||
|
self.curve()
|
||||||
points = self._points
|
points = self._points
|
||||||
start_p = self._start_p
|
start_p = self._start_p
|
||||||
end_p = self._end_p
|
end_p = self._end_p
|
||||||
|
|
@ -108,24 +131,50 @@ class Line:
|
||||||
[end_p.x, end_p.y, end_p.z],
|
[end_p.x, end_p.y, end_p.z],
|
||||||
)
|
)
|
||||||
swing_point = swing_rotation.rotate(points)
|
swing_point = swing_rotation.rotate(points)
|
||||||
|
msp = self._msp
|
||||||
|
msp.add_polyline3d(swing_point)
|
||||||
return swing_point
|
return swing_point
|
||||||
|
|
||||||
|
|
||||||
class Canvas:
|
class Canvas:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._doc = None
|
self._doc = None
|
||||||
self._msp = None
|
self.msp = None
|
||||||
|
|
||||||
def init_canvas(self):
|
def init_canvas(self):
|
||||||
doc = ezdxf.new()
|
doc = ezdxf.new()
|
||||||
self._doc = doc
|
self._doc = doc
|
||||||
msp = doc.modelspace()
|
msp = doc.modelspace()
|
||||||
self._msp = msp
|
self.msp = msp
|
||||||
|
doc.styles.new("TXT", dxfattribs={"font": "romans.shx"})
|
||||||
|
|
||||||
def draw(self, points):
|
def draw(self, points):
|
||||||
msp = self._msp
|
msp = self.msp
|
||||||
msp.add_polyline3d(points)
|
msp.add_polyline3d(points)
|
||||||
|
|
||||||
def save(self, file_path):
|
def save(self, file_path):
|
||||||
doc = self._doc
|
doc = self._doc
|
||||||
doc.saveas(file_path)
|
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
|
||||||
|
|
|
||||||
19
main.py
19
main.py
|
|
@ -1,13 +1,22 @@
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from graphic import Canvas, Line
|
from graphic import Canvas, Line, Ruler, draw_text
|
||||||
from type3d import Vector3D
|
from type3d import Vector3D
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
canvas = Canvas()
|
canvas = Canvas()
|
||||||
canvas.init_canvas()
|
canvas.init_canvas()
|
||||||
line = Line(Vector3D(20, 30, 0), Vector3D(400, 100, 1), 0.3 * 1e-3, 400)
|
line = Line(canvas.msp, Vector3D(20, 30, 0), Vector3D(400, 100, 1), 0.3 * 1e-3, 800)
|
||||||
canvas.draw(line.curve())
|
line.curve()
|
||||||
canvas.draw(line.sag())
|
line.sag()
|
||||||
canvas.draw(line.swing(80/180*math.pi))
|
# 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")
|
canvas.save("lwpolyline1.dxf")
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ def rotation(
|
||||||
angel: float, axis_start: np.ndarray, axis_end: np.ndarray, points: np.ndarray
|
angel: float, axis_start: np.ndarray, axis_end: np.ndarray, points: np.ndarray
|
||||||
) -> np.ndarray:
|
) -> np.ndarray:
|
||||||
_rotation = Rotation(angel, axis_start.tolist(), axis_end.tolist())
|
_rotation = Rotation(angel, axis_start.tolist(), axis_end.tolist())
|
||||||
return _rotation.rotate(points.tolist())
|
return np.array(_rotation.rotate(points.tolist()))
|
||||||
|
|
||||||
|
|
||||||
class Rotation:
|
class Rotation:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue