~freecad-community/freecad-extras/lattice2

110 by DeepSOIC
V2: renaming .py files
1
#***************************************************************************
2
#*                                                                         *
3
#*   Copyright (c) 2015 - Victor Titov (DeepSOIC)                          *
4
#*                                               <vv.titov@gmail.com>      *  
5
#*                                                                         *
6
#*   This program is free software; you can redistribute it and/or modify  *
7
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
8
#*   as published by the Free Software Foundation; either version 2 of     *
9
#*   the License, or (at your option) any later version.                   *
10
#*   for detail see the LICENCE text file.                                 *
11
#*                                                                         *
12
#*   This program is distributed in the hope that it will be useful,       *
13
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
#*   GNU Library General Public License for more details.                  *
16
#*                                                                         *
17
#*   You should have received a copy of the GNU Library General Public     *
18
#*   License along with this program; if not, write to the Free Software   *
19
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
20
#*   USA                                                                   *
21
#*                                                                         *
22
#***************************************************************************
23
24
import FreeCAD, Part
163 by DeepSOIC
Fix Lattice2 failing to load when no Lattice v1 is installed
25
from lattice2Executer import CancelError
110 by DeepSOIC
V2: renaming .py files
26
if FreeCAD.GuiUp:
27
    import FreeCADGui
28
    from PySide import QtCore, QtGui
29
273 by DeepSOIC
FreeCADCmd: fix imports
30
def translate(context, text, disambig):
31
    #Lattice2 is not translatable, sorry...
32
    return text
110 by DeepSOIC
V2: renaming .py files
33
34
35
def getParamRefine():
36
    return FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Part/Boolean").GetBool("RefineModel")
279 by DeepSOIC
New feature: PartDesign Pattern!
37
def getParamPDRefine():
38
    return FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/PartDesign").GetBool("RefineModel")
110 by DeepSOIC
V2: renaming .py files
39
40
def getIconPath(icon_dot_svg):
41
    return ":/icons/" + icon_dot_svg
42
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
43
class SelectionError(FreeCAD.Base.FreeCADError):
44
    '''Error that isused inside Gui command code'''
45
    def __init__(self, title, message):
46
        self.message = message
250 by DeepSOIC
Py3: fix "err has no attribute message"
47
        self.args = (message,)
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
48
        self.title = title
289 by DeepSOIC
Utils: print traceback for pop-up error messages
49
50
def msgError(err = None, message = u'{errmsg}'):
51
    import sys
52
    if err is None:
53
        err = sys.exc_info()[1]
54
    if type(err) is CancelError: return # doesn't work! Why!
132 by DeepSOIC
Fix "Canceled by user" messagebox (wasn't intended to pop up)
55
    if hasattr(err, "isCancelError") and err.isCancelError: return   #workaround
289 by DeepSOIC
Utils: print traceback for pop-up error messages
56
57
    # can we get a traceback?
58
    b_tb =  err is sys.exc_info()[1]
59
    if b_tb:
60
        import traceback
61
        tb = traceback.format_exc()
62
        import FreeCAD as App
63
        App.Console.PrintError(tb+'\n')
64
    
65
    #make messagebox object
66
    from PySide import QtGui
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
67
    mb = QtGui.QMessageBox()
68
    mb.setIcon(mb.Icon.Warning)
289 by DeepSOIC
Utils: print traceback for pop-up error messages
69
    
70
    #fill in message
71
    errmsg = ''
72
    if hasattr(err,'message'):
73
        if isinstance(err.message, dict):
74
            errmsg = err.message['swhat']
75
        elif len(err.message) > 0:
76
            errmsg = err.message
77
        else: 
78
            errmsg = str(err)
79
    else:
80
        errmsg = str(err)
81
    mb.setText(message.format(errmsg= errmsg, err= err))
82
    
83
    # fill in title
84
    if hasattr(err, "title"):
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
85
        mb.setWindowTitle(err.title)
86
    else:
87
        mb.setWindowTitle("Error")
289 by DeepSOIC
Utils: print traceback for pop-up error messages
88
        
89
    #add traceback button
90
    if b_tb:
91
        btnClose = mb.addButton(QtGui.QMessageBox.StandardButton.Close)
92
        btnCopy = mb.addButton("Copy traceback", QtGui.QMessageBox.ButtonRole.ActionRole)
93
        mb.setDefaultButton(btnClose)
94
        
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
95
    mb.exec_()
289 by DeepSOIC
Utils: print traceback for pop-up error messages
96
    if b_tb:
97
        if mb.clickedButton() is btnCopy:
98
            cb = QtGui.QClipboard()
99
            cb.setText(tb)
100
        
122 by DeepSOIC
[Breaking] Substantial changes to populate features, and latticeBaseFeature
101
102
def infoMessage(title, message):
103
    mb = QtGui.QMessageBox()
104
    mb.setIcon(mb.Icon.Information)
105
    mb.setText(message)
106
    mb.setWindowTitle(title)
107
    mb.exec_()
108
109
def deselect(sel):
110
    '''deselect(sel): remove objects in sel from selection'''
111
    for selobj in sel:
112
        FreeCADGui.Selection.removeSelection(selobj.Object)
227.1.1 by DeepSOIC
Use shallow copy for arraying
113
        
110 by DeepSOIC
V2: renaming .py files
114
# OCC's Precision::Confusion; should have taken this from FreeCAD but haven't found; unlikely to ever change.
115
DistConfusion = 1e-7
116
ParaConfusion = 1e-8
117
273 by DeepSOIC
FreeCADCmd: fix imports
118
if FreeCAD.GuiUp:
119
    import lattice2_rc
245 by DeepSOIC
Workaround for FreeCAD bugs 2296, 2902
120
121
def screen(feature):
122
    """screen(feature): protects link properties from being overwritten. 
123
    This is to be used as workaround for a bug where modifying an object accessed through 
124
    a link property of another object results in the latter being touched.
125
    
126
    returns: feature"""
127
    if not hasattr(feature,"isDerivedFrom"):
128
        return feature
129
    if not feature.isDerivedFrom("App::DocumentObject"):
130
        return feature
131
    if feature.Document is None:
132
        return feature
133
    feature = getattr(feature.Document, feature.Name)
134
    return feature
277 by DeepSOIC
PartDesign: Placements land to active body
135
136
def activeBody():
283 by DeepSOIC
fix for FC v0.16
137
    if FreeCAD.ActiveDocument is None: return None
138
    if not hasattr(FreeCADGui.ActiveDocument.ActiveView, 'getActiveObject'): #prevent errors in 0.16
139
        return None
277 by DeepSOIC
PartDesign: Placements land to active body
140
    return FreeCADGui.ActiveDocument.ActiveView.getActiveObject("pdbody")
279 by DeepSOIC
New feature: PartDesign Pattern!
141
    
142
def bodyOf(feature):
143
    body = feature.getParentGeoFeatureGroup()
144
    if body.isDerivedFrom('PartDesign::Body'):
145
        return body
146
    else:
147
        return None