Creating Surface.ByPatch from a polyline with another polyline inside

Hello. I have a difficulty with creating a surface that has a hole inside. I need to do it in Python (need for for loops) but when I try to find out if there are any surfaces covering other surfaces with IntersectAll all I get is array full of zeros.

This is the code that i used:

import clr
import os
import traceback

# Import openpyxl

try:
import openpyxl
from openpyxl import load_workbook
except ImportError:
raise Exception(“Moduł openpyxl nie jest dostępny. Zainstaluj go w środowisku Dynamo.”)

# DesignScript (Dynamo) Geometry

clr.AddReference(“ProtoGeometry”)
from Autodesk.DesignScript.Geometry import \*

polyline_geometry_isclosed_list = IN\[0\]
filtered_polylines = IN\[1\]
excel_path = IN\[2\]
circle_surface = IN\[3\]
circle_objects = IN\[4\]

# ================= ZMIENNE =================

surfaces = [ ]
errors = [ ]
polyline_surface_pairs = [ ]
debug_lisp_entries = [ ]
intersect_data = [ ]
circle_surface_pairs = list(zip(circle_objects, circle_surface))

for i, (polyline, geometry, is_closed) in enumerate(polyline_geometry_isclosed_list):
if geometry **is** **not** None **and** is_closed == True:
try:
srf = Surface.ByPatch(geometry)
surfaces.append(srf)
errors.append(None)
polyline_surface_pairs.append(\[polyline, srf\])
except Exception **as** e:
surfaces.append(None)
errors.append(traceback.format_exc())
polyline_surface_pairs.append(\[polyline, None\])
else:
surfaces.append(None)
errors.append(“Brak geometrii lub niezamykanie”)
polyline_surface_pairs.append(\[polyline, None\])
polyline_surface_pairs.extend(circle_surface_pairs)
intersections = \[\]
int_areas = \[\]
errors = \[\]

surfaces_all = list({pair\[1\] for pair in polyline_surface_pairs if pair\[1\] **is** **not** None})

for i, (polyline1, surface1) in enumerate(polyline_surface_pairs):
    area_1 = surface1.Area
    total_area = 0

    for surface2 in surfaces_all:
        if surface2 == surface1:
            continue
        try:
            area_2 = surface2.Area
            if area_1 > area_2:
                # Intersect surface1 with surface2
                intersection = surface1.IntersectAll(\[surface2\])
                if intersection **and** isinstance(intersection, list):
                    for geom in intersection:
                        if hasattr(geom, "Area"):
                            total_area += geom.Area

        except Exception **as** e:
            errors.append(f"Błąd IntersectAll dla surface1\[{i}\] z inną powierzchnią: {str(e)}")

    int_areas.append(total_area)

And visual representation of a problem (I dont want to create hatches :))

Can you explain what you’re providing as inputs? Assuming these are Dynamo surfaces, I think you can process these much faster using OOTB nodes rather than this, but I can’t tell just yet as what you’re after isn’t entirely clear.

My inputs are:

polyline_geometry_isclosed_list = IN\[0\] - a list that has a structure [[polyline, geometry_of_polyline, is_the_polyline_closed],…] - it is the output of a for function in another node

filtered_polylines = IN\[1\] - list of surfaces from another set of filtered polylines (I know I named it badly but it is not used in a problem I’m trying to fix right now so it’s fine). I use it in this part of code and it works fine:

# ================= INTERSEKCJE ================= #
surfaces_in1 = filtered_polylines # zakładamy, że to lista powierzchni
surfaces_generated = surfaces

for i, s1 in enumerate(surfaces_in1):
if s1 is None:
continue
for j, s2 in enumerate(surfaces_generated):
if s2 is None:
continue
try:
result = s1.Intersect(s2)
if result and len(result) > 0:
total_area = 0
for geom in result:
if hasattr(geom, “Area”):
total_area += geom.Area
if total_area > 0:
layer1 = “Brak”
try:
polyline = polyline_surface_pairs[j][0]
layer2 = getattr(polyline, “Layer”, “Brak”)
except:
layer2 = “Brak”
intersect_data.append([i+1, j, total_area, layer2])
except Exception as e:
errors.append(f"Błąd przecięcia s1[{i}] z s2[{j}]: {str(e)}")

With the exception that the surfaces from surfaces_generated = surfaces dont have holes that I am trying to create :slight_smile:

excel_path = IN\[2\] - excel path to write down everything (works fine)

circle_surface = IN\[3\], circle_objects = IN\[4\] - I add this to the list of polyline_surface_pairs beacuse I havent took it into consideration before

But you’re completely right. I maybe should do it somewhere outside the python node because it works well when I’m using normal DoesIntersect, IntersectAll and Difference nodes.

1 Like

So the goal is that given a list of shapes like this:

You want a set of surfaces like this:

Which means you need a graph like this:

No python needed. :wink:

If you want to suppress the warning, you can shift back to Python and deal with the error handling, or you can look into function passing, a custom node, or other advanced tooling.

2 Likes

Thank you so much!

1 Like