- changing compos for sketches works

This commit is contained in:
bklronin 2024-12-31 14:34:41 +01:00
parent 8530f6f8b9
commit 6c8462a7f3

347
main.py
View File

@ -114,7 +114,7 @@ class MainWindow(QMainWindow):
self.ui.pb_origin_wp.pressed.connect(self.add_new_sketch_origin)
self.ui.pb_origin_face.pressed.connect(self.add_new_sketch_wp)
self.ui.pb_nw_sktch.pressed.connect(self.add_sketch)
self.ui.pb_nw_sktch.pressed.connect(self.add_sketch_to_compo)
self.ui.pb_del_sketch.pressed.connect(self.del_sketch)
self.ui.pb_edt_sktch.pressed.connect(self.edit_sketch)
@ -149,9 +149,194 @@ class MainWindow(QMainWindow):
self.ui.new_compo.pressed.connect(self.new_component)
"""Project -> (Timeline) -> Component -> Sketch -> Body / Interactor -> Connector -> Assembly -> PB Render"""
def new_project(self):
print("New project")
timeline = []
self.project.timeline = timeline
self.new_component()
def new_component(self):
print("Creating a new component...")
# Lazily initialize self.compo_layout if it doesn't exist
if not hasattr(self, 'compo_layout'):
print("Initializing compo_layout...")
self.compo_layout = QHBoxLayout()
# Ensure the QGroupBox has a layout
if not self.ui.compo_box.layout():
self.ui.compo_box.setLayout(QVBoxLayout()) # Set a default layout for QGroupBox
# Add the horizontal layout to the QGroupBox's layout
self.ui.compo_box.layout().addLayout(self.compo_layout)
# Align the layout to the left
self.compo_layout.setAlignment(Qt.AlignLeft)
# Create and initialize a new Component
compo = Component()
compo.id = f"Component {len(self.project.timeline) + 1}"
compo.descript = "Initial Component"
compo.sketches = {}
compo.body = {}
self.project.timeline.append(compo)
# Create a button for the new component
button = QPushButton()
button.setToolTip(compo.id)
button.setText(str(len(self.project.timeline)))
button.setFixedSize(QSize(40, 40)) # Set button size
button.setCheckable(True)
button.setAutoExclusive(True)
button.setChecked(False)
button.released.connect(self.on_compo_change)
# Add the button to the layout
self.compo_layout.addWidget(button)
print(f"Added component {compo.id} to the layout.")
def get_activated_compo(self):
# Iterate through all items in the layout
total_elements = self.compo_layout.count()
print(total_elements)
for i in range(total_elements):
widget = self.compo_layout.itemAt(i).widget() # Get the widget at the index
if widget: # Check if the widget is not None
if isinstance(widget, QPushButton) and widget.isCheckable():
state = widget.isChecked() # Get the checked state
print(f"{widget.text()} is {'checked' if state else 'unchecked'}.")
if state:
return i
def add_new_sketch_origin(self):
name = f"sketches-{str(names.get_first_name())}"
sketch = Sketch()
sketch.id = name
sketch.origin = [0,0,0]
self.sketchWidget.reset_buffers()
self.sketchWidget.create_sketch(sketch)
def add_new_sketch_wp(self):
## Sketch projected from 3d view into 2d
name = f"sketches-{str(names.get_first_name())}"
sketch = Sketch()
sketch.id = name
sketch.origin = self.custom_3D_Widget.centroid
sketch.normal = self.custom_3D_Widget.selected_normal
sketch.slv_points = []
sketch.slv_lines = []
sketch.proj_points = self.custom_3D_Widget.project_tosketch_points
sketch.proj_lines = self.custom_3D_Widget.project_tosketch_lines
self.sketchWidget.reset_buffers()
self.sketchWidget.create_sketch(sketch)
self.sketchWidget.create_workplane_projected()
if not sketch.proj_lines:
self.sketchWidget.convert_proj_points(sketch.proj_points)
self.sketchWidget.convert_proj_lines(sketch.proj_lines)
self.sketchWidget.update()
# CLear all selections after it has been projected
self.custom_3D_Widget.project_tosketch_points.clear()
self.custom_3D_Widget.project_tosketch_lines.clear()
self.custom_3D_Widget.clear_actors_projection()
self.custom_3D_Widget.clear_actors_normals()
def add_sketch_to_compo(self):
"""
Add sketch to component
:return:
"""
sketch = Sketch()
sketch_from_widget = self.sketchWidget.get_sketch()
points = sketch_from_widget.points
sketch.convert_points_for_sdf(points)
sketch.id = sketch_from_widget.id
sketch.filter_lines_for_interactor(sketch_from_widget.lines)
# Register sketch to timeline
### Add selection compo here
compo_id = self.get_activated_compo()
print("newsketch_name", sketch.id)
self.project.timeline[compo_id].sketches[sketch.id] = sketch
# Add Item to slection menu
self.ui.sketch_list.addItem(sketch.id)
# Deactivate drawing
self.ui.pb_linetool.setChecked(False)
self.sketchWidget.line_mode = False
items = self.ui.sketch_list.findItems(sketch.id, Qt.MatchExactly)[0]
self.ui.sketch_list.setCurrentItem(items)
def on_compo_change(self):
compo_id = self.get_activated_compo()
if compo_id:
self.ui.sketch_list.clear()
print("id", compo_id)
print("sketch_registry", self.project.timeline[compo_id].sketches)
for sketch in self.project.timeline[compo_id].sketches:
print(sketch)
self.ui.sketch_list.addItem(sketch)
def edit_sketch(self):
name = self.ui.sketch_list.currentItem().text()
selected = self.ui.sketch_list.currentItem()
name = selected.text()
# TODO: add selected element from timeline
sel_compo = self.project.timeline[-1]
sketch = sel_compo.sketches[name]
self.sketchWidget.set_sketch(sketch)
self.sketchWidget.update()
def del_sketch(self):
# Old
print("Deleting")
name = self.ui.sketch_list.currentItem() # Get the current item
print(self.model)
if name is not None:
item_name = name.text()
print("obj_name", item_name)
# Check if the 'sketches' key exists in the model dictionary
if 'sketches' in self.model and item_name in self.model['sketches']:
if self.model['sketches'][item_name]['id'] == item_name:
row = self.ui.sketch_list.row(name) # Get the row of the current item
self.ui.sketch_list.takeItem(row) # Remove the item from the list widget
self.sketchWidget.clear_sketch()
self.model['sketches'].pop(item_name) # Remove the item from the sketches dictionary
print(f"Removed sketches: {item_name}")
# Check if the 'operation' key exists in the model dictionary
elif 'operation' in self.model and item_name in self.model['operation']:
if self.model['operation'][item_name]['id'] == item_name:
row = self.ui.sketch_list.row(name) # Get the row of the current item
self.ui.sketch_list.takeItem(row) # Remove the item from the list widget
self.sketchWidget.clear_sketch()
self.model['operation'].pop(item_name) # Remove the item from the operation dictionary
print(f"Removed operation: {item_name}")
else:
print(f"Item '{item_name}' not found in either 'sketches' or 'operation' dictionary.")
else:
print("No item selected.")
def on_flip_face(self):
self.send_command.emit("flip")
@ -228,159 +413,6 @@ class MainWindow(QMainWindow):
#self.view_update()
print(f"Selected item: {name}")
def new_project(self):
print("New project")
timeline = []
self.project.timeline = timeline
self.new_component()
def new_component(self):
print("Creating a new component...")
# Lazily initialize self.compo_layout if it doesn't exist
if not hasattr(self, 'compo_layout'):
print("Initializing compo_layout...")
self.compo_layout = QHBoxLayout()
# Ensure the QGroupBox has a layout
if not self.ui.compo_box.layout():
self.ui.compo_box.setLayout(QVBoxLayout()) # Set a default layout for QGroupBox
# Add the horizontal layout to the QGroupBox's layout
self.ui.compo_box.layout().addLayout(self.compo_layout)
# Align the layout to the left
self.compo_layout.setAlignment(Qt.AlignLeft)
# Create and initialize a new Component
compo = Component()
compo.id = f"Component {len(self.project.timeline) + 1}"
compo.descript = "Initial Component"
compo.sketches = {}
compo.body = {}
self.project.timeline.append(compo)
# Create a button for the new component
button = QPushButton()
button.setToolTip(compo.id)
button.setText(str(len(self.project.timeline)))
button.setFixedSize(QSize(40, 40)) # Set button size
# Add the button to the layout
self.compo_layout.addWidget(button)
print(f"Added component {compo.id} to the layout.")
def add_new_sketch_origin(self):
name = f"sketches-{str(names.get_first_name())}"
sketch = Sketch()
sketch.id = name
sketch.origin = [0,0,0]
self.sketchWidget.reset_buffers()
self.sketchWidget.create_sketch(sketch)
def add_new_sketch_wp(self):
## Sketch projected from 3d view into 2d
name = f"sketches-{str(names.get_first_name())}"
sketch = Sketch()
sketch.id = name
sketch.origin = self.custom_3D_Widget.centroid
sketch.normal = self.custom_3D_Widget.selected_normal
sketch.slv_points = []
sketch.slv_lines = []
sketch.proj_points = self.custom_3D_Widget.project_tosketch_points
sketch.proj_lines = self.custom_3D_Widget.project_tosketch_lines
self.sketchWidget.reset_buffers()
self.sketchWidget.create_sketch(sketch)
self.sketchWidget.create_workplane_projected()
if not sketch.proj_lines:
self.sketchWidget.convert_proj_points(sketch.proj_points)
self.sketchWidget.convert_proj_lines(sketch.proj_lines)
self.sketchWidget.update()
# CLear all selections after it has been projected
self.custom_3D_Widget.project_tosketch_points.clear()
self.custom_3D_Widget.project_tosketch_lines.clear()
self.custom_3D_Widget.clear_actors_projection()
self.custom_3D_Widget.clear_actors_normals()
def add_sketch(self):
"""
:return:
"""
sketch = Sketch()
sketch_from_widget = self.sketchWidget.get_sketch()
points = sketch_from_widget.points
sketch.convert_points_for_sdf(points)
sketch.id = sketch_from_widget.id
sketch.filter_lines_for_interactor(sketch_from_widget.lines)
# Register sketch to timeline
self.project.timeline[-1].sketches[sketch.id] = sketch
# Add Item to slection menu
self.ui.sketch_list.addItem(sketch.id)
# Deactivate drawing
self.ui.pb_linetool.setChecked(False)
self.sketchWidget.line_mode = False
items = self.ui.sketch_list.findItems(sketch.id, Qt.MatchExactly)[0]
self.ui.sketch_list.setCurrentItem(items)
def edit_sketch(self):
name = self.ui.sketch_list.currentItem().text()
selected = self.ui.sketch_list.currentItem()
name = selected.text()
# TODO: add selected element from timeline
sel_compo = self.project.timeline[-1]
sketch = sel_compo.sketches[name]
self.sketchWidget.set_sketch(sketch)
self.sketchWidget.update()
def del_sketch(self):
# Old
print("Deleting")
name = self.ui.sketch_list.currentItem() # Get the current item
print(self.model)
if name is not None:
item_name = name.text()
print("obj_name", item_name)
# Check if the 'sketches' key exists in the model dictionary
if 'sketches' in self.model and item_name in self.model['sketches']:
if self.model['sketches'][item_name]['id'] == item_name:
row = self.ui.sketch_list.row(name) # Get the row of the current item
self.ui.sketch_list.takeItem(row) # Remove the item from the list widget
self.sketchWidget.clear_sketch()
self.model['sketches'].pop(item_name) # Remove the item from the sketches dictionary
print(f"Removed sketches: {item_name}")
# Check if the 'operation' key exists in the model dictionary
elif 'operation' in self.model and item_name in self.model['operation']:
if self.model['operation'][item_name]['id'] == item_name:
row = self.ui.sketch_list.row(name) # Get the row of the current item
self.ui.sketch_list.takeItem(row) # Remove the item from the list widget
self.sketchWidget.clear_sketch()
self.model['operation'].pop(item_name) # Remove the item from the operation dictionary
print(f"Removed operation: {item_name}")
else:
print(f"Item '{item_name}' not found in either 'sketches' or 'operation' dictionary.")
else:
print("No item selected.")
def update_body(self):
pass
@ -411,7 +443,7 @@ class MainWindow(QMainWindow):
name = selected.text()
# TODO: add selected element from timeline
sel_compo = self.project.timeline[-1]
sel_compo = self.project.timeline[self.get_activated_compo()]
#print(sel_compo)
sketch = sel_compo.sketches[name]
#print(sketch)
@ -510,11 +542,10 @@ class MainWindow(QMainWindow):
self.custom_3D_Widget.load_stl(file)
self.custom_3D_Widget.update()
@dataclass
class Timeline:
"""Timeline """
### Collection of the Components
timeline: list = None
"""add to time,