Skip to content

Commit c7c7644

Browse files
committed
Fix: __sub__ method to prevent AttributeError when subtracting from CompoundGeometry
1 parent f4b3604 commit c7c7644

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

sectionproperties/pre/geometry.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,26 +1058,54 @@ def __sub__(self, other):
10581058
"""
10591059
Perform difference on Geometry objects with the - operator
10601060
"""
1061-
material = self.material or other.material
1061+
material = pre.DEFAULT_MATERIAL
1062+
if isinstance(self, Geometry):
1063+
material = self.material or other.material
10621064
try:
10631065
new_polygon = filter_non_polygons(self.geom - other.geom)
1064-
if isinstance(new_polygon, MultiPolygon):
1066+
if isinstance(new_polygon, GeometryCollection): # Non-polygon results
1067+
return None
1068+
elif isinstance(new_polygon, MultiPolygon):
10651069
return CompoundGeometry(
10661070
[Geometry(polygon, material) for polygon in new_polygon.geoms]
10671071
)
1068-
if isinstance(new_polygon, GeometryCollection):
1069-
return None
1070-
# Check to see if assigned_control_point is still valid
1071-
if self.assigned_control_point and new_polygon.contains(
1072-
self.assigned_control_point
1073-
):
1074-
return Geometry(new_polygon, material, self.control_points[0])
1075-
return Geometry(new_polygon, material)
1072+
elif isinstance(new_polygon, Polygon):
1073+
if not isinstance(self, CompoundGeometry):
1074+
if self.assigned_control_point and new_polygon.contains(
1075+
self.assigned_control_point
1076+
):
1077+
return Geometry(
1078+
new_polygon, material, self.assigned_control_point
1079+
)
1080+
else:
1081+
return Geometry(new_polygon, material)
1082+
else:
1083+
return Geometry(new_polygon, material)
10761084
except:
10771085
raise ValueError(
10781086
f"Cannot perform 'difference' on these two objects: {self} - {other}"
10791087
)
10801088

1089+
# material = self.material or other.material
1090+
# try:
1091+
# new_polygon = filter_non_polygons(self.geom - other.geom)
1092+
# if isinstance(new_polygon, MultiPolygon):
1093+
# return CompoundGeometry(
1094+
# [Geometry(polygon, material) for polygon in new_polygon.geoms]
1095+
# )
1096+
# if isinstance(new_polygon, GeometryCollection):
1097+
# return None
1098+
# # Check to see if assigned_control_point is still valid
1099+
# if self.assigned_control_point and new_polygon.contains(
1100+
# self.assigned_control_point
1101+
# ):
1102+
# return Geometry(new_polygon, material, self.control_points[0])
1103+
# return Geometry(new_polygon, material)
1104+
# except:
1105+
# raise ValueError(
1106+
# f"Cannot perform 'difference' on these two objects: {self} - {other}"
1107+
# )
1108+
10811109
def __add__(self, other):
10821110
"""
10831111
Combine Geometry objects into a CompoundGeometry using the + operator

0 commit comments

Comments
 (0)