Skip to content

Commit 928684e

Browse files
committed
Fix ngon handling bug in compas_ghpython
1 parent 698a204 commit 928684e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
* Generalized the parameter `color` of `compas_blender.draw_texts` and various label drawing methods.
3939
* Changed `compas.IPY` to `compas.RHINO` in `orientation_rhino`.
4040
* Changed `planarity` to `requires_extra` for pip installations.
41+
* Fixed bug in handling of ngonal meshes in `compas_ghpython` artists / drawing functions.
4142

4243
### Removed
4344

src/compas_ghpython/utilities/drawing.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import rhinoscriptsyntax as rs
1212
import scriptcontext as sc
1313

14+
from compas.geometry import centroid_points
15+
from compas.utilities import pairwise
16+
1417
from Rhino.Geometry import Point3d
1518
from Rhino.Geometry import Vector3d
1619
from Rhino.Geometry import Line
@@ -26,6 +29,11 @@
2629
from Rhino.Geometry import Vector3f
2730
from Rhino.Geometry import Point2f
2831

32+
try:
33+
from Rhino.Geometry import MeshNgon
34+
except ImportError:
35+
MeshNgon = False
36+
2937
TOL = sc.doc.ModelAbsoluteTolerance
3038

3139

@@ -374,10 +382,22 @@ def draw_mesh(vertices, faces, color=None, vertex_normals=None, texture_coordina
374382
for a, b, c in vertices:
375383
mesh.Vertices.Add(a, b, c)
376384
for face in faces:
377-
if len(face) < 4:
378-
mesh.Faces.AddFace(face[0], face[1], face[2])
385+
f = len(face)
386+
if f < 3:
387+
continue
388+
if f == 3:
389+
mesh.Faces.AddFace(*face)
390+
elif f == 4:
391+
mesh.Faces.AddFace(*face)
379392
else:
380-
mesh.Faces.AddFace(face[0], face[1], face[2], face[3])
393+
if MeshNgon:
394+
centroid = centroid_points([vertices[index] for index in face])
395+
c = mesh.Vertices.Add(*centroid)
396+
facets = []
397+
for i, j in pairwise(face + face[:1]):
398+
facets.append(mesh.Faces.AddFace(i, j, c))
399+
ngon = MeshNgon.Create(face, facets)
400+
mesh.Ngons.AddNgon(ngon)
381401

382402
if vertex_normals:
383403
count = len(vertex_normals)
@@ -394,7 +414,7 @@ def draw_mesh(vertices, faces, color=None, vertex_normals=None, texture_coordina
394414
mesh.TextureCoordinates.SetTextureCoordinates(tcs)
395415

396416
if color:
397-
count = len(vertices)
417+
count = len(mesh.Vertices)
398418
colors = CreateInstance(Color, count)
399419
for i in range(count):
400420
colors[i] = rs.coercecolor(color)

0 commit comments

Comments
 (0)