Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
984 views
in Technique[技术] by (71.8m points)

stl - How to connect two separate set position values in python vtk?

I put two separate sliders for a stl to move it in X direction and Y direction. For connecting sliders value to position I wrote the code below

        #First STL file
        reader = vtk.vtkSTLReader()
        reader.SetFileName(filename)

        self.transform = vtk.vtkTransform()
        self.transform.Scale(1, 1, 1)

        
        self.transformFilter = vtk.vtkTransformPolyDataFilter()
        self.transformFilter.SetTransform(self.transform)
        self.transformFilter.SetInputConnection(reader.GetOutputPort())
        self.transformFilter.Update()

        mapper = vtk.vtkPolyDataMapper()
        if vtk.VTK_MAJOR_VERSION <= 5:
            mapper.SetInput(self.transformFilter.GetOutput())
        else:
            mapper.SetInputConnection(self.transformFilter.GetOutputPort())


        self.actor = vtk.vtkActor()

        
        self.vtkwidget = QVTKRenderWindowInteractor(self.centralwidget)
        self.gridLayout.addWidget(self.vtkwidget, 0, 0, 1, 1)

        self.ren = vtk.vtkRenderer()
        self.vtkwidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkwidget.GetRenderWindow().GetInteractor()

        self.actor.SetMapper(mapper)

        self.ren.AddActor(self.actor)

        self.iren.Start()
        
        self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setGeometry(QtCore.QRect(70, 360, 291, 22))
        self.horizontalSlider.setMinimum(-100)
        self.horizontalSlider.setMaximum(100)
        self.horizontalSlider.setProperty("value", 0)
        self.horizontalSlider.setSliderPosition(0)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.valueChanged.connect(self.handler)
    

        self.horizontalSlider_2 = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider_2.setGeometry(QtCore.QRect(70, 410, 291, 22))
        self.horizontalSlider_2.setMinimum(-100)
        self.horizontalSlider_2.setMaximum(100)
        self.horizontalSlider_2.setProperty("value", 0)
        self.horizontalSlider_2.setSliderPosition(0)
        self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_2.valueChanged.connect(self.handler2)

        def handler(self, value):
            self.positionx = value
            self.actor.SetPosition(value, 0, 0)
            self.iren.GetRenderWindow().Render()

        def handler2(self, value2):
            self.positiony = value2
            self.actor.SetPosition(0, value2, 0)
            self.iren.GetRenderWindow().Render()

(This is just a part of the program)

but as you see when I change one direction (for example X) all applied directions(Y or Z) before that removed and stl file start to move from the beginning position(0 ,0 ,0).

Is there any way to merge these two sliders output to work together?

Or merge two handlers into one?

question from:https://stackoverflow.com/questions/65845002/how-to-connect-two-separate-set-position-values-in-python-vtk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

self.actor.SetPosition(value, 0, 0) is your problem: you explicitly set y and z coordinates to 0!

you should use current position for y and z:

pos = self.actor.GetPosition()
self.actor.SetPosition(value, pos[1], pos[2])

And similar for second handler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...