[Solved] Python object with OriginGroupExtension

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

I just love this fantastic piece of software and this community!
wall experiment.gif
wall experiment.gif (345.5 KiB) Viewed 1108 times
Here is a very raw (i'm a real noob) implementation of what i wanted.
The wall can move every children accordingly, and they are always represented "in place".
Long road before this could become usable, but... why not working on it?

Code: Select all

class MyWall(object):
    def __init__(self, obj=None):
        #self.Type = 'wall'

        if obj:
            obj.Proxy = self
            self.Object = obj
            self.attach(obj)

    def __getstate__(self):
        return

    def __setstate__(self,_state):
        return

    def execute(self,obj):
        print("running Execute")

        "builds the wall shape"
        import Part

        if hasattr(obj,"Length") and hasattr(obj,"Width") and hasattr(obj,"Height"):
            wall_shape = Part.makeBox(obj.Length,obj.Width, obj.Height)
            obj.Shape = wall_shape
            children_shapes= []

            for child in obj.Group:
                if hasattr(child, "Shape"):
                    children_shapes.append(child.Shape)
            children_shapes.append(wall_shape)
            obj.Shape = Part.Compound(children_shapes)


    def attach(self,obj):
        print("running obj attach method")
        obj.addExtension('App::OriginGroupExtensionPython', None)
        obj.Origin = FreeCAD.ActiveDocument.addObject('App::Origin','Origin')


    def onDocumentRestored(self, obj):
        self.Object = obj

class ViewProviderMyWall(object):
    def __init__(self,vobj=None):
        if vobj:
            vobj.Proxy = self
            self.attach(vobj)
        else:
            self.ViewObject = None

    def updateData(self, obj, prop):
        return

    def attach(self,vobj):
        print("running vobj attach method")
        vobj.addExtension('Gui::ViewProviderOriginGroupExtensionPython', None)
        self.ViewObject = vobj

    def __getstate__(self):
        return None

    def __setstate__(self, _state):
        return None


    def getDefaultDisplayMode(self):
        """
        Return the name of the default display mode. It must be defined in getDisplayModes.
        """
        return "Flat Lines"

obj = App.ActiveDocument.addObject('Part::FeaturePython', 'Wall', MyWall(), ViewProviderMyWall(), True)

obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '2 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'
obj.addProperty('App::PropertyAngle', 'FirstAngle', 'Dimensions', 'Box height').FirstAngle = '90 deg'
obj.addProperty('App::PropertyAngle', 'LastAngle', 'Dimensions', 'Box height').LastAngle = '90 deg'
follow my experiments on BIM modelling for architecture design
paullee
Veteran
Posts: 5118
Joined: Wed May 04, 2016 3:58 pm

Re: Python object with OriginGroupExtension

Post by paullee »

Looks good ! :)

Wound the base object of Window / Wall move together ?

Thanks.
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

realthunder wrote: Sun Apr 05, 2020 8:34 am Note that you want to write your own Python class only if you decide to write your own workbench, because others will have to install your workbench before they can open document containing your object. You can probably achieve what you want using existing objects.
I'm very happy with the solution you provided, but what did you mean when you wrote I can achieve the result with existing objects? Did you have something particular in mind?
No hurry to respond :)
follow my experiments on BIM modelling for architecture design
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: Python object with OriginGroupExtension

Post by realthunder »

carlopav wrote: Mon Apr 06, 2020 10:19 am I'm very happy with the solution you provided, but what did you mean when you wrote I can achieve the result with existing objects? Did you have something particular in mind?
Not anything in particular. It really depends on your intention. If it is just a one time thing, or the custom logic is simple, then maybe writing a macro is enough. You can use script to install (ViewProvider)OriginGroupExtension to an existing object. But if you intend this to be a reusable parametric object with some not so simple logic, then writing your own class is the right way.
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Python object with OriginGroupExtension

Post by vocx »

realthunder wrote: Mon Apr 06, 2020 9:48 pm ...But if you intend this to be a reusable parametric object with some not so simple logic, then writing your own class is the right way.
Carlo's idea is not a one time use. This could be the future Arch Wall object in the Arch Workbench. There are several objects in this workbench which act like groups (Arch BuildingPart, for example), so moving all objects inside of it makes sense. The issue is understanding the best way of doing it. If we introduce this new object in Arch, we also need to verify that it works correctly with other commands in the workbench in a way that is intuitive for the user.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

realthunder wrote: Mon Apr 06, 2020 9:48 pm But if you intend this to be a reusable parametric object with some not so simple logic, then writing your own class is the right way.
Yes, @vocx is right! My final goal is that :twisted:: rewriting several Arch tools to behave in an "assembly way". In the end FreeCAD is a lot focused on mechanical modelling (Parts and way to Assemble them), and architectural modelling can also be considered a way of assembly component, just the components are often simpler and they don't need a solver (at least not at the beginning). This could also make the Arch module more FreeCADish i think.
But I'm far from thinking that I'm somehow near it. I will still have to bother you all a lot I think :)
If we introduce this new object in Arch, we also need to verify that it works correctly with other commands in the workbench in a way that is intuitive for the user.
Yes I know, this will be a difficult thing, but I already started to bring an initial support for App::Part to the most basic tools.

EDIT: Reference to the topic disscussing further implementation of the new wall object
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

realthunder wrote: Mon Apr 06, 2020 9:48 pm ping
Here I am asking again for help.
I went quite far with the prototype of Bim Assembly concept, and it works nicely, but trying to reduce the objects in the document (BIM models can be really huge), I need to have just one object for a wall.
At the moment it's like this:


Wall container
-> Origin
-> WallSegment object
-> Window
-> Wall additions
-> Wall subtractions


And I want to get rid of the WallSegment object.
Is there a way for the Wall container object to have at the same time the Display Mode set to Group and to for example Flat Lines?
So it can show its own shape and also the children objects at the same time?
Hope I was clear enough :)
thanks for any hint you could give :)
follow my experiments on BIM modelling for architecture design
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: Python object with OriginGroupExtension

Post by realthunder »

carlopav wrote: Thu Apr 30, 2020 7:46 pm Is there a way for the Wall container object to have at the same time the Display Mode set to Group and to for example Flat Lines?
So it can show its own shape and also the children objects at the same time?
This is a bit more complicated, specifically, to get the selection work. The attached script builds on my last MyGroup demo. It adds a new display mode 'ShapeGroup' which groups the 'Group' mode and the 'Flat Lines' mode. The trick to get selection right is done in two important functions 'getElementPicked()' and 'getDetailPath()'.
Attachments
group.py
(3.43 KiB) Downloaded 57 times
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

realthunder wrote: Fri May 01, 2020 4:11 am The attached script builds on my last MyGroup demo.
My god, this is just coooool! that's exactly what we need :D
thanks so much :)
I'll try to adapt it to our wall prototype NOW!
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

realthunder wrote: Fri May 01, 2020 4:11 am
carlopav wrote: Thu Apr 30, 2020 7:46 pm Is there a way for the Wall container object to have at the same time the Display Mode set to Group and to for example Flat Lines?
So it can show its own shape and also the children objects at the same time?
This is a bit more complicated, specifically, to get the selection work. The attached script builds on my last MyGroup demo. It adds a new display mode 'ShapeGroup' which groups the 'Group' mode and the 'Flat Lines' mode. The trick to get selection right is done in two important functions 'getElementPicked()' and 'getDetailPath()'.
Sorry, but I can't help asking another question.
Is it possible to override existing DisplayModes with the new "ShapeGroup"?
The goal is to have the ShapeGroup behaviour with FlatLines, Wireframe or Shaded, where Grouped objects are shown, and the main object gets the visual style of the selected DisplayMode. (So when an user use Std_DrawStyle the objects are correctly formatted)
follow my experiments on BIM modelling for architecture design
Post Reply