admin 发表于 2023-7-15 12:08:47

如何获取选择点映射到视图空间中?源码分享



如何获取选择点映射到视图空间中?源码分享

//------------------------------------------------------------------------------
// Select Screen Position and map to Modeling View Plane
//------------------------------------------------------------------------------
bool GtacPmi::SelectPmiPosition(NXOpen::Point3d &pos)
{
    NXOpen::View* myView = NULL;
    NXOpen::Point3d myCursor(0, 0, 0);

    NXOpen::Selection::DialogResponse myResponse = m_ui->SelectionManager()->SelectScreenPosition("Select Screen Origin", &myView, &myCursor);
    if (myResponse == NXOpen::Selection::DialogResponsePick)
    {
      // obtain the current default annotation plane where PMIs are placed on
      NXOpen::Xform *xform1;
      xform1 = m_wpart->Annotations()->GetDefaultAnnotationPlane(NXOpen::Annotations::PmiDefaultPlaneModelView);
      NXOpen::Matrix3x3 vmxDefaultPlane = xform1->Orientation();
      NXOpen::Point3d ptDefaultPlane = xform1->Origin();
      NXOpen::Vector3d vecDefaultPlane(vmxDefaultPlane.Zx, vmxDefaultPlane.Zy, vmxDefaultPlane.Zz);

      // create the ray 'into the screen' at the selected cursor position
      NXOpen::Matrix3x3 vmx = myView->Matrix();
      NXOpen::Point3d p1(myCursor.X - vmx.Zx * 1000, myCursor.Y - vmx.Zy * 1000, myCursor.Z - vmx.Zz * 1000);
      NXOpen::Point3d p2(myCursor.X + vmx.Zx * 1000, myCursor.Y + vmx.Zy * 1000, myCursor.Z + vmx.Zz * 1000);
      NXOpen::Line *lineRay = m_wpart->Curves()->CreateLine(p1, p2);
      lineRay->SetName("Ray");

      // create the plane from the view to intersect with the ray
      NXOpen::Plane *planeView = m_wpart->Planes()->CreatePlane(ptDefaultPlane, vecDefaultPlane, NXOpen::SmartObject::UpdateOptionWithinModeling);
      planeView->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);
      planeView->SetName("ViewPlane");

      // now create the intersection point
      NXOpen::Point *pntInterSect = m_wpart->Points()->CreatePoint(planeView, lineRay, NULL, NULL, NXOpen::SmartObject::UpdateOptionWithinModeling);
      pntInterSect->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);
      pntInterSect->SetColor(10);
      pntInterSect->SetName("Intersection");

      pos = pntInterSect->Coordinates();
      print("\nPMI Position:", pos);

      // clean up - comment to see intermediate objects
      NXOpen::Session::UndoMarkId markDel = m_session->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "Delete");
      int nErrs1 = m_session->UpdateManager()->AddToDeleteList(pntInterSect);
      int nErrs2 = m_session->UpdateManager()->AddToDeleteList(planeView);
      int nErrs3 = m_session->UpdateManager()->AddToDeleteList(lineRay);
      int nErrs4 = m_session->UpdateManager()->DoUpdate(markDel);
      m_session->DeleteUndoMark(markDel, NULL);

      return true;
    }

    return false;
}

页: [1]
查看完整版本: 如何获取选择点映射到视图空间中?源码分享