- Fixed 2d sketch with transfrom
This commit is contained in:
@@ -4,14 +4,10 @@ from copy import copy
|
||||
|
||||
import numpy as np
|
||||
from PySide6.QtWidgets import QApplication, QWidget, QMessageBox, QInputDialog
|
||||
from PySide6.QtGui import QPainter, QPen, QColor
|
||||
from PySide6.QtGui import QPainter, QPen, QColor, QTransform
|
||||
from PySide6.QtCore import Qt, QPoint, QPointF, Signal
|
||||
from python_solvespace import SolverSystem, ResultFlag
|
||||
|
||||
class DrawingTools():
|
||||
pass
|
||||
class Costrains():
|
||||
pass
|
||||
|
||||
class SketchWidget(QWidget):
|
||||
constrain_done = Signal()
|
||||
@@ -59,13 +55,10 @@ class SketchWidget(QWidget):
|
||||
"""Lines as orientation projected from the sketch"""
|
||||
|
||||
for point in lines:
|
||||
print(point)
|
||||
x, y = point
|
||||
self.proj_snap_lines = lines
|
||||
self.proj_snap_points.append(QPoint(x, y))
|
||||
|
||||
|
||||
#point = self.solv.add_point_2d(x, y, self.wp)
|
||||
coord = QPoint(x, y)
|
||||
self.proj_snap_points.append(coord)
|
||||
|
||||
"""relation_point = {} # Reinitialize the dictionary
|
||||
#handle_nr = self.get_handle_nr(str(point))
|
||||
@@ -215,7 +208,6 @@ class SketchWidget(QWidget):
|
||||
return None
|
||||
|
||||
def viewport_to_local_coord(self, qt_pos : QPoint) -> QPoint:
|
||||
self.to_quadrant_coords(qt_pos)
|
||||
return QPoint(self.to_quadrant_coords(qt_pos))
|
||||
|
||||
def check_all_points(self,) -> list:
|
||||
@@ -647,11 +639,13 @@ class SketchWidget(QWidget):
|
||||
painter.setPen(QPen(Qt.red, 4))
|
||||
painter.drawPoint(middle_x, middle_y)
|
||||
|
||||
def draw_cross(self, painter, x, y, size=10):
|
||||
def draw_cross(self, painter, pos: QPoint, size=10):
|
||||
# Set up the pen
|
||||
pen = QPen(QColor('green')) # You can change the color as needed
|
||||
pen.setWidth(int(2 / self.zoom)) # Set the line widt)h
|
||||
painter.setPen(pen)
|
||||
x = pos.x()
|
||||
y = pos.y()
|
||||
|
||||
# Calculate the endpoints of the cross
|
||||
half_size = size // 2
|
||||
@@ -667,23 +661,47 @@ class SketchWidget(QWidget):
|
||||
center_x = self.width() // 2
|
||||
center_y = self.height() // 2
|
||||
quadrant_x = point.x() - center_x
|
||||
quadrant_y = point.y() - center_y
|
||||
quadrant_y = center_y - point.y() # Note the change here
|
||||
return QPoint(quadrant_x, quadrant_y) / self.zoom
|
||||
|
||||
def from_quadrant_coords(self, point: QPoint):
|
||||
"""Translate quadrant coordinates to linear coordinates."""
|
||||
center_x = self.width() // 2
|
||||
center_y = self.height() // 2
|
||||
widget_x = center_x + point.x() * self.zoom
|
||||
widget_y = center_y - point.y() * self.zoom # Note the subtraction here
|
||||
|
||||
return QPoint(int(widget_x), int(widget_y))
|
||||
|
||||
def from_quadrant_coords_no_center(self, point):
|
||||
"""Translate quadrant coordinates to linear coordinates."""
|
||||
center_x = 0
|
||||
center_y = 0
|
||||
widget_x = center_x + point.x() * self.zoom
|
||||
widget_y = center_y - point.y() * self.zoom # Note the subtraction here
|
||||
return QPoint(int(widget_x), int(widget_y))
|
||||
|
||||
def paintEvent(self, event):
|
||||
painter = QPainter(self)
|
||||
painter.setRenderHint(QPainter.Antialiasing)
|
||||
|
||||
self.drawAxes(painter)
|
||||
|
||||
# Create a QTransform object
|
||||
transform = QTransform()
|
||||
|
||||
# Translate the origin to the center of the widget
|
||||
center = QPoint(self.width() // 2, self.height() // 2)
|
||||
painter.translate(center)
|
||||
center = QPointF(self.width() / 2, self.height() / 2)
|
||||
transform.translate(center.x(), center.y())
|
||||
|
||||
# Apply the zoom factor
|
||||
painter.scale(self.zoom, self.zoom)
|
||||
transform.scale(self.zoom, -self.zoom) # Negative y-scale to invert y-axis
|
||||
|
||||
# Set the transform to the painter
|
||||
painter.setTransform(transform)
|
||||
|
||||
pen = QPen(Qt.gray)
|
||||
pen.setWidth(2 / self.zoom)
|
||||
pen.setWidthF(2 / self.zoom)
|
||||
painter.setPen(pen)
|
||||
|
||||
# Draw points
|
||||
@@ -700,36 +718,33 @@ class SketchWidget(QWidget):
|
||||
painter.drawText(mid, str(round(dis, 2)))
|
||||
|
||||
pen = QPen(Qt.green)
|
||||
pen.setWidth(2)
|
||||
pen.setWidthF(2 / self.zoom)
|
||||
painter.setPen(pen)
|
||||
|
||||
if self.solv.entity_len():
|
||||
for i in range(self.solv.entity_len()):
|
||||
# 3 Entitys in the beginning of the workplane normal and point
|
||||
entity = self.solv.entity(i)
|
||||
if entity.is_point_2d() and self.solv.params(entity.params):
|
||||
x, y = self.solv.params(entity.params)
|
||||
point = QPoint(x, y)
|
||||
point = QPointF(x, y)
|
||||
painter.drawEllipse(point, 6 / self.zoom, 6 / self.zoom)
|
||||
|
||||
#Highlight point hovered
|
||||
# Highlight point hovered
|
||||
if self.hovered_point:
|
||||
highlight_pen = QPen(QColor(255, 0, 0))
|
||||
highlight_pen.setWidth(2)
|
||||
highlight_pen.setWidthF(2 / self.zoom)
|
||||
painter.setPen(highlight_pen)
|
||||
painter.drawEllipse(self.hovered_point, 5 / self.zoom, 5 / self.zoom)
|
||||
|
||||
# Highlight line hovered
|
||||
if self.selected_line and not self.hovered_point:
|
||||
p1, p2 = self.selected_line
|
||||
painter.setPen(QPen(Qt.red, 2))
|
||||
painter.setPen(QPen(Qt.red, 2 / self.zoom))
|
||||
painter.drawLine(p1, p2)
|
||||
|
||||
for cross in self.proj_snap_lines:
|
||||
# Calculate the endpoints of the cross
|
||||
self.draw_cross(painter, cross[0], cross[1], 10)
|
||||
for cross in self.proj_snap_points:
|
||||
self.draw_cross(painter, cross, 10 / self.zoom)
|
||||
|
||||
# self.drawBackgroundGrid(painter)
|
||||
painter.end()
|
||||
|
||||
def wheelEvent(self, event):
|
||||
|
||||
Reference in New Issue
Block a user