RobotPanel.py
1 # -*- coding: utf-8 -*-
2 
3 # ***************************************************************************
4 # * *
5 # * Animate workbench - FreeCAD Workbench for lightweight animation *
6 # * Copyright (c) 2019 Jiří Valášek jirka362@gmail.com *
7 # * *
8 # * This file is part of the Animate workbench. *
9 # * *
10 # * This program is free software; you can redistribute it and/or modify *
11 # * it under the terms of the GNU Lesser General Public License (LGPL) *
12 # * as published by the Free Software Foundation; either version 2 of *
13 # * the License, or (at your option) any later version. *
14 # * for detail see the LICENCE text file. *
15 # * *
16 # * Animate workbench is distributed in the hope that it will be useful, *
17 # * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 # * GNU Lesser General Public License for more details. *
20 # * *
21 # * You should have received a copy of the GNU Library General Public *
22 # * License along with Animate workbench; if not, write to the Free *
23 # * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24 # * MA 02111-1307 USA *
25 # * *
26 # ***************************************************************************/
27 
28 
34 
35 import FreeCAD
36 import FreeCADGui
37 
38 from PySide2.QtWidgets import QDialogButtonBox
39 from PySide2.QtCore import QObject
40 
41 
42 
50 
51 class RobotPanel(QObject):
52 
53 
55 
56 
58 
59 
73 
74  def __init__(self, robot_joints, forms):
75  super(RobotPanel, self).__init__()
76  self.robot_joints = robot_joints
77 
78  # Disable editing of RobRotation properties, leave some properties
79  # hidden and store previous joint variable values
80  self.previous_joint_values = []
81  for joint in robot_joints:
82  if joint.Proxy.__class__.__name__ == "RobRotationProxy":
83  self.previous_joint_values.append(joint.theta)
84  joint.setEditorMode("ValidRotation", 2)
85  elif joint.Proxy.__class__.__name__ == "RobTranslationProxy":
86  self.previous_joint_values.append(joint.d)
87  joint.setEditorMode("ValidTranslation", 2)
88 
89  for prop in joint.PropertiesList:
90  joint.setEditorMode(prop, 1)
91  joint.setEditorMode("Placement", 2)
92  joint.setEditorMode("RobotPanelActive", 2)
93  joint.RobotPanelActive = True
94 
95  # Add QDialogs to be displayed in freeCAD
96  self.form = forms
97 
98  # Add callbacks to sliders on all forms and move sliders to a position
99  # corresponding with time values
100  for i in range(len(forms)):
101  rj = robot_joints[i]
102  forms[i].sld_value.valueChanged.connect(
103  lambda value, form=forms[i], joint=rj:
104  self.sliderChanged(value, form, joint))
105  if rj.Proxy.__class__.__name__ == "RobRotationProxy":
106  val = (100 * (rj.theta - rj.thetaOffset - rj.thetaMinimum)) / \
107  (rj.thetaMaximum - rj.thetaMinimum)
108  val = min([100, max([val, 0])])
109  elif rj.Proxy.__class__.__name__ == "RobTranslationProxy":
110  val = (100 * (rj.d - rj.dOffset - rj.dMinimum)) / \
111  (rj.dMaximum - rj.dMinimum)
112  val = min([100, max([val, 0])])
113 
114  forms[i].sld_value.setValue(val)
115 
116 
127 
128  def sliderChanged(self, value, form, joint):
129  # Compute a time from the slider position and joint variable range
130  if joint.Proxy.__class__.__name__ == "RobRotationProxy":
131  val = value * (joint.thetaMaximum - joint.thetaMinimum) / 100 \
132  + joint.thetaMinimum + joint.thetaOffset
133  joint.theta = val
134  elif joint.Proxy.__class__.__name__ == "RobTranslationProxy":
135  val = value * (joint.dMaximum - joint.dMinimum) / 100 \
136  + joint.dMinimum + joint.dOffset
137  joint.d = val
138 
139  form.lbl_value.setText("Value: " + ("%5.3f" % val))
140 
141  # Recompute the document to show changes
142  FreeCAD.ActiveDocument.recompute()
143  FreeCADGui.updateGui()
144 
145 
153 
154  def reject(self):
155  # Return RobRotation times to previous values
156  for i in range(len(self.robot_joints)):
157  if self.robot_joints[i].Proxy.__class__.__name__ \
158  == "RobRotationProxy":
159  self.robot_joints[i].theta = self.previous_joint_values[i]
160  elif self.robot_joints[i].Proxy.__class__.__name__ \
161  == "RobTranslationProxy":
162  self.robot_joints[i].d = self.previous_joint_values[i]
163 
164  # Close the panel and recompute the document to show changes
165  # Allow editing of properties again
166  for joint in self.robot_joints:
167  for prop in joint.PropertiesList:
168  joint.setEditorMode(prop, 0)
169  joint.ViewObject.Proxy.panel = None
170 
171  # Keep some properties in read-only or hidden state
172  # if they were in it before
173  joint.setEditorMode("ObjectPlacement", 1)
174  joint.setEditorMode("ParentFramePlacement", 1)
175  if joint.Proxy.__class__.__name__ == "RobRotationProxy":
176  joint.setEditorMode("theta", 1)
177  joint.setEditorMode("ValidRotation", 2)
178  elif joint.Proxy.__class__.__name__ == "RobTranslationProxy":
179  joint.setEditorMode("d", 1)
180  joint.setEditorMode("ValidTranslation", 2)
181  joint.setEditorMode("Placement", 2)
182  joint.setEditorMode("RobotPanelActive", 2)
183  joint.RobotPanelActive = False
184 
185  FreeCADGui.Control.closeDialog()
186  FreeCAD.ActiveDocument.recompute()
187  FreeCADGui.updateGui()
188 
189 
194 
195  def getStandardButtons(self, *args):
196  return QDialogButtonBox.Close
197 
198 
203 
205  return False
206 
207 
212 
214  return True
215 
216 
221 
223  return True
def __init__(self, robot_joints, forms)
Initialization method for RobotPanel.
Definition: RobotPanel.py:74
def reject(self)
Feedback method called when 'Close' button was pressed to close the panel.
Definition: RobotPanel.py:154
def isAllowedAlterDocument(self)
Method to tell FreeCAD if dialog is allowed to alter a document.
Definition: RobotPanel.py:222
def getStandardButtons(self, *args)
Method to set just one button (close) to close the dialog.
Definition: RobotPanel.py:195
robot_joints
A list of RobRotation and RobTranslation instances.
Definition: RobotPanel.py:76
def isAllowedAlterView(self)
Method to tell FreeCAD if dialog is allowed to alter a view.
Definition: RobotPanel.py:213
def isAllowedAlterSelection(self)
Method to tell FreeCAD if dialog is allowed to alter a selection.
Definition: RobotPanel.py:204
def sliderChanged(self, value, form, joint)
Feedback method called when any slider position is changed.
Definition: RobotPanel.py:128
Class providing funcionality to a RobRotation panel inside the TaskView.
Definition: RobotPanel.py:51
form
A list of QDialog instances to show in the TaskView.
Definition: RobotPanel.py:96