From 07acfba4b94a2391909a5e3a1107a41e0c79d166 Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Thu, 6 Aug 2015 00:13:19 -0400 Subject: [PATCH] Made Shape.Center() more intelligent to handle switch-ups between Shape and Solid. --- .coverage | Bin 7935 -> 7943 bytes cadquery/CQ.py | 3 +-- cadquery/freecad_impl/shapes.py | 16 +++++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.coverage b/.coverage index 2d312f01cced111aa20603ac09caba734bfe487a..4129eb8cab1bdf69e0c7737f3544328908a0a655 100644 GIT binary patch delta 683 zcmWO2S!f6V9LI6L-(NzL9Mfp>fVm>q90?gAe_zFfup%KPM2Z|MFOHnA$fI(^P>ztX z<|xA45s{k^CM4|n`|zpnzu_B4R4pp5Jf9b`I&1P;Ue6nOGjC;G-pPA;KOg3!e4J16 zX+F#6`66HDn|zz^^J{*~`uveU^H=`PKlwNR(MU*GG#1T7foLt-h_<3ov=c?5t0)oO zL=Vwh^b!3<Kviv!}II3lXV8F5zBh+1)3Tot!P zowzIRi3j4LcqAT+C*qZOE#8Zd;*@o+; zL379)Hb+gBIcAQV6Xv8jWlozjrt+LQZ>r5jbIH`0T65W4G1tr+^VYmGAIwMd$$T-* zgMy%C&?;yh6b3~>hoCs<7<39sg6=`jpjXg47#NfWgM*R5sGuwu6O0X}1m&HBX~Fbh XR!|Yl3FZe2f`!4NU~$Y}xw6lH)kVX2 delta 675 zcmV~$XNV5~9L90a^E=5np_3Ojk#$C1$eCHi&lll}6jJsZ;wW$a$b5^uR7PeQp>Xa{ zgtH?e+l8!kDRHF+&>4NWiP2(=7%Rq!6ywDtF-=StGsHYGUn~%d#1gSo zEECJc3b9hG5^Kdev0iKto5U8eRcsU6#SXDk6n2TdV!t>hj*CiBC91`FaaGia>*9vE zC2os5;;y(So`|R7rKlCJ#T)TfycZwDNAXE~6?Njb_#^&`dPb(m)HlVZxhXMiOxuF# zY`U55rquK?WoD2WZbq1qW~@mw-b^-AO}UwA7MaCnnW-=<%xbg7Y&4tAPP5DGHhauI zv)>#r2hAaK*c>rO%`tProHVCg;IuhoDovHCHs{O*^UORqFU%`bYhIhT#z9fgAZQpg z3W|efL5rXyXc@E$+65hgPC@6OOVBGQ4f+HFf`LIA}ok QRxmr56U>dd6&1bz1KZES8UO$Q diff --git a/cadquery/CQ.py b/cadquery/CQ.py index 5ac4ceb..20d190c 100644 --- a/cadquery/CQ.py +++ b/cadquery/CQ.py @@ -685,8 +685,7 @@ class CQ(object): endVec = Vector(axisEndPoint) def _rot(obj): - # TODO: compute the weighted average instead of using the first solid - startPt = obj.Solids()[0].Center() + startPt = obj.Center() endPt = startPt + endVec return obj.rotate(startPt, endPt, angleDegrees) diff --git a/cadquery/freecad_impl/shapes.py b/cadquery/freecad_impl/shapes.py index e3227f9..a7e3d1c 100644 --- a/cadquery/freecad_impl/shapes.py +++ b/cadquery/freecad_impl/shapes.py @@ -185,7 +185,18 @@ class Shape(object): return BoundBox(self.wrapped.BoundBox) def Center(self): - return Vector(self.wrapped.CenterOfMass) + # A Part.Shape object doesn't have the CenterOfMass function, but it's wrapped Solid(s) does + if isinstance(self.wrapped, FreeCADPart.Shape): + # If there are no Solids, we're probably dealing with a Face or something similar + if len(self.Solids()) == 0: + return Vector(self.wrapped.CenterOfMass) + else: + # TODO: compute the weighted average instead of using the first solid + return Vector(self.Solids()[0].wrapped.CenterOfMass) + elif isinstance(self.wrapped, FreeCADPart.Solid): + return Vector(self.wrapped.CenterOfMass) + else: + raise ValueError("Cannot find the center of %s object type" % str(type(self.Solids()[0].wrapped))) def Closed(self): return self.wrapped.Closed @@ -848,8 +859,7 @@ class Compound(Shape): self.wrapped = obj def Center(self): - # TODO: compute the weighted average instead of the first solid - return self.Solids()[0].Center() + return self.Center() @classmethod def makeCompound(cls, listOfShapes):