Repository: FreeCAD/FreeCAD
File: src/Mod/Sketcher/App/SketchObject.cpp (C++)

Code before:
            GeomAPI_ProjectPointOnSurf proj(P, gPlane);
            P = proj.NearestPoint();
            Base::Vector3d p(P.X(), P.Y(), P.Z());
            invPlm.multVec(p, p);

            auto* point = new Part::GeomPoint(p);
            GeometryFacade::setConstruction(point, true);
            geos.emplace_back(point);
        };

        try {
            TopoDS_Shape refSubShape;

            // Handles LCS ,resolve to actual datum object
            const App::DocumentObject* resolvedObj = Obj;
            if (Obj->isDerivedFrom<App::LocalCoordinateSystem>() && !SubElement.empty()) {
                auto* lcs = static_cast<const App::LocalCoordinateSystem*>(Obj);
                // get the datum element by name
                App::DatumElement* datum = lcs->getDatumElement(SubElement.c_str());
                if (datum) {
                    resolvedObj = datum;
                }
            }

            if (resolvedObj->isDerivedFrom<Part::Datum>()) {
                auto* datum = static_cast<const Part::Datum*>(resolvedObj);
                refSubShape = datum->getShape();
            }
            else if (Obj->isDerivedFrom<Part::Feature>()) {
                auto* refObj = static_cast<const Part::Feature*>(Obj);
                const Part::TopoShape& refShape = refObj->Shape.getShape();
                refSubShape = refShape.getSubShape(SubElement.c_str());
            }
            else if (resolvedObj->isDerivedFrom<App::Plane>()) {
                auto* pl = static_cast<const App::Plane*>(resolvedObj);
                Base::Vector3d base = pl->getBasePoint();
                Base::Vector3d normal = pl->getDirection();
                gp_Pln plane(gp_Pnt(base.x, base.y, base.z), gp_Dir(normal.x, normal.y, normal.z));
                BRepBuilderAPI_MakeFace fBuilder(plane);
                if (!fBuilder.IsDone())
                    throw Base::RuntimeError(
                        "Sketcher: addExternal(): Failed to build face from App::Plane");

                TopoDS_Face f = TopoDS::Face(fBuilder.Shape());
                refSubShape = f;
            }
            else if (resolvedObj->isDerivedFrom<Part::DatumLine>()) {
                auto* line = static_cast<const Part::DatumLine*>(resolvedObj);
                Base::Placement plm = line->Placement.getValue();
                Base::Vector3d base = plm.getPosition();
                Base::Vector3d dir = line->getDirection();

Review comment (suggestion):
Through the whole PR you could replace 
```
     if (resolvedObj->isDerivedFrom<Part::Datum>()) {
         auto* datum = static_cast<const Part::Datum*>(resolvedObj);
```
                
 by 

```
     if (auto* datum = freecad_cast<const Part::Datum*>(resolvedObj)) {
```
Just a slightly easier on the eye syntax

Code after:
            GeomAPI_ProjectPointOnSurf proj(P, gPlane);
            P = proj.NearestPoint();
            Base::Vector3d p(P.X(), P.Y(), P.Z());
            invPlm.multVec(p, p);

            auto* point = new Part::GeomPoint(p);
            GeometryFacade::setConstruction(point, true);
            geos.emplace_back(point);
        };

        try {
            TopoDS_Shape refSubShape;

            // Handles LCS ,resolve to actual datum object
            const App::DocumentObject* resolvedObj = Obj;
            if (Obj->isDerivedFrom<App::LocalCoordinateSystem>() && !SubElement.empty()) {
                auto* lcs = static_cast<const App::LocalCoordinateSystem*>(Obj);
                // get the datum element by name
                App::DatumElement* datum = lcs->getDatumElement(SubElement.c_str());
                if (datum) {
                    resolvedObj = datum;
                }
            }

            if (auto* datum = freecad_cast<const Part::Datum*>(resolvedObj)) {
                refSubShape = datum->getShape();
            }
            else if (auto* refObj = freecad_cast<const Part::Feature*>(resolvedObj)) {
                const Part::TopoShape& refShape = refObj->Shape.getShape();
                refSubShape = refShape.getSubShape(SubElement.c_str());
            }
            else if (auto* pl = freecad_cast<const App::Plane*>(resolvedObj)) {
                Base::Vector3d base = pl->getBasePoint();
                Base::Vector3d normal = pl->getDirection();
                gp_Pln plane(gp_Pnt(base.x, base.y, base.z), gp_Dir(normal.x, normal.y, normal.z));
                BRepBuilderAPI_MakeFace fBuilder(plane);
                if (!fBuilder.IsDone())
                    throw Base::RuntimeError(
                        "Sketcher: addExternal(): Failed to build face from App::Plane");

                TopoDS_Face f = TopoDS::Face(fBuilder.Shape());
                refSubShape = f;
            }
            else if (auto* line = freecad_cast<const Part::DatumLine*>(resolvedObj)) {
                Base::Placement plm = line->Placement.getValue();
                Base::Vector3d base = plm.getPosition();
                Base::Vector3d dir = line->getDirection();
                gp_Lin l(gp_Pnt(base.x, base.y, base.z), gp_Dir(dir.x, dir.y, dir.z));
                BRepBuilderAPI_MakeEdge eBuilder(l);
                if (!eBuilder.IsDone()) {
                    throw Base::RuntimeError(