|
错误有点多,我给你修改了,这下就可以运行达到书上的效果了。- #coding=utf-8
- import rhinoscriptsyntax as rs
- def smoothingvector(point,prev_point,next_point,s):
- pm = (prev_point + next_point)/2
- va = rs.VectorCreate(pm,point)
- vm = rs.VectorScale(va,s)
- return vm
-
- def smoothingcurve(curve_id,s):
- curve_point = rs.CurvePoints(curve_id)
- new_curve_point=[]
- #还有一个逻辑错误就是你这里没有把起始点和终点加入列表
- new_curve_point.append(curve_point[0])
- for i in range(1,len(curve_point)-1):
- vm = smoothingvector(curve_point[i],curve_point[i-1],curve_point[i+1],s)
- #curve_point写错了
- new_curve_point.append(rs.PointAdd(curve_point[i],vm))
- #少了一个后括号
- new_curve_point.append(curve_point[-1])
- knot = rs.CurveKnots(curve_id)
- degree = rs.CurveDegree(curve_id)
- weights = rs.CurveWeights(curve_id)
- new_curve_id = rs.AddNurbsCurve(new_curve_point,knot,degree,weights)
- #new_curve_point写成new_curve_points了
- #if new_curve_id: rs.DeleteObject(curve_id)
- #把这句删了才能体现过程,达到和书的效果一样
- return new_curve_id
- def iterativeshortencurve():
- curve_id = rs.GetObject("Open curve to smooth",4,True)
- if curve_id is None or rs.IsCurveClosed(curve_id): return
-
- min = rs.Distance(rs.CurveStartPoint(curve_id),rs.CurveEndPoint(curve_id))
- max = rs.CurveLength(curve_id)
- goal = rs.GetReal("goal length",0.5*(min+max),min,max)
- if goal is None: return
-
- while rs.CurveLength(curve_id)>goal:
- rs.EnableRedraw(False)
- curve_id = smoothingcurve(curve_id, 0.1)
- rs.EnableRedraw(True)
- if curve_id is None: break
-
- iterativeshortencurve()
复制代码
|
|