在三维空间中,我们可以通过使用SolidUtil下的ClosestPointToFace方法来求出点到曲面的投影。该函数只需要传入曲面(ISubEntity)和空间中的点坐标,就可以返回投影点坐标,空间点到投影点的法向量,以及uv参数。
void CloseestPointToFaceTest()
{
ElementHandle ptEH(20594, ACTIVEMODEL);
ElementHandle surfEH(20620, ACTIVEMODEL);
CurveVectorPtr pCV = ICurvePathQuery::ElementToCurveVector(ptEH);
DPoint3d spacePt;
pCV->at(0)->GetStartPoint(spacePt);
ISolidKernelEntityPtr body;
SolidUtil::Convert::ElementToBody(body, surfEH);
bvector<ISubEntityPtr> faces;
SolidUtil::GetBodyFaces(&faces, *body);
DPoint3d ptOnFace;
DVec3d normal;
DPoint2d param;
SolidUtil::ClosestPointToFace(*faces[0], spacePt, ptOnFace, normal, param);
DSegment3d segment = DSegment3d::From(ptOnFace, ptOnFace);
ICurvePrimitivePtr pCP = ICurvePrimitive::CreateLine(segment);
EditElementHandle eeh;
DraftingElementSchema::ToElement(eeh, *pCP, nullptr, ACTIVEMODEL->Is3d(), *ACTIVEMODEL);
ElementPropertiesSetterPtr setter = ElementPropertiesSetter::Create();
setter->SetColor(4);
setter->SetWeight(7);
setter->Apply(eeh);
eeh.AddToModel();
}