from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
from draftobjects.base import DraftObject
from draftutils import gui_utils
from draftutils import params
class BSpline(DraftObject):
def __init__(self, obj):
super().__init__(obj, "BSpline")
_tip = QT_TRANSLATE_NOOP("App::Property", "The points of the B-spline")
obj.addProperty("App::PropertyVectorList", "Points", "Draft", _tip, locked=True)
_tip = QT_TRANSLATE_NOOP("App::Property", "If the B-spline is closed or not")
obj.addProperty("App::PropertyBool", "Closed", "Draft", _tip, locked=True)
_tip = QT_TRANSLATE_NOOP("App::Property", "Create a face if this B-spline is closed")
obj.addProperty("App::PropertyBool", "MakeFace", "Draft", _tip, locked=True)
_tip = QT_TRANSLATE_NOOP("App::Property", "The area of this object")
obj.addProperty("App::PropertyArea", "Area", "Draft", _tip, locked=True)
obj.MakeFace = params.get_param("MakeFaceMode")
obj.Closed = False
obj.Points = []
self.assureProperties(obj)
def onDocumentRestored(self, obj):
super().onDocumentRestored(obj)
gui_utils.restore_view_object(obj, vp_module="view_bspline", vp_class="ViewProviderBSpline")
def assureProperties(self, obj): if not hasattr(obj, "Parameterization"):
_tip = QT_TRANSLATE_NOOP("App::Property", "Parameterization factor")
obj.addProperty("App::PropertyFloat", "Parameterization", "Draft", _tip, locked=True)
obj.Parameterization = 1.0
self.knotSeq = []
def parameterization(self, pts, a, closed):
if closed: pts.append(pts[0])
params = [0]
for i in range(1, len(pts)):
p = pts[i].sub(pts[i - 1])
pl = pow(p.Length, a)
params.append(params[-1] + pl)
return params
def onChanged(self, fp, prop):
self.props_changed_store(prop)
if prop == "Parameterization":
if fp.Parameterization < 0.0:
fp.Parameterization = 0.0
if fp.Parameterization > 1.0:
fp.Parameterization = 1.0
def execute(self, obj):
if self.props_changed_placement_only() or not obj.Points:
obj.positionBySupport()
self.props_changed_clear()
return
import Part
self.assureProperties(obj)
self.knotSeq = self.parameterization(obj.Points, obj.Parameterization, obj.Closed)
plm = obj.Placement
if obj.Closed and (len(obj.Points) > 2):
if obj.Points[0] == obj.Points[-1]: _err = QT_TRANSLATE_NOOP(
"Draft",
"_BSpline.createGeometry: "
"Closed with same first/last Point. Geometry not updated.",
)
App.Console.PrintError(_err + "\n")
return
spline = Part.BSplineCurve()
spline.interpolate(obj.Points, PeriodicFlag=True, Parameters=self.knotSeq)
shape = Part.Wire(spline.toShape())
try:
if hasattr(obj, "MakeFace"):
if obj.MakeFace:
shape = Part.Face(shape)
else:
shape = Part.Face(shape)
except Part.OCCError:
pass
obj.Shape = shape
if hasattr(obj, "Area") and hasattr(shape, "Area"):
obj.Area = shape.Area
else:
spline = Part.BSplineCurve()
spline.interpolate(obj.Points, PeriodicFlag=False, Parameters=self.knotSeq)
shape = spline.toShape()
obj.Shape = shape
if hasattr(obj, "Area") and hasattr(shape, "Area"):
obj.Area = shape.Area
obj.Placement = plm
obj.positionBySupport()
self.props_changed_clear()
_BSpline = BSpline