-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
188 lines (165 loc) · 6.71 KB
/
install.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
# -*- coding: utf-8 -*-
import xml.dom.minidom
import sys
import shutil
import os
import os.path
def getNode(node, fcPath, level=0):
words = fcPath.split("/");
if(len(words) <= level):
return node
ret = None
children = node.childNodes
for node in children:
if node.nodeType != node.ELEMENT_NODE:
continue
if words[level] == node.getAttribute("Name"):
ret = getNode(node, fcPath, level+1)
return ret
def createFCParamGroup(dom, name):
newNode = dom.createElement("FCParamGroup")
newNode.setAttribute("Name", name)
return newNode
def createFCText(dom, name, text):
newNode = dom.createElement("FCText")
newNode.setAttribute("Name", name)
textNode = dom.createTextNode(text)
newNode.appendChild(textNode)
return newNode
def createFCBool(dom, name, value):
newNode = dom.createElement("FCBool")
newNode.setAttribute("Name", name)
newNode.setAttribute("Value", value)
return newNode
def registerMacro(title, iconPath, macro):
if os.name == "nt":
freeCADHome = os.getenv('APPDATA') +"/FreeCAD"
elif os.name == "posix":
freeCADHome = os.path.expanduser("~") +"/.FreeCAD"
dom = xml.dom.minidom.parse(freeCADHome + "/user.cfg")
root = dom.documentElement
# Gets macro path
macroPath = ""
macroPathNode = getNode(root, "Root/BaseApp/Preferences/Macro/MacroPath");
if macroPathNode != None and macroPathNode.hasChildNodes() and (macroPathNode.childNodes[0].nodeType == macroPathNode.TEXT_NODE):
macroPath = macroPathNode.childNodes[0].data
else:
# If not exists , create macro path.
macroPath = freeCADHome + "/"
macroNode = getNode(root, "Root/BaseApp/Preferences/Macro")
macroNode.appendChild(createFCText(dom, "MacroPath", macroPath))
# Register macro icon path for ExTools
bitmapsNode = getNode(root, "Root/BaseApp/Preferences/Bitmaps")
existPath = False
for path in bitmapsNode.childNodes:
if path != None and path.hasChildNodes() and (path.childNodes[0].nodeType == path.TEXT_NODE):
if path.childNodes[0].data == iconPath:
existPath = True
break
if not existPath:
i = len(bitmapsNode.childNodes)
bitmapsNode.appendChild(createFCText(dom, "CustomPath" + str(i), iconPath))
alreadyRegistered = False
macros = getNode(root, "Root/BaseApp/Macro/Macros")
for m in macros.childNodes:
script = getNode(m, "Script")
if script != None and script.hasChildNodes() and (script.childNodes[0].nodeType == script.TEXT_NODE):
if script.childNodes[0].data == macro.Script:
alreadyRegistered = True
break
if not alreadyRegistered:
# Register Macro
macroName = "Std_Macro_" + str(len(macros.childNodes))
newMacro = createFCParamGroup(dom, macroName)
newMacro.appendChild(createFCText(dom, "Script", macro.Script))
newMacro.appendChild(createFCText(dom, "Menu", macro.Menu))
newMacro.appendChild(createFCText(dom, "Tooltip", macro.Tooltip))
newMacro.appendChild(createFCText(dom, "WhatsThis", macro.WhatsThis))
newMacro.appendChild(createFCText(dom, "Statustip", macro.Statustip))
newMacro.appendChild(createFCText(dom, "Pixmap", macro.Pixmap))
newMacro.appendChild(createFCText(dom, "Accel", macro.Accel))
macros.appendChild(newMacro)
# Register to Workbench
destWorkbench = getNode(root, "Root/BaseApp/Workbench/Global")
if destWorkbench == None :
destWorkbench = createFCParamGroup(dom, "Global")
workbench = getNode(root, "Root/BaseApp/Workbench")
workbench.appendChild(destWorkbench)
destToolbar = getNode(root, "Root/BaseApp/Workbench/Global/Toolbar")
if destToolbar == None:
destToolbar = createFCParamGroup(dom, "Toolbar")
destWorkbench.appendChild(destToolbar)
existToolbar = False
for custom in destToolbar.childNodes:
name = getNode(custom, "Name")
if name != None and name.hasChildNodes() and (name.childNodes[0].nodeType == name.TEXT_NODE):
if name.childNodes[0].data == title:
existToolbar = True
custom.appendChild(createFCText(dom, macroName, "FreeCAD"))
break
if not existToolbar:
custom1 = createFCParamGroup(dom, "Custom_" + str(len(destToolbar.childNodes) + 1))
custom1.appendChild(createFCText(dom, "Name", title))
custom1.appendChild(createFCBool(dom, "Active", "1"))
custom1.appendChild(createFCText(dom, macroName, "FreeCAD"))
destToolbar.appendChild(custom1)
# Register to MainWindow
mainWindowToolbar = getNode(root, "Root/BaseApp/MainWindow/Toolbars")
if mainWindowToolbar == None:
mainWindowToolbar = createFCParamGroup(dom, "PartDesignWorkbench")
mainWindows = getNode(root, "Root/BaseApp/MainWindow")
mainWindows.appendChild(mainWindowToolbar)
mainWindowToolbarTools = getNode(root, "Root/BaseApp/MainWindow/Toolbars/" + title)
if mainWindowToolbarTools == None:
mainWindowToolbar.appendChild(createFCBool(dom, title, '1'))
dom.writexml(open(freeCADHome + "/user.cfg","w"))
return macroPath
def install():
scriptDir = os.path.dirname(os.path.realpath(__file__))
if os.name == "nt":
freeCADHome = os.getenv('APPDATA') +"/FreeCAD"
elif os.name == "posix":
freeCADHome = os.path.expanduser("~") +"/.FreeCAD"
class Macro:
def __init__(self):
self.Script = ""
self.Menu = ""
self.Tooltip = ""
self.WhatsThis = ""
self.Statustip = ""
self.Pixmap = ""
self.Accel = ""
title = 'ExTools'
iconPath = freeCADHome + "/ExTools"
macroFunctionPolyline = Macro()
macroFunctionPolyline.Script = "FunctionPolyline.FCMacro"
macroFunctionPolyline.Menu = "Function Polyline..."
macroFunctionPolyline.Tooltip = "Draw polyline with function."
macroFunctionPolyline.WhatsThis = "Draw polyline with function."
macroFunctionPolyline.Statustip = 'Type your function and click "Add".'
macroFunctionPolyline.Pixmap = "FunctionPolyline"
macroFunctionPolyline.Accel = "Alt+P"
macroPath = registerMacro(title, iconPath, macroFunctionPolyline)
if macroPath == None:
sys.exit()
macroShapeInformation = Macro()
macroShapeInformation.Script = "ShapeInformation.FCMacro"
macroShapeInformation.Menu = "Shape Information..."
macroShapeInformation.Tooltip = "Show volume, area and center of mass."
macroShapeInformation.WhatsThis = "Show volume, area and center of mass."
macroShapeInformation.Statustip = "Select objects and run this tool."
macroShapeInformation.Pixmap = "ShapeInformation"
macroShapeInformation.Accel = "Alt+I"
macroPath = registerMacro(title, iconPath, macroShapeInformation)
if macroPath == None:
sys.exit()
shutil.copy(scriptDir + "/macros/FunctionPolyline.FCMacro", macroPath)
shutil.copy(scriptDir + "/macros/ShapeInformation.FCMacro", macroPath)
shutil.copyfile(scriptDir + "/uninstall.py", freeCADHome + "/uninstall-" + title + ".py");
if not os.path.exists(iconPath):
os.mkdir(iconPath)
shutil.copy(scriptDir + "/icons/FunctionPolyline.svg", iconPath)
shutil.copy(scriptDir + "/icons/ShapeInformation.svg", iconPath)
if __name__ == "__main__":
install()
print("done.")