-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSurfaceEditing.py
440 lines (372 loc) · 15 KB
/
SurfaceEditing.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# coding=UTF-8
import FreeCAD, FreeCADGui
from FreeCAD import Gui
from SMesh import SMesh
import Draft
from DraftGui import translate, DraftDockWidget, DraftLineEdit
from DocumentObject import prtb
try:
from PySide import QtCore,QtGui,QtSvg
except:
FreeCAD.Console.PrintMessage("Error: Python-pyside package must be installed on your system to use the Draft module.")
"""
import SurfaceEditing
reload (SurfaceEditing)
FreeCADGui.seToolbar.baseWidget.hide()
FreeCADGui.seToolbar=SurfaceEditing.SEToolbar()
FreeCADGui.seToolbar.baseWidget.show()
FreeCADGui.seToolbar.w1.show()
FreeCADGui.seToolbar.seWidget.setVisible(True)
"""
class SEToolbar:
def __init__(self):
# create the se Toolbar
self.mw = FreeCADGui.getMainWindow()
self.seWidget = QtGui.QDockWidget()
self.baseWidget = DraftDockWidget()
self.seWidget.setObjectName("seToolbar")
self.seWidget.setTitleBarWidget(self.baseWidget)
self.seWidget.setWindowTitle(translate("se", "Surface Editor Command Bar"))
self.mw.addDockWidget(QtCore.Qt.TopDockWidgetArea,self.seWidget)
self.baseWidget.setVisible(False)
self.seWidget.setVisible(False)
#self.seWidget.toggleViewAction().setVisible(False)
#self.sourceCmd = None
#self.taskmode = Draft.getParam("UiMode")
self.layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight,self.baseWidget)
self.layout.setDirection(QtGui.QBoxLayout.LeftToRight)
self.layout.setObjectName("layout")
self.l1 = self.label("l1","current command: ")
self.lcommand = self.label("lcommand", "None")
self.w1 = self.lineedit("w1",width=100)
self.l2 = self.label("l1","value:")
self.w2 = self.lineedit("w2",width=100)
self.q1 = self.combo("q1",[("String","App::PropertyString"),("Float","App::PropertyFloat"),("Vector","App::PropertyVector")])
self.b1 = self.button("b1","OK")
self.layout.addStretch(100)
def show(self,text=None):
self.l1.show()
self.lcommand.show()
self.baseWidget.show()
self.seWidget.setVisible(True)
if text:
self.lcommand.setText(text)
def hide(self):
self.seWidget.setVisible(False)
self.reset()
def showPropEntry(self,callback):
"""
Shows the widgets for property entry
"""
self.show()
self.lcommand.setText("Add Property")
self.w1.show()
self.l2.show()
self.w2.show()
self.q1.show()
self.b1.show()
def reset(self):
self.w1.hide()
self.l2.hide()
self.w2.hide()
self.q1.hide()
self.b1.hide()
self.lcommand.setText('None')
def button (self,name, text, hide=True, icon=None, width=66, checkable=False):
button = QtGui.QPushButton(self.baseWidget)
button.setObjectName(name)
button.setText(text)
button.setMaximumSize(QtCore.QSize(width,26))
if hide:
button.hide()
if checkable:
button.setCheckable(True)
button.setChecked(False)
self.layout.addWidget(button)
return button
def combo(self, name, choices, hide=True):
combo = QtGui.QComboBox(self.baseWidget)
combo.setObjectName(name)
if hide: combo.hide()
for (name,userdata) in choices:
combo.addItem(name,userdata)
self.layout.addWidget(combo)
return combo
def label (self,name, text=None, hide=True):
label = QtGui.QLabel(self.baseWidget)
label.setObjectName(name)
if hide: label.hide()
if text: label.setText(text)
self.layout.addWidget(label)
return label
def lineedit (self, name, hide=True, width=None):
lineedit = DraftLineEdit(self.baseWidget)
lineedit.setObjectName(name)
if hide: lineedit.hide()
if not width: width = 800
lineedit.setMaximumSize(QtCore.QSize(width,22))
self.layout.addWidget(lineedit)
return lineedit
class ToggleCrease:
def Activated(self):
sel = FreeCADGui.Selection.getSelection()
for obj in sel:
try:
pt = obj.Proxy.pytype
except AttributeError:
continue
if pt != 'SMEdge':
continue
mesh = obj.Proxy.getParentByType('SMesh')
mesh.doop('ToggleCrease', [obj.Label])
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Toggle Crease', 'ToolTip': 'Toggles creasedbess of selected edges'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class AddProp:
def Activated(self):
FreeCADGui.seToolbar.showPropEntry(FreeCADGui.seEditor.addProp)
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Add/Edit property', 'ToolTip': 'Adds/edits user defined property of selected objects'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class AddEdge:
def Activated(self):
FreeCADGui.seEditor.addEdge()
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Add Edge', 'ToolTip': 'Adds edge between selected points'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class AddFace:
def Activated(self):
FreeCADGui.seEditor.addFace()
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Add Face', 'ToolTip': 'Adds face with selected points'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class ClearSelection:
def Activated(self):
FreeCADGui.Selection.clearSelection()
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Clear Selection', 'ToolTip': 'Clears the selection'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class AddMesh:
def Activated(self):
FreeCAD.ActiveDocument.openTransaction("Adding Mesh\n")
self.mesh = SMesh()
sel = FreeCADGui.Selection.getSelection()
for ob in sel:
self.mesh.doop('AddOb',[ob.Label])
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
def meshWire(self,ob):
firstp=None
lastp=None
points=[]
for p in ob.Points:
sp = self.mesh.getOrCreatePoint(p,"points")
points.append(sp)
if firstp is None:
firstp=sp
if lastp is not None:
self.mesh.getOrCreateEdge(lastp,sp,"edges")
lastp = sp
if len(ob.Points) > 2 and ob.Closed:
self.mesh.getOrCreateEdge(lastp,firstp,"edges")
if ob.ViewObject.DisplayMode == "Flat Lines" and ob.Closed:
self.mesh.getOrCreateFace(points,"faces")
@staticmethod
def obtype(ob):
p = getattr(ob,"Proxy",None)
if p:
ob = p
return getattr(ob,"Type",None)
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Add Mesh', 'ToolTip': 'Adds an empty surface mesh'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class MovePoints:
def Activated(self):
FreeCADGui.seEditor.activateMover()
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Move Points', 'ToolTip': 'Moves points around'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class InsertPoints:
def Activated(self):
FreeCADGui.seEditor.activateInserter()
def GetResources(self):
return {'Pixmap' : '', 'MenuText': 'Insert Points', 'ToolTip': 'Inserts points'}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
class SurfaceEdit:
def __init__(self):
self.obj=None
QtCore.QObject.connect(FreeCADGui.seToolbar.b1,QtCore.SIGNAL("pressed()"),self.addProp)
def activateMover(self):
self.mode='Move Points'
self.activated()
def activateInserter(self):
self.mode='Insert Points'
self.activated()
def activated(self):
#FreeCAD.Console.PrintMessage("activate\n")
self.obj=None
self.view=Gui.activeDocument().activeView()
self.call = self.view.addEventCallback("SoEvent",self.observe)
FreeCADGui.seToolbar.show(self.mode)
def deactivate(self):
FreeCAD.Console.PrintMessage("deactivate\n")
self.view.removeEventCallback("SoEvent",self.call)
FreeCADGui.seToolbar.hide()
self.do = False
def dummycb(self,p):
pass
def callSelected(self,methname):
#FreeCAD.Console.PrintMessage("cs %s\n"%methname)
sel = FreeCADGui.Selection.getSelection()
for ob in sel:
#FreeCAD.Console.PrintMessage("ob %s\n"%ob)
if getattr(ob,"Proxy",False):
#FreeCAD.Console.PrintMessage("ob %s\n"%ob.Proxy)
meth = getattr(ob.Proxy,methname,None)
if meth:
#FreeCAD.Console.PrintMessage("calling %s\n"%meth)
meth()
def addProp(self):
#FreeCAD.Console.PrintMessage("addprop %s\n"%(self,))
tb = FreeCADGui.seToolbar
name = str(tb.w1.text())
value = str(tb.w2.text())
proptype = str(tb.q1.itemData(tb.q1.currentIndex()).toString())
sel = FreeCADGui.Selection.getSelection()
for ob in sel:
#FreeCAD.Console.PrintMessage("addprop %s, %s\n"%(self,ob))
mesh = ob.Proxy.getParentByType('SMesh')
mesh.doop('AddProperty',[ob.Label],name=name,proptype=proptype,value=value)
tb.hide()
FreeCAD.ActiveDocument.recompute()
def addPoint(self,pos):
p=self.getPoint(pos)
sel = FreeCADGui.Selection.getSelection()
for s in sel:
FreeCAD.Console.PrintMessage("s= %s\n"%(s))
try:
pt = s.Proxy.pytype
except AttributeError:
pt = None
if s.Proxy.pytype == 'SMesh':
mesh = s.Proxy
layername = str(mesh.getOrCreateLayer().Label)
mesh.doop("AddPoint",[],layer=layername,x=p.x,y=p.y,z=p.z)
return
elif s.Proxy.pytype == 'SMLayer':
mesh = s.Proxy.getParentByType('SMesh')
layername = str(s.Label)
mesh.doop("AddPoint",list(),layer=layername,x=p.x,y=p.y,z=p.z)
return
FreeCAD.Console.PrintMessage("You should select a Mesh or Layer to create a point in it\n")
def getSelectedPoints(self):
sel = FreeCADGui.Selection.getSelection()
points=[]
mesh=None
for s in sel:
pt = False
try:
pt = s.Proxy.pytype
except AttributeError:
continue
if pt == 'SMPoint':
if not mesh:
mesh = s.Proxy.getParentByType('SMesh')
elif mesh != s.Proxy.getParentByType('SMesh'):
FreeCAD.Console.PrintMessage("You should select points in the same mesh to create edges/faces!\n")
return mesh,[]
points.append(s.Proxy)
elif pt == 'SMEdge':
if not mesh:
mesh = s.Proxy.getParentByType('SMesh')
elif mesh != s.Proxy.getParentByType('SMesh'):
FreeCAD.Console.PrintMessage("You should select points in the same mesh to create edges/faces!\n")
return mesh,[]
points.append(s.Start.Proxy)
points.append(s.End.Proxy)
return mesh,points
def addEdge(self):
mesh,points = self.getSelectedPoints()
if len(points) < 2:
FreeCAD.Console.PrintMessage("You should select at least 2 points in the same mesh to create edges!\n")
return
p0 = points[0]
for p in points[1:]:
mesh.getOrCreateEdge(p0,p)
p0 = p
def addFace(self):
mesh,points = self.getSelectedPoints()
if len(points) < 3:
FreeCAD.Console.PrintMessage("You should select at least 3 points in the same mesh to create a face!\n")
return
mesh.getOrCreateFace(points)
def observe(self,event):
#FreeCAD.Console.PrintMessage("EVENT %s\n"%(event["Type"],))
if event["Type"] == 'SoKeyboardEvent' and event['State']=='DOWN':
if event['Key'] == 'ESCAPE':
self.deactivate()
else:
FreeCAD.Console.PrintMessage("unhandled keypress %s\n"%(event['Key'],))
elif event["Type"] == 'SoLocation2Event':
if not self.obj:
return
#FreeCAD.Console.PrintMessage("moving\n")
delta = self.getPoint(event['Position']) - self.p0
self.obj.Coordinates = self.op0 + delta
elif event["Type"] == 'SoMouseButtonEvent':
FreeCAD.Console.PrintMessage("EVENT %s\n"%(event,))
if event['State'] == 'DOWN':
pos = event['Position']
if self.mode == 'Insert Points':
FreeCAD.Console.PrintMessage("inserting at %s\n"%(pos,))
self.addPoint(pos)
return
objs = Gui.ActiveDocument.ActiveView.getObjectsInfo(pos)
if objs:
for obj in objs:
try:
ob=FreeCAD.ActiveDocument.getObject(obj['Object']).Proxy
except AttributeError:
continue
if getattr(ob,'pytype',False) and ob.pytype == 'SMPoint':
self.obj=ob
if self.obj:
self.p0 = self.getPoint(pos)
self.op0 = self.obj.Coordinates
elif event['State'] == 'UP':
pos = event['Position']
if not self.obj:
return
delta = self.getPoint(pos) - self.p0
mesh = self.obj.getParentByType('SMesh')
self.obj.Coordinates = self.op0
mesh.doop('MovePoint',[str(self.obj.Label)],relative=True,x=delta.x,y=delta.y,z=delta.z)
self.obj=None
#else:
# FreeCAD.Console.PrintMessage("unhandled event %s\n"%(event,))
def getPoint(self,pos):
return Gui.ActiveDocument.ActiveView.getPoint(pos)
"""
import SurfaceEditing
reload(SurfaceEditing)
se = SurfaceEditing.SurfaceEdit()
se.Activated()
"""
FreeCADGui.seToolbar = SEToolbar()
FreeCADGui.seEditor = SurfaceEdit()
FreeCADGui.addCommand('Add Mesh', AddMesh())
FreeCADGui.addCommand('Insert Points', InsertPoints())
FreeCADGui.addCommand('Move Points', MovePoints())
FreeCADGui.addCommand('Toggle Crease', ToggleCrease())
FreeCADGui.addCommand('Add Property', AddProp())
FreeCADGui.addCommand('Add Edge', AddEdge())
FreeCADGui.addCommand('Add Face', AddFace())
FreeCADGui.addCommand('Clear Selection', ClearSelection())