Skip to content

Commit 4b42974

Browse files
committed
FIX: __sub__ now conforms to generally expected behaviour
1 parent c7c7644 commit 4b42974

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

sectionproperties/pre/geometry.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,18 +1059,23 @@ def __sub__(self, other):
10591059
Perform difference on Geometry objects with the - operator
10601060
"""
10611061
material = pre.DEFAULT_MATERIAL
1062-
if isinstance(self, Geometry):
1063-
material = self.material or other.material
1064-
try:
1065-
new_polygon = filter_non_polygons(self.geom - other.geom)
1066-
if isinstance(new_polygon, GeometryCollection): # Non-polygon results
1067-
return None
1068-
elif isinstance(new_polygon, MultiPolygon):
1069-
return CompoundGeometry(
1070-
[Geometry(polygon, material) for polygon in new_polygon.geoms]
1071-
)
1072-
elif isinstance(new_polygon, Polygon):
1073-
if not isinstance(self, CompoundGeometry):
1062+
if isinstance(self, CompoundGeometry):
1063+
subs_geom_acc = []
1064+
for geom in self.geoms:
1065+
new_geom = geom - other
1066+
subs_geom_acc.append(new_geom)
1067+
return CompoundGeometry(subs_geom_acc)
1068+
else:
1069+
material = self.material or pre.DEFAULT_MATERIAL
1070+
try:
1071+
new_polygon = filter_non_polygons(self.geom - other.geom)
1072+
if isinstance(new_polygon, GeometryCollection): # Non-polygon results
1073+
return None
1074+
elif isinstance(new_polygon, MultiPolygon):
1075+
return CompoundGeometry(
1076+
[Geometry(polygon, material) for polygon in new_polygon.geoms]
1077+
)
1078+
elif isinstance(new_polygon, Polygon):
10741079
if self.assigned_control_point and new_polygon.contains(
10751080
self.assigned_control_point
10761081
):
@@ -1079,12 +1084,10 @@ def __sub__(self, other):
10791084
)
10801085
else:
10811086
return Geometry(new_polygon, material)
1082-
else:
1083-
return Geometry(new_polygon, material)
1084-
except:
1085-
raise ValueError(
1086-
f"Cannot perform 'difference' on these two objects: {self} - {other}"
1087-
)
1087+
except:
1088+
raise ValueError(
1089+
f"Cannot perform 'difference' on these two objects: {self} - {other}"
1090+
)
10881091

10891092
# material = self.material or other.material
10901093
# try:

0 commit comments

Comments
 (0)