improve workplane() method to create workplanes from multiple co-planar face selection

This commit is contained in:
Hasan Yavuz ÖZDERYA 2015-09-17 22:40:22 +03:00
parent 8763d42505
commit ea4ba70b06
2 changed files with 44 additions and 23 deletions

View File

@ -298,11 +298,11 @@ class CQ(object):
For now you can work around by creating a workplane and then offsetting the center For now you can work around by creating a workplane and then offsetting the center
afterwards. afterwards.
""" """
if len(self.objects) > 1: def _isCoPlanar(f0, f1):
raise ValueError("Workplane cannot be created if more than" p0 = f0.Center()
" 1 object is selected.") p1 = f1.Center()
n = f0.normalAt()
obj = self.objects[0] return abs(n.dot(p0.sub(p1)) < self.ctx.tolerance)
def _computeXdir(normal): def _computeXdir(normal):
""" """
@ -317,28 +317,33 @@ class CQ(object):
xd = Vector(1, 0, 0) xd = Vector(1, 0, 0)
return xd return xd
faceToBuildOn = None if len(self.objects) > 1:
center = None # are all objects 'PLANE'?
#if isinstance(obj,Vertex): if not all(o.geomType() == 'PLANE' for o in self.objects):
# f = self.parent.objects[0] raise ValueError("If multiple objects selected, they all must be planar faces.")
# if f != None and isinstance(f,Face):
# center = obj.Center() # are all faces co-planar with each other?
# normal = f.normalAt(center) if not all(_isCoPlanar(self.objects[0], f) for f in self.objects[1:]):
# xDir = _computeXdir(normal) raise ValueError("Selected faces must be co-planar.")
# else:
# raise ValueError("If a vertex is selected, a face must be the immediate parent") center = Shape.CombinedCenter(self.objects)
if isinstance(obj, Face): normal = self.objects[0].normalAt()
faceToBuildOn = obj
center = obj.Center()
normal = obj.normalAt(center)
xDir = _computeXdir(normal) xDir = _computeXdir(normal)
else: else:
if hasattr(obj, 'Center'): obj = self.objects[0]
if isinstance(obj, Face):
center = obj.Center() center = obj.Center()
normal = self.plane.zDir normal = obj.normalAt(center)
xDir = self.plane.xDir xDir = _computeXdir(normal)
else: else:
raise ValueError("Needs a face or a vertex or point on a work plane") if hasattr(obj, 'Center'):
center = obj.Center()
normal = self.plane.zDir
xDir = self.plane.xDir
else:
raise ValueError("Needs a face or a vertex or point on a work plane")
#invert if requested #invert if requested
if invert: if invert:

View File

@ -198,6 +198,22 @@ class Shape(object):
else: else:
raise ValueError("Cannot find the center of %s object type" % str(type(self.Solids()[0].wrapped))) raise ValueError("Cannot find the center of %s object type" % str(type(self.Solids()[0].wrapped)))
@staticmethod
def CombinedCenter(objects):
"""
Calculates the center of mass of multiple objects.
:param objects: a list of objects with mass
"""
total_mass = sum(o.wrapped.Mass for o in objects)
weighted_centers = [o.wrapped.CenterOfMass.multiply(o.wrapped.Mass) for o in objects]
sum_wc = weighted_centers[0]
for wc in weighted_centers[1:] :
sum_wc = sum_wc.add(wc)
return Vector(sum_wc.multiply(1./total_mass))
def Closed(self): def Closed(self):
return self.wrapped.Closed return self.wrapped.Closed