-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAbroller.py
More file actions
440 lines (297 loc) · 9.48 KB
/
Abroller.py
File metadata and controls
440 lines (297 loc) · 9.48 KB
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
440
import FreeCAD
import FreeCADGui
import PySide
from PySide import QtGui
import os
App=FreeCAD
Gui=FreeCAD.Gui
import numpy as np
import Draft
import Part
import time
def updateData(obj,sk=None):
if sk != None: obj.path=sk
sk=obj.path
sk.Placement.Rotation.Angle=0
w=sk.Shape
# assert (abs(w.Vertexes[0].Point.y) <10**-2)
if obj.countPoints<5:obj.countPoints=5
if obj.densityPoints<1:obj.densityPoints=1
c=w.Edges[0].Curve
anz=obj.countPoints
pts=w.discretize(anz+1)
# pts=[FreeCAD.Vector(p.y,p.x,p.z) for p in pts]
obj.pathName=sk.Name+'_Path'
cyy=App.ActiveDocument.getObject(obj.pathName)
if cyy==None:
cyy=App.ActiveDocument.addObject('Part::Feature',obj.pathName)
##pol=Part.makePolygon(w.discretize(anz+1))
pol=Part.makePolygon(pts)
# obj.pola=w.discretize(200)
cyy.Shape=pol
##obj.pola=w.discretize(anz+1)
obj.pola=pts
pol=Part.makePolygon(obj.pola)
alphaq=[np.arctan2(p.y,p.x) for p in pts]
ns=np.arange(anz+1)
alpha=[]
for a in alphaq[:-1]:
if a <0: a += 2*np.pi
alpha += [a]
alpha += [2*np.pi]
# jetzt die Animationsschritte
anz2=10
anz2=4
rra=np.arange(2*anz2+1)*np.pi*2/(2*anz2+1)
# pos=np.interp(rra, alpha, ns)/(anz)
# if 0:
# import scipy
# from scipy.interpolate import interp1d
#
# fx = interp1d(alpha,ns,kind="cubic",bounds_error=False)
# pos=fx(rra)
# pos /=anz
#
# kk=[]
# for p in pos:
# kk += [c.value(p),FreeCAD.Vector()]
of=App.ActiveDocument.getObject(sk.Name+"_Offset")
if of==None:
of=App.ActiveDocument.addObject("Part::Offset2D",sk.Name+"_Offset")
of.Source = obj.path
of.Value = obj.offsetValue
obj.offset=of
App.activeDocument().recompute()
Gui.updateGui()
w2=of.Shape.Wires[0]
ptsa=[]
for e in w2.Edges:
#print "Wire ---",e
dian=int(round(e.Length*obj.densityPoints))
print(dian)
if dian<1: dian=1
try:
ptsa += e.discretize(dian+1)[:-1]
except:
print( "kann kante nicht discretize")
ptsa.reverse()
# print "ptsa"
# for p in ptsa:print p
alphaq=[np.arctan2(p.y,p.x) for p in ptsa]
# print "alphaq"
# for a in alphaq: print a
alpha=[]
for a in alphaq[:-1]:
if a <0: a += 2*np.pi
alpha += [a]
alpha += [2*np.pi+alpha[0]]
anz=len(alpha)
ns=np.arange(len(alpha))
#if obj.useBSpline:
bc=Part.BSplineCurve()
bc.approximate(ptsa,DegMin=1,DegMax=obj.degreeBSpline,Tolerance=obj.approxTolerance)
obj.polb=ptsa
if 0:
cww=App.ActiveDocument.getObject(obj.pathName+"_OFFSET")
if cww== None:
cww=App.ActiveDocument.addObject('Part::Feature',obj.pathName+'_OFFSET')
pol=Part.makePolygon(ptsa)
cww.Shape=pol
if obj.useBSpline:
cww.Shape=bc.toShape()
App.ActiveDocument.ActiveObject.ViewObject.LineColor=(1.,0.,0.)
App.ActiveDocument.ActiveObject.ViewObject.LineWidth=8
Gui.updateGui()
# jetzt die Animationsschritte
anz2=50
print("Interpolation anz, anz2",anz,anz2)
rra=np.arange(2*anz2+1)*np.pi*2/(2*anz2+1)
pos=np.interp(rra, alpha, ns)/(anz)
#-
# import scipy
# from scipy.interpolate import interp1d
# fx = interp1d(alpha,ns,kind="cubic",bounds_error=True)
# #fx = interp1d(alpha[2:-2],ns[2:-2],kind="linear",bounds_error=False)
# pos=fx(rra[2:-2])
# pos /=anz
#--
kk=[]
kka=[]
# print "pos --"
# for p in pos:
# print p
for p in pos:
pp=bc.value(p)
pp2=bc.value(p)
pp2.normalize()
kk += [pp,FreeCAD.Vector()]
kka += [pp]
Gui.updateGui()
if obj.circle==None:
obj.circle=Draft.makeCircle(obj.offsetValue)
obj.circle.Label="dummy Scooter"
if obj.circle2==None:
obj.circle2=Draft.makeCircle(1)
obj.circle2.Label="dummy Scooter center"
obj.kka=[FreeCAD.Vector(p) for p in kka]
obj.kka=ptsa
# Draft.makeWire(kk)
return pol
def runAnimation(obj=None,loop=False):
cww=App.ActiveDocument.getObject(obj.path.Name+"_Path")
cyy=App.ActiveDocument.getObject(obj.pathName)
if loop: ixs=obj.kka
else:
ixs=[obj.kka[obj.anim]]
ixs=[obj.kka[obj.anim-1]]
ll=len(obj.kka)
posl=int(round(obj.anim*ll*0.01))
ixs=[obj.kka[posl]]
# print "obj.kka",
# for k in obj.kka: print k
for i,p in enumerate(ixs):
pp=Part.Point(FreeCAD.Vector(p))
# print ("run Anim",i,pp)
if 0:
w=App.ActiveDocument.Sketch.Shape
dist=w.distToShape(pp.toShape())
print(dist[0])
testmode=True
# obj.circle.Placement.Base=p
# obj.circle.Radius=1
# kkxx=App.ActiveDocument.Compound001
kkxx=obj.scooter
#print kkxx.Label
if kkxx != None:
kkxx.Placement.Base=FreeCAD.Vector(p.Length,0,0)
alpha=np.arctan2(p.y,p.x)*180/np.pi
try:pos=obj.path.Shape.Curve.parameter(p)
except: pos=obj.path.Shape.Edge1.Curve.parameter(p)
arc2=-pos/obj.offsetValue/np.pi/2*180
#print ("rot",arc2,alpha)
if obj.mode=='rotate camshaft':
#print "circle ",alpha
obj.circle.Placement=FreeCAD.Placement(FreeCAD.Vector(),FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-alpha)).multiply(FreeCAD.Placement(p,FreeCAD.Rotation(FreeCAD.Vector(0,0,1),arc2+alpha)))
obj.circle2.Placement=obj.circle.Placement
if kkxx != None:
kkxx.Placement=obj.circle.Placement
else:
obj.circle.Placement=FreeCAD.Placement(p,FreeCAD.Rotation(FreeCAD.Vector(0,0,1),arc2+alpha))
obj.circle2.Placement=obj.circle.Placement
if kkxx != None:
kkxx.Placement=obj.circle.Placement
if obj.mode=='rotate camshaft':
cww.Shape=Part.makePolygon(obj.pola)
cww.Placement.Rotation=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-alpha)
cyy.Shape=Part.makePolygon(obj.polb)
cyy.Placement.Rotation=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-alpha)
# App.ActiveDocument.DWire.Placement.Rotation=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-alpha)
# kkyy=App.ActiveDocument.Compound
kkyy=obj.camshaft
# print kkyy.Label
if kkyy != None:
kkyy.Placement.Rotation=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-alpha)
# print cyy.Placement
if loop:
App.activeDocument().recompute()
Gui.updateGui()
time.sleep(0.1)
if i>10: return
def step(self,now):
self.obj2.time=float(now)
class Abroller:
''' basic defs'''
def __init__(self, obj):
obj.Proxy = self
self.Object = obj
def attach(self, vobj):
self.Object = vobj.Object
def __getstate__(self):
return None
def __setstate__(self, state):
return None
def execute(self,obj):
pass
# def attach(self,vobj):
# self.Object = vobj.Object
def onChanged(self,obj,prop):
# print "prop ",prop
if prop=='anim':
runAnimation(obj)
if prop in ['offsetValue','path','densityPoints','countPoints','degreeBSpline','useBSpline']:
updateData(obj)
if prop == "mode":
obj.camshaft.Placement.Rotation=FreeCAD.Rotation()
obj.offset.Placement.Rotation=FreeCAD.Rotation()
def step(self,now):
self.Object.anim=int(round(now))
def initialize(self):
pass
def onDocumentRestored(self, fp):
self.Object=fp
class ViewProvider:
''' basic defs '''
def __init__(self, obj):
obj.Proxy = self
self.Object = obj
def __getstate__(self):
return None
def __setstate__(self, state):
return None
def getIcon(self):
__dir__ = os.path.dirname(__file__)
return __dir__+ '/icons/abroller.png'
#-------------------------------
def createAbroller():
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","MeinAbroller")
obj.addProperty("App::PropertyLink", "circle", "_aux", "dummy scooter")
obj.addProperty("App::PropertyLink", "circle2", "_aux", "center of scooter")
obj.addProperty("App::PropertyLink", "scooter", "", "scooter")
obj.addProperty("App::PropertyLink", "camshaft", "", "camshaft")
obj.addProperty("App::PropertyLink", "offset", "_aux", "end")
obj.addProperty("App::PropertyLink", "path", "Base", "closed single wire path for the scooter")
obj.addProperty("App::PropertyInteger", "anim", "Base", "displayed frame number")
obj.addProperty("App::PropertyInteger", "countPoints", "_aux", "number of points of the path to interpolate").countPoints=40
obj.addProperty("App::PropertyInteger", "densityPoints", "_aux", "number of points of the generated curves per mm").densityPoints=4
obj.addProperty("App::PropertyFloat", "offsetValue", "Base", "size of the scooter").offsetValue=10.5
obj.addProperty("App::PropertyFloat", "approxTolerance", "_aux", "tolerance for BSpline Approximation").approxTolerance=1.
obj.addProperty("App::PropertyInteger", "degreeBSpline", "_aux", "degree of the BSpline ").degreeBSpline=3
obj.addProperty("App::PropertyBool", "useBSpline", "_aux", "use BSpline Approx for offset curve").useBSpline=True
# helper data
obj.addProperty("App::PropertyVectorList", "kka", "_comp", "end")
obj.addProperty("App::PropertyVectorList", "pola", "_comp", "end")
obj.addProperty("App::PropertyVectorList", "polb", "_comp", "end")
obj.addProperty("App::PropertyString", "pathName", "_aux", "label for the path")
obj.addProperty("App::PropertyEnumeration","mode","","Animation mode").mode=['rotate camshaft','roll scooter']
print("create abroller")
try: sk=Gui.Selection.getSelection()[0]
except: sk= None
try: m=Gui.Selection.getSelection()[1]
except: m=None
try: m2=Gui.Selection.getSelection()[2]
except: m2=None
shape=updateData(obj,sk)
Abroller(obj)
# obj.Proxy.Shape=shape
obj.Shape=Part.Shape()
ViewProvider(obj.ViewObject)
obj.scooter=m
obj.camshaft=m2
# obj.scooter=App.ActiveDocument.Compound001
# obj.camshaft=App.ActiveDocument.Compound
#if m != None:
# obj.setExpression('anim', m.Name+'.step')
# runAnimation(obj,True)
for prop in ["approxTolerance","degreeBSpline","densityPoints","useBSpline"]:
obj.setEditorMode(prop, 2)
'''
fuer das motor projekt
# drehen
dann spline
obj=App.ActiveDocument.Part__Feature086
import Draft
obj=App.ActiveDocument.Wire
pts=obj.Shape.discretize(40)
pts=[FreeCAD.Vector(p.y,p.x,p.z) for p in pts]
Draft.makeBSpline(pts)
'''