|
6 | 6 | import scriptcontext as sc # type: ignore |
7 | 7 |
|
8 | 8 | import compas_rhino.objects |
| 9 | +from compas.colors import Color |
| 10 | +from compas.geometry import Line |
| 11 | +from compas.geometry import Point |
9 | 12 | from compas.geometry import centroid_points |
10 | 13 | from compas.scene import MeshObject |
11 | 14 | from compas_rhino.conversions import line_to_rhino |
@@ -447,86 +450,96 @@ def draw_facelabels(self, text, color=None, group=None, fontheight=10, fontface= |
447 | 450 | # draw normals |
448 | 451 | # ========================================================================== |
449 | 452 |
|
450 | | - # def draw_vertexnormals(self, vertices=None, color=(0, 255, 0), scale=1.0, group=None): |
451 | | - # """Draw the normals at the vertices of the mesh. |
| 453 | + def draw_vertexnormals(self, color=None, scale=1.0, group=None): |
| 454 | + """Draw the normals at the vertices of the mesh. |
452 | 455 |
|
453 | | - # Parameters |
454 | | - # ---------- |
455 | | - # vertices : list[int], optional |
456 | | - # A selection of vertex normals to draw. |
457 | | - # Default is to draw all vertex normals. |
458 | | - # color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional |
459 | | - # The color specification of the normal vectors. |
460 | | - # scale : float, optional |
461 | | - # Scale factor for the vertex normals. |
462 | | - # group : str, optional |
463 | | - # The name of a group to join the created Rhino objects in. |
| 456 | + Parameters |
| 457 | + ---------- |
| 458 | + color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional |
| 459 | + The color specification of the normal vectors. |
| 460 | + If no color is specified, the color of the corresponding vertex is used. |
| 461 | + scale : float, optional |
| 462 | + Scale factor for the vertex normals. |
| 463 | + group : str, optional |
| 464 | + The name of a group to join the created Rhino objects in. |
464 | 465 |
|
465 | | - # Returns |
466 | | - # ------- |
467 | | - # list[System.Guid] |
468 | | - # The GUIDs of the created Rhino objects. |
| 466 | + Returns |
| 467 | + ------- |
| 468 | + list[System.Guid] |
| 469 | + The GUIDs of the created Rhino objects. |
469 | 470 |
|
470 | | - # """ |
471 | | - # guids = [] |
| 471 | + """ |
| 472 | + guids = [] |
472 | 473 |
|
473 | | - # color = Color.coerce(color) |
| 474 | + vertices = list(self.mesh.vertices()) if self.show_vertices is True else self.show_vertices or [] |
| 475 | + transformation = transformation_to_rhino(self.worldtransformation) |
474 | 476 |
|
475 | | - # for vertex in vertices or self.mesh.vertices(): # type: ignore |
476 | | - # name = "{}.vertex.{}.normal".format(self.mesh.name, vertex) # type: ignore |
477 | | - # attr = self.compile_attributes(name=name, color=color) |
| 477 | + color = Color.coerce(color) |
478 | 478 |
|
479 | | - # point = self.mesh.vertex_point(vertex) |
480 | | - # normal = self.mesh.vertex_normal(vertex) # type: ignore |
| 479 | + for vertex in vertices: |
| 480 | + name = "{}.vertex.{}.normal".format(self.mesh.name, vertex) # type: ignore |
| 481 | + attr = self.compile_attributes(name=name, color=color or self.vertexcolor[vertex]) # type: ignore |
481 | 482 |
|
482 | | - # guid = sc.doc.Objects.AddLine(point_to_rhino(point), point_to_rhino(point + normal * scale), attr) |
483 | | - # guids.append(guid) |
| 483 | + point = self.mesh.vertex_point(vertex) |
| 484 | + normal = self.mesh.vertex_normal(vertex) # type: ignore |
| 485 | + line = Line.from_point_and_vector(point, normal) |
484 | 486 |
|
485 | | - # if group: |
486 | | - # self.add_to_group(group, guids) |
| 487 | + geometry = line_to_rhino(line) |
| 488 | + geometry.Transform(transformation) |
487 | 489 |
|
488 | | - # self._guids_vertexnormals = guids |
489 | | - # self._guids += guids |
490 | | - # return guids |
| 490 | + guid = sc.doc.Objects.AddLine(geometry, attr) |
| 491 | + guids.append(guid) |
491 | 492 |
|
492 | | - # def draw_facenormals(self, faces=None, color=(0, 255, 255), scale=1.0, group=None): |
493 | | - # """Draw the normals of the faces. |
| 493 | + if group: |
| 494 | + self.add_to_group(group, guids) |
494 | 495 |
|
495 | | - # Parameters |
496 | | - # ---------- |
497 | | - # faces : list[int], optional |
498 | | - # A selection of face normals to draw. |
499 | | - # Default is to draw all face normals. |
500 | | - # color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional |
501 | | - # The color specification of the normal vectors. |
502 | | - # scale : float, optional |
503 | | - # Scale factor for the face normals. |
504 | | - # group : str, optional |
505 | | - # The name of a group to join the created Rhino objects in. |
| 496 | + self._guids_vertexnormals = guids |
| 497 | + self._guids += guids |
| 498 | + return guids |
506 | 499 |
|
507 | | - # Returns |
508 | | - # ------- |
509 | | - # list[System.Guid] |
510 | | - # The GUIDs of the created Rhino objects. |
| 500 | + def draw_facenormals(self, color=None, scale=1.0, group=None): |
| 501 | + """Draw the normals of the faces. |
511 | 502 |
|
512 | | - # """ |
513 | | - # guids = [] |
| 503 | + Parameters |
| 504 | + ---------- |
| 505 | + color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional |
| 506 | + The color specification of the normal vectors. |
| 507 | + If no color is specified, the color of the corresponding face is used. |
| 508 | + scale : float, optional |
| 509 | + Scale factor for the face normals. |
| 510 | + group : str, optional |
| 511 | + The name of a group to join the created Rhino objects in. |
| 512 | +
|
| 513 | + Returns |
| 514 | + ------- |
| 515 | + list[System.Guid] |
| 516 | + The GUIDs of the created Rhino objects. |
| 517 | +
|
| 518 | + """ |
| 519 | + guids = [] |
514 | 520 |
|
515 | | - # color = Color.coerce(color) |
| 521 | + faces = list(self.mesh.faces()) if self.show_faces is True else self.show_faces or [] |
| 522 | + transformation = transformation_to_rhino(self.worldtransformation) |
516 | 523 |
|
517 | | - # for face in faces or self.mesh.faces(): # type: ignore |
518 | | - # name = "{}.face.{}.normal".format(self.mesh.name, face) # type: ignore |
519 | | - # attr = self.compile_attributes(name=name, color=color) |
| 524 | + color = Color.coerce(color) |
520 | 525 |
|
521 | | - # point = Point(*centroid_points([self.vertex_xyz[vertex] for vertex in self.mesh.face_vertices(face)])) # type: ignore |
522 | | - # normal = self.mesh.face_normal(face) # type: ignore |
| 526 | + for face in faces: |
| 527 | + name = "{}.face.{}.normal".format(self.mesh.name, face) # type: ignore |
| 528 | + attr = self.compile_attributes(name=name, color=color or self.facecolor[face]) # type: ignore |
523 | 529 |
|
524 | | - # guid = sc.doc.Objects.AddLine(point_to_rhino(point), point_to_rhino(point + normal * scale), attr) |
525 | | - # guids.append(guid) |
| 530 | + point = self.mesh.face_centroid(face) |
| 531 | + normal = self.mesh.face_normal(face) |
| 532 | + line = Line.from_point_and_vector(point, normal) |
526 | 533 |
|
527 | | - # if group: |
528 | | - # self.add_to_group(group, guids) |
| 534 | + geometry = line_to_rhino(line) |
| 535 | + geometry.Transform(transformation) |
529 | 536 |
|
530 | | - # self._guids_facenormals = guids |
| 537 | + guid = sc.doc.Objects.AddLine(geometry, attr) |
| 538 | + guids.append(guid) |
531 | 539 |
|
532 | | - # return guids |
| 540 | + if group: |
| 541 | + self.add_to_group(group, guids) |
| 542 | + |
| 543 | + self._guids_facenormals = guids |
| 544 | + |
| 545 | + return guids |
0 commit comments