New feature demo: Measurements of selected elements displayed in bottom bar

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!
User avatar
Saturn
Posts: 5
Joined: Fri Mar 12, 2021 5:19 pm

New feature demo: Measurements of selected elements displayed in bottom bar

Post by Saturn »

Hello, for making measurements in parts and in assemblies, for example the axial distance between cyclindrial surfaces or the distance between parallel planar surfaces, or parallel edges, I created a modification which displays various measurements in the bottom bar, based on the items currently selected.

The following measurements are performed:

1 element:
___Face: Display area
___Edge, Wire: length
___Solid, Compound, Compound Solid: volume
___Vertex: Position (TODO)

2 elements:
___Faces: total area
_____Parallel: distance
_____Non-parallel: angle (TODO)
___Edges: total length
_____Parallel: distance
_____Non-parallel: angle (TODO)
___Vertices: delta XYZ, distance
___Solids: total volume (TODO)

3+ elements:
___Faces: total area
___Edges: total length
___Wires: total length (TODO)
___Vertices: amount
___Solids: total volume

All code is in a new C++ file in src/Gui.

It uses Part::Feature::getShape and Selection().getCompleteSelection(); it implements Gui::SelectionObserver::onSelectionChanged to listen for selection events.

Once the TopoDS_Shape is obtained using Part::Feature::getShape all measurements are done using OpenCascade.

There are commits on the vanilla freecad repo, as well as linkstage3. The code is identical across these 2 repos.
In Application.cpp, the class is instantiated, since it inherits Gui::SelectionObserver, it will automatically be registered as a selection observer and receive events.
In src/Gui/Selection.cpp at the very least calls to getMainWindow()->showMessage have to be removed or commented out, otherwise the measurement message might be overwritten. It takes over the role of some of the code in Selection.cpp, specifically the code which shows info when selecting an element. Probably some of that info should be displayed by this new code as well if this code were ever to be merged.

Some ugly things:
- I had to link Part to the Gui module in order to invoke Part::Feature::getShape, needed to get the TopoDS_Shapes to perform measurements with.
- I had to add ${OCC_INCLUDE_DIR} to CMakeLists.txt in order to use OpenCascade from the Gui module. I think the code has to be moved elsewhere ideally, but I do not know where.
- The class is initialized as the last statement of Application::Application() in Gui/Application.cpp. There must be a better entry point for this code more consistent with the rest of the codebase, but I am unsure where to put it.
- When performing control+A on a large assembly, your PC will emit the magic smoke or at least consume 100% CPU for longer then I am willing to wait. So a mechanism is needed to handle very large selections. For now limiting the selection size would likely fix this problem. Limiting to 10 selections should be a reasonable compromise.
- Some code can be removed from Selection.cpp since it no longer needs to display messages with info about selected elements. This allows a minor restructuring of Selection.cpp which I have not done yet. Right now I just uncommented showMessage() invocations. In linkstage3 branch I also remove some of the code which generates the old selection summary, but also on this repo I have not fullt removed all unused code from Selection.cpp yet.

Future improvements:
- Apart from moving the code elsewhere in the codebase to avoid having to link Part to the Gui module and having to add OpenCascade include directories to the GUi module, angular measurements would be usefull addition.

Other limitations:
- Currently it passes a tolerance value of 0 to OCC for checking if surfaces/axis/edges are parallel. In my tests it sometimes does not detect the paralleism, especially in A2+ assemblies. I am not sure what the ideal tolerance values would be.
- Units are not taken into account, there are no units. With some minor change it could display the correct units.

The goal of this code is to avoid having to use the Part_Measure_X commands and and for me to be able to perform axial measurements on cylindrical faces. That was the prime motivator for this piece of code.

I have made the commit on the 20.2 tag in FreeCAD vanilla and on one of the latest daily commits on the LinkStage3 repo:
The main piece of code is here: https://github.com/Celemation/FreeCAD/b ... ummary.cpp
This commit on the FreeCAD repo contains all the changes needed: https://github.com/Celemation/FreeCAD/c ... 2a5a570a96
On the LinkStage3 repo the changes are done over three commits (1 initial comit with 2 minor revisions): https://github.com/EDG5000/FreeCAD_RT/c ... ts/src/Gui

I'm happy to hear any feedback.
Attachments
vid1.gif
vid1.gif (399.63 KiB) Viewed 1742 times
vid2.gif
vid2.gif (169.17 KiB) Viewed 1742 times
vid3.gif
vid3.gif (186.21 KiB) Viewed 1742 times
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by adrianinsaval »

This probably needs to all go into the Part module rather than Gui
User avatar
Saturn
Posts: 5
Joined: Fri Mar 12, 2021 5:19 pm

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by Saturn »

adrianinsaval wrote: Wed Dec 28, 2022 12:10 pm This probably needs to all go into the Part module rather than Gui
That would avoid having to adjust the target_link_libraries statement as well as the target_include_directory in the CMake file. It seems the overall pattern in the codebase is that the code in the Part module is not exclusively used for Part Workbench features, but also for some shared code. E.g. the Part_Measure_X commands are implemented in Part but made available also in other workbenches.
This feature is globally active and should work with all workbenches, but I also had the feeling that Part maybe the best place.
User avatar
easyw-fc
Veteran
Posts: 3629
Joined: Thu Jul 09, 2015 9:34 am

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by easyw-fc »

Saturn wrote: Wed Dec 28, 2022 8:45 am Hello, for making measurements in parts and in assemblies, for example the axial distance between cyclindrial surfaces or the distance between parallel planar surfaces, or parallel edges, I created a modification which displays various measurements in the bottom bar, based on the items currently selected.

The following measurements are performed:

1 element:
___Face: Display area
___Edge, Wire: length
___Solid, Compound, Compound Solid: volume
___Vertex: Position (TODO)

2 elements:
___Faces: total area
_____Parallel: distance
_____Non-parallel: angle (TODO)
___Edges: total length
_____Parallel: distance
_____Non-parallel: angle (TODO)
___Vertices: delta XYZ, distance
___Solids: total volume (TODO)

3+ elements:
___Faces: total area
___Edges: total length
___Wires: total length (TODO)
___Vertices: amount
___Solids: total volume

All code is in a new C++ file in src/Gui.

It uses Part::Feature::getShape and Selection().getCompleteSelection(); it implements Gui::SelectionObserver::onSelectionChanged to listen for selection events.

Once the TopoDS_Shape is obtained using Part::Feature::getShape all measurements are done using OpenCascade.
...
I'm happy to hear any feedback.
Would it ne possibile to add radius and approx radius of bsplines?
All these measurements features would be a great add for FC interface
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by adrianinsaval »

I don't quite understand how you'll get a radius for bsplines but +1 for displaying circle radius and maybe also ellipse major and minor radius
User avatar
easyw-fc
Veteran
Posts: 3629
Joined: Thu Jul 09, 2015 9:34 am

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by easyw-fc »

adrianinsaval wrote: Wed Dec 28, 2022 1:41 pm I don't quite understand how you'll get a radius for bsplines but +1 for displaying circle radius and maybe also ellipse major and minor radius
I use it in my Caliper tool to get the approximation of a radius of a shape with bspline, and often happens with STEP models

https://forum.freecadweb.org/viewtopic. ... 76#p257476
some tips and code at the thread
If you have a B-spline curve you can "bi-arc" it and then you see directly the radius of each arc. Ideally the radius of all arcs is equal.

Code: Select all

bspline=Gui.SelectiongetSelectionEx()[0].SubObjects[0].curve
arcs=bspline.toBiArcs(0.0001)
arcs[0].Radius
arcs[1].Radius
Caliper @line 1391

Code: Select all

                    arcs=nwshp.Edges[0].Curve.toBiArcs(tobiarc_tol)
                    sayerr('BSpline approximated to Arc')
                    #print (arcs[0].Radius)
                    #circ=arcs[0].Curve
                    bbxCenter=arcs[0].Center
                    rads = []
                    for a in arcs:
                        rads.append(a.Radius)
                    r0 = rads[0]; delta_tol = False
                    for r in rads:
                        if abs(r0 - r) > tobiarc_tol:
                            delta_tol = True
                    if delta_tol:
                        sayerr("more than an Arc in Bspline approximation!")
                        sayerr("only the first is dimensioned!")
                        for a in arcs:
                            say('Radius: '+str(a.Radius))
                    #print (arcs[0].Center)
                    #arcs[1].Radius
Image
dan-miel
Posts: 391
Joined: Thu Sep 13, 2018 12:29 am
Location: Spokane WA. USA

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by dan-miel »

Saturn wrote: Wed Dec 28, 2022 8:45 am Other limitations:
- Currently it passes a tolerance value of 0 to OCC for checking if surfaces/axis/edges are parallel. In my tests it sometimes does not detect the paralleism, especially in A2+ assemblies. I am not sure what the ideal tolerance values would be.
- Units are not taken into account, there are no units. With some minor change it could display the correct units.
Hi Saturn
This looks very similar to the Quick Measure addon so if you are working with A2+, want to report the measurements in the units that are in the settings or want to paste the results to the clipboard, Quick measure is still available.
Dan
User avatar
Saturn
Posts: 5
Joined: Fri Mar 12, 2021 5:19 pm

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by Saturn »

dan-miel wrote: Thu Dec 29, 2022 5:24 pm (...)This looks very similar to the Quick Measure addon(...)
Thanks for pointing out, I was not aware of that addon. I gave it a try and it is great, it takes the kinds of measurements I wanted to do, and also does a point from plane distance measurement, it's perfect.
User avatar
easyw-fc
Veteran
Posts: 3629
Joined: Thu Jul 09, 2015 9:34 am

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by easyw-fc »

Saturn wrote: Fri Dec 30, 2022 4:50 am Thanks for pointing out, I was not aware of that addon. I gave it a try and it is great, it takes the kinds of measurements I wanted to do, and also does a point from plane distance measurement, it's perfect.
so are you abandoning your C++ project?
Are we missing the option to have basic measurement information tips in FreeCAD without having to install an add-on?
Most of CAD sw just have these...
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: New feature demo: Measurements of selected elements displayed in bottom bar

Post by onekk »

Why reinvent a ton of times the wheel.

There are around some measurement tools, why not trying to coordinate efforts to modify the actual tool to make one most useful?

I wonder why this "reasonable" approach is not taken in account.

you want to modify a software, try to guess what is already present in source code and submit a PR, maybe prior discussing the thing, in this case a post here with this title:

"Possible way to improve measurement tool" or

"Anyone interested in collaborating in improving measurement tool" or maybe:

"I'm volunteering on improving existing measurement tool"

Or similar, probably offering help will get help from some developers that most probably has something in his "to do list" and could share some hints and maybe some wip code to improve or develop further.

Or I have too much expectations.

@wmayer or @yorik or maybe @Kunda1 I think will be very happy to supply some suggestions.

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/
Post Reply