Part.show()

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
onekk
Veteran
Posts: 6199
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Part.show()

Post by onekk »

Maybe it is not the place.

But how do you think, if:

Code: Select all

obj = Part.show(obj_solid, "Obj_Name")
will put the created Document Object in obj

it will make easy to interact with document objects, without having to create them explicitly using:

Code: Select all

obj = DOC.addObject("Part::Feature", "obj_Name")
obj.shape = obj_solid
Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Part.show()

Post by TheMarkster »

I don't see why not. It currently returns Py::None, but that could easily be the python object. Here is the current code:

Code: Select all

    Py::Object show(const Py::Tuple& args)
    {
        PyObject *pcObj = 0;
        char *name = "Shape";
        if (!PyArg_ParseTuple(args.ptr(), "O!|s", &(TopoShapePy::Type), &pcObj, &name))
            throw Py::Exception();

        App::Document *pcDoc = App::GetApplication().getActiveDocument();
        if (!pcDoc)
            pcDoc = App::GetApplication().newDocument();
        TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObj);
        Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name));
        // copy the data
        pcFeature->Shape.setValue(pShape->getTopoShapePtr()->getShape());
        pcDoc->recompute();

        return Py::None();
    }
I think if the last line was:

Code: Select all

return pcFeature->getPyObject();
it would work, but I need to compile and test it.

I think also to modernize the first line should be:

Code: Select all

PyObject *pcObj = nullptr;
Edit: this seems to work:

Code: Select all

return Py::Object(pcFeature->getPyObject());
Edit #2. I guess this should be:

Code: Select all

return Py::asObject(pcFeature->getPyObject());
to avoid a memory leak.
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Part.show()

Post by TheMarkster »

User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Part.show()

Post by uwestoehr »

It is in master now.
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Part.show()

Post by TheMarkster »

Of course, Part.show() still returns None for users with 0.19 or an earlier build of 0.20. So, you would still need to check against None if the code might be run on an earlier version:

Code: Select all

box = Part.show(Part.makeBox(10,10,10))
if not box:
    box = App.ActiveDocument.ActiveObject
Post Reply