| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | """Provides GUI tools to do various operations with groups. |
| | |
| | For example, add objects to groups, select objects inside groups, |
| | set the automatic group in which to create objects, and add objects |
| | to the construction group. |
| | """ |
| | |
| | |
| | |
| |
|
| | |
| | |
| | from PySide import QtCore |
| | from PySide import QtWidgets |
| | from PySide.QtCore import QT_TRANSLATE_NOOP |
| |
|
| | import FreeCAD as App |
| | import FreeCADGui as Gui |
| | import Draft_rc |
| | from draftguitools import gui_base |
| | from draftmake import make_layer |
| | from draftutils import groups |
| | from draftutils import params |
| | from draftutils import utils |
| | from draftutils.translate import translate |
| |
|
| |
|
| | |
| | True if Draft_rc.__name__ else False |
| |
|
| |
|
| | class AddToGroup(gui_base.GuiCommandNeedsSelection): |
| | """GuiCommand for the Draft_AddToGroup tool. |
| | |
| | It adds selected objects to a group, or removes them from any group. |
| | |
| | It inherits `GuiCommandNeedsSelection` to only be available |
| | when there is a document and a selection. |
| | See this class for more information. |
| | """ |
| |
|
| | def __init__(self): |
| | super().__init__(name="Draft_AddToGroup") |
| |
|
| | def GetResources(self): |
| | """Set icon, menu and tooltip.""" |
| | return { |
| | "Pixmap": "Draft_AddToGroup", |
| | "MenuText": QT_TRANSLATE_NOOP("Draft_AddToGroup", "Add to Group"), |
| | "ToolTip": QT_TRANSLATE_NOOP( |
| | "Draft_AddToGroup", |
| | "Adds selected objects to a group, or removes them from any group", |
| | ), |
| | } |
| |
|
| | def Activated(self): |
| | """Execute when the command is called.""" |
| | super().Activated() |
| |
|
| | if not hasattr(Gui, "draftToolBar"): |
| | return |
| |
|
| | self.ui = Gui.draftToolBar |
| | objs = [obj for obj in self.doc.Objects if groups.is_group(obj)] |
| | objs.sort(key=lambda obj: obj.Label) |
| | self.objects = [None] + [None] + objs |
| | self.labels = ( |
| | [translate("draft", "Ungroup")] |
| | + ["---"] |
| | + [obj.Label for obj in objs] |
| | + ["---"] |
| | + [translate("draft", "Add to New Group")] |
| | ) |
| | self.icons = ( |
| | [self.ui.getIcon(":/icons/list-remove.svg")] |
| | + [None] |
| | + [obj.ViewObject.Icon for obj in objs] |
| | + [None] |
| | + [self.ui.getIcon(":/icons/list-add.svg")] |
| | ) |
| |
|
| | |
| | |
| | |
| | self.ui.sourceCmd = self |
| | self.ui.popupMenu(self.labels, self.icons) |
| |
|
| | def proceed(self, option): |
| | """Place the selected objects in the chosen group or ungroup them. |
| | Parameters |
| | ---------- |
| | option: str |
| | The passed string. |
| | """ |
| | self.ui.sourceCmd = None |
| |
|
| | if option == self.labels[0]: |
| | |
| | self.doc.openTransaction(translate("draft", "Ungroup")) |
| | for obj in Gui.Selection.getSelection(): |
| | try: |
| | groups.ungroup(obj) |
| | except Exception: |
| | pass |
| | self.doc.commitTransaction() |
| | self.doc.recompute() |
| | return |
| |
|
| | if option == self.labels[-1]: |
| | |
| | grp = AddNamedGroup() |
| | grp.Activated() |
| | return |
| |
|
| | |
| | self.doc.openTransaction(translate("draft", "Add to Group")) |
| | i = self.labels.index(option) |
| | grp = self.objects[i] |
| | moveToGroup(grp) |
| | self.doc.commitTransaction() |
| | self.doc.recompute() |
| |
|
| |
|
| | Gui.addCommand("Draft_AddToGroup", AddToGroup()) |
| |
|
| |
|
| | def moveToGroup(group): |
| | """ |
| | Place the selected objects in the chosen group. |
| | """ |
| |
|
| | for obj in Gui.Selection.getSelection(): |
| | try: |
| | |
| | obj.ViewObject.Visibility = group.ViewObject.Visibility |
| | group.addObject(obj) |
| |
|
| | except Exception: |
| | pass |
| |
|
| |
|
| | class SelectGroup(gui_base.GuiCommandNeedsSelection): |
| | """GuiCommand for the Draft_SelectGroup tool.""" |
| |
|
| | def __init__(self): |
| | super().__init__(name="Draft_SelectGroup") |
| |
|
| | def GetResources(self): |
| | """Set icon, menu and tooltip.""" |
| | return { |
| | "Pixmap": "Draft_SelectGroup", |
| | "MenuText": QT_TRANSLATE_NOOP("Draft_SelectGroup", "Select Group"), |
| | "ToolTip": QT_TRANSLATE_NOOP( |
| | "Draft_SelectGroup", |
| | "Selects the contents of selected groups. For selected non-group objects, the contents of the group they are in are selected.", |
| | ), |
| | } |
| |
|
| | def Activated(self): |
| | """Execute when the command is called.""" |
| | super().Activated() |
| |
|
| | sel = Gui.Selection.getSelection() |
| | subs = [] |
| | for obj in sel: |
| | if groups.is_group(obj): |
| | for sub in obj.Group: |
| | subs.append(sub) |
| | else: |
| | for parent in obj.InList: |
| | if groups.is_group(parent): |
| | for sub in parent.Group: |
| | subs.append(sub) |
| |
|
| | |
| | Gui.Selection.clearSelection() |
| |
|
| | |
| | for sub in subs: |
| | Gui.Selection.addSelection(sub) |
| |
|
| | |
| | if not Gui.Selection.hasSelection(): |
| | msg = translate( |
| | "draft", "No new selection. Select non-empty groups or objects inside groups." |
| | ) |
| | App.Console.PrintMessage(msg + "\n") |
| |
|
| |
|
| | Gui.addCommand("Draft_SelectGroup", SelectGroup()) |
| |
|
| |
|
| | class SetAutoGroup(gui_base.GuiCommandSimplest): |
| | """GuiCommand for the Draft_AutoGroup tool.""" |
| |
|
| | def __init__(self): |
| | super().__init__(name="Draft_AutoGroup") |
| |
|
| | def GetResources(self): |
| | """Set icon, menu and tooltip.""" |
| | return { |
| | "Pixmap": "Draft_AutoGroup", |
| | "MenuText": QT_TRANSLATE_NOOP("Draft_AutoGroup", "Auto-Group"), |
| | "ToolTip": QT_TRANSLATE_NOOP( |
| | "Draft_AutoGroup", "Adds new Draft and BIM objects to the selected layer or group" |
| | ), |
| | } |
| |
|
| | def Activated(self): |
| | """Execute when the command is called. |
| | |
| | It calls the `setAutogroup` method of the `DraftToolBar` class |
| | installed inside the global `Gui` namespace. |
| | """ |
| | super().Activated() |
| |
|
| | if not hasattr(Gui, "draftToolBar"): |
| | return |
| |
|
| | |
| | |
| | |
| | self.ui = Gui.draftToolBar |
| | sel = Gui.Selection.getSelection() |
| | if len(sel) == 1: |
| | if utils.get_type(sel[0]) == "Layer" or ( |
| | params.get_param("AutogroupAddGroups") and groups.is_group(sel[0]) |
| | ): |
| | self.ui.setAutoGroup(sel[0].Name) |
| | return |
| |
|
| | |
| | |
| | if params.get_param("AutogroupAddGroups"): |
| | grps = [obj for obj in self.doc.Objects if groups.is_group(obj)] |
| | grps.sort(key=lambda obj: obj.Label) |
| | else: |
| | grps = [] |
| | lyrs = [obj for obj in self.doc.Objects if utils.get_type(obj) == "Layer"] |
| | lyrs.sort(key=lambda obj: obj.Label) |
| | self.names = ( |
| | [None] + [None] + [obj.Name for obj in grps] + [None] + [obj.Name for obj in lyrs] |
| | ) |
| | self.labels = ( |
| | [translate("draft", "None")] |
| | + ["---"] |
| | + [obj.Label for obj in grps] |
| | + ["---"] |
| | + [obj.Label for obj in lyrs] |
| | + ["---"] |
| | + [translate("draft", "New Layer")] |
| | ) |
| | self.icons = ( |
| | [self.ui.getIcon(":/icons/button_invalid.svg")] |
| | + [None] |
| | + [obj.ViewObject.Icon for obj in grps] |
| | + [None] |
| | + [obj.ViewObject.Icon for obj in lyrs] |
| | + [None] |
| | + [self.ui.getIcon(":/icons/Draft_NewLayer.svg")] |
| | ) |
| |
|
| | |
| | |
| | self.ui.sourceCmd = self |
| | pos = self.ui.autoGroupButton.mapToGlobal( |
| | QtCore.QPoint(0, self.ui.autoGroupButton.geometry().height()) |
| | ) |
| | self.ui.popupMenu(self.labels, self.icons, pos) |
| |
|
| | def proceed(self, option): |
| | """Set the defined autogroup, or create a new layer. |
| | |
| | Parameters |
| | ---------- |
| | option: str |
| | The passed string. |
| | """ |
| | self.ui.sourceCmd = None |
| |
|
| | if option == self.labels[0]: |
| | |
| | self.ui.setAutoGroup(None) |
| | return |
| |
|
| | if option == self.labels[-1]: |
| | |
| | txt, ok = QtWidgets.QInputDialog.getText( |
| | None, |
| | translate("draft", "New Layer"), |
| | translate("draft", "Layer name"), |
| | text=translate("draft", "Layer", "Object label"), |
| | ) |
| | if not ok: |
| | return |
| | if not txt: |
| | return |
| | self.doc.openTransaction(translate("draft", "New layer")) |
| | lyr = make_layer.make_layer( |
| | name=txt, |
| | line_color=None, |
| | shape_color=None, |
| | line_width=None, |
| | draw_style=None, |
| | transparency=None, |
| | ) |
| | self.doc.commitTransaction() |
| | self.doc.recompute() |
| | self.ui.setAutoGroup(lyr.Name) |
| | |
| | return |
| |
|
| | |
| | i = self.labels.index(option) |
| | self.ui.setAutoGroup(self.names[i]) |
| |
|
| |
|
| | Gui.addCommand("Draft_AutoGroup", SetAutoGroup()) |
| |
|
| |
|
| | class AddToConstruction(gui_base.GuiCommandNeedsSelection): |
| | """GuiCommand for the Draft_AddConstruction tool. |
| | |
| | It adds the selected objects to the construction group |
| | defined in the `DraftToolBar` class which is initialized |
| | in the `Gui` namespace when the workbench loads. |
| | |
| | It adds a construction group if it doesn't exist. |
| | |
| | Added objects are also given the visual properties of the construction |
| | group. |
| | """ |
| |
|
| | def __init__(self): |
| | super().__init__(name="Draft_AddConstruction") |
| |
|
| | def GetResources(self): |
| | """Set icon, menu and tooltip.""" |
| | return { |
| | "Pixmap": "Draft_AddConstruction", |
| | "MenuText": QT_TRANSLATE_NOOP("Draft_AddConstruction", "Add to Construction Group"), |
| | "ToolTip": QT_TRANSLATE_NOOP( |
| | "Draft_AddConstruction", |
| | "Adds the selected objects to the construction group,\nand changes their appearance to the construction style.\nThe construction group is created if it does not exist.", |
| | ), |
| | } |
| |
|
| | def Activated(self): |
| | """Execute when the command is called.""" |
| | super().Activated() |
| |
|
| | if not hasattr(Gui, "draftToolBar"): |
| | return |
| |
|
| | self.doc.openTransaction(translate("draft", "Add to Construction Group")) |
| | col = params.get_param("constructioncolor") | 0x000000FF |
| |
|
| | |
| | grp = self.doc.getObject("Draft_Construction") |
| | if not grp: |
| | grp = self.doc.addObject("App::DocumentObjectGroup", "Draft_Construction") |
| | grp.Label = params.get_param("constructiongroupname") |
| |
|
| | for obj in Gui.Selection.getSelection(): |
| | grp.addObject(obj) |
| | |
| | vobj = obj.ViewObject |
| | if "TextColor" in vobj.PropertiesList: |
| | vobj.TextColor = col |
| | if "PointColor" in vobj.PropertiesList: |
| | vobj.PointColor = col |
| | if "LineColor" in vobj.PropertiesList: |
| | vobj.LineColor = col |
| | if "ShapeColor" in vobj.PropertiesList: |
| | vobj.ShapeColor = col |
| | if hasattr(vobj, "Transparency"): |
| | vobj.Transparency = 80 |
| |
|
| | self.doc.commitTransaction() |
| | self.doc.recompute() |
| |
|
| |
|
| | Draft_AddConstruction = AddToConstruction |
| | Gui.addCommand("Draft_AddConstruction", AddToConstruction()) |
| |
|
| |
|
| | class AddNamedGroup(gui_base.GuiCommandSimplest): |
| | """GuiCommand for the Draft_AddNamedGroup tool. |
| | |
| | It adds a new named group. |
| | """ |
| |
|
| | def __init__(self): |
| | super().__init__(name="Draft_AddNamedGroup") |
| |
|
| | def GetResources(self): |
| | """Set icon, menu and tooltip.""" |
| | return { |
| | "Pixmap": "Draft_AddNamedGroup", |
| | "MenuText": QT_TRANSLATE_NOOP("Draft_AddNamedGroup", "New Named Group"), |
| | "ToolTip": QT_TRANSLATE_NOOP("Draft_AddNamedGroup", "Adds a group with a given name"), |
| | } |
| |
|
| | def Activated(self): |
| | super().Activated() |
| |
|
| | txt, ok = QtWidgets.QInputDialog.getText( |
| | None, |
| | translate("draft", "New Group"), |
| | translate("draft", "Group name"), |
| | text=translate("draft", "Group", "Object label"), |
| | ) |
| | if not ok: |
| | return |
| | if not txt: |
| | return |
| | self.doc.openTransaction(translate("draft", "New named group")) |
| | grp = self.doc.addObject("App::DocumentObjectGroup", translate("draft", "Group")) |
| | grp.Label = txt |
| | moveToGroup(grp) |
| | self.doc.commitTransaction() |
| | self.doc.recompute() |
| |
|
| |
|
| | Gui.addCommand("Draft_AddNamedGroup", AddNamedGroup()) |
| |
|
| | |
| |
|