/
core.py
93 lines (70 loc) · 2.61 KB
/
core.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
import FreeCAD,FreeCADGui,Part
path_to_ui = str(FreeCAD.getUserAppDataDir()) + 'Mod/FreeCAD_Python/ui/task.ui'
class PythonObjectTool():
def GetResources(self):
return {'Pixmap' : 'My_Command_Icon', # the name of a svg file available in the resources
'Accel' : "Shift+S", # a default shortcut (optional)
'MenuText': "PythonObject Command",
'ToolTip' : "PythonObject"}
def Activated(self):
obj = makePythonObject()
FreeCADGui.activeDocument().setEdit(obj.Name,0)
def IsActive(self):
return True
class PythonObject:
def __init__(self, obj):
obj.addProperty("App::PropertyPythonObject","Script","python script","python script").Script="#fp is current object\nif not 'length' in fp.PropertiesList:\n\
fp.addProperty('App::PropertyLength','length').length=5.0\n\
fp.Shape = Part.makeBox(fp.length,fp.length,fp.length)\n\
FreeCAD.Console.PrintMessage('makeBox\\n')"
obj.Proxy = self
FreeCAD.Console.PrintMessage("init\n")
def onChanged(self, fp, prop):
'''Do something when a property has changed'''
def execute(self, fp):
exec(fp.Script)
FreeCAD.Console.PrintMessage("\n")
class PythonObjectViewProvider:
def __init__(self,vobj):
vobj.Proxy = self
# def getIcon(self):
# return ":/icons/PartDesign_InternalExternalGear.svg"
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
def setEdit(self,vobj,mode):
taskd = PythonObjectTaskPanel(self.Object,mode)
taskd.obj = vobj.Object
FreeCADGui.Control.showDialog(taskd)
return True
def unsetEdit(self,vobj,mode):
FreeCADGui.Control.closeDialog()
return
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class PythonObjectTaskPanel:
def __init__(self,obj,mode):
self.obj = obj
self.form = FreeCADGui.PySideUic.loadUi(path_to_ui)
self.form.textEdit.setPlainText(obj.Script)
self.form.refreshButton.clicked.connect( self.refresh)
def refresh(self):
self.obj.Script = self.form.textEdit.toPlainText()
FreeCAD.ActiveDocument.recompute()
def accept(self):
self.obj.Script = self.form.textEdit.toPlainText()
FreeCADGui.ActiveDocument.resetEdit()
def reject(self):
FreeCADGui.ActiveDocument.resetEdit()
FreeCADGui.addCommand('PythonObjectTool',PythonObjectTool())
def makePythonObject():
FreeCAD.Console.PrintMessage("makePythonObject\n")
if FreeCAD.ActiveDocument is None:
FreeCAD.newDocument("pyObject")
obj=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","PythonObject")
PythonObject(obj)
PythonObjectViewProvider(obj.ViewObject)
FreeCAD.ActiveDocument.recompute()
return obj