Esempio n. 1
0
        /// <summary>
        /// 左键释放时获取选择的对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_interactor_LeftButtonReleaseEvt(vtkObject sender, vtkObjectEventArgs e)
        {
            if (m_pickTarget == ePickTarget.None)
            {
                return;
            }

            //if (m_iren.GetControlKey() != 0)
            //    m_pickBoolean = ePickBollean.Add;
            //else if (m_iren.GetShiftKey() != 0)
            //    m_pickBoolean = ePickBollean.Sub;

            m_iren.GetEventPosition(ref endx, ref endy);
            #region 点选对象
            if (m_pickMode == ePickMode.DotPickMode) //如果是点选模式
            {
                vtkCellPicker cellPicker = new vtkCellPicker();
                cellPicker.SetTolerance(0.005);
                if (0 == cellPicker.Pick(endx, endy, 0, m_renderer))
                {
                    return;  //什么都没有选到就返回
                }
                FEMActor actor = cellPicker.GetActor() as FEMActor;
                if (actor == null)
                {
                    return;
                }
                if (m_pickTarget == ePickTarget.Actor)
                {                          //如果选择对象为
                    actor.IsSelected = true;
                    m_RWCtrl.Invalidate(); //貌似必须要用这个函数,update 不行
                    OnObjectsSelected(sender, e);
                }
                else if (m_pickTarget == ePickTarget.Cell)
                {
                    int cellId = cellPicker.GetCellId();
                    actor.AddCellId(cellId, m_pickBoolean);
                    OnObjectsSelected(sender, e);
                    //if (cellId > 0)
                    //    Console.WriteLine("dotselect cellid:{0}", cellId);
                }
                else if (m_pickTarget == ePickTarget.Point)
                {
                    int pointId = cellPicker.GetPointId();
                    if (pointId != -1)
                    {
                        actor.AddPointId(pointId, m_pickBoolean);
                        OnObjectsSelected(sender, e);
                        //   Console.WriteLine("dotselect pointid:{0}", pointId);
                    }
                }
            }
            #endregion
            else if (m_pickMode == ePickMode.RectPickMode) //如果是框选
            {
                vtkAreaPicker areaPicker = new vtkAreaPicker();
                if (0 == areaPicker.AreaPick(startx, starty, endx, endy, m_renderer))
                { //如果什么也没选到就返回
                    //this.impactViewForm.ClearAllSelectCells();
                    return;
                }
                vtkProp3DCollection selectedActors = areaPicker.GetProp3Ds();
                selectedActors.InitTraversal();
                FEMActor selectedActor;
                while ((selectedActor = selectedActors.GetNextProp3D() as FEMActor) != null)
                {
                    vtkSelection     selection     = new vtkSelection();
                    vtkSelectionNode selectionNode = new vtkSelectionNode();
                    selection.AddNode(selectionNode);

                    vtkIdFilter idFilter = new vtkIdFilter();
                    idFilter.FieldDataOn();
                    idFilter.SetInput(selectedActor.GetMapper().GetInput());
                    idFilter.Update();

                    vtkExtractSelection frustum = new vtkExtractSelection();
                    frustum.SetInput(0, idFilter.GetOutput());
                    frustum.SetInput(1, selection);

                    selectionNode.Initialize();
                    selectionNode.SetContentType((int)vtkSelectionNode.SelectionContent.FRUSTUM);

                    if (m_pickTarget == ePickTarget.Cell)//如果选择的目标是cell
                    {
                        selectionNode.SetFieldType((int)vtkSelectionNode.SelectionField.CELL);
                    }
                    else if (m_pickTarget == ePickTarget.Point) //如果选择的目标是point
                    {
                        selectionNode.SetFieldType((int)vtkSelectionNode.SelectionField.POINT);
                    }

                    vtkPoints      points         = areaPicker.GetClipPoints();
                    vtkDoubleArray frustumcorners = new vtkDoubleArray();
                    frustumcorners.SetNumberOfComponents(4);
                    frustumcorners.SetNumberOfTuples(8);
                    for (int i = 0; i < 8; ++i)
                    {
                        double[] point = points.GetPoint(i);
                        frustumcorners.SetTuple4(i, point[0], point[1], point[2], 0.0);
                    }
                    selectionNode.SetSelectionList(frustumcorners);

                    frustum.Update();
                    vtkUnstructuredGrid grid = new vtkUnstructuredGrid();
                    grid = vtkUnstructuredGrid.SafeDownCast(frustum.GetOutput());
                    vtkIdTypeArray ids = null;
                    if (m_pickTarget == ePickTarget.Cell)
                    {
                        ids = vtkIdTypeArray.SafeDownCast(grid.GetCellData().GetArray("vtkOriginalCellIds"));
                        if (ids == null)
                        {
                            continue;
                        }
                        selectedActor.AddCellId(ids, m_pickBoolean);
                        OnObjectsSelected(sender, e);
                    }
                    else if (m_pickTarget == ePickTarget.Point)
                    {
                        ids = vtkIdTypeArray.SafeDownCast(grid.GetPointData().GetArray("vtkOriginalPointIds"));
                        if (ids == null)
                        {
                            continue;
                        }
                        selectedActor.AddPointId(ids, m_pickBoolean);
                        OnObjectsSelected(sender, e);
                    }
                }
            }
        }
Esempio n. 2
0
        private void SelectAreaClick(vtkObject sender, vtkObjectEventArgs e)
        {
            int[]         clickPos = Inter.GetEventPosition();
            vtkAreaPicker picker   = vtkAreaPicker.New();

            picker.AreaPick(clickPos[0], clickPos[1], clickPos[0] + 100, clickPos[1] + 100, Viewport);

            if (picker.GetActor() != null)
            {
                vtkPlanes          Boundary = picker.GetFrustum();
                vtkExtractGeometry Box      = vtkExtractGeometry.New();
                Box.SetImplicitFunction(Boundary);
                Box.SetInput(picker.GetActor().GetMapper().GetInput());

                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(Box.GetOutputPort());
                glyphFilter.Update();

                vtkPolyData         selected = glyphFilter.GetOutput();
                vtkPoints           points   = vtkPoints.New();
                vtkUnstructuredGrid grid     = vtkUnstructuredGrid.New();
                for (int i = 0; i < selected.GetNumberOfPoints(); i++)
                {
                    points.InsertNextPoint(selected.GetPoint(i)[0], selected.GetPoint(i)[1], selected.GetPoint(i)[2]);
                }
                grid.SetPoints(points);
                vtkSphereSource sphere = vtkSphereSource.New();
                sphere.SetPhiResolution(6);
                sphere.SetThetaResolution(6);
                sphere.SetRadius(0.1);
                vtkGlyph3D glyph3D = vtkGlyph3D.New();
                glyph3D.SetInput(grid);
                glyph3D.SetSourceConnection(sphere.GetOutputPort());

                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyph3D.GetOutputPort());

                //double[] P = new double[3];
                //bool selected = false;
                //vtkPoints points = Faces.GetPoints();
                //double[] ClickedPoint = PointPicker.GetActor().GetMapper().GetInput().GetPoint(PointPicker.GetPointId());
                //for (int i = 0; i < points.GetNumberOfPoints(); i++)
                //{
                //    if (Math.Abs(points.GetPoint(i)[0] - ClickedPoint[0]) < 1e-6 &&
                //        Math.Abs(points.GetPoint(i)[1] - ClickedPoint[1]) < 1e-6 &&
                //        Math.Abs(points.GetPoint(i)[2] - ClickedPoint[2]) < 1e-6)
                //    {
                //        selected = true;
                //        P = points.GetPoint(i);
                //        break;
                //    }
                //}
                //
                //if (selected == true)
                //{
                //    SelectionPoints.InsertNextPoint(P[0], P[1], P[2]);
                //
                //    SelectionGlyph = vtkGlyph3D.New();
                //    SelectionGlyph.SetInput(SelectionPolyData);
                //    SelectionGlyph.SetSourceConnection(SelectionSphere.GetOutputPort());
                //    SelectionMapper.SetInputConnection(SelectionGlyph.GetOutputPort());
                //
                //    // Refresh Viewport
                //    Refresh();
                //}
            }
        }