From d1d8c61e78ae300f5657e42e49adf6d6c38cda89 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Sat, 6 Jul 2024 08:09:31 +0200 Subject: [PATCH] clean up and structure --- CHANGELOG.md | 1 - src/compas_rhino/conversions/surfaces.py | 3 - src/compas_rhino/drawing.py | 4 +- src/compas_rhino/objects.py | 1051 +++++++++------------- 4 files changed, 434 insertions(+), 625 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0217bf41fbe..1cd88e2dc00b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,7 +107,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - ## [2.2.0] 2024-06-24 ### Added diff --git a/src/compas_rhino/conversions/surfaces.py b/src/compas_rhino/conversions/surfaces.py index 219e401a924e..ada0588f315a 100644 --- a/src/compas_rhino/conversions/surfaces.py +++ b/src/compas_rhino/conversions/surfaces.py @@ -2,12 +2,9 @@ from __future__ import division from __future__ import print_function -# import Rhino # type: ignore from compas.datastructures import Mesh from compas.geometry import NurbsSurface from compas.geometry import Surface - -# from compas.tolerance import TOL from compas.utilities import memoize from .geometry import point_to_compas diff --git a/src/compas_rhino/drawing.py b/src/compas_rhino/drawing.py index e5245353821a..1158d93796a3 100644 --- a/src/compas_rhino/drawing.py +++ b/src/compas_rhino/drawing.py @@ -781,7 +781,7 @@ def populate_vertexcolors(face): if guid != System.Guid.Empty: if vertexcolors: try: - compas_rhino.objects.set_mesh_vertex_colors(guid, vertexcolors) + rs.MeshVertexColors(guid, vertexcolors) except Exception: pass @@ -859,7 +859,7 @@ def draw_faces(faces, **kwargs): if vertexcolors: try: - compas_rhino.objects.set_mesh_vertex_colors(guid, vertexcolors) + rs.MeshVertexColors(guid, vertexcolors) except Exception: pass diff --git a/src/compas_rhino/objects.py b/src/compas_rhino/objects.py index 533bedadb66d..dae80f58712c 100644 --- a/src/compas_rhino/objects.py +++ b/src/compas_rhino/objects.py @@ -5,7 +5,6 @@ import json import warnings -import Rhino # type: ignore import rhinoscriptsyntax as rs # type: ignore import scriptcontext as sc # type: ignore import System # type: ignore @@ -21,7 +20,148 @@ # ============================================================================== -# Objects +# Deprecated +# ============================================================================== + + +def get_point_coordinates(guids): + """Get the coordintes of the locations of point objects. + + Parameters + ---------- + guids : list[System.Guid] + + Returns + ------- + list[[float, float, float]] + The location coordinates of the points. + + Warnings + -------- + .. deprecated:: 2.3 + Use `compas_rhino.conversions` instead. + + """ + warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) + + points = [] + for guid in guids: + point = rs.PointCoordinates(guid) + if point: + points.append(point) + return points + + +def get_line_coordinates(guids): + """Get the start and end point coordinates of line curves. + + Parameters + ---------- + guids : list[System.Guid] + Line curve identifiers. + + Returns + ------- + list[tuple[[float, float, float], [float, float, float]]] + A start and end point per line. + + Warnings + -------- + .. deprecated:: 2.3 + Use `compas_rhino.conversions` instead. + + """ + warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) + + if isinstance(guids, System.Guid): + sp = rs.CurveStartPoint(guids) + ep = rs.CurveEndPoint(guids) + return [sp.X, sp.Y, sp.Z], [ep.X, ep.Y, ep.Z] + lines = [] + for guid in guids: + sp = rs.CurveStartPoint(guid) + ep = rs.CurveEndPoint(guid) + lines.append(([sp.X, sp.Y, sp.Z], [ep.X, ep.Y, ep.Z])) + return lines + + +def get_polyline_coordinates(guids): + """Get the point coordinates of polylines. + + Parameters + ---------- + guids : list[System.Guid] + Polyline curve identifiers. + + Returns + ------- + list[list[[float, float, float]]] + A list of point coordinates per polyline. + + Warnings + -------- + .. deprecated:: 2.3 + Use `compas_rhino.conversions` instead. + + """ + warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) + + if isinstance(guids, System.Guid): + points = rs.PolylineVertices(guids) + coords = [] + if points: + coords = [map(float, point) for point in points] + return coords + polylines = [] + for guid in guids: + points = rs.PolylineVertices(guid) + coords = [] + if points: + coords = [map(float, point) for point in points] + polylines.append(coords) + return polylines + + +def get_polygon_coordinates(guids): + """Get the point coordinates of polygons. + + Parameters + ---------- + guids : list[System.Guid] + Polygon curve identifiers. + + Returns + ------- + list[list[[float, float, float]]] + A list of point coordinates per polygon. + + Warnings + -------- + .. deprecated:: 2.3 + Use `compas_rhino.conversions` instead. + + """ + warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) + + if isinstance(guids, System.Guid): + points = rs.CurvePoints(guids) + coords = [] + if points: + coords = [list(point) for point in points] + return coords + polygons = [] + if guids: + for guid in guids: + points = rs.CurvePoints(guid) + coords = [] + if points: + coords = map(list, points) + polygons.append(coords) + return polygons + + +# ============================================================================== +# Get objects by filtering # ============================================================================== @@ -44,147 +184,196 @@ def get_objects(name=None, color=None, layer=None, type=None): list[System.Guid] The System.Guids of the objects matching the filter parameters. - Examples - -------- - .. code-block:: python - - import compas_rhino + """ + guids = rs.AllObjects() + if name: + guids = list(set(guids) & set(rs.ObjectsByName(name))) + if color: + guids = list(set(guids) & set(rs.ObjectsByColor(color))) + if layer: + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + if type: + guids = list(set(guids) & set(rs.ObjectsByType(type))) + return guids - guids_all = compas_rhino.get_objects() - guids_compas = compas_rhino.get_objects(name="COMPAS.*") - guids_red = compas_rhino.get_objects(color=(255, 0, 0)) - guids_points = compas_rhino.get_objects(type=compas_rhino.rs.filter.point) - guids_redpoints = compas_rhino.get_objects(color=(255, 0, 0), type=compas_rhino.rs.filter.point) - .. code-block:: python +def get_points(layer=None): + """Get all points. - guids_all = set(compas_rhino.get_objects()) - guids_compas = set(compas_rhino.get_objects(name='COMPAS.*')) - guids_red = set(compas_rhino.get_objects(color=(255, 0, 0))) - guids_points = set(compas_rhino.get_objects(type=compas_rhino.rs.filter.point)) - guids_redpoints = set(compas_rhino.get_objects(color=(255, 0, 0), type=compas_rhino.rs.filter.point)) + Parameters + ---------- + layer : str, optional + Name of a layer containing the points. - print guids_compas.issubset(guids_all) - print guids_all.issubset(guids_compas) + Returns + ------- + list[System.Guid] + The identifiers of the points. - # True, False + """ + if layer: + rs.EnableRedraw(False) + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.point) + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + rs.LayerVisible(layer, visible, True) + rs.EnableRedraw(True) + else: + guids = rs.ObjectsByType(rs.filter.point) + return guids - print guids_red.issubset(guids_all) - print guids_points.issubset(guids_all) - print guids_redpoints.issubset(guids_all) - # True, True, True +def get_curves(layer=None): + """Get all curves. - print guids_redpoints.issubset(guids_points) - print guids_redpoints.issubset(guids_red) - print guids_points.issubset(guids_red) + Parameters + ---------- + layer : str, optional + Name of a layer containing the curves. - # True, True, False + Returns + ------- + list[System.Guid] + The identifiers of the curves. """ - guids = rs.AllObjects() - if name: - guids = list(set(guids) & set(rs.ObjectsByName(name))) - if color: - guids = list(set(guids) & set(rs.ObjectsByColor(color))) if layer: + rs.EnableRedraw(False) + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.curve) guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - if type: - guids = list(set(guids) & set(rs.ObjectsByType(type))) + rs.LayerVisible(layer, visible, True) + rs.EnableRedraw(True) + else: + guids = rs.ObjectsByType(rs.filter.curve) return guids -def delete_object(guid, purge=None, redraw=True): - """Delete Rhino object. +def get_lines(layer=None): + """Get all lines. Parameters ---------- - guid : System.Guid - Object identifier. - purge : None or bool, optional - If None, the value of the global purge setting (:obj:`compas_rhino.PURGE_ON_DELETE`) will be used. - If True, purge the object from history after deleting. - If False, delete but don't purge. - redraw : bool, optional - If True, redrawing will be enabled and enacted. - If False, redrawing will be disabled. + layer : str, optional + Name of a layer containing the lines. Returns ------- - None + list[System.Guid] + The identifiers of the lines. """ - if purge is None: - purge = compas_rhino.PURGE_ON_DELETE - if purge and purge_object: - purge_objects([guid], redraw=redraw) + if layer: + rs.EnableRedraw(False) + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_line(guid)] + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + rs.LayerVisible(layer, visible, True) + rs.EnableRedraw(True) else: - delete_objects([guid], purge, redraw=redraw) + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_line(guid)] + return guids -def delete_objects(guids, purge=None, redraw=True): - """Delete multiple Rhino objects. +def get_polylines(layer=None): + """Get all polylines. Parameters ---------- - guids : list[System.Guid] - Object identifiers. - purge : None or bool, optional - If None, the value of the global purge setting (:obj:`compas_rhino.PURGE_ON_DELETE`) will be used. - If True, purge the objects from history after deleting. - If False, delete but don't purge. - redraw : bool, optional - If True, redrawing will be enabled and enacted. - If False, redrawing will be disabled. + layer : str, optional + Name of a layer containing the polylines. Returns ------- - None + list[System.Guid] + The identifiers of the polylines. """ - if purge is None: - purge = compas_rhino.PURGE_ON_DELETE - if purge and purge_object: - purge_objects(guids, redraw=redraw) + if layer: + rs.EnableRedraw(False) + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_polyline(guid)] + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + rs.LayerVisible(layer, visible, True) + rs.EnableRedraw(True) else: + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_polyline(guid)] + return guids + + +def get_polygons(layer=None): + """Get all polygons. + + Parameters + ---------- + layer : str, optional + Name of a layer containing the polygons. + + Returns + ------- + list[System.Guid] + The identifiers of the polygons. + + """ + if layer: rs.EnableRedraw(False) - for guid in guids: - if rs.IsObjectHidden(guid): - rs.ShowObject(guid) - rs.DeleteObjects(guids) - if redraw: - rs.EnableRedraw(True) - sc.doc.Views.Redraw() + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_polygon(guid)] + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + rs.LayerVisible(layer, visible, True) + rs.EnableRedraw(True) + else: + guids = rs.ObjectsByType(rs.filter.curve) + guids = [guid for guid in guids if is_curve_polygon(guid)] + return guids -def purge_objects(guids, redraw=True): - """Purge objects from memory. +def get_meshes(layer=None): + """Get (all) meshes. Parameters ---------- - guids : list[System.Guid] - Object identifiers. - redraw : bool, optional - If True, redrawing will be enabled and enacted. - If False, redrawing will be disabled. + layer : str, optional + Name of a layer containing the meshes. Returns ------- - None + list[System.Guid] + The identifiers of the meshes. """ - if not purge_object: - raise RuntimeError("Cannot purge outside Rhino script context") - rs.EnableRedraw(False) - for guid in guids: - if rs.IsObject(guid): - if rs.IsObjectHidden(guid): - rs.ShowObject(guid) - o = find_object(guid) - purge_object(o.RuntimeSerialNumber) - if redraw: + if layer: + rs.EnableRedraw(False) + # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 + # that is why we use positional instead of named arguments + visible = rs.LayerVisible(layer, True, True) + guids = rs.ObjectsByType(rs.filter.mesh) + guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) + rs.LayerVisible(layer, visible, True) rs.EnableRedraw(True) - sc.doc.Views.Redraw() + else: + guids = rs.ObjectsByType(rs.filter.mesh) + return guids + + +# ============================================================================== +# Object info +# ============================================================================== def get_object_layers(guids): @@ -334,172 +523,229 @@ def get_object_attributes_from_name(guids, prefix=None): return attrs -def select_object(message="Select an object."): - """Select one object in the Rhino view. +def is_curve_line(guid): + """Verify that a curve is really a line. Parameters ---------- - message : str, optional - Instruction for the user. + guid : System.Guid + The identifier of the curve. Returns ------- - System.Guid - The identifer of the selected object. + bool + True if the curve is a line. + False otherwise. """ - return rs.GetObject(message) + return rs.IsCurve(guid) and rs.IsLine(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) == 2 -def select_objects(message="Select multiple objects."): - """Select multiple objects in the Rhino view. +def is_curve_polyline(guid): + """Verify that a curve is really a polyline. Parameters ---------- - message : str, optional - Instruction for the user. + guid : System.Guid + The identifier of the curve. Returns ------- - list[System.Guid] - The identifers of the selected objects. + bool + True if the curve is a polyline. + False otherwise. """ - guids = [] - temp = rs.GetObjects(message) - if temp: - return temp - return guids + return rs.IsCurve(guid) and rs.IsPolyline(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) > 2 + + +def is_curve_polygon(guid): + """Verify that a curve is really a polygon. + + Parameters + ---------- + guid : System.Guid + The identifier of the curve. + + Returns + ------- + bool + True if the curve is a polygon. + False otherwise. + + """ + return rs.IsCurve(guid) and rs.IsCurveClosed(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) > 2 # ============================================================================== -# Points +# Delete objects # ============================================================================== -def select_point(message="Select one point."): - """Select one point in the Rhino view. +def delete_object(guid, purge=None, redraw=True): + """Delete Rhino object. Parameters ---------- - message : str, optional - Instruction for the user. + guid : System.Guid + Object identifier. + purge : None or bool, optional + If None, the value of the global purge setting (:obj:`compas_rhino.PURGE_ON_DELETE`) will be used. + If True, purge the object from history after deleting. + If False, delete but don't purge. + redraw : bool, optional + If True, redrawing will be enabled and enacted. + If False, redrawing will be disabled. Returns ------- - System.Guid - The identifer of the selected point. + None """ - return rs.GetObject(message, preselect=True, select=True, filter=rs.filter.point) + if purge is None: + purge = compas_rhino.PURGE_ON_DELETE + if purge and purge_object: + purge_objects([guid], redraw=redraw) + else: + delete_objects([guid], purge, redraw=redraw) -def select_points(message="Select multiple points."): - """Select multiple points in the Rhino view. +def delete_objects(guids, purge=None, redraw=True): + """Delete multiple Rhino objects. Parameters ---------- - message : str, optional - Instruction for the user. + guids : list[System.Guid] + Object identifiers. + purge : None or bool, optional + If None, the value of the global purge setting (:obj:`compas_rhino.PURGE_ON_DELETE`) will be used. + If True, purge the objects from history after deleting. + If False, delete but don't purge. + redraw : bool, optional + If True, redrawing will be enabled and enacted. + If False, redrawing will be disabled. Returns ------- - list[System.Guid] - The identifers of the selected points. + None """ - guids = [] - temp = rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.point) - if temp: - guids = temp - return guids + if purge is None: + purge = compas_rhino.PURGE_ON_DELETE + if purge and purge_object: + purge_objects(guids, redraw=redraw) + else: + rs.EnableRedraw(False) + for guid in guids: + if rs.IsObjectHidden(guid): + rs.ShowObject(guid) + rs.DeleteObjects(guids) + if redraw: + rs.EnableRedraw(True) + sc.doc.Views.Redraw() -def get_points(layer=None): - """Get all points. +def purge_objects(guids, redraw=True): + """Purge objects from memory. Parameters ---------- - layer : str, optional - Name of a layer containing the points. + guids : list[System.Guid] + Object identifiers. + redraw : bool, optional + If True, redrawing will be enabled and enacted. + If False, redrawing will be disabled. Returns ------- - list[System.Guid] - The identifiers of the points. + None """ - if layer: - rs.EnableRedraw(False) - # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 - # that is why we use positional instead of named arguments - visible = rs.LayerVisible(layer, True, True) - guids = rs.ObjectsByType(rs.filter.point) - guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - rs.LayerVisible(layer, visible, True) + if not purge_object: + raise RuntimeError("Cannot purge outside Rhino script context") + rs.EnableRedraw(False) + for guid in guids: + if rs.IsObject(guid): + if rs.IsObjectHidden(guid): + rs.ShowObject(guid) + o = find_object(guid) + purge_object(o.RuntimeSerialNumber) + if redraw: rs.EnableRedraw(True) - else: - guids = rs.ObjectsByType(rs.filter.point) - return guids + sc.doc.Views.Redraw() -def get_point_coordinates(guids): - """Get the coordintes of the locations of point objects. +# ============================================================================== +# Select objects +# ============================================================================== + + +def select_object(message="Select an object."): + """Select one object in the Rhino view. Parameters ---------- - guids : list[System.Guid] + message : str, optional + Instruction for the user. Returns ------- - list[[float, float, float]] - The location coordinates of the points. - - Warnings - -------- - .. deprecated:: 2.3 - Use `compas_rhino.conversions` instead. + System.Guid + The identifer of the selected object. """ - warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) + return rs.GetObject(message) - points = [] - for guid in guids: - point = rs.PointCoordinates(guid) - if point: - points.append(point) - return points +def select_objects(message="Select multiple objects."): + """Select multiple objects in the Rhino view. -# ============================================================================== -# Curves -# ============================================================================== + Parameters + ---------- + message : str, optional + Instruction for the user. + + Returns + ------- + list[System.Guid] + The identifers of the selected objects. + """ + return rs.GetObjects(message) -def is_curve_line(guid): - """Verify that a curve is a line. + +def select_point(message="Select one point."): + """Select one point in the Rhino view. Parameters ---------- - guid : System.Guid - The identifier of the curve. + message : str, optional + Instruction for the user. Returns ------- - bool - True if the curve is a line. - False otherwise. + System.Guid + The identifer of the selected point. """ - return rs.IsCurve(guid) and rs.IsLine(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) == 2 + return rs.GetObject(message, preselect=True, select=True, filter=rs.filter.point) -def is_curve_polyline(guid): - return rs.IsCurve(guid) and rs.IsPolyline(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) > 2 +def select_points(message="Select multiple points."): + """Select multiple points in the Rhino view. + Parameters + ---------- + message : str, optional + Instruction for the user. -def is_curve_polygon(guid): - return rs.IsCurve(guid) and rs.IsCurveClosed(guid) and rs.CurveDegree(guid) == 1 and len(rs.CurvePoints(guid)) > 2 + Returns + ------- + list[System.Guid] + The identifers of the selected points. + + """ + return rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.point) def select_curve(message="Select one curve."): @@ -533,11 +779,7 @@ def select_curves(message="Select multiple curves."): The identifers of the selected curves. """ - guids = [] - temp = rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.curve) - if temp: - guids = temp - return guids + return rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.curve) def select_line(message="Select line."): @@ -574,13 +816,8 @@ def select_lines(message="Select multiple lines."): The identifers of the selected lines. """ - guids = [] temp = rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.curve) - if temp: - for guid in temp: - if is_curve_line(guid): - guids.append(guid) - return guids + return [guid for guid in temp if is_curve_line(guid)] def select_polyline( @@ -621,13 +858,8 @@ def select_polylines( The identifers of the selected polylines. """ - guids = [] temp = rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.curve) - if temp: - for guid in temp: - if is_curve_polyline(guid): - guids.append(guid) - return guids + return [guid for guid in temp if is_curve_polyline(guid)] def select_polygon(message="Select one polygon (closed curve with degree = 1)"): @@ -664,244 +896,8 @@ def select_polygons(message="Select multiple polygons (closed curves with degree The identifers of the selected polygons. """ - guids = [] temp = rs.GetObjects(message, preselect=True, select=True, group=False, filter=rs.filter.curve) - if temp: - for guid in temp: - if is_curve_polygon(guid): - guids.append(guid) - return guids - - -def get_curves(layer=None): - """Get all curves. - - Parameters - ---------- - layer : str, optional - Name of a layer containing the curves. - - Returns - ------- - list[System.Guid] - The identifiers of the curves. - - """ - if layer: - rs.EnableRedraw(False) - # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 - # that is why we use positional instead of named arguments - visible = rs.LayerVisible(layer, True, True) - guids = rs.ObjectsByType(rs.filter.curve) - guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - rs.LayerVisible(layer, visible, True) - rs.EnableRedraw(True) - else: - guids = rs.ObjectsByType(rs.filter.curve) - return guids - - -def get_lines(layer=None): - """Get all lines. - - Parameters - ---------- - layer : str, optional - Name of a layer containing the lines. - - Returns - ------- - list[System.Guid] - The identifiers of the lines. - - """ - if layer: - rs.EnableRedraw(False) - # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 - # that is why we use positional instead of named arguments - visible = rs.LayerVisible(layer, True, True) - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_line(guid)] - guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - rs.LayerVisible(layer, visible, True) - rs.EnableRedraw(True) - else: - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_line(guid)] - return guids - - -def get_polylines(layer=None): - """Get all polylines. - - Parameters - ---------- - layer : str, optional - Name of a layer containing the polylines. - - Returns - ------- - list[System.Guid] - The identifiers of the polylines. - - """ - if layer: - rs.EnableRedraw(False) - # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 - # that is why we use positional instead of named arguments - visible = rs.LayerVisible(layer, True, True) - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_polyline(guid)] - guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - rs.LayerVisible(layer, visible, True) - rs.EnableRedraw(True) - else: - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_polyline(guid)] - return guids - - -def get_polygons(layer=None): - """Get all polygons. - - Parameters - ---------- - layer : str, optional - Name of a layer containing the polygons. - - Returns - ------- - list[System.Guid] - The identifiers of the polygons. - - """ - if layer: - rs.EnableRedraw(False) - # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 - # that is why we use positional instead of named arguments - visible = rs.LayerVisible(layer, True, True) - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_polygon(guid)] - guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) - rs.LayerVisible(layer, visible, True) - rs.EnableRedraw(True) - else: - guids = rs.ObjectsByType(rs.filter.curve) - guids = [guid for guid in guids if is_curve_polygon(guid)] - return guids - - -def get_line_coordinates(guids): - """Get the start and end point coordinates of line curves. - - Parameters - ---------- - guids : list[System.Guid] - Line curve identifiers. - - Returns - ------- - list[tuple[[float, float, float], [float, float, float]]] - A start and end point per line. - - Warnings - -------- - .. deprecated:: 2.3 - Use `compas_rhino.conversions` instead. - - """ - warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) - - if isinstance(guids, System.Guid): - sp = rs.CurveStartPoint(guids) - ep = rs.CurveEndPoint(guids) - return [sp.X, sp.Y, sp.Z], [ep.X, ep.Y, ep.Z] - lines = [] - for guid in guids: - sp = rs.CurveStartPoint(guid) - ep = rs.CurveEndPoint(guid) - lines.append(([sp.X, sp.Y, sp.Z], [ep.X, ep.Y, ep.Z])) - return lines - - -def get_polyline_coordinates(guids): - """Get the point coordinates of polylines. - - Parameters - ---------- - guids : list[System.Guid] - Polyline curve identifiers. - - Returns - ------- - list[list[[float, float, float]]] - A list of point coordinates per polyline. - - Warnings - -------- - .. deprecated:: 2.3 - Use `compas_rhino.conversions` instead. - - """ - warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) - - if isinstance(guids, System.Guid): - points = rs.PolylineVertices(guids) - coords = [] - if points: - coords = [map(float, point) for point in points] - return coords - polylines = [] - for guid in guids: - points = rs.PolylineVertices(guid) - coords = [] - if points: - coords = [map(float, point) for point in points] - polylines.append(coords) - return polylines - - -def get_polygon_coordinates(guids): - """Get the point coordinates of polygons. - - Parameters - ---------- - guids : list[System.Guid] - Polygon curve identifiers. - - Returns - ------- - list[list[[float, float, float]]] - A list of point coordinates per polygon. - - Warnings - -------- - .. deprecated:: 2.3 - Use `compas_rhino.conversions` instead. - - """ - warnings.warn("This function will be removed in v2.3. Please use `compas_rhino.conversions` instead.", DeprecationWarning, stacklevel=2) - - if isinstance(guids, System.Guid): - points = rs.CurvePoints(guids) - coords = [] - if points: - coords = [list(point) for point in points] - return coords - polygons = [] - if guids: - for guid in guids: - points = rs.CurvePoints(guid) - coords = [] - if points: - coords = map(list, points) - polygons.append(coords) - return polygons - - -# ============================================================================== -# Surfaces -# ============================================================================== + return [guid for guid in temp if is_curve_polygon(guid)] def select_surface(message="Select one surface."): @@ -940,22 +936,13 @@ def select_surfaces(message="Select multiple surfaces."): The identifers of the selected surfaces. """ - guids = [] - temp = rs.GetObjects( + return rs.GetObjects( message, preselect=True, select=True, group=False, filter=rs.filter.surface | rs.filter.polysurface, ) - if temp: - guids = temp - return guids - - -# ============================================================================== -# Meshes -# ============================================================================== def select_mesh(message="Select one mesh."): @@ -994,177 +981,3 @@ def select_meshes(message="Select multiple meshes."): if temp: guids = temp return guids - - -# def get_meshes(layer=None): -# if layer: -# rs.EnableRedraw(False) -# # Argument names for LayerVisible command are not the same for Rhino5 and Rhino6 -# # that is why we use positional instead of named arguments -# visible = rs.LayerVisible(layer, True, True) -# guids = rs.ObjectsByType(rs.filter.mesh) -# guids = list(set(guids) & set(rs.ObjectsByLayer(layer))) -# rs.LayerVisible(layer, visible, True) -# rs.EnableRedraw(True) -# else: -# guids = rs.ObjectsByType(rs.filter.mesh) -# return guids - - -# def get_mesh_border(guid): -# return rs.DuplicateMeshBorder(guid) - - -# def get_mesh_face_vertices(guid): -# faces = [] -# if guid: -# temp = rs.MeshFaceVertices(guid) -# faces = map(list, temp) -# return faces - - -# def get_mesh_vertex_coordinates(guid): -# vertices = [] -# if guid: -# vertices = [map(float, vertex) for vertex in rs.MeshVertices(guid)] -# return vertices - - -# def get_mesh_vertex_colors(guid): -# colors = [] -# if guid: -# temp = rs.MeshVertexColors(guid) -# if temp: -# colors = map(list, temp) -# return colors - - -# def set_mesh_vertex_colors(guid, colors): -# if not guid: -# return -# return rs.MeshVertexColors(guid, colors) - - -# def get_mesh_vertices_and_faces(guid): -# if not guid: -# return -# vertices = [map(float, vertex) for vertex in rs.MeshVertices(guid)] -# faces = map(list, rs.MeshFaceVertices(guid)) -# return vertices, faces - - -# def get_mesh_vertex_index(guid): -# class CustomGetObject(Rhino.Input.Custom.GetObject): -# def CustomGeometryFilter(self, rhino_object, geometry, component_index): -# return guid == rhino_object.Id - -# go = CustomGetObject() -# go.SetCommandPrompt("Select one vertex of the mesh.") -# go.GeometryFilter = Rhino.DocObjects.ObjectType.MeshVertex -# go.AcceptNothing(True) -# if go.Get() != Rhino.Input.GetResult.Object: -# return None -# objref = go.Object(0) -# if not objref: -# return None -# tvindex = objref.GeometryComponentIndex.Index -# mobj = sc.doc.Objects.Find(guid) -# mgeo = mobj.Geometry -# temp = mgeo.TopologyVertices.MeshVertexIndices(tvindex) -# vindex = temp[0] -# go.Dispose() -# return vindex - - -# def get_mesh_face_index(guid): -# class CustomGetObject(Rhino.Input.Custom.GetObject): -# def CustomGeometryFilter(self, rhino_object, geometry, component_index): -# return guid == rhino_object.Id - -# go = CustomGetObject() -# go.SetCommandPrompt("Select one face of the mesh.") -# go.GeometryFilter = Rhino.DocObjects.ObjectType.MeshFace -# go.AcceptNothing(True) -# if go.Get() != Rhino.Input.GetResult.Object: -# return None -# objref = go.Object(0) -# if not objref: -# return None -# findex = objref.GeometryComponentIndex.Index -# go.Dispose() -# return findex - - -# def get_mesh_edge_index(guid): -# class CustomGetObject(Rhino.Input.Custom.GetObject): -# def CustomGeometryFilter(self, rhino_object, geometry, component_index): -# return guid == rhino_object.Id - -# go = CustomGetObject() -# go.SetCommandPrompt("Select an edge of the mesh.") -# go.GeometryFilter = Rhino.DocObjects.ObjectType.MeshEdge -# go.AcceptNothing(True) -# if go.Get() != Rhino.Input.GetResult.Object: -# return None -# objref = go.Object(0) -# if not objref: -# return None -# eindex = objref.GeometryComponentIndex.Index -# go.Dispose() -# return eindex - - -# def get_mesh_vertex_indices(guid): -# tvindices = rs.GetMeshVertices(guid, "Select mesh vertices.") -# if not tvindices: -# return -# mobj = sc.doc.Objects.Find(guid) -# mgeo = mobj.Geometry -# vindices = [] -# for tvindex in tvindices: -# temp = mgeo.TopologyVertices.MeshVertexIndices(tvindex) -# vindices.append(temp[0]) -# return vindices - - -# def get_mesh_face_indices(guid): -# return rs.GetMeshFaces(guid, "Select mesh faces.") - - -# def get_mesh_vertex_face_indices(guid): -# vindex = get_mesh_vertex_index(guid) -# if vindex is None: -# return -# mobj = sc.doc.Objects.Find(guid) -# mgeo = mobj.Geometry -# findices = mgeo.TopologyVertices.ConnectedFaces(vindex) -# return findices - - -# def get_mesh_face_vertex_indices(guid): -# findex = get_mesh_face_index(guid) -# if findex is None: -# return -# mobj = sc.doc.Objects.Find(guid) -# mgeo = mobj.Geometry -# tvertices = mgeo.Faces.GetTopologicalVertices(findex) -# vindices = [] -# for tvertex in tvertices: -# temp = mgeo.TopologyVertices.MeshVertexIndices(tvertex) -# vindices.append(temp[0]) -# return vindices - - -# def get_mesh_edge_vertex_indices(guid): -# eindex = get_mesh_edge_index(guid) -# if eindex is None: -# return -# mobj = sc.doc.Objects.Find(guid) -# mgeo = mobj.Geometry -# temp = mgeo.TopologyEdges.GetTopologyVertices(eindex) -# tvindices = temp.I, temp.J -# vindices = [] -# for tvindex in tvindices: -# temp = mgeo.TopologyVertices.MeshVertexIndices(tvindex) -# vindices.append(temp[0]) -# return vindices