FreeCAD-Doc/localwiki/Macro_View_Rotation-it.html
2018-07-19 18:47:02 -05:00

286 lines
9.7 KiB
HTML

<html><head><title>Macro View Rotation/it</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type='text/css' href='wiki.css' rel='stylesheet'></head><body><h1>Macro View Rotation/it</h1></div>
<div id="mw-content-text" lang="it" dir="ltr" class="mw-content-ltr"><hr/><div class="mw-parser-output"><table class="fcinfobox wikitable ct" width="100%" style="float: right; width: 230px; margin-left: 10px;">
<tr>
<td class="ctTitle">
<h3><span class="mw-headline" id="Rotazione_della_vista"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Text-x-python.png" class="image"><img alt="Text-x-python.png" src="32px-Text-x-python.png" width="32" height="32" srcset="/wiki/images/2/2c/Text-x-python.png 1.5x" /></a> Rotazione della vista</span></h3>
</td></tr>
<tr>
<th class="ctOdd">Descrizione
</th></tr>
<tr>
<td class="ctEven left">Questa macro fornisce una GUI per consentire una rotazione di precisione degli oggetti nella vista.
</td></tr>
<tr>
<th class="ctOdd">Autore
</th></tr>
<tr>
<td class="ctEven"><a href="https://www.freecadweb.org/wiki/index.php?title=User:Joe_Dowsett&amp;action=edit&amp;redlink=1" class="new" title="User:Joe Dowsett (page does not exist)">Joe Dowsett</a>
</td></tr>
<tr>
<th class="ctOdd">Link
</th></tr>
<tr>
<td class="ctEven"><a href="https://www.freecadweb.org/wiki/index.php?title=Macros_recipes/it" title="Macros recipes/it">Esempi di macro</a><br /><a href="https://www.freecadweb.org/wiki/index.php?title=How_to_install_macros/it" title="How to install macros/it">Come installare le Macro</a><br /><a href="https://www.freecadweb.org/wiki/index.php?title=Customize_Toolbars/it" title="Customize Toolbars/it">Personalizzare la barra degli strumenti</a>
</td></tr>
<tr>
<th class="ctOdd">Versione
</th></tr>
<tr>
<td class="ctEven macro-version">1.0
</td></tr>
<tr>
<th class="ctOdd">Data ultima modifica
</th></tr>
<tr>
<td class="ctEven macro-date">2012-01-04
</td></tr>
<tr>
<th class="ctOdd">
</th></tr>
<tr>
<td class="ctToc"><br /><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1"><a href="#Rotazione_della_vista"><span class="tocnumber">1</span> <span class="toctext">Rotazione della vista</span></a></li>
<li class="toclevel-1 tocsection-1"><a href="#Discussion_page"><span class="tocnumber">2</span> <span class="toctext">Discussion page</span></a></li>
</ul>
</div>
</td></tr>
</table>
<p><br />
</p><p>Questa GUI permette di ruotare la vista con maggiore precisione rispetto a quando si utilizza il mouse. La rotazione avviene intorno ad assi fissati rispetto all'utente e non agli oggetti, lo scopo è quello di ruotare gli oggetti approssimativamente attorno al loro centro invece che attorno al centro della vista.
</p><p><br />
Per impostazione predefinita, la GUI viene visualizzata in alto a destra dello schermo, si può cambiare questo comportamento modificando
</p><p><br />
</p>
<div class="floatleft"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Macro_View_Rotation_00.png" class="image" title="Rotate view"><img alt="Rotate view" src="480px-Macro_View_Rotation_00.png" width="480" height="410" srcset="/wiki/images/6/63/Macro_View_Rotation_00.png 1.5x" /></a></div>
<div style="clear:both"></div>
<pre>a = QtGui.QDesktopWidget()
right = a.availableGeometry().width()
self.setGeometry(right-300, 0, 300, 150) </pre>
<p><br />
alla fine della funzione 'initUI'. I primi due argomenti (right-300, 0) stabiliscono come posizione l'angolo in alto a sinistra della finestra - per esperienza questa posizione si adatta bene ad Ubuntu, ma con Vista la finestra risulta troppo in alto per cui è meglio sostituire lo 0 con un valore vicino a 30.
</p><p>I sensi di rotazione sono rappresentati da tre icone. Un file zip contenente queste icone è disponibile <a rel="nofollow" class="external text" href="http://forum.freecadweb.org/download/file.php?id=453">quí</a>, le immagini devono essere collocate nella cartella che contiene le macro. Non esitate a migliorarla!
</p><p><br />
</p>
<pre>import PySide
from PySide import QtGui, QtCore
from pivy import coin
from math import pi
def find_centre():
xmax = xmin = ymax = ymin = zmax = zmin = 0
for obj in App.ActiveDocument.Objects:
if obj.TypeId[:4] == "Mesh":
box = obj.Mesh.BoundBox
elif obj.TypeId[:6] == "Points":
box = obj.Points.BoundBox
elif obj.TypeId[:4] == "Part":
box = obj.Shape.BoundBox
else:
continue
xmax = max(xmax, box.XMax)
xmin = min(xmin, box.XMin)
ymax = max(ymax, box.YMax)
ymin = min(ymin, box.YMin)
zmax = max(zmax, box.ZMax)
zmin = min(zmin, box.ZMin)
centre = FreeCAD.Vector((xmax+xmin)/2.0, (ymax+ymin)/2.0, (zmax+zmin)/2.0)
return centre
class rotate_gui(QtGui.QWidget):
def __init__(self):
super(rotate_gui, self).__init__()
self.initUI()
self.initRotate()
def initUI(self):
self.sld = [0,1,2]
self.tbox = [0,1,2]
path = FreeCAD.ConfigGet("UserAppData")
icon = [0,1,2]
icons = ('right.png', 'up.png', 'out.png')
for i in range(3):
self.sld[i] = QtGui.QSlider(QtCore.Qt.Horizontal, self)
self.sld[i].setFocusPolicy(QtCore.Qt.NoFocus)
self.sld[i].setSingleStep(5)
self.sld[i].setPageStep(15)
self.sld[i].setValue(0)
self.sld[i].setMaximum(180)
self.sld[i].setMinimum(-180)
self.tbox[i] = QtGui.QLineEdit(self)
self.tbox[i].setText("0")
self.tbox[i].setAlignment(QtCore.Qt.AlignRight)
icon[i] = QtGui.QLabel(self)
icon[i].setPixmap(QtGui.QPixmap(path + icons[i]))
self.sld[i].valueChanged[int].connect(self.valueChange)
self.tbox[i].returnPressed.connect(self.valueEntered)
resetButton = QtGui.QPushButton("Reset")
resetButton.clicked.connect(self.reset)
okButton = QtGui.QPushButton("OK")
okButton.clicked.connect(self.close)
cancelButton = QtGui.QPushButton("Cancel")
cancelButton.clicked.connect(self.cancel)
hbox = [0,1,2,3]
vbox = QtGui.QVBoxLayout()
for i in range(3):
hbox[i] = QtGui.QHBoxLayout()
hbox[i].addWidget(icon[i],1, QtCore.Qt.AlignCenter)
hbox[i].addWidget(self.sld[i],4)
hbox[i].addWidget(self.tbox[i],1)
vbox.addLayout(hbox[i])
hbox[3] = QtGui.QHBoxLayout()
hbox[3].addWidget(resetButton,1)
hbox[3].addWidget(okButton,1)
hbox[3].addWidget(cancelButton,1)
vbox.addStretch(1)
vbox.addLayout(hbox[3])
self.setLayout(vbox)
a = QtGui.QDesktopWidget()
right = a.availableGeometry().width()
self.setGeometry(right-300, 0, 300, 150)
self.setWindowTitle('Rotate view...')
self.show()
def initRotate(self):
self.internal = False
self.current = 0
self.cam = Gui.ActiveDocument.ActiveView.getCameraNode()
self.centre = coin.SbVec3f(find_centre())
self.view = self.cam.orientation.getValue()
self.pos = self.cam.position.getValue()
#store a copy of the original view to be restored in the case of user selecting Reset or Cancel
self.original_view = coin.SbRotation(self.view.getValue())
self.original_pos = coin.SbVec3f(self.pos.getValue())
self.config_direction(0)
def reset(self):
#reset the view to the original one
self.cam.orientation = self.original_view
self.cam.position = self.original_pos
self.internal = True
for sld in self.sld:
sld.setValue(0)
self.internal = False
for tbox in self.tbox:
tbox.setText("0")
self.config_direction(0)
def cancel(self):
self.reset()
self.close()
def config_direction(self, i):
#evaluate the vectors corresponding to the three directions for the current view, and assign the i-th one to self.direction
self.view = self.cam.orientation.getValue()
self.view = coin.SbRotation(self.view.getValue())
self.pos = self.cam.position.getValue()
self.pos = coin.SbVec3f(self.pos.getValue())
up = coin.SbVec3f(0,1,0)
self.up = self.view.multVec(up)
out = coin.SbVec3f(0,0,1)
self.out = self.view.multVec(out)
u = self.up.getValue()
o = self.out.getValue()
r = (u[1]*o[2]-u[2]*o[1], u[2]*o[0]-u[0]*o[2], u[0]*o[1]-u[1]*o[0])
self.right = coin.SbVec3f(r)
self.direction = [self.right, self.up, self.out][i]
def check(self, i):
#check if the direction of rotation has changed, if so then set previous slider &amp; textbox to zero, and setup the new direction
if i &lt;&gt; self.current:
self.internal = True
self.sld[self.current].setValue(0)
self.tbox[self.current].setText("0")
self.internal = False
self.current = i
self.config_direction(i)
def rotate(self, value):
#carry out the desired rotation about self.direction
val = value*pi/180.0
rot = coin.SbRotation(self.direction, -val)
nrot = self.view*rot
prot = rot.multVec(self.pos - self.centre) + self.centre
self.cam.orientation = nrot
self.cam.position = prot
def valueChange(self, value):
#respond to the change in value of a slider, update the corresponding text box, check for a direction change then rotate
#if the value was changed internally, ignore event.
if self.internal:
return
sender = self.sender()
for i in range(3):
if sender == self.sld[i]:
break
self.tbox[i].setText(str(value))
self.check(i)
self.rotate(value)
def valueEntered(self):
#respond to a value being entered in a text box, updating the corresponding slider, check for direction change then rotate
sender = self.sender()
for i in range(3):
if sender == self.tbox[i]:
break
value = int(self.tbox[i].text())
self.internal = True
self.sld[i].setValue(value)
self.internal = False
self.check(i)
self.rotate(value)
rotate = rotate_gui() </pre>
<div style="clear:both"></div>
<p><br />
</p>
<h2><span class="mw-headline" id="Discussion_page">Discussion page</span></h2>
<p><a rel="nofollow" class="external text" href="http://forum.freecadweb.org/viewtopic.php?f=3&amp;t=1784&amp;hilit=View+Rotation#p12012">View+Rotation</a>
</p>
</div>
</div>
</div><div class="printfooter">
Online version: "<a dir="ltr" href="https://www.freecadweb.org/wiki/index.php?title=Macro_View_Rotation/it&amp;oldid=239815">http://www.freecadweb.org/wiki/index.php?title=Macro_View_Rotation/it&amp;oldid=239815</a>"</div>
<div id="catlinks" class="catlinks" data-mw="interface"></div><div class="visualClear"></div>
</div>
</div>
<div id="mw-navigation">
<h2>Navigation menu</h2>
</body></html>