~freecad-community/freecad-extras/lattice2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#***************************************************************************
#*                                                                         *
#*   Copyright (c) 2015 - Victor Titov (DeepSOIC)                          *
#*                                               <vv.titov@gmail.com>      *  
#*                                                                         *
#*   This program is free software; you can redistribute it and/or modify  *
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
#*   as published by the Free Software Foundation; either version 2 of     *
#*   the License, or (at your option) any later version.                   *
#*   for detail see the LICENCE text file.                                 *
#*                                                                         *
#*   This program is distributed in the hope that it will be useful,       *
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#*   GNU Library General Public License for more details.                  *
#*                                                                         *
#*   You should have received a copy of the GNU Library General Public     *
#*   License along with this program; if not, write to the Free Software   *
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
#*   USA                                                                   *
#*                                                                         *
#***************************************************************************

__title__="Command to inspect selected object compounding structure"
__author__ = "DeepSOIC"
__url__ = ""

import FreeCAD as App
import Part

from lattice2Common import *
import lattice2CompoundExplorer as LCE
import lattice2BaseFeature

def shapeInfoString(shape):
    strMsg = shape.ShapeType 
    if shape.ShapeType == 'Vertex':
        strMsg += " ("+repr(list(shape.Point))+")"
    elif shape.ShapeType == 'Edge':
        strMsg += " ("+repr(shape.Curve)+")"
    elif shape.ShapeType == 'Wire':
        strMsg += " ("+str(len(shape.childShapes()))+" segments)"
    elif shape.ShapeType == 'Face':
        strMsg += " ("+repr(shape.Surface)+")"
    elif shape.ShapeType == 'Shell':
        strMsg += " ("+str(len(shape.childShapes()))+" faces)"
    elif shape.ShapeType == 'Solid':
        strMsg += " ("+str(len(shape.childShapes()))+" shells)"
    elif shape.ShapeType == 'CompSolid':
        strMsg += " ("+str(len(shape.childShapes()))+" solids)"
    elif shape.ShapeType == 'Compound':
        strMsg += " ("+str(len(shape.childShapes()))+" objects)"
    return strMsg

class _CommandInspect:
    "Command to inspect compounding structure"
        
    def __init__(self):
        pass
    
    def GetResources(self):
        return {'Pixmap'  : getIconPath("Lattice2_Inspect.svg"),
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Inspect","Inspect selection") , # FIXME: not translation-friendly!
                'Accel': "",
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Inspect","Lattice Inspect: display info on compounding structure of selected object.")}
        
    def Activated(self):
        sel = FreeCADGui.Selection.getSelectionEx()[0]
        isLattice = lattice2BaseFeature.isObjectLattice(sel.Object)
        
        strStructure = []
        if not hasattr(sel.Object,"Shape"):
            strStructure = ["<object has no shape!>"]
        else:
            if sel.Object.Shape.isNull():
                strStructure.append("<NULL SHAPE!>")
            else:
                for (child, msg, it) in LCE.CompoundExplorer(sel.Object.Shape):
                    #child is a shape. 
                    #msg is int equal to one of three constants:
                    #    CompoundExplorer.MSG_LEAF  - child is a leaf (non-compound)
                    #    CompoundExplorer.MSG_DIVEDOWN  - child is a compound that is about to be traversed
                    #    CompoundExplorer.MSG_BUBBLEUP  - child is a compound that was just finished traversing        
                    #it is reference to iterator class (can be useful to extract current depth, or index stack)
                    if msg == LCE.CompoundExplorer.MSG_LEAF or msg == LCE.CompoundExplorer.MSG_DIVEDOWN:
                        try:
                            strMsg =  '    ' * it.curDepth() + shapeInfoString(child)
                            if msg == LCE.CompoundExplorer.MSG_DIVEDOWN:
                                strMsg += ":"
                        except Exception as err:
                            strMsg = "ERROR: " + str(err)
                        strStructure.append(strMsg)
            
        strSubInfo = []
        if sel.HasSubObjects:
            subNames = sel.SubElementNames
            subObjects = sel.SubObjects
            for i in range(0,len(subNames)):
                strMsg = subNames[i] + ": "
                child = subObjects[i]
                
                try:
                    strMsg += shapeInfoString(child)
                except Exception as err:
                    strMsg += "ERROR: " + str(err)
                strSubInfo.append(strMsg)
        
        allText = u''
        if sel.HasSubObjects:
            allText += u"Selected " + str(len(sel.SubElementNames)) + u" subelements:\n"
            allText += u'\n'.join(strSubInfo) + u'\n\n'
        
        allText += u'Selected document object:\n'
        allText += u'  Name = ' + sel.Object.Name + u'\n'
        allText += u'  Label = ' + sel.Object.Label + u'\n'
        allText += u'  Is placement/array = ' + repr(isLattice) + u'\n'
        allText += u'Structure: \n'
        allText += u'\n'.join(strStructure)
        mb = QtGui.QMessageBox()
        mb.setIcon(mb.Icon.Information)
        lines = allText.split(u"\n") 
        if len(lines)>30:
            lines = lines[0:30]
            lines.append(u"...")
        mb.setText(u"\n".join(lines))
        mb.setWindowTitle(translate("Lattice2_Inspect","Selection info", None))
        
        btnClose = mb.addButton(QtGui.QMessageBox.StandardButton.Close)
        btnCopy = mb.addButton("Copy to clipboard",QtGui.QMessageBox.ButtonRole.ActionRole)
        mb.setDefaultButton(btnClose)
        mb.exec_()
        
        if mb.clickedButton() is btnCopy:
            cb = QtGui.QClipboard()
            cb.setText(allText)

    def IsActive(self):
        if len(FreeCADGui.Selection.getSelectionEx()) == 1:
            return True
        else:
            return False

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Lattice2_Inspect',_CommandInspect())

import lattice2ShapeInfoFeature

class GroupCommandInspect:
    def GetCommands(self):
        return ('Lattice2_Inspect', lattice2ShapeInfoFeature.exportedCommands[0]) # a tuple of command names that you want to group

    def GetDefaultCommand(self): # return the index of the tuple of the default command. This method is optional and when not implemented '0' is used  
        return 0

    def GetResources(self):
        return { 'MenuText': 'Inspect:', 'ToolTip': 'Inspect: tools to analyze shape structure.'}
        
    def IsActive(self): # optional
        return True

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Lattice2_Inspect_GroupCommand',GroupCommandInspect())

exportedCommands = ['Lattice2_Inspect_GroupCommand']