Lattice2 mininum enclosing Bounding Box by python script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

Lattice2 mininum enclosing Bounding Box by python script

Post by rluq »

I am using Lattice2 workbench for getting the oriented bounding box, OBB (or mininum enclosing rectangle) (see https://forum.freecadweb.org/viewtopic.php?t=47408) for different parts.

So far I have used the tools via the FreeCAD GUI.
1. make Attached Placement.
2. Attach it so that axes are parallel to the sides (Align O-X-Y mode, for example)
3. select the shape, the placement, click "Bounding box"

But now I would like to replicate the process by python scripting, and I would also like it to work for any other part automatically. Do you know any python example snippet?

If this is not possible, is there any other way I can calculate the minimum enclosing rectangle for any geometry?

(Find attached the example I used to test Lattice2 tools through the FreeCAD GUI)
Attachments
test_bb.png
test_bb.png (5.99 KiB) Viewed 1461 times
lattice_test.FCStd
(10.48 KiB) Downloaded 37 times
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by DeepSOIC »

rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by rluq »

Thank you DeepSOIC for your answer. However, I am not able to find in the repository the python statemens where the created Placement Object is modified depending on the field values from the GUI (References, Attachment mode, etc.).

I mean, I know the statements required for creating a new default Placement Object:

Code: Select all

f = lattice2AttachablePlacement.makeAttachablePlacement(name='Placment')
Once the object is created, it can be modified with the different parameters I mentioned before: References, Attachment mode (Align O-X-Y, Inertial CS, ...), etc., by means of the GUI, and after that, the Placement Object is recomputed.

The statemens where these parameters are collected and processed are those I cannot find.

The intention is I can indicate for example the values of the References by code and not by the GUI.
Attachments
lattice2_params.png
lattice2_params.png (16.55 KiB) Viewed 1349 times
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by DeepSOIC »

It's in FreeCAD. The important stuff is in Part.AttachEngine, but just the constructors of App.Placement should get you covered in most cases.
rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by rluq »

I'm sorry but I don't see how to do it.

I have tried the following:

1) Create new Placement object (by python code):

Code: Select all

plac = lattice2AttachablePlacement.makeAttachablePlacement(name='Placment')
2) Edit Placement object parameters (manually by GUI):
Reference1: Wire001:Vertex5
Reference2: Wire001:Edge5
Reference3: Wire001:Vertex6
Attachment mode: Align O-X-Y
3) Create bounding box (by python code):

Code: Select all

bbox = lattice2BoundBox.makeBoundBox(name= 'BoundBox')
bbox.ShapeLink = App.ActiveDocument.Wire001
bbox.Label = 'BoundBox of Wire001'
bbox.CompoundTraversal = 'Use as a whole'
bbox.Precision = False
bbox.OrientMode = 'use OrientLink'
bbox.OrientLink = App.ActiveDocument.Placment
lattice2Executer.executeFeature(bbox)
But I have not been able to find how to code the second step.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by DeepSOIC »

ah, yeah, these commands aren't logged to console. Here's a workaround:

After you have created an attached placement, go to properties, select MapMode, and click three-dots button there. You'll get attachment dialog. Do something, click OK, and a bunch of commands will appear in py console.

The one Lattice2 activates does not log, because it's actually a different version of the same dialog, which happens to not have this logging. ("..." button invokes a c++ version of the dialog, Lattice2 invokes a python version of the dialog, they are similar but independent)
rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

Re: Lattice2 mininum enclosing Bounding Box by python script

Post by rluq »

Ah okey I see. And is there any way to invoke these commands from python script?
Post Reply