- Fixed 2d sketch with transfrom

This commit is contained in:
bklronin
2024-07-16 18:02:27 +02:00
parent 0c3e4eeb5e
commit c6f48a6e78
7 changed files with 273 additions and 200 deletions

127
main.py
View File

@@ -1,3 +1,8 @@
# nuitka-project: --plugin-enable=pyside6
# nuitka-project: --plugin-enable=numpy
# nuitka-project: --standalone
# nuitka-project: --macos-create-app-bundle
import uuid
import names
from PySide6.QtCore import Qt, QPoint, Signal
@@ -12,6 +17,7 @@ from mesh_modules import simple_mesh, vesta_mesh, interactor_mesh
# main, draw_widget, gl_widget
class ExtrudeDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
@@ -30,6 +36,7 @@ class ExtrudeDialog(QDialog):
# Symmetric checkbox
self.symmetric_checkbox = QCheckBox('Symmetric Extrude')
self.invert_checkbox = QCheckBox('Invert Extrusion')
# OK and Cancel buttons
button_layout = QHBoxLayout()
@@ -43,12 +50,16 @@ class ExtrudeDialog(QDialog):
# Add all widgets to main layout
layout.addLayout(length_layout)
layout.addWidget(self.symmetric_checkbox)
layout.addLayout(length_layout)
layout.addWidget(self.invert_checkbox)
layout.addLayout(button_layout)
self.setLayout(layout)
def get_values(self):
return self.length_input.value(), self.symmetric_checkbox.isChecked()
return self.length_input.value(), self.symmetric_checkbox.isChecked() ,self.invert_checkbox.isChecked()
class MainWindow(QMainWindow):
send_command = Signal(str)
@@ -129,7 +140,7 @@ class MainWindow(QMainWindow):
self.sketchWidget.create_proj_lines(edges)
# CLear all selections after it has been projected
self.custom_3D_Widget.clear_edge_select()
#self.custom_3D_Widget.clear_edge_select()
self.custom_3D_Widget.clear_actors_projection()
#self.sketchWidget.create_workplane_space(edges, normal)
@@ -189,13 +200,6 @@ class MainWindow(QMainWindow):
self.sketchWidget.mouse_mode = None
self.sketchWidget.reset_buffers()
def calc_sketch_projection_3d(self, lines, z_origin, depth):
print(f"Gemetries {lines}, {z_origin}, {depth}")
edges = interactor_mesh.generate_mesh(lines, z_origin, depth)
print("final_mesh", edges)
self.custom_3D_Widget.load_interactor_mesh(edges)
def draw_mesh(self):
name = self.ui.body_list.currentItem().text()
print("selected_for disp", name)
@@ -225,8 +229,10 @@ class MainWindow(QMainWindow):
points_for_interact = []
for point_to_poly in self.sketchWidget.slv_lines_main:
start, end = point_to_poly['ui_points']
start_draw = self.translate_points_tup(start)
end_draw = self.translate_points_tup(end)
from_coord_start = self.sketchWidget.from_quadrant_coords_no_center(start)
from_coord_end = self.sketchWidget.from_quadrant_coords_no_center(end)
start_draw = self.translate_points_tup(from_coord_start)
end_draw = self.translate_points_tup(from_coord_end)
line = start_draw, end_draw
points_for_interact.append(line)
@@ -330,9 +336,44 @@ class MainWindow(QMainWindow):
if isinstance(point, QPoint):
return point.x(), point.y()
def rotate_point_to_normal(self, centroid, normal):
# Ensure the normal is a unit vector
normal = normal / np.linalg.norm(normal)
# Initial direction (assuming positive Z-axis)
initial_direction = np.array([0, 0, 1])
# Compute rotation axis
rotation_axis = np.cross(initial_direction, normal)
# If rotation axis is zero (vectors are parallel), no rotation is needed
if np.allclose(rotation_axis, 0):
return centroid
# Compute rotation angle
rotation_angle = np.arccos(np.dot(initial_direction, normal))
# Create rotation matrix using Rodrigues' formula
K = np.array([
[0, -rotation_axis[2], rotation_axis[1]],
[rotation_axis[2], 0, -rotation_axis[0]],
[-rotation_axis[1], rotation_axis[0], 0]
])
rotation_matrix = (
np.eye(3) +
np.sin(rotation_angle) * K +
(1 - np.cos(rotation_angle)) * np.dot(K, K)
)
# Apply rotation to centroid
rotated_centroid = np.dot(rotation_matrix, centroid)
return rotated_centroid
def send_extrude(self):
is_symmetric = None
length = None
invert = None
selected = self.ui.sketch_list.currentItem()
name = selected.text()
points = self.model['sketch'][name]['sketch_points']
@@ -342,13 +383,10 @@ class MainWindow(QMainWindow):
#detect loop that causes problems in mesh generation
del points[-1]
"""length, ok = QInputDialog.getDouble(self, 'Extrude Length', 'Enter a mm value:', decimals=2)
#TODO : Implement cancel"""
dialog = ExtrudeDialog(self)
if dialog.exec():
length, is_symmetric = dialog.get_values()
print(f"Extrude length: {length}, Symmetric: {is_symmetric}")
length, is_symmetric, invert = dialog.get_values()
print(f"Extrude length: {length}, Symmetric: {is_symmetric} Invert: {invert}")
else:
length = 0
print("Extrude cancelled")
@@ -358,15 +396,31 @@ class MainWindow(QMainWindow):
# Rotation is done in vtk matrix trans
angle = 0
normal = [0, 0, 1]
f = geo.extrude_shape(points, length, angle, normal, is_symmetric)
if not is_symmetric:
origin_z_lvl = length / 2
normal = self.custom_3D_Widget.selected_normal
print("Normie enter", normal)
if normal is None:
normal = [0, 0, 1]
centroid = self.custom_3D_Widget.centroid
if centroid is None:
centroid = [0, 0, 0]
else:
origin_z_lvl = 0
centroid = list(centroid)
print("THis centroid ",centroid)
self.calc_sketch_projection_3d(lines, origin_z_lvl, length)
f = geo.extrude_shape(points, length, angle, normal, centroid, is_symmetric, invert)
z_origin = centroid[2]
if is_symmetric:
z_origin = z_origin - length / 2
if invert:
edges = interactor_mesh.generate_mesh(lines, z_origin, length, True)
else:
edges = interactor_mesh.generate_mesh(lines, z_origin, length, False)
self.custom_3D_Widget.load_interactor_mesh(edges)
name_op = f"extrd-{name}"
element = {
@@ -388,7 +442,7 @@ class MainWindow(QMainWindow):
points = self.model['operation'][name]['sdf_object']
self.list_selected.append(points)
if len(self.list_selected) > 1:
if len(self.list_selected) == 2:
geo = Geometry()
f = geo.cut_shapes(self.list_selected[0], self.list_selected[1] )
@@ -401,9 +455,12 @@ class MainWindow(QMainWindow):
name_op = f"cut-{name}"
self.model['operation'][name_op] = element
self.ui.body_list.addItem(name_op)
items = self.ui.sketch_list.findItems(name_op, Qt.MatchExactly)
#self.ui.body_list.setCurrentItem(items[-1])
items = self.ui.body_list.findItems(name_op, Qt.MatchExactly)
self.ui.body_list.setCurrentItem(items[-1])
self.custom_3D_Widget.clear_body_actors()
self.draw_mesh()
elif len(self.list_selected) > 2:
self.list_selected.clear()
else:
print("mindestens 2!")
@@ -443,16 +500,22 @@ class Geometry:
print("p2", p2)
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def extrude_shape(self, points, length: float, angle, normal, symet: bool = True):
def extrude_shape(self, points, length: float, angle, normal, centroid, symet: bool = True, invert: bool = False):
"""2D to 3D sdf always first"""
f = polygon(points).rotate(angle)
f = polygon(points)
f = f.extrude(length)
if not symet:
print("Offsetting", symet)
f = f.extrude(length).orient(normal).translate((0, 0, length/2)) # orient(normal)
# Calculate the offset vector (half the length in the direction of the normal)
offset = [n * (length / 2) for n in normal]
else:
f = f.extrude(length).orient(normal)
# Apply the offset in the direction of the normal
f = f.translate(offset)
# Apply the centroid translation
#f = f.translate(centroid)
# Apply the orientation
f = f.orient(normal)
return f