hi
freedman wrote: ↑Mon Nov 18, 2019 4:49 am
is there an easy way to switch the camera to the opposite side, many times the sketches are based on the other side and I can install a button to flip the view.
normally (0,0,-1) (and (0,0,1)) but no result
here little code to play with camera and collection snippets picked in forum and links for study
Code: Select all
# -*- coding: utf-8 -*-
#from __future__ import unicode_literals
#
__title__ = "Titre"
__author__ = "Auteur"
__url__ = "http://www.freecadweb.org/index-fr.html"
__Wiki__ = "http://www.freecadweb.org/wiki/index.php?title="
__version__ = "00.00"
__date__ = "04/05/2019"
import PySide2
from PySide2 import QtGui ,QtCore, QtWidgets
from PySide2.QtGui import *
from PySide2.QtCore import *
global camera
from pivy import coin
Gui = FreeCADGui
App = FreeCAD
def errorDialog(msg):
diag = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Critical,u"Error Message",msg )
# diag.setWindowModality(QtCore.Qt.ApplicationModal) # la fonction a ete desactivee pour favoriser "WindowStaysOnTopHint"
diag.setWindowFlags(PySide2.QtCore.Qt.WindowStaysOnTopHint) # PySide2 # cette fonction met la fenetre en avant
diag.exec_()
class Ui_MainWindow(object):
def __init__(self ):
self.window = MainWindow
#self.path = FreeCAD.ConfigGet("AppHomePath")
#self.path = FreeCAD.ConfigGet("UserAppData")
#self.path = "your path"
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Macro")# macro path
self.path = param.GetString("MacroPath","") + "/" # macro path
self.path = self.path.replace("\\","/")
#print("Path for the icons : " , self.path)
def setupUi(self, MainWindow):
self.window = MainWindow
# section horizontalSlider
self.horizontalSlider = PySide2.QtWidgets.QSlider(MainWindow) # create horizontalSlider
self.horizontalSlider.setGeometry(QtCore.QRect(10, 10, 400, 16)) # coordinates position
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal) # orientation Horizontal
# self.horizontalSlider.setMinimum(-10000.0) # minimum value
self.horizontalSlider.setMaximum(100000.0) # maximum value
# self.horizontalSlider.setSingleStep(0.1) #
# self.horizontalSlider.setTickInterval(0.1) #
self.horizontalSlider.setInvertedAppearance(False) # displacement rigth to left or left to rigth value "True" or "False"
self.horizontalSlider.setObjectName("horizontalSlider") # object Name
self.horizontalSlider.valueChanged.connect(self.on_horizontal_slider) # connect on "def on_horizontal_slider:" for execute action
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", __title__+" ("+__version__+", "+__date__+")"))
MainWindow.setWindowFlags(PySide2.QtCore.Qt.WindowStaysOnTopHint) # PySide2 cette fonction met la fenetre en avant
#______________________________________________________________________________________
def on_horizontal_slider(self, value):
global camera
value = value
# value = -value
# value = value/100#
# value = -(value/1000)#
print(value)
##### for first Subobject selected
#sel = Gui.Selection.getSelectionEx()[0].SubObjects[0].BoundBox.Center # sub element BoundBox coordinates
#print(sel)
##### for first subobject selected
##### for first Object selected
sel = FreeCADGui.Selection.getSelection()[0]
selb= sel.Shape.BoundBox.Center
#print(selb)
##### for first Object selected
# #http://web.mit.edu/ivlib/www/iv/cameras.html
# PerspectiveCamera. A PerspectiveCamera shows you the scene and applies foreshortening to give you a better impression of distance.
#
# PerspectiveCamera {
# viewportMapping ADJUST_CAMERA
# position 0 0 1
# orientation 0 0 1 0
# aspectRatio 1
# nearDistance 1
# farDistance 10
# focalDistance 5
# heightAngle 0.785398
# }
# Gui.activeDocument().activeView().setCamera(' OrthographicCamera { \n viewportMapping ADJUST_CAMERA \n position 0 0 1 \n orientation 0 0 1 0 \n aspectRatio 1 \n nearDistance 1 \n farDistance 100 \n focalDistance 5 \n height ' + str(value) + ' }')
camera=Gui.ActiveDocument.ActiveView.getCameraNode()
# print(camera)
# focalDistance=camera.focalDistance.getValue()
# Position = 0, 0, 0 # XYZ
Position = selb[0], selb[1], selb[2] # XYZ
Orientation = 0, 0, -1, 0
AspectRatio = 1
NearDistance = 1
FarDistance = 10
FocalDistance = 5
Height = value
# HeightAngle = value
#https://forum.freecadweb.org/viewtopic.php?f=22&t=10157
camera.position.setValue(Position)
camera.orientation.setValue(Orientation)
camera.aspectRatio.setValue(AspectRatio)
camera.nearDistance.setValue(NearDistance)
camera.farDistance.setValue(FarDistance)
camera.focalDistance.setValue(FocalDistance)
camera.height.setValue(Height)
# camera.heightAngle.setValue(HeightAngle)
# print(position)
def on_PB_Quit_clicked(self):
self.window.hide() # hide the window and close the macro
print("Quit ")
#print(Gui.activeDocument().activeView().getCamera())
doc = FreeCAD.ActiveDocument
if doc == None:
doc = FreeCAD.newDocument()
MainWindow = QtWidgets.QWidget() #PySide2
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
#https://forum.freecadweb.org/viewtopic.php?f=22&t=5576&start=10#p96188
#https://forum.freecadweb.org/viewtopic.php?t=26215#p207714
#how to change the distance of the camera?import Part
##http://web.mit.edu/ivlib/www/iv/cameras.html
##https://forum.freecadweb.org/viewtopic.php?f=22&t=6745
#
#"""
#
# PerspectiveCamera. A PerspectiveCamera shows you the scene and applies foreshortening to give you a better impression of distance.
#
# PerspectiveCamera {
# viewportMapping ADJUST_CAMERA
# position 0 0 1
# orientation 0 0 1 0
# aspectRatio 1
# nearDistance 1
# farDistance 10
# focalDistance 5
# heightAngle 0.785398
# }
#
# OrthographicCamera. A OrthographicCamera shows you the scene but doesn't apply any foreshortening.
#
# OrthographicCamera {
# viewportMapping ADJUST_CAMERA
# position 0 0 1
# orientation 0 0 1 0
# aspectRatio 1
# nearDistance 1
# farDistance 10
# focalDistance 5
# height 2
# }
#"""
#
#Gui.activeDocument().activeView().setCamera('#Inventor V2.1 ascii \n OrthographicCamera { \n viewportMapping ADJUST_CAMERA \n position 0 0 87 \n orientation 0 0 1 0 \n nearDistance 37 \n farDistance 137 \n aspectRatio 1 \n focalDistance 87 \n height 119 }')
#
##https://www.forum.freecadweb.org/viewtopic.php?f=22&t=26391
##How to apply Near and Far clipping from a script?
##https://forum.freecadweb.org/viewtopic.php?f=22&t=40938
##Set camera view to sketch center
##https://forum.freecadweb.org/viewtopic.php?t=24788
##Sketch enter Edit Mode by Python Script
#
#try:
# view=Gui.ActiveDocument.ActiveView
# view.fitAll()
#
# from pivy import coin
# sel0 = Gui.Selection.getSelection()
## Gui.Selection.addSelection(App.ActiveDocument.getObject(sel0[0].Name))
# b = sel0[0].Shape.BoundBox
#
# box=coin.SbBox3f()
# box.setBounds(b.XMin,b.YMin,b.ZMin,b.XMax,b.YMax,b.ZMax)
#
# cam=view.getCameraNode()
# cam.nearDistance.getValue()
# cam.farDistance.getValue() # far distance is quite high
# cam.focalDistance.getValue()
#
# cam.viewBoundingBox(box,1,1) # focuses on the cylinder
#
# Gui.ActiveDocument.setEdit(Gui.Selection.getSelectionEx()[0].Object) # Edit the Object
#except Exception:
# print("Error")
#from pivy import coin
#camera=Gui.activeDocument().activeView().getCameraNode()
#print(camera)
#camera.height.setValue(min(150, 150))
###https://forum.freecadweb.org/viewtopic.php?t=37841#p321922
##cam = Gui.ActiveDocument.ActiveView.getCamera() #store camera settings to 'cam'
##Gui.ActiveDocument.ActiveView.setCamera(cam) #restore previously stored camera settings
##https://forum.freecadweb.org/viewtopic.php?f=22&t=5576&start=10
##How can you zoom in and out of a document via scripting?
##https://forum.freecadweb.org/viewtopic.php?t=26215#p207714
##how to change the distance of the camera?import Part
#from FreeCAD import Base
#from pivy import coin
#Gui.activeDocument().activeView().setCameraType("Perspective")
#camera = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
#
#center=coin.SbVec3f(0,0,0)
#orient=camera.orientation.getValue()
#viewdir=coin.SbVec3f(0,0,-1)
#viewdir=orient.multVec(viewdir)
#fd=100
#camera.focalDistance.setValue(fd)
##focalDistance=camera.focalDistance.getValue()
#position=center-viewdir*fd
#camera.position.setValue(position)
sorry
mario