diff --git a/src/compas_ghpython/scene/meshobject.py b/src/compas_ghpython/scene/meshobject.py index 46f4aada7949..78328be8bfe4 100644 --- a/src/compas_ghpython/scene/meshobject.py +++ b/src/compas_ghpython/scene/meshobject.py @@ -29,6 +29,10 @@ class MeshObject(GHSceneObject, BaseMeshObject): def __init__(self, disjoint=False, **kwargs): super(MeshObject, self).__init__(**kwargs) self.disjoint = disjoint + self._guid_mesh = None + self._guid_face = {} + self._guid_edge = {} + self._guid_vertex = {} def draw(self): """Draw the mesh. @@ -50,45 +54,38 @@ def draw(self): self._guids = [] if self.show_faces is True: + vertexcolors = [] - if len(self.vertexcolor): - vertexcolors = [self.vertexcolor[vertex] for vertex in self.mesh.vertices()] + if len(self.vertexcolor): # type: ignore + vertexcolors = [self.vertexcolor[vertex] for vertex in self.mesh.vertices()] # type: ignore facecolors = [] - if len(self.facecolor): - facecolors = [self.facecolor[face] for face in self.mesh.faces()] - - color = None - if not vertexcolors and not facecolors: - color = self.color + if len(self.facecolor): # type: ignore + facecolors = [self.facecolor[face] for face in self.mesh.faces()] # type: ignore vertex_index = self.mesh.vertex_index() - vertex_xyz = self.vertex_xyz - vertices = [vertex_xyz[vertex] for vertex in self.mesh.vertices()] + vertices = [self.mesh.vertex_attributes(vertex, "xyz") for vertex in self.mesh.vertices()] faces = [[vertex_index[vertex] for vertex in self.mesh.face_vertices(face)] for face in self.mesh.faces()] geometry = conversions.vertices_and_faces_to_rhino( vertices, faces, - color=color, + color=self.color, vertexcolors=vertexcolors, facecolors=facecolors, disjoint=self.disjoint, ) - # geometry.Transform(conversions.transformation_to_rhino(self.worldtransformation)) + geometry.Transform(conversions.transformation_to_rhino(self.worldtransformation)) self._guids.append(geometry) elif self.show_faces: - self._guids += self.draw_faces() + self.draw_faces() - if self.show_vertices: - self._guids += self.draw_vertices() - - if self.show_edges: - self._guids += self.draw_edges() + self.draw_vertices() + self.draw_edges() return self.guids @@ -104,9 +101,13 @@ def draw_vertices(self): vertices = list(self.mesh.vertices()) if self.show_vertices is True else self.show_vertices or [] + transformation = conversions.transformation_to_rhino(self.worldtransformation) + if vertices: for vertex in vertices: - points.append(conversions.point_to_rhino(self.vertex_xyz[vertex])) + geometry = conversions.point_to_rhino(self.mesh.vertex_attributes(vertex, "xyz")) + geometry.Transform(transformation) + points.append(geometry) return points @@ -122,9 +123,14 @@ def draw_edges(self): edges = list(self.mesh.edges()) if self.show_edges is True else self.show_edges or [] + transformation = conversions.transformation_to_rhino(self.worldtransformation) + if edges: for edge in edges: - lines.append(conversions.line_to_rhino((self.vertex_xyz[edge[0]], self.vertex_xyz[edge[1]]))) + line = self.mesh.edge_line(edge) + geometry = conversions.line_to_rhino(line) + geometry.Transform(transformation) + lines.append(geometry) return lines @@ -140,12 +146,17 @@ def draw_faces(self): faces = list(self.mesh.faces()) if self.show_faces is True else self.show_faces or [] + transformation = conversions.transformation_to_rhino(self.worldtransformation) + if faces: for face in faces: - color = self.facecolor[face] - vertices = [self.vertex_xyz[vertex] for vertex in self.mesh.face_vertices(face)] + color = self.facecolor[face] # type: ignore + vertices = [self.mesh.vertex_attributes(vertex, "xyz") for vertex in self.mesh.face_vertices(face)] # type: ignore facet = ngon(len(vertices)) + if facet: - meshes.append(conversions.vertices_and_faces_to_rhino(vertices, [facet], color=color)) + geometry = conversions.vertices_and_faces_to_rhino(vertices, [facet], color=color) + geometry.Transform(transformation) + meshes.append(geometry) return meshes diff --git a/src/compas_rhino/scene/meshobject.py b/src/compas_rhino/scene/meshobject.py index 405b11fc1f64..18b945bfe385 100644 --- a/src/compas_rhino/scene/meshobject.py +++ b/src/compas_rhino/scene/meshobject.py @@ -2,9 +2,11 @@ from __future__ import division from __future__ import print_function +import Rhino # type: ignore import scriptcontext as sc # type: ignore import compas_rhino.objects +from compas.geometry import centroid_points from compas.scene import MeshObject from compas_rhino.conversions import line_to_rhino from compas_rhino.conversions import point_to_rhino @@ -291,155 +293,155 @@ def draw_faces(self): # draw labels # ========================================================================== - # def draw_vertexlabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): - # """Draw labels for a selection of vertices. + def draw_vertexlabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): + """Draw labels for a selection of vertices. + + Parameters + ---------- + text : dict[int, str] + A dictionary of vertex labels as vertex-text pairs. + color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional + The color of the vertex labels. + group : str, optional + The name of a group to join the created Rhino objects in. + fontheight : int, optional + Font height of the vertex labels. + fontface : str, optional + Font face of the vertex labels. - # Parameters - # ---------- - # text : dict[int, str] - # A dictionary of vertex labels as vertex-text pairs. - # color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional - # The color of the vertex labels. - # group : str, optional - # The name of a group to join the created Rhino objects in. - # fontheight : int, optional - # Font height of the vertex labels. - # fontface : str, optional - # Font face of the vertex labels. - - # Returns - # ------- - # list[System.Guid] - # The GUIDs of the created Rhino point objects. - - # """ - # guids = [] - - # self.vertexcolor = color + Returns + ------- + list[System.Guid] + The GUIDs of the created Rhino point objects. - # transformation = transformation_to_rhino(self.worldtransformation) + """ + guids = [] - # for vertex in text: - # name = "{}.vertex.{}.label".format(self.mesh.name, vertex) # type: ignore - # color = self.vertexcolor[vertex] # type: ignore - # attr = self.compile_attributes(name=name, color=color) + self.vertexcolor = color - # location = point_to_rhino(self.mesh.vertex_attributes(vertex, "xyz")) - # location.Transform(transformation) + transformation = transformation_to_rhino(self.worldtransformation) - # dot = Rhino.Geometry.TextDot(str(text[vertex]), location) # type: ignore - # dot.FontHeight = fontheight - # dot.FontFace = fontface + for vertex in text: + name = "{}.vertex.{}.label".format(self.mesh.name, vertex) # type: ignore + color = self.vertexcolor[vertex] # type: ignore + attr = self.compile_attributes(name=name, color=color) - # guid = sc.doc.Objects.AddTextDot(dot, attr) - # guids.append(guid) + location = point_to_rhino(self.mesh.vertex_attributes(vertex, "xyz")) + location.Transform(transformation) - # if group: - # self.add_to_group(group, guids) + dot = Rhino.Geometry.TextDot(str(text[vertex]), location) # type: ignore + dot.FontHeight = fontheight + dot.FontFace = fontface - # self._guids_vertexlabels = guids - # self._guids += guids - # return guids + guid = sc.doc.Objects.AddTextDot(dot, attr) + guids.append(guid) - # def draw_edgelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): - # """Draw labels for a selection of edges. + if group: + self.add_to_group(group, guids) - # Parameters - # ---------- - # text : dict[tuple[int, int], str] - # A dictionary of edge labels as edge-text pairs. - # color : :class:`compas.colors.Color` | dict[tuple[int, int], :class:`compas.colors.Color`], optional - # The color of the edge labels. - # group : str, optional - # The name of a group to join the created Rhino objects in. - # fontheight : int, optional - # Font height of the edge labels. - # fontface : str, optional - # Font face of the edge labels. + self._guids_vertexlabels = guids + self._guids += guids + return guids - # Returns - # ------- - # list[System.Guid] - # The GUIDs of the created Rhino point objects. + def draw_edgelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): + """Draw labels for a selection of edges. + + Parameters + ---------- + text : dict[tuple[int, int], str] + A dictionary of edge labels as edge-text pairs. + color : :class:`compas.colors.Color` | dict[tuple[int, int], :class:`compas.colors.Color`], optional + The color of the edge labels. + group : str, optional + The name of a group to join the created Rhino objects in. + fontheight : int, optional + Font height of the edge labels. + fontface : str, optional + Font face of the edge labels. - # """ - # guids = [] + Returns + ------- + list[System.Guid] + The GUIDs of the created Rhino point objects. - # self.edgecolor = color + """ + guids = [] - # transformation = transformation_to_rhino(self.worldtransformation) + self.edgecolor = color - # for edge in text: - # name = "{}.edge.{}-{}".format(self.mesh.name, *edge) # type: ignore - # color = self.edgecolor[edge] # type: ignore - # attr = self.compile_attributes(name="{}.label".format(name), color=color) + transformation = transformation_to_rhino(self.worldtransformation) - # line = self.mesh.edge_line(edge) - # location = point_to_rhino(line.midpoint) - # location.Transform(transformation) + for edge in text: + name = "{}.edge.{}-{}".format(self.mesh.name, *edge) # type: ignore + color = self.edgecolor[edge] # type: ignore + attr = self.compile_attributes(name="{}.label".format(name), color=color) - # dot = Rhino.Geometry.TextDot(str(text[edge]), location) # type: ignore - # dot.FontHeight = fontheight - # dot.FontFace = fontface + line = self.mesh.edge_line(edge) + location = point_to_rhino(line.midpoint) + location.Transform(transformation) - # guid = sc.doc.Objects.AddTextDot(dot, attr) - # guids.append(guid) + dot = Rhino.Geometry.TextDot(str(text[edge]), location) # type: ignore + dot.FontHeight = fontheight + dot.FontFace = fontface - # if group: - # self.add_to_group(group, guids) + guid = sc.doc.Objects.AddTextDot(dot, attr) + guids.append(guid) - # self._guids_edgelabels = guids - # self._guids += guids - # return guids + if group: + self.add_to_group(group, guids) - # def draw_facelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): - # """Draw labels for a selection of faces. + self._guids_edgelabels = guids + self._guids += guids + return guids - # Parameters - # ---------- - # text : dict[int, str] - # A dictionary of face labels as face-text pairs. - # color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional - # The color of the face labels. - # group : str, optional - # The name of a group to join the created Rhino objects in. - # fontheight : int, optional - # Font height of the face labels. - # fontface : str, optional - # Font face of the face labels. + def draw_facelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"): + """Draw labels for a selection of faces. + + Parameters + ---------- + text : dict[int, str] + A dictionary of face labels as face-text pairs. + color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional + The color of the face labels. + group : str, optional + The name of a group to join the created Rhino objects in. + fontheight : int, optional + Font height of the face labels. + fontface : str, optional + Font face of the face labels. - # Returns - # ------- - # list[System.Guid] - # The GUIDs of the created Rhino point objects. + Returns + ------- + list[System.Guid] + The GUIDs of the created Rhino point objects. - # """ - # guids = [] + """ + guids = [] - # transformation = transformation_to_rhino(self.worldtransformation) + transformation = transformation_to_rhino(self.worldtransformation) - # for face in text: - # name = "{}.face.{}.label".format(self.mesh.name, face) # type: ignore - # color = self.facecolor[face] # type: ignore - # attr = self.compile_attributes(name=name, color=color) + for face in text: + name = "{}.face.{}.label".format(self.mesh.name, face) # type: ignore + color = self.facecolor[face] # type: ignore + attr = self.compile_attributes(name=name, color=color) - # points = [self.mesh.vertex_attributes(vertex, "xyz") for vertex in self.mesh.face_vertices(face)] # type: ignore - # location = point_to_rhino(centroid_points(points)) - # location.Transform(transformation) + points = [self.mesh.vertex_attributes(vertex, "xyz") for vertex in self.mesh.face_vertices(face)] # type: ignore + location = point_to_rhino(centroid_points(points)) + location.Transform(transformation) - # dot = Rhino.Geometry.TextDot(str(text[face]), location) # type: ignore - # dot.FontHeight = fontheight - # dot.FontFace = fontface + dot = Rhino.Geometry.TextDot(str(text[face]), location) # type: ignore + dot.FontHeight = fontheight + dot.FontFace = fontface - # guid = sc.doc.Objects.AddTextDot(dot, attr) - # guids.append(guid) + guid = sc.doc.Objects.AddTextDot(dot, attr) + guids.append(guid) - # if group: - # self.add_to_group(group, guids) + if group: + self.add_to_group(group, guids) - # self._guids_facelabels = guids - # self._guids += guids - # return guids + self._guids_facelabels = guids + self._guids += guids + return guids # ========================================================================== # draw normals