BIM workbench statusbar Units Chooser for other benches

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

BIM workbench statusbar Units Chooser for other benches

Post by mLucas »

I'm sure this is a simple thing to solve for those who know how to program and know a little about the Freecad algorithm.
But I'm not a programmer, my knowledge comes from an intermediate Linux user. I tried to use the old trick of cutting, copying and pasting parts of code, but without success.
Captura de tela de 2022-09-15 08-56-48.png
Captura de tela de 2022-09-15 08-56-48.png (8.64 KiB) Viewed 1898 times
If anyone can help any help is welcome. I believe that everyone who noticed this option in the BIM Workbench thought the same.
Why don't you make this the default of the program?

BimStatusBar.py

https://github.com/yorikvanhavre/BIM_Wo ... atusBar.py
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

I believe these are the parts of the code that creates this option in the statusbar

Post by mLucas »

Code: Select all

import os
import FreeCAD

# dummy function for the QT translator
def QT_TRANSLATE_NOOP(ctx,txt): 
    return txt
# use latest available translate function
if hasattr(FreeCAD,"Qt"):
    translate = FreeCAD.Qt.translate
else:
    from DraftTools import translate

# Language path for InitGui.py

def getLanguagePath():

    return os.path.join(os.path.dirname(__file__),"translations")

# Status bar buttons

def setStatusIcons(show=True):

    "shows or hides the BIM icons in the status bar"

    import FreeCADGui
    from PySide import QtCore,QtGui

    unitsList = [translate("BIM","Millimeters"),
                 translate("BIM","Centimeters"),
                 translate("BIM","Meters"),
                 translate("BIM","Inches"),
                 translate("BIM","Feet"),
                 translate("BIM","Architectural"),
                    ]

    def setUnit(action):

        # set the label of the unit button
        utext = action.text().replace("&","")
        unit = [0,4,1,3,7,5][unitsList.index(utext)]
        FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units").SetInt("UserSchema",unit)
        if hasattr(FreeCAD.Units,"setSchema"):
            FreeCAD.Units.setSchema(unit)
        action.parent().parent().parent().setText(utext)

        # change the unit of the nudge button
        nudge = action.parent().parent().parent().parent().nudge
        nudgeactions = nudge.menu().actions()
        if unit in [2,3,5,7]:
            nudgelabels = nudgeLabelsI
        else:
            nudgelabels = nudgeLabelsM
        for i in range(len(nudgelabels)):
            nudgeactions[i].setText(nudgelabels[i])
        try:
            nudge.setText(FreeCAD.Units.Quantity(nudge.text().replace("&","")).UserString)
        except:
            pass

    def toggleContextMenu(point):

        FreeCADGui.BimToggleMenu = QtGui.QMenu()
        for t in ["Report view","Python console","Selection view","Combo View"]:
            a = QtGui.QAction(t)
            FreeCADGui.BimToggleMenu.addAction(a)
        pos = FreeCADGui.getMainWindow().cursor().pos()
        FreeCADGui.BimToggleMenu.triggered.connect(toggleSaveSettings)
        FreeCADGui.BimToggleMenu.popup(pos)

    def toggleSaveSettings(action):

        t = action.text()
        FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM").SetBool("toggle"+t.replace(" ",""),action.isChecked())
        if hasattr(FreeCADGui,"BimToggleMenu"):
            del FreeCADGui.BimToggleMenu

    # main code
    mw = FreeCADGui.getMainWindow()
    if mw:
        st = mw.statusBar()
        statuswidget = st.findChild(QtGui.QToolBar,"BIMStatusWidget")
        if show:
            if statuswidget:
                statuswidget.show()
            else:
                statuswidget = QtGui.QToolBar()
                statuswidget.setObjectName("BIMStatusWidget")

                # units chooser
                statuswidget.unitsList = unitsList
                unitLabel = QtGui.QPushButton("Unit")
                unitLabel.setObjectName("UnitLabel")
                unitLabel.setFlat(True)
                unit = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units").GetInt("UserSchema",0)
                menu = QtGui.QMenu(unitLabel)
                gUnits = QtGui.QActionGroup(menu)
                for u in unitsList:
                    a = QtGui.QAction(gUnits)
                    a.setText(u)
                    menu.addAction(a)
                unitLabel.setMenu(menu)
                gUnits.triggered.connect(setUnit)
                unitLabel.setText([unitsList[0],unitsList[2],unitsList[3],unitsList[3],unitsList[1],unitsList[5],unitsList[0],unitsList[4]][unit])
                unitLabel.setToolTip(translate("BIM","The preferred unit you are currently working with. You can still use any other unit anywhere in FreeCAD"))
                statuswidget.addWidget(unitLabel)
                statuswidget.unitLabel = unitLabel
                st.addPermanentWidget(statuswidget)

        else:
            if statuswidget:
                statuswidget.hide()
            else:
                # when switching workbenches, the toolbar sometimes "jumps"
                # out of the status bar to any other dock area...
                statuswidget = mw.findChild(QtGui.QToolBar,"BIMStatusWidget")
                if statuswidget:
                    statuswidget.hide()
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BIM workbench statusbar Units Chooser for other benches

Post by onekk »

mLucas wrote: Thu Sep 29, 2022 5:49 pm ...
I see no problem, as you can set units in the preferences.

IMHO UI is already crowded and adding another button or icons, will not ease things, simply you already have the ability to change units in FC:

Edit >> Preferences >> General >> Units will suffice.

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/
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by mLucas »

onekk wrote: Fri Sep 30, 2022 6:53 am ...
I see no problem, as you can set units in the preferences.

IMHO UI is already crowded and adding another button or icons, will not ease things, simply you already have the ability to change units in FC:

Edit >> Preferences >> General >> Units will suffice.
I don't know how you work, but for me using 5 clicks for a repetitive task is totally counterproductive.
Every time we modify our desktop, create shortcuts, macros and our own toolbars, it's because we seek to optimize time and improve our performance to get our work done in the shortest possible time. All of FreeCad is designed to be used in this way.

When you say I can go to Edit >> Preferences >> General >> Units, I believe that really is enough for you.

However, my reality is different from yours and certainly my need is the same as Yorik's, because he decided to put this function in the status bar of his workbench.

More than 200 people have accessed the topic, and although you are the only one to respond, it means that there is some interest in the topic.

I may not have the solutions at the moment, but as soon as I have time I will study and learn to create my own solutions.

It is the machines that must adapt to us and make all the tiring efforts, not the other way around.


Maybe I made my post wrong, what I'm looking for is any tips or help to find the fastest way to solve this problem.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BIM workbench statusbar Units Chooser for other benches

Post by onekk »

Probably for most people here there is simply "no problem to solve".

Most of us use FC to design in a rather "fixed" measurement system.

Probably other people has the need to change "measurement system" when dealing with different drawings.

So I think that this feature is handy to have only for a subset of users.

For same reason probably this is not at "top of the list" of needed things.

Plus modifying UI is not so trivial and few people know "the best way" to do such type of thing for the whole FC UI and not for a limited WB.

Probably something similar could be added when you create a new "FCStd" file, to trigger a panel where you are asked what "measurement system" to use for the new file, and this panel could maybe be optional, so people that have the need to use or create different "measurement units" will have a quick way to change "measurement units".

I don't know the implication of a change of "measurement units" done in the middle of editing a file, and what happens to expressions or other "lenghts" expressed in a "measurement unit" when you change it in the edited file.

Probably asking @yorik why this mechanism has not been integrated in the UI as a standard will be a legitimate question. :D

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/
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by mLucas »

onekk wrote: Sun Oct 02, 2022 12:42 pm For same reason probably this is not at "top of the list" of needed things.
For this reason this topic was created in "Python scripting and macros" ;)
onekk wrote: Sun Oct 02, 2022 12:42 pm I don't know the implication of a change of "measurement units" done in the middle of editing a file, and what happens to expressions or other "lenghts" expressed in a "measurement unit" when you change it in the edited file.
In several tutorials I watched teach that it is possible to change at any time, and also change only one option in the properties.
onekk wrote: Sun Oct 02, 2022 12:42 pm Probably asking @yorik why this mechanism has not been integrated in the UI as a standard will be a legitimate question. :D
I hope @yorik has some live time to contribute some tips on how to solve this.
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by Syres »

mLucas wrote: Thu Sep 29, 2022 5:49 pm If anyone can help any help is welcome.
I've added it to the Draft Status Bar (I personally use the Autoload Draft Wb in Preferences) so it's always available. The changes can be seen here https://github.com/FreeCAD/FreeCAD/comp ... D:patch-57 and the full file is attached.
Autoload.png
Autoload.png (49.68 KiB) Viewed 1523 times
DraftStatusBar.png
DraftStatusBar.png (7.13 KiB) Viewed 1523 times

Tested using:

Code: Select all

OS: Linux Mint 20.3 (X-Cinnamon/cinnamon)
Word size of FreeCAD: 64-bit
Version: 0.21.30509 (Git)
Build type: Release
Branch: master
Hash: 8a77ea8c989550566246d37916affe7c4c0dc351
Python 3.8.10, Qt 5.12.8, Coin 4.0.0, Vtk 7.1.1, OCC 7.3.0
Locale: English/United Kingdom (en_GB)
Installed mods: 
  * ThreadProfile 1.83.0
  * A2plus 0.4.59
  * BIM 2021.12.0
  * Curves 0.5.8
  * ExtremeProDark 1.0.4
  * Plot 2022.4.17
  * fasteners 0.4.6
  * sheetmetal 0.2.57
Attachments
init_draft_statusbar.py
(17.55 KiB) Downloaded 28 times
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by mLucas »

Syres wrote: Mon Oct 03, 2022 11:51 am I've added it to the Draft Status Bar (I personally use the Autoload Draft Wb in Preferences) so it's always available. The changes can be seen here https://github.com/FreeCAD/FreeCAD/comp ... D:patch-57 and the full file is attached.
Thank you very much for your contribution.
Sorry for the delay in responding, I just got some free time today to try to reproduce what you did, but unfortunately I found out that the Draft workbench has been removed.

At first I could see the option in the statusbar, but after restarting Freecad it started to generate an error it didn't show anymore.

Development of the Drawing Workbench stopped in FreeCAD 0.16, and it became obsolete in FreeCAD 0.17, when the TechDraw Workbench was introduced.
The most important thing is that now I have better defined which part of the code I should use, and now I need to study the process of implementing it.
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by Syres »

The Draft Wb has definitely not been removed otherwise I wouldn't have suggested it. The Drawing Wb is completely different functionality.
mLucas
Posts: 10
Joined: Fri Nov 05, 2021 2:14 am

Re: BIM workbench statusbar Units Chooser for other benches

Post by mLucas »

Syres wrote: Wed Oct 05, 2022 9:41 pm The Draft Wb has definitely not been removed otherwise I wouldn't have suggested it. The Drawing Wb is completely different functionality.
I removed all old settings and downloaded the last package, unpacked the appimage, replaced the file and packaged it again.
Now, everything works as expected.

:D
Captura de tela de 2022-10-08 01-31-06.png
Captura de tela de 2022-10-08 01-31-06.png (12.68 KiB) Viewed 1269 times
Post Reply