IVtkTools_ShapePicker 未选取任何内容

问题描述 投票:0回答:1

大家好

我在 Windows 上使用 Qt5、Opencascade 7.7.0 和 VTK 9.2.6。

我想加载并显示一个步骤文件,然后能够单击并选择加载对象的一部分。

我已经对加载器进行了编码,现在我尝试使用 IVtkTools_ShapePicker 对选择器进行编码,但它根本不起作用。 选择器不返回任何内容。

谢谢大家的回复和提示!

致以诚挚的问候。

这是我的一些密码:

我的主要内容:

vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(occSource->GetOutputPort()); // occSource type : IVtkTools_ShapeDataSource
mapper->ScalarVisibilityOff();

vtkNew<vtkActor> actor;
actor->GetProperty()->SetPointSize(5);
actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("SeaGreen").GetData());
actor->GetProperty()->EdgeVisibilityOn();
actor->SetMapper(mapper);

...

vtkNew<StepElementPicker> picker;
picker->Init(renderWindow->GetRenderers()->GetFirstRenderer());
vtkRenderWindowInteractor* renderWindowInteractor = renderWindow->GetInteractor(); // renderWindow type : vtkRenderWindow
renderWindowInteractor->SetInteractorStyle(picker);

我的类StepElementPicker继承自vtkInteractorStyleTrackballCamera包含一个init函数:

void Init(vtkRenderer* renderer)
{
    vtkInteractorStyleTrackballCamera::SetDefaultRenderer(renderer);
}

void OnLeftButtonDown() override
{
    // Get the location of the click (in window coordinates)
    int* pos = this->GetInteractor()->GetEventPosition();

    vtkNew<IVtkTools_ShapePicker> picker;
    picker->SetRenderer(this->GetDefaultRenderer());
    picker->SetTolerance(0.0005);
    picker->SetSelectionMode(SM_Face);

    // Pick from this location
    int nb = picker->Pick(pos[0], pos[1], 0);
    std::cout << "nb : " << nb << std::endl; // <<== always return 0
    
    IVtk_ShapeIdList ids = picker->GetPickedShapesIds(true);
    std::cout << "ids.Size : " << ids.Size() << std::endl; // <<== always return 0
}

在我的函数中,我的选择器 (picker->Pick(pos[0], pos[1], 0)) 始终返回 0,并且 ids.Size() 始终返回 0,所以我无法从我的选择器中获取任何内容。

我尝试过使用 VTK 中的另一个选择器,它可以工作,但我需要使用 Opencascade 中的选择器才能获取所有子形状。

vtkNew<vtkCellPicker> picker;
picker->SetTolerance(0.005f);
// Pick from this location.
int nb = picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
std::cout << "nb : " << nb << std::endl; // return 1 or 0
std::cout << "Cell id : " << picker->GetCellId() << std::endl; // return the VTK ID of the selected cell (or -1 if any)
3d selection vtk opencascade step
1个回答
0
投票

我正在努力解决类似的问题,但我不是添加单个形状,而是分别添加多个 TopoDS_Shape,其中每个单独的演员都添加到 vtk 渲染器中。

我尝试使用IVtkTools_ShapePicker,但它没有选择任何内容。我通过使用标准 vtkPropPicker 使其工作。

这是回调:

virtual void OnLeftButtonDown()
{
    int *clickPos = this->GetInteractor()->GetEventPosition();

    // Pick from this location.
    auto defaultPicker = vtkSmartPointer<vtkPropPicker>::New();
    defaultPicker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());

    double *pos = defaultPicker->GetPickPosition();
    std::cout << "Pick position (world coordinates) is: "
              << pos[0] << " " << pos[1] << " " << pos[2] << std::endl;

    auto actor = defaultPicker->GetActor();

    if (actor)
    {
        std::cout << "Picked actor (addr): " << actor << std::endl;

        IVtkTools_ShapeDataSource *aDataSource = IVtkTools_ShapeObject::GetShapeSource(actor);
        if (aDataSource)
        {
            IVtkOCC_Shape::Handle anOccShape = aDataSource->GetShape();
            if (!anOccShape.IsNull())
            {

                // use the shape from anOccShape->GetShape() to do something...
            }
        }
    }

    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

这就是我添加形状的方法:

IVtkOCC_Shape::Handle aShapeImpl = new IVtkOCC_Shape(shape);
auto dataSource = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
dataSource->SetShape(aShapeImpl);
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(dataSource->GetOutputPort());
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetPickable(true);
actor->SetMapper(mapper);
IVtkTools_ShapeObject::SetShapeSource(dataSource, actor); // <--- MVP: sets a property on the actor and links shape and actor together
renderer->AddActor(actor);
© www.soinside.com 2019 - 2024. All rights reserved.