forked from easyw/kicadStepUpMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explode.py
393 lines (362 loc) · 15.9 KB
/
explode.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Cad\Progetti_K\3D-FreeCad-tools\explode.ui'
#
# Created: Fri Sep 21 14:09:48 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!
__version__ = "v1.0.5"
import FreeCAD, FreeCADGui, os
from PySide import QtCore, QtGui
from sys import platform as _platform
global s # selection observer
global instance_nbr, explode_dwg
instance_nbr=0
# window GUI dimensions parameters
wdsExpx=300;wdsExpy=140
pt_osx=False
if _platform == "linux" or _platform == "linux2":
# linux
sizeX=wdsExpx;sizeY=wdsExpy-22+34 #516 #536
else:
sizeX=wdsExpx;sizeY=wdsExpy-22 #482#502
if _platform == "darwin":
pt_osx=True
## # MAC OS X
##elif _platform == "win32":
## # Windows
toBeReset = None
btn_iconsize=28
import ksu_locator
ksuWBpath = os.path.dirname(ksu_locator.__file__)
#sys.path.append(ksuWB + '/Gui')
ksuWB_icons_path = os.path.join( ksuWBpath, 'Resources', 'icons')
def get_top_level (obj):
lvl=10000
top=None
for ap in obj.InListRecursive:
if hasattr(ap,'Placement') and ap.TypeId!='App::FeaturePython' and ap.TypeId!='Part::Part2DObjectPython':
if len(ap.InListRecursive) < lvl:
top = ap
lvl = len(ap.InListRecursive)
if top is None:
if 'App::Part' in obj.TypeId or 'App::LinkGroup' in obj.TypeId:
top = obj
return top
###
class expSelectionObserver:
def addSelection(self,document, object, element, position):
"""Add single object to selection. Usually gets called when selecting in the 3d view"""
global explode_dwg
sel = FreeCADGui.Selection.getSelection()
if len (sel)== 1:
tlo = get_top_level(sel[0])
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
explode_dwg.ui.hSlider_explode.setEnabled(True)
else:
explode_dwg.ui.hSlider_explode.setEnabled(False)
# FreeCAD.Console.PrintMessage(document+"\n")
# FreeCAD.Console.PrintMessage(object+"\n")
# FreeCAD.Console.PrintMessage(element+"\n")
# FreeCAD.Console.PrintMessage(str(position)+"\n")
# ...
def removeSelection(self,document, object, element):
"""Remove single object from selection. Usually gets called when deselecting in the 3d view"""
global explode_dwg
sel = FreeCADGui.Selection.getSelection()
if len (sel)== 1:
tlo = get_top_level(sel[0])
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
explode_dwg.ui.hSlider_explode.setEnabled(True)
else:
explode_dwg.ui.hSlider_explode.setEnabled(False)
#FreeCAD.Console.PrintMessage(document+"\n")
#FreeCAD.Console.PrintMessage(object+"\n")
#FreeCAD.Console.PrintMessage(element+"\n")
# ...
def setSelection(self,document):
"""Add one or more objects to selection. Usually gets called when selecting in the tree view"""
global explode_dwg
sel = FreeCADGui.Selection.getSelection()
if len (sel)== 1:
tlo = get_top_level(sel[0])
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
explode_dwg.ui.hSlider_explode.setEnabled(True)
else:
explode_dwg.ui.hSlider_explode.setEnabled(False)
#FreeCAD.Console.PrintMessage(document+"\n")
#selection=Gui.Selection.getSelection(document)
# ...
def clearSelection(self,document):
"""Remove all objects of the given document from the selection"""
global explode_dwg
sel = FreeCADGui.Selection.getSelection()
if len (sel)== 1:
tlo = get_top_level(sel[0])
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
explode_dwg.ui.hSlider_explode.setEnabled(True)
else:
explode_dwg.ui.hSlider_explode.setEnabled(False)
#FreeCAD.Console.PrintMessage(document+"\n")
# ...
###
###########################################
class Ui_explode_dwg(object):
def setupUi(self, explode_dwg):
explode_dwg.setObjectName("explode_dwg")
explode_dwg.resize(306, 141)
explode_dwg.setMinimumSize(QtCore.QSize(91, 48))
explode_dwg.setLayoutDirection(QtCore.Qt.LeftToRight)
explode_dwg.setFeatures(QtGui.QDockWidget.AllDockWidgetFeatures)
explode_dwg.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea|QtCore.Qt.RightDockWidgetArea)
self.dockWidgetContents = QtGui.QWidget()
self.dockWidgetContents.setObjectName("dockWidgetContents")
self.mainGroup = QtGui.QGroupBox(self.dockWidgetContents)
self.mainGroup.setGeometry(QtCore.QRect(4, 0, 293, 109))
self.mainGroup.setTitle("Explode 3D PCB")
self.mainGroup.setObjectName("mainGroup")
self.gridLayoutWidget_13 = QtGui.QWidget(self.mainGroup)
self.gridLayoutWidget_13.setGeometry(QtCore.QRect(8, 24, 277, 37))
self.gridLayoutWidget_13.setObjectName("gridLayoutWidget_13")
self.gridLayout_15 = QtGui.QGridLayout(self.gridLayoutWidget_13)
self.gridLayout_15.setContentsMargins(0, 0, 0, 0)
self.gridLayout_15.setHorizontalSpacing(1)
self.gridLayout_15.setVerticalSpacing(2)
self.gridLayout_15.setObjectName("gridLayout_15")
self.hSlider_explode = QtGui.QSlider(self.gridLayoutWidget_13)
self.hSlider_explode.setToolTip("Explode PCB (in mm)")
self.hSlider_explode.setOrientation(QtCore.Qt.Horizontal)
self.hSlider_explode.setObjectName("hSlider_explode")
self.gridLayout_15.addWidget(self.hSlider_explode, 0, 0, 1, 1)
self.horizontalLayoutWidget = QtGui.QWidget(self.mainGroup)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(8, 64, 277, 41))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pb_Reset = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pb_Reset.setToolTip("Reset")
self.pb_Reset.setText("Reset")
self.pb_Reset.setObjectName("pb_Reset")
self.horizontalLayout.addWidget(self.pb_Reset)
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtGui.QLabel(self.horizontalLayoutWidget)
self.label.setText("(+/- mm)")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.step_lineEdit = QtGui.QLineEdit(self.horizontalLayoutWidget)
self.step_lineEdit.setMaximumSize(QtCore.QSize(32, 28))
self.step_lineEdit.setToolTip("incremental step (mm)")
self.step_lineEdit.setObjectName("step_lineEdit")
self.horizontalLayout.addWidget(self.step_lineEdit)
spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pb_Close = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pb_Close.setToolTip("Exit")
self.pb_Close.setText("Exit")
self.pb_Close.setObjectName("pb_Close")
self.horizontalLayout.addWidget(self.pb_Close)
explode_dwg.setWidget(self.dockWidgetContents)
self.retranslateUi(explode_dwg)
QtCore.QMetaObject.connectSlotsByName(explode_dwg)
#def retranslateUi(self, explode_dwg):
# explode_dwg.setWindowTitle(QtGui.QApplication.translate("explode_dwg", "Explode 3D PCB", None, QtGui.QApplication.UnicodeUTF8))
################################################################################################
explode_dwg.setWindowTitle("Explode 3D PCB")
self.pb_Close.clicked.connect(onCloseExp)
self.pb_Reset.clicked.connect(onReset)
self.hSlider_explode.valueChanged.connect(SlideValueChange)
self.hSlider_explode.setEnabled(False)
self.hSlider_explode.setToolTip("Explode PCB\nSelect the top container of a kicad PCB to exlode it")
icon = QtGui.QIcon()
myicon=os.path.join( ksuWB_icons_path , 'closeSm.svg')
icon.addPixmap(QtGui.QPixmap(myicon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pb_Close.setIcon(icon)
#self.pb_Close.setIconSize(QtCore.QSize(btn_iconsize, btn_iconsize))
self.pb_Close.setGeometry(QtCore.QRect(290, 72, btn_iconsize, btn_iconsize))
self.pb_Close.setText("")
icon = QtGui.QIcon()
myicon=os.path.join( ksuWB_icons_path , 'Reset-to-Center.svg')
icon.addPixmap(QtGui.QPixmap(myicon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pb_Reset.setIcon(icon)
#self.pb_Reset.setIconSize(QtCore.QSize(btn_iconsize, btn_iconsize))
self.pb_Reset.setGeometry(QtCore.QRect(12, 72, btn_iconsize, btn_iconsize))
self.pb_Reset.setText("")
self.step_lineEdit.setText("0.5")
#self.step_lineEdit.setGeometry(QtCore.QRect(12, 12, 12, 12))
self.step_lineEdit.setMaximumSize(QtCore.QSize(btn_iconsize*2, btn_iconsize))
self.label.setToolTip("incremental step (mm)")
def retranslateUi(self, explode_dwg):
pass
##############################################################
def explode_pcb(pos):
doc = FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sc = 2.0
sel = FreeCADGui.Selection.getSelection()
if len(sel) == 1:
tlo = get_top_level(sel[0])
#print (tlo.Label)
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
for o in tlo.OutListRecursive:
if 'Pcb' in o.Label:
if o.TypeId == 'App::Part' or o.TypeId == 'App::LinkGroup':
for ob in o.OutList:
if hasattr(ob,'Shape'):
if (pos != 0):
docG.getObject(ob.Name).Transparency=70
else:
docG.getObject(ob.Name).Transparency=0
else:
if hasattr(o,'Shape'):
if (pos != 0):
docG.getObject(o.Name).Transparency=70
else:
docG.getObject(o.Name).Transparency=0
elif 'Top' in o.Label and 'TopV' not in o.Label:
if hasattr(o,'Placement'):
o.Placement.Base.z=pos
elif 'Bot' in o.Label and 'BotV' not in o.Label:
if hasattr(o,'Placement'):
o.Placement.Base.z=-pos
elif 'TopV' in o.Label:
if hasattr(o,'Placement'):
o.Placement.Base.z=pos*sc
elif 'BotV' in o.Label:
if hasattr(o,'Placement'):
o.Placement.Base.z=-pos*sc
elif 'topTracks' in o.Label or 'botTracks' in o.Label:
if hasattr (o, 'Shape'):
if (pos != 0):
docG.getObject(o.Name).Transparency = 50
else:
docG.getObject(o.Name).Transparency = 0
elif 'topSilk' in o.Label or 'botSilk' in o.Label:
if hasattr (o, 'Shape'):
if (pos != 0):
docG.getObject(o.Name).Transparency = 30
else:
docG.getObject(o.Name).Transparency = 0
return tlo
#return None
def SlideValueChange():
global toBeReset, explode_dwg
pos = explode_dwg.ui.hSlider_explode.sliderPosition() * float(explode_dwg.ui.step_lineEdit.text())
#print('moving',pos)
toBeReset = explode_pcb(pos)
#
def onReset():
global toBeReset, explode_dwg
print ('Imploding')
sel = FreeCADGui.Selection.getSelection()
if len (sel) == 0 and toBeReset is not None:
try:
FreeCADGui.Selection.addSelection(toBeReset)
except:
pass
explode_dwg.ui.hSlider_explode.setValue(0)
explode_pcb(0)
#
def onCloseExp():
global s, explode_dwg
onReset()
"""closing dialog"""
print('closing')
try:
FreeCADGui.Selection.removeObserver(s)
print('observer removed')
except:
print('observer not removed')
explode_dwg.deleteLater()
#
##
def Exp_singleInstance():
app = QtGui.QApplication #QtGui.qApp
for i in app.topLevelWidgets():
#i_say (str(i.objectName()))
if i.objectName() == "ksuExplode":
#i_say (str(i.objectName()))
#i.close()
#i.deleteLater()
#i_say ('closed')
return False
t=FreeCADGui.getMainWindow()
dw=t.findChildren(QtGui.QDockWidget)
#say( str(dw) )
for i in dw:
#i_say (str(i.objectName()))
if str(i.objectName()) == "ksuExplode": #"kicad StepUp 3D tools":
#i_say (str(i.objectName())+' docked')
#i.deleteLater()
return False
return True
##
def Exp_centerOnScreen ():
'''centerOnScreen()
Centers the window on the screen.'''
# sayw(widg.width());sayw(widg.height())
# sayw(widg.pos().x());sayw(widg.pos().y())
resolution = QtGui.QDesktopWidget().screenGeometry()
xp=(resolution.width() / 2) - sizeX/2 # - (KSUWidget.frameSize().width() / 2)
yp=(resolution.height() / 2) - sizeY/2 # - (KSUWidget.frameSize().height() / 2))
# xp=widg.pos().x()-sizeXMax/2;yp=widg.pos().y()#+sizeY/2
explode_dwg.setGeometry(xp, yp, sizeX, sizeY)
##
def Exp_putOnTopRightCorner():
resolution = QtGui.QDesktopWidget().screenGeometry()
margin = 80
xp=(resolution.width()) - sizeX -margin/5 # - (KSUWidget.frameSize().width() / 2)
yp=(resolution.height()) - sizeY - margin # - (KSUWidget.frameSize().height() / 2))
# xp=widg.pos().x()-sizeXMax/2;yp=widg.pos().y()#+sizeY/2
explode_dwg.setGeometry(xp, margin, sizeX, sizeY)
#self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
#self.show()
def runExplodeGui():
global explode_dwg
doc=FreeCAD.ActiveDocument
if Exp_singleInstance():
global s
explode_dwg = QtGui.QDockWidget()
explode_dwg.ui = Ui_explode_dwg()
explode_dwg.ui.setupUi(explode_dwg)
explode_dwg.setObjectName("ksuExplode")
explode_dwg.raise_()
explode_dwg.setFeatures( QtGui.QDockWidget.DockWidgetMovable | QtGui.QDockWidget.DockWidgetFloatable ) # | QtGui.QDockWidget.DockWidgetClosable )
#RHDockWidget.destroyed.connect(onDestroy)
#explode_dwg.visibilityChanged.connect(onClickClose)
ExpMw = FreeCADGui.getMainWindow() # PySide # the active qt window, = the freecad window since we are inside it
ExpMw.addDockWidget(QtCore.Qt.RightDockWidgetArea,explode_dwg)
explode_dwg.setFloating(True) #undock
#RHDockWidget.resize(sizeX,sizeY)
#Exp_centerOnScreen()
Exp_putOnTopRightCorner()
s=expSelectionObserver()
FreeCADGui.Selection.addObserver(s)
onReset()
#explode_dwg.ui.Version.setText(__version__)
#else:
#raising up
explode_dwg.activateWindow()
explode_dwg.raise_()
sel = FreeCADGui.Selection.getSelection()
if len (sel)== 1:
tlo = get_top_level(sel[0])
if tlo is not None:
#if 'App::Part' in sel[0].TypeId:
explode_dwg.ui.hSlider_explode.setEnabled(True)
else:
explode_dwg.ui.hSlider_explode.setEnabled(False)
#if __name__ == "__main__":
# import sys
# app = QtGui.QApplication(sys.argv)
# explode_dwg = QtGui.QDockWidget()
# ui = Ui_explode_dwg()
# ui.setupUi(explode_dwg)
# explode_dwg.show()
# sys.exit(app.exec_())