forked from tomate44/CurvesWB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editableSpline.py
369 lines (323 loc) · 14.6 KB
/
editableSpline.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
from __future__ import division # allows floating point division from integers
import FreeCAD, Part, math
import os, dummy, FreeCADGui
from FreeCAD import Base
from pivy import coin
import CoinNodes
import selFilter
path_curvesWB = os.path.dirname(dummy.__file__)
path_curvesWB_icons = os.path.join( path_curvesWB, 'Resources', 'icons')
DEBUG = 1
def debug(string):
if DEBUG:
FreeCAD.Console.PrintMessage(string)
FreeCAD.Console.PrintMessage("\n")
def getString(weights):
weightStr = []
for w in weights:
if w == 1.0:
weightStr.append("")
elif w.is_integer():
weightStr.append(" %d"%int(w))
else:
weightStr.append(" %0.2f"%w)
return(weightStr)
class makeSpline:
def __init__(self, obj , edge):
''' Add the properties '''
debug("\Spline class Init\n")
obj.addProperty("App::PropertyIntegerConstraint", "Degree", "General", "Degree")
obj.addProperty("App::PropertyInteger", "Pole", "Poles", "Pole number").Pole = 1
obj.addProperty("App::PropertyFloat", "X", "Poles", "X coordinate of the selected pole").X=0.0
obj.addProperty("App::PropertyFloat", "Y", "Poles", "Y coordinate of the selected pole").Y=0.0
obj.addProperty("App::PropertyFloat", "Z", "Poles", "Z coordinate of the selected pole").Z=0.0
obj.addProperty("App::PropertyFloatConstraint", "W", "Poles", "Weight of the selected pole")
obj.addProperty("App::PropertyVectorList", "Poles", "General", "Poles")
obj.addProperty("App::PropertyVectorList", "KnotPoints","General", "KnotPoints")
obj.addProperty("App::PropertyFloatList", "Weights", "General", "Weights")
obj.addProperty("App::PropertyFloatList", "Knots", "General", "Knots")
obj.addProperty("App::PropertyIntegerList", "Mults", "General", "Mults")
obj.addProperty("App::PropertyVectorList", "CurvePts", "General", "CurvePts")
#obj.addProperty("Part::PropertyPartShape", "Shape", "General", "Shape")
obj.Proxy = self
try:
self.curve = edge.Curve.toBSpline()
self.curve.segment(edge.FirstParameter,edge.LastParameter)
except:
e = edge.toNurbs().Edges[0]
self.curve = e.Curve.toBSpline()
self.curve.segment(e.FirstParameter,e.LastParameter)
obj.Poles = self.curve.getPoles()
obj.Weights = self.curve.getWeights()
if isinstance(self.curve,Part.BSplineCurve):
obj.Knots = self.curve.getKnots()
self.getKnotPoints(obj)
obj.Mults = [int(i) for i in self.curve.getMultiplicities()]
else:
obj.Knots = []
obj.KnotPoints = []
obj.Mults = []
obj.Degree = (int(self.curve.Degree),1,8,1)
obj.W = (1.0,0.0001,1000.0,0.1)
obj.setEditorMode("Weights", 2)
self.execute(obj)
#def curve(self, obj):
#if obj.Knots:
#bs = Part.BSplineCurve()
#bs.buildFromPolesMultsKnots(obj.Poles, obj.Mults, obj.Knots, False, obj.Degree, obj.Weights)
#else:
#bs = Part.BezierCurve()
#bs.increase(obj.Degree)
#bs.setPoles(obj.Poles)
#for i in range(len(obj.Weights)):
#bs.setWeight(i+1,obj.Weights[i])
#return(bs)
def getKnotPoints(self, obj):
knotPoints = []
for k in obj.Knots:
p = self.curve.value(k)
knotPoints.append((p.x,p.y,p.z))
obj.KnotPoints = knotPoints
def checkCurve(self, obj):
try:
c = self.curve
except:
if hasattr(obj.Shape,'Curve'):
self.curve = obj.Shape.Curve
def execute(self, obj):
debug("\n* Spline : execute *\n")
self.checkCurve(obj)
obj.CurvePts = self.curve.discretize(100)
obj.Shape = self.curve.toShape()
def onChanged(self, fp, prop):
self.checkCurve(fp)
if (prop == "Degree"):
if fp.Degree > int(self.curve.Degree):
if isinstance(self.curve,Part.BezierCurve):
self.curve.increase(fp.Degree)
elif isinstance(self.curve,Part.BSplineCurve):
self.curve.increaseDegree(fp.Degree)
elif fp.Degree < int(self.curve.Degree):
pts = self.curve.discretize(Number = 100)
bscurve = Part.BSplineCurve() #self.curve.approximateBSpline(0.1,12,fp.Degree,'C2')
bscurve.approximate(Points = pts, DegMin = fp.Degree, DegMax = fp.Degree, Tolerance = 0.01)
self.curve = bscurve
fp.Degree = int(bscurve.Degree)
fp.Poles = self.curve.getPoles()
fp.Weights = self.curve.getWeights()
if isinstance(self.curve,Part.BSplineCurve):
fp.Knots = self.curve.getKnots()
self.getKnotPoints(fp)
fp.Mults = [int(i) for i in self.curve.getMultiplicities()]
fp.Pole = fp.Pole
fp.CurvePts = self.curve.discretize(100)
debug("Spline : Degree changed to "+str(fp.Degree)+"\n")
if prop == "Pole":
if fp.Pole < 1:
fp.Pole = 1
elif fp.Pole > len(self.curve.getPoles()):
fp.Pole = len(self.curve.getPoles())
v = self.curve.getPole(fp.Pole)
w = self.curve.getWeight(fp.Pole)
fp.X = v.x
fp.Y = v.y
fp.Z = v.z
fp.W = w
#fp.Poles = self.curve.getPoles()
#fp.Weights = self.curve.getWeights()
#fp.touch()
debug("Spline : Pole changed to "+str(fp.Pole)+"\n")
if (prop == "X") | (prop == "Y") | (prop == "Z"):
v = FreeCAD.Vector(fp.X,fp.Y,fp.Z)
self.curve.setPole(fp.Pole,v)
fp.Poles = self.curve.getPoles()
debug("Spline : Coordinate changed\n")
if (prop == "W"):
#v = FreeCAD.Vector(fp.X,fp.Y,fp.Z)
self.curve.setWeight(fp.Pole,fp.W)
fp.Weights = self.curve.getWeights()
debug("Spline : Weight changed\n")
if (prop == "Poles"):
#fp.Weights = self.curve.getWeights()
debug("Spline : Poles changed\n")
if (prop == "Weights"):
#fp.Poles = self.curve.getPoles()
debug("Spline : Weights changed\n")
if (prop == "Knots"):
if fp.Knots:
self.getKnotPoints(fp)
#fp.Mults = [int(i) for i in self.curve.getMultiplicities()]
debug("Spline : Knots changed\n")
if (prop == "Mults"):
#fp.Knots = self.curve.getKnots()
debug("Spline : Mults changed\n")
oldMults = self.curve.getMultiplicities()
if len(fp.Mults) == len(oldMults):
for i in range(len(fp.Mults)):
if fp.Mults[i] > oldMults[i]:
self.curve.increaseMultiplicity(i+1,fp.Mults[i])
elif fp.Mults[i] < oldMults[i]:
self.curve.removeKnot(i+1, fp.Mults[i], 0.01) # add property tolerance here
fp.Mults = [int(i) for i in self.curve.getMultiplicities()]
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class SplineVP:
def __init__(self, obj ):
''' Add the properties '''
self.oldDM = 'Wireframe'
obj.Proxy = self
def attach(self, obj):
debug("\nSplineVP.attach \n")
self.curveDM = coin.SoSeparator()
self.polesDM = coin.SoSeparator()
self.polesDM.setName("Poles")
self.curveDM.setName("Curve")
self.curvePolesDM = coin.SoSeparator()
# *** Set the Poles view nodes ***
self.polesnode = CoinNodes.coordinate3Node()
self.weightStr = []
self.polySep = CoinNodes.polygonNode((0.5,0.5,0.5),1)
self.markerSep = CoinNodes.markerSetNode((1,0,0),coin.SoMarkerSet.DIAMOND_FILLED_7_7)
self.weightSep = CoinNodes.multiTextNode((1,0,0),"osiFont,FreeSans,sans",16,0)
# *** Set knots ***
#knotPoints = []
#for k in knots:
#p = cur.value(k)
#knotPoints.append((p.x,p.y,p.z))
# *** Set the knots view nodes ***
self.knotsnode = CoinNodes.coordinate3Node() #knotPoints)
self.multStr = []
self.knotMarkerSep = CoinNodes.markerSetNode((0,0,1),coin.SoMarkerSet.CIRCLE_FILLED_7_7)
self.multSep = CoinNodes.multiTextNode((0,0,1),"osiFont,FreeSans,sans",16,1)
# *** Set the active pole view nodes ***
self.activePole = CoinNodes.markerSetNode((1,1,0),coin.SoMarkerSet.CIRCLE_LINE_9_9)
self.activePole.markers.startIndex = 0
self.activePole.markers.numPoints = 1
# *** Set the curve view node ***
self.curvePts = CoinNodes.coordinate3Node()
self.curveSep = CoinNodes.polygonNode((0,0,0),1)
self.polesDM.addChild(self.polesnode)
self.polesDM.addChild(self.polySep)
self.polesDM.addChild(self.markerSep)
self.polesDM.addChild(self.weightSep)
self.polesDM.addChild(self.activePole)
self.polesDM.addChild(self.knotsnode)
self.polesDM.addChild(self.knotMarkerSep)
self.polesDM.addChild(self.multSep)
self.curveDM.addChild(self.curvePts)
self.curveDM.addChild(self.curveSep)
#self.selectionNode = coin.SoType.fromName("SoFCSelection").createInstance()
#self.selectionNode.documentName.setValue(FreeCAD.ActiveDocument.Name)
#self.selectionNode.objectName.setValue(obj.Object.Name) # here obj is the ViewObject, we need its associated App Object
#self.selectionNode.subElementName.setValue("Curve")
#self.selectionNode.addChild(self.curveDM)
self.curvePolesDM.addChild(self.curveDM)
self.curvePolesDM.addChild(self.polesDM)
#self.curveDM.addChild(self.selectionNode)
#self.curvePolesDM.addChild(self.selectionNode)
#obj.addDisplayMode(self.curveDM,"Curve")
obj.addDisplayMode(self.curvePolesDM,"Poles")
def updateData(self, fp, prop):
#debug("updateData : "+str(prop)+"\n")
if prop == "Poles":
self.polesnode.points = fp.Poles
self.weightStr = getString(fp.Weights)
self.polySep.vertices = self.polesnode.points
debug("--- "+str(len(self.weightStr))+"\n")
debug("--- "+str(len(self.polesnode.points))+"\n")
self.weightSep.data = (self.polesnode.points,self.weightStr)
if prop == "Weights":
self.polesnode.points = fp.Poles
self.weightStr = getString(fp.Weights)
debug("--- "+str(len(self.weightStr))+"\n")
debug("--- "+str(len(self.polesnode.points))+"\n")
#self.polySep.vertices = self.polesnode.points
self.weightSep.data = (self.polesnode.points,self.weightStr)
if prop == "Knots":
if fp.Knots:
self.knotsnode.points = fp.KnotPoints
self.multStr = []
for m in fp.Mults:
self.multStr.append("%d"%m)
#self.polySep.vertices = self.polesnode.points
self.multSep.data = (self.knotsnode.points,self.multStr)
else:
self.knotsnode.points = [fp.Poles[0]]
#elif fp.Poles:
#self.knotsnode.points = [fp.Poles[0]]
#self.multStr = [""]
#self.multSep.data = (self.knotsnode.points,self.multStr)
#else:
#debug("%s"%str(fp.Poles[0]))
if prop == "Mults":
if fp.Mults:
#self.polesnode.points = fp.Poles
self.multStr = []
for m in fp.Mults:
self.multStr.append("%d"%m)
#self.polySep.vertices = self.polesnode.points
self.multSep.data = (self.knotsnode.points,self.multStr)
if prop == "Pole":
self.activePole.markers.startIndex = fp.Pole - 1
if prop == "CurvePts":
self.curvePts.points = fp.CurvePts
self.curveSep.vertices = self.curvePts.points
def getDisplayModes(self,obj):
"Return a list of display modes."
modes=[]
#modes.append("Curve")
modes.append("Poles")
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'''
debug("\nonChanged : "+str(prop)+"\n")
#if prop == "Edge":
#debug("\n\n\nvp detected a Edge change\n\n\n")
#if prop == "CurveColor":
#self.curveColor.rgb = (vp.CurveColor[0],vp.CurveColor[1],vp.CurveColor[2])
#elif prop == "CombColor":
#self.combColor.rgb = (vp.CombColor[0],vp.CombColor[1],vp.CombColor[2])
return
def doubleClicked(self,vobj):
debug("Double-clicked")
if not vobj.DisplayMode == "Poles":
self.oldDM = vobj.DisplayMode
vobj.DisplayMode = "Poles"
else:
vobj.DisplayMode = self.oldDM
return True
def getIcon(self):
return (path_curvesWB_icons+'/editableSpline.svg')
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class editableSpline:
def Activated(self):
s = FreeCADGui.Selection.getSelectionEx()
f = selFilter.selFilter(s)
edges = f.getEdgeShapes()
vertexes = f.getVertexShapes()
for i in range(0,len(vertexes)-1,2):
pts = [vertexes[i].Point,vertexes[i+1].Point]
bez = Part.BezierCurve()
bez.setPoles(pts)
print(bez)
edges.append(bez.toShape())
for e in edges:
obj=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Spline") #add object to document
makeSpline(obj,e)
SplineVP(obj.ViewObject)
obj.ViewObject.DisplayMode = "Poles"
f.hideAll()
FreeCAD.ActiveDocument.recompute()
def GetResources(self):
return {'Pixmap' : path_curvesWB_icons+'/editableSpline.svg', 'MenuText': 'editableSpline', 'ToolTip': 'Creates an editable spline from selected edges'}
FreeCADGui.addCommand('editableSpline', editableSpline())