Keyboard shortcut, View toolbar - Wireframe

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Renato Rebelo
Posts: 255
Joined: Mon May 19, 2014 1:14 pm
Location: Vouzela - Portugal

Re: Keyboard shortcut, View toolbar - Wireframe

Post by Renato Rebelo »

triplus wrote:Yes but you don't have to change to English language. Just use the correct terms as seen in menu in your language instead.
Yes, I understood that.
thank you,
Renato
my native language is not English, please excuse me any incorrectness, I apologize for any inconvenience caused, thank you
User avatar
piffpoof
Posts: 346
Joined: Sun Nov 30, 2014 11:00 am
Location: Mare Nostrum
Contact:

Re: Keyboard shortcut, View toolbar - Wireframe

Post by piffpoof »

thanks for finding that and posting it Renato - I had no idea such was the case.

I guess I assumed that at the Python level the menu strings would stay in a base language and a presentation layer would localise for the user on the screen. Apparently not.

To avoid hijacking this thread, I've moved the topic into a new thread in the Python forum Are macros that specify menu options language specific?

Dean
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by triplus »

OK here it goes. This is the "original" code snippet in its most basic form:

Code: Select all

# ==============================

name = "Wireframe"

# ==============================

from PySide import QtGui
mw = FreeCADGui.getMainWindow()

for i in mw.findChildren(QtGui.QAction):
    if i.text() == name.decode("UTF-8"):
        i.activate(QtGui.QAction.Trigger)
It takes menu text and triggers the underlying QAction. Any additional features like supporting language changes are left to the potential user/macro writer to explore if needed.

As for one possible use case that will be hosted on the Wiki and it could/should therefore do a bit more for user convenience. In the other thread @wandererfan provided straightforward solution regarding user changing language and i did a few tests and this seems to work fine:

Code: Select all

# ==============================

nameA = "Wireframe"
nameB = "As is"

# ==============================

from PySide import QtGui
mw = FreeCADGui.getMainWindow()

contextA = "Std_DrawStyle"
nameA =  (QtGui.QApplication.translate(contextA, nameA, None)).encode("UTF-8")

contextB = "Std_DrawStyle"
nameB =  (QtGui.QApplication.translate(contextB, nameB, None)).encode("UTF-8")

for i in mw.findChildren(QtGui.QAction):
    if i.text() == nameA.decode("UTF-8"):
        actionA = i
    elif i.text() == nameB.decode("UTF-8"):
        actionB = i
    else:
        pass

if actionA.isChecked():
    actionB.activate(QtGui.QAction.Trigger)
else:
    actionA.activate(QtGui.QAction.Trigger)
herrdeh
Posts: 437
Joined: Wed Sep 03, 2014 12:15 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by herrdeh »

I have to bring this up again - as it's a most desired feature for me, and I cannot make the macro work.

I'm using a German FreeCAD, but it has got the English words "Shaded, Wireframe, Flat Lines, Points" in the relevant window.

I used the code from the wiki:
# -*- coding: utf-8 -*-
#
#
# Macro: Toggle Draw Style
#
# This macros allows the user to switch between different Drawstyles by clicking on
# the button of a Macro in a toolbar.
#
# initial code: triplus
# macro-isation: piffpoof
#
# This macro switches (or toggles) between 2 selected styles from the Drawstyle menu.
# As written the macro toggles between "WireFrame" and "As is".
# Immediately below this text is a list of the legal values for the Drawstyle menu.
# The first 2 lines of executable code are of the form "DrawstyleA = " followed by
# the 2nd line which is of the form "DrawstyleB = ".
# These 2 lines specify which of the Drawstyle values the macro will toggle between.
# Drawstyle "As is" is the system default and so is specified as the first drawstyle.
# The second line specifies which drawstyle will be toggled to and from.
# Any of the legal values may be used, so if, for example, it is desired to toggle between
# the Shaded and Points drawstyles, then the 2 lines of code would be modified to be:
#
# drawstyleA = "Shaded"
# drawstyleB = "Points"
#
# but remember that the hash signs ('#') are not to be present on the executable lines.
#
###Legal Values for Drawstyle###
#
# "As is"
# "FlatLines"
# "Shaded
# "Wireframe"
# "Points"
#
################################

from PySide import QtGui

# Constant definitions
drawstyleA = "As is"
drawstyleB = "Wireframe"

# code ***********************************************************************************

mw = FreeCADGui.getMainWindow()

for i in mw.findChildren(QtGui.QAction):
if i.text() == drawstyleA.decode("UTF-8"):
actionA = i
elif i.text() == drawstyleB.decode("UTF-8"):
actionB = i
else:
pass

if actionA.isChecked():
actionB.activate(QtGui.QAction.Trigger)
else:
actionA.activate(QtGui.QAction.Trigger)
nevertheless, I always get a
"line 56, in <module> if actionA.isChecked(): <type 'exceptions.NameError'>: name 'actionA' is not defined"
error.

So, do I need to translate any code into English?

BTW: How can I copy the Error message? - It only pops up briefly, and it does not show up in the python console.

Cheers,
Wolf
mario52
Veteran
Posts: 4701
Joined: Wed May 16, 2012 2:13 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by mario52 »

hi
you must adapt your language ex:
replace the English code :

Code: Select all

drawstyleA = "As is"
drawstyleB = "Wireframe"
to your language German

Code: Select all

drawstyleA = "Original"
drawstyleB = "Drahtgitter"
Macro Toggle Drawstyle 00 .png
Macro Toggle Drawstyle 00 .png (30.18 KiB) Viewed 2122 times
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
herrdeh
Posts: 437
Joined: Wed Sep 03, 2014 12:15 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by herrdeh »

Thanks for the quick reply.

I inserted:
drawstyleA = "Schattiert"
drawstyleB = "Drahtgitter"

Changing from "Schattiert" to "Drahtgitter" works - vice versa blocks the GUI for 20sec or so and does nothing.
"Flache Linien" does not work at all - "Flache_Linien" neither.

What can I do?
NB: I always used the ctrl-D command - and there, the German version has the English termini. (-:

NB2: Before I found this macro, I filed a feature request http://www.freecadweb.org/tracker/view.php?id=2794. Maybe people want to contribute...

Cheers,
Wolf
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by triplus »

Hi @herrdeh.

Macro on the Wiki wasn't updated therefore just use this version:

viewtopic.php?f=3&t=14336&start=20#p115666

As it takes translations into consideration and it works as is. Note that in FreeCAD 0.17 macro with such functionality could likely be further simplified as the appropriate commands got named.
herrdeh
Posts: 437
Joined: Wed Sep 03, 2014 12:15 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by herrdeh »

I tried this code:

Code: Select all

# ==============================

nameA = "Drahtgitter"
nameB = "Schattiert"

# ==============================

from PySide import QtGui
mw = FreeCADGui.getMainWindow()

contextA = "Std_DrawStyle"
nameA =  (QtGui.QApplication.translate(contextA, nameA, None)).encode("UTF-8")

contextB = "Std_DrawStyle"
nameB =  (QtGui.QApplication.translate(contextB, nameB, None)).encode("UTF-8")

for i in mw.findChildren(QtGui.QAction):
    if i.text() == nameA.decode("UTF-8"):
        actionA = i
    elif i.text() == nameB.decode("UTF-8"):
        actionB = i
    else:
        pass

if actionA.isChecked():
    actionB.activate(QtGui.QAction.Trigger)
else:
    actionA.activate(QtGui.QAction.Trigger)
Neither back, nor forward are working.
W

System:
OS: Ubuntu 16.10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.6692 (Git)
Build type: None
Branch: master
Hash: 87293fac9bb85c5f5e0ebf563374e27d565116ae
Python version: 2.7.10
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by triplus »

You don't have to translate anything in the latest revision:

Code: Select all

nameA = "Drahtgitter"
nameB = "Schattiert"
Macro will take care of that for you. Just copy/paste and Run it:

viewtopic.php?f=3&t=14336&start=20#p115666
herrdeh
Posts: 437
Joined: Wed Sep 03, 2014 12:15 pm

Re: Keyboard shortcut, View toolbar - Wireframe

Post by herrdeh »

Sorry. Does not work. Neither back nor forward - just blocks the GUI for ~20s.
Post Reply