forked from cblt2l/FreeCAD-CuraEngine-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachineDef.py
309 lines (255 loc) · 11.2 KB
/
MachineDef.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
#***************************************************************************
#* *
#* Copyright (c) 2014 *
#* cblt2l <[email protected]> *
#* *
#* 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 *
#* *
#***************************************************************************
import FreeCAD
from SliceVars import *
from pivy import coin
if FreeCAD.GuiUp:
import FreeCADGui
from FreeCADGui import PySideUic as uic
from PySide import QtCore, QtGui
# Default Values
defaultVals = {"machinex":100, "machiney":100, "machinez":100, "offsetx":20, "offsety":20, "bedx":100, "bedy":100}
#-------------------------------------------------
def makePrintBedGrp():
# obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython","PrintBed")
grp = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup","PrintBedGroup")
PrintBedGroup(grp)
FreeCADGui.activeDocument().activeView().viewAxometric()
FreeCADGui.SendMsgToActiveView("ViewFit")
# PrintBed(obj)
# ViewProviderPrintBed(obj.ViewObject)
class PrintBedGroup:
def __init__(self, grp):
# Create objects for the printbed and print volume
pbed = FreeCAD.ActiveDocument.addObject("App::FeaturePython","PrintBed")
pvol = FreeCAD.ActiveDocument.addObject("App::FeaturePython","PrintVolume")
# Create an object & view provider for the print bed
PrintBed(pbed)
ViewProviderPrintBed(pbed.ViewObject)
# Create an object & view provider for the print volume
PrintVolume(pvol)
ViewProviderPrintVolume(pvol.ViewObject)
# Add the PrintBed and PrintVolume objects to the group
grp.addObject(pbed)
grp.addObject(pvol)
class PrintBed:
'The PrintBed Object'
def __init__(self, obj):
obj.addProperty("App::PropertyDistance", "XSize", "PrintBed", "The X Dimension of the Bed").XSize=readSetting("bedx")
obj.addProperty("App::PropertyDistance", "YSize", "PrintBed", "The Y Dimension of the Bed").YSize=readSetting("bedy")
obj.addProperty("App::PropertyDistance", "XOffset", "PrintBed", "The X Offset of the Bed").XOffset=readSetting("offsetx")
obj.addProperty("App::PropertyDistance", "YOffset", "PrintBed", "The Y Offset of the Bed").YOffset=readSetting("offsety")
obj.Proxy = self
self.Type = "PrintBed"
def onChanged(self, fp, prop):
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
def execute(self, fp):
FreeCAD.Console.PrintMessage("Recompute PrintBed feature\n")
class ViewProviderPrintBed:
'The PrintBed View Provider Object'
def __init__(self, obj):
obj.addProperty("App::PropertyColor","Color","PrintBed","Color of the box").Color=(1.0,0.0,0.0)
obj.Proxy = self
def attach(self, obj):
"'''Setup the scene sub-graph of the view provider, this method is mandatory'''"
self.shaded = coin.SoGroup()
self.wireframe = coin.SoGroup()
self.scale = coin.SoScale()
self.color = coin.SoBaseColor()
self.trans = coin.SoTranslation()
self.data=coin.SoCube()
# Top of print bed is X-Y plane
self.trans.translation.setValue([0,0,-0.5])
self.shaded.addChild(self.trans)
self.shaded.addChild(self.scale)
self.shaded.addChild(self.color)
self.shaded.addChild(self.data)
obj.addDisplayMode(self.shaded,"Shaded")
style=coin.SoDrawStyle()
style.style = coin.SoDrawStyle.LINES
self.wireframe.addChild(style)
self.wireframe.addChild(self.scale)
self.wireframe.addChild(self.color)
self.wireframe.addChild(self.data)
obj.addDisplayMode(self.wireframe,"Wireframe")
self.onChanged(obj,"Color")
def updateData(self, fp, prop):
"'''If a property of the handled feature has changed we have the chance to handle this here'''"
# fp is the handled feature, prop is the name of the property that has changed
x = fp.getPropertyByName("XSize").Value
y = fp.getPropertyByName("YSize").Value
z = 1.0
ox = fp.getPropertyByName("XOffset").Value
oy = fp.getPropertyByName("YOffset").Value
# Set the size of the PrintBed
self.data.width = x
self.data.height = y
self.data.depth = z
# Translate the printbed to proper location
self.trans.translation.setValue([(x/2)+ox, (y/2)+oy, -0.5])
pass
def getDisplayModes(self,obj):
"'''Return a list of display modes.'''"
modes=[]
modes.append("Shaded")
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''"
return "Shaded"
def setDisplayMode(self,mode):
return mode
def onChanged(self, vp, prop):
"'''Here we can do something when a single property got changed'''"
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
if prop == "Color":
c = vp.getPropertyByName("Color")
self.color.rgb.setValue(c[0],c[1],c[2])
def getIcon(self):
return ":/icons/PartDesign_Revolution.svg"
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class PrintVolume:
'The StrokeLimit Object'
def __init__(self, obj):
obj.addProperty("App::PropertyDistance", "XStroke", "Machine", "The X Axis Stroke").XStroke=readSetting("machinex")
obj.addProperty("App::PropertyDistance", "YStroke", "Machine", "The Y Axis Stroke").YStroke=readSetting("machiney")
obj.addProperty("App::PropertyDistance", "ZStroke", "Machine", "The Z Axis Stroke").ZStroke=readSetting("machinez")
obj.Proxy = self
self.Type = "Machine"
def onChanged(self, fp, prop):
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
def execute(self, fp):
FreeCAD.Console.PrintMessage("Recompute Machine feature\n")
class ViewProviderPrintVolume:
'The StrokeLimit View Provider Object'
def __init__(self, obj):
#obj.addProperty("App::PropertyColor","Color","Machine","Color of the box").Color=(1.0,0.0,0.0)
obj.Proxy = self
def attach(self, obj):
"'''Setup the scene sub-graph of the view provider, this method is mandatory'''"
self.wireframe = coin.SoGroup()
self.scale = coin.SoScale()
# self.color = coin.SoBaseColor()
self.trans = coin.SoTranslation()
self.data=coin.SoCube()
# Switch Z to proper default value
self.trans.translation.setValue([0,0,100])
self.wireframe.addChild(self.trans)
style=coin.SoDrawStyle()
style.style = coin.SoDrawStyle.LINES
self.wireframe.addChild(style)
self.wireframe.addChild(self.scale)
# self.wireframe.addChild(self.color)
self.wireframe.addChild(self.data)
obj.addDisplayMode(self.wireframe,"Wireframe")
def updateData(self, fp, prop):
"'''If a property of the handled feature has changed we have the chance to handle this here'''"
# fp is the handled feature, prop is the name of the property that has changed
x = fp.getPropertyByName("XStroke").Value
y = fp.getPropertyByName("YStroke").Value
z = fp.getPropertyByName("ZStroke").Value
# Translate box to keep corner at (0,0)
self.trans.translation.setValue([x/2 ,y/2 , z/2])
self.data.width = x
self.data.height = y
self.data.depth = z
pass
def getDisplayModes(self,obj):
"'''Return a list of display modes.'''"
modes=[]
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''"
return "Wireframe"
def setDisplayMode(self,mode):
return mode
def onChanged(self, vp, prop):
"'''Here we can do something when a single property got changed'''"
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
# if prop == "Color":
# c = vp.getPropertyByName("Color")
# self.color.rgb.setValue(c[0],c[1],c[2])
def getIcon(self):
return ":/icons/PartDesign_Revolution.svg"
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class PrintBedTaskPanel:
def __init__(self):
# Get the user's home directory.
self.homeDir = os.path.expanduser("~")
# Load the qt uic form. It _must_ be in ~/.FreeCAD/Mod/FreeCAD-CuraEngine-Plugin, Perhaps there is a better way...
self.form = uic.loadUi(self.homeDir + "/.FreeCAD/Mod/FreeCAD-CuraEngine-Plugin/MachineDef.ui")
self.form.doubleSpinBox_1.setValue(readSetting("machinex"))
self.form.doubleSpinBox_2.setValue(readSetting("machiney"))
self.form.doubleSpinBox_3.setValue(readSetting("machinez"))
self.form.doubleSpinBox_6.setValue(readSetting("offsetx"))
self.form.doubleSpinBox_7.setValue(readSetting("offsety"))
self.form.doubleSpinBox_4.setValue(readSetting("bedx"))
self.form.doubleSpinBox_5.setValue(readSetting("bedy"))
self.form.doubleSpinBox_1.valueChanged.connect(self._machineXStroke)
self.form.doubleSpinBox_2.valueChanged.connect(self._machineYStroke)
self.form.doubleSpinBox_3.valueChanged.connect(self._machineZStroke)
self.form.doubleSpinBox_6.valueChanged.connect(self._bedXOffset)
self.form.doubleSpinBox_7.valueChanged.connect(self._bedYOffset)
self.form.doubleSpinBox_4.valueChanged.connect(self._bedXSize)
self.form.doubleSpinBox_5.valueChanged.connect(self._bedYSize)
def accept(self):
makePrintBedGrp()
FreeCADGui.Control.closeDialog()
def reject(self):
FreeCADGui.Control.closeDialog()
def getStandardButtons(self):
return int(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel)
def _machineXStroke(self, val):
writeSetting("machinex", val)
def _machineYStroke(self, val):
writeSetting("machiney", val)
def _machineZStroke(self, val):
writeSetting("machinez", val)
def _bedXOffset(self, val):
writeSetting("offsetx", val)
def _bedYOffset(self, val):
writeSetting("offsety", val)
def _bedXSize(self, val):
writeSetting("bedx", val)
def _bedYSize(self, val):
writeSetting("bedy", val)
def readSetting(key):
global defaultVals
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/3DPrinting/MachineDef")
val = grp.GetFloat(key, defaultVals[key])
FreeCAD.Console.PrintMessage("Reading Key: " + key + " Value: " + str(val) + "\n")
return val
def writeSetting(key, val):
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/3DPrinting/MachineDef")
FreeCAD.Console.PrintMessage("Setting " + key + " to " + str(val) + '\n')
grp.SetFloat(key, val)
# Run as macro
#panel=PrintBedTaskPanel()
#FreeCADGui.Control.showDialog(panel)