I added the Apply button and decided to share the result as it makes testing easier. As for the Close button. The macro is in too rough state ATM to care about the Close button. As it wouldn't do the correct thing anyway.

- Apply.png (28.9 KiB) Viewed 2112 times
Code: Select all
# version 0.1
from PySide import QtGui
from PySide import QtCore
import FreeCAD,Draft
import nurbswb.needle
from nurbswb.needle import npa2ssa,ssa2npa
def updateSS(ss,curve):
'''update curve data into spreadsheet'''
ss.clearAll()
npa2ssa(curve,ss,2,3,(1.0,1.0,0.5))
ss.set('B1',str(len(curve)))
App.activeDocument().recompute()
def updateDraft(ss,obj):
'''update Draft Bspline object points from spreadsheet ss'''
cl=int(ss.get('B1'))
curve=ssa2npa(ss,2,3,4,3+cl-1)
obj.Points=[FreeCAD.Vector(c) for c in curve]
global writeBack,ss,obj2
def writeBack():
global ss
global obj2
updateDraft(ss,obj2)
App.activeDocument().recompute()
Gui.SendMsgToActiveView("ViewFit")
btnApply = QtGui.QPushButton("Apply")
btnApply.clicked.connect(writeBack)
def pressed(index):
pass
def clicked():
pass
def undock(ss):
''' open the data spreadsheet as top level window'''
label=ss.Label
mw=FreeCADGui.getMainWindow()
mdiarea=mw.findChild(QtGui.QMdiArea)
ss.ViewObject.startEditing(0)
subw=mdiarea.subWindowList()
for i in subw:
if i.widget().metaObject().className() == "SpreadsheetGui::SheetView":
sheet = i.widget()
table=sheet.findChild(QtGui.QTableView)
print "table found"
break
# table.clicked.connect(clicked)
table.pressed.connect(pressed)
sws=mdiarea.subWindowList()
print "windows ..."
for w2 in sws:
print str(w2.windowTitle())
if str(w2.windowTitle()).startswith(label):
sw=w2
print "found"
bl=w2.children()[3]
blcc=bl.children()[2].children()
w=QtGui.QWidget()
w.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
buttons = QtGui.QHBoxLayout()
buttons.addStretch(0)
buttons.addWidget(btnApply)
box = QtGui.QVBoxLayout()
w.setLayout(box)
ss=blcc[3]
box.addWidget(ss)
box.insertLayout(1, buttons)
w.setGeometry(50, 30, 650, 350)
w.show()
sw.close()
return w
# test case
#create the Bspline
p1 = FreeCAD.Vector(0,0,0)
p2 = FreeCAD.Vector(1,1,0)
p3 = FreeCAD.Vector(0,2,0)
p4 = FreeCAD.Vector(-1,1,0)
Draft.makeBSpline([p1,p2,p3,p4],closed=True)
# extract the data
obj=App.ActiveDocument.ActiveObject
bc=obj.Shape.Edge1.Curve
poles=bc.getPoles()
knots=bc.getKnots()
we=bc.getWeights()
mults=bc.getMultiplicities()
pts=obj.Points
poles
knots
we
mults
# create a d fill the spreadsheet
ss = App.activeDocument().addObject('Spreadsheet::Sheet','Spreadsheet')
updateSS(ss,pts)
# create an other Draft BSpline
p1 = FreeCAD.Vector(0,0,0)
p2 = FreeCAD.Vector(10,1,0)
p3 = FreeCAD.Vector(0,2,0)
Draft.makeBSpline([p1,p2,p3],closed=True)
obj2=App.ActiveDocument.ActiveObject
Gui.SendMsgToActiveView("ViewFit")
# write Points back
updateDraft(ss,obj2)
# open Spreadsheet
w=undock(ss)