def network_draw_edge_labels(network, text=None, layer=None, color=None): """Draw labels for the edges of a network. Parameters ---------- network : compas.datastructures.Network A network object. text : str (None) The name of the attribute value to display in the label. Default is to display the edge keys. layer : str (None) The layer to draw in. Default is to draw in the current layer. color : str, tuple, list, dict (None) The color specififcation for the labels. Colors should be specified in the form of a string (hex colors) or as a tuple of RGB components. To apply the same color to all face labels, provide a single color specification. Individual colors can be assigned using a dictionary of key-color pairs. Missing keys will be assigned the default edge color (``self.defaults['edge.color']``). Default is to inherit color from the parent layer. formatter : callable (None) A formatting function. Defaults to the built-in ``str`` function. Notes ----- The labels are named using the following template: ``"{}.edge.label.{}".format(self.network.name, key)``. This name is used afterwards to identify edges of the network in the Rhino model. Examples -------- >>> """ # if formatter: # assert callable(formatter), 'The provided formatter is not callable.' # else: # formatter = str if not text or text == 'key': text = {(u, v): '{}-{}'.format(u, v) for u, v in network.edges()} elif text == 'index': text = {(u, v): str(index) for index, (u, v) in enumerate(network.edges())} else: pass artist = NetworkArtist(network) artist.layer = layer artist.clear_edgelabels() artist.draw_edgelabels(text=text, color=color) artist.redraw()
def clear_edges(self, **kwattr): artist = NetworkArtist(self) artist.clear_edges(**kwattr)
def clear(self, **kwattr): artist = NetworkArtist(self) artist.clear()
import random from compas_rhino.artists import NetworkArtist from compas.datastructures import Network network = Network() network.add_edge(1, 2) network.add_edge(2, 3) network.add_edge(1, 4) network.add_edge(4, 5) network.add_edge(4, 6) for node in network.nodes(): x = random.choice(range(5)) y = random.choice(range(5)) z = random.choice(range(5)) network.node_attributes(node, 'xyz', [x, y, z]) print(network.summary()) text = {node: str(node) for node in network.nodes()} artist = NetworkArtist(network, layer='network') artist.clear_layer() artist.draw_nodelabels(text) artist.draw() artist.redraw()
x1 = x0 + 0.5 * rx y1 = y0 + 0.5 * ry z1 = z0 + 0.5 * rz network.node_attributes(node, 'xyz', [x1, y1, z1]) # compute all residuals in the new geometry for node in network.nodes(): r = compute_residual(network, node) network.node_attributes(node, ['rx', 'ry', 'rz'], r) # visualize the geometry layer = "ITA20::L5::FormFinding" artist = NetworkArtist(network, layer=layer) artist.draw_nodes(color={ node: (255, 0, 0) for node in network.nodes_where({'is_anchor': True}) }) artist.draw_edges() # visualize the reaction forces lines = [] for node in network.nodes_where({'is_anchor': True}): start = network.node_attributes(node, 'xyz') residual = network.node_attributes(node, ['rx', 'ry', 'rz']) end = subtract_vectors(start, residual) color = (0, 255, 0)
a = network.add_node(x=0, y=0, z=0, is_anchor=True) b = network.add_node(x=10, y=0, z=10, is_anchor=True) c = network.add_node(x=10, y=10, z=0, is_anchor=True) d = network.add_node(x=0, y=10, z=10, is_anchor=True) e = network.add_node(x=5, y=5, z=0) network.add_edge(a, e) network.add_edge(b, e) network.add_edge(c, e) network.add_edge(d, e) # visualize the geometry layer = "ITA20::L5::FormFinding" artist = NetworkArtist(network, layer=layer) node_color = {node: (255, 0, 0) for node in network.nodes_where({'is_anchor': True})} artist.draw_nodes(color=node_color) artist.draw_edges() # visualize the forces height = 1.0 world = Frame.worldXY() circle = [[0, 0, 0.5 * 0.7 * height], [0, 0, 1]], 0.05 cylinder = Cylinder(circle, 0.7 * height) circle = [[0, 0, 0.7 * height], [0, 0, 1]], 0.1
def network_draw_vertices(network, keys=None, color=None, layer=None, clear_layer=False, redraw=True): """Draw a selection of vertices of a network. Parameters ---------- keys : list (None) A list of vertex keys identifying which vertices to draw. Default is to draw all vertices. color : str, tuple, dict (None) The color specififcation for the vertices. Colors should be specified in the form of a string (hex colors) or as a tuple of RGB components. To apply the same color to all vertices, provide a single color specification. Individual colors can be assigned using a dictionary of key-color pairs. Missing keys will be assigned the default vertex color (``self.defaults['color.vertex']``). Default is to inherit the color from the layer. layer : str (None) The layer in which the vertices are drawn. Default is to draw in the current layer. clear_layer : bool (False) Clear the drawing layer. redraw : bool (True) Redraw the view after adding the vertices. Notes ----- The vertices are named using the following template: ``"{}.vertex.{}".format(self.network.name, key)``. This name is used afterwards to identify vertices of the networkin the Rhino model. Examples -------- >>> network_draw_vertices(network) >>> network_draw_vertices(network, color='#ff0000') >>> network_draw_vertices(network, color=(255, 0, 0)) >>> network_draw_vertices(network, keys=network.vertices_on_boundary()) >>> network_draw_vertices(network, color={(u, v): '#00ff00' for u, v in network.vertices_on_boundary()}) """ artist = NetworkArtist(network) artist.layer = layer if clear_layer: artist.clear_layer() artist.clear_vertices() artist.draw_vertices(keys=keys, color=color) if redraw: artist.redraw()
# 2. make dual network (form diagram) # ------------------------------------------------------------------------------ layer_form = 'form_network' formdiagram = volmesh_dual_network(forcediagram, cls=FormNetwork) formdiagram.layer = layer_form formdiagram.attributes['name'] = layer_form # move dual_network offset = 2 width = formdiagram.bounding_box()[1][0] - formdiagram.bounding_box()[0][0] for vkey in formdiagram.nodes(): x = formdiagram.node_attribute(vkey, 'x') formdiagram.node_attribute(vkey, 'x', x + width * offset) form_artist = NetworkArtist(formdiagram, layer=layer_form) form_artist.draw_edges() rs.EnableRedraw(True) # ------------------------------------------------------------------------------ # 3. get reciprocation weight factor # ------------------------------------------------------------------------------ weight = rs.GetReal( "Enter weight factor : 1 = form only... 0 = force only...", 1.0, 0) # ------------------------------------------------------------------------------ # 4. reciprocate # ------------------------------------------------------------------------------
a = network.add_node(x=0, y=0, z=0, is_anchor=True) b = network.add_node(x=10, y=0, z=10, is_anchor=True) c = network.add_node(x=10, y=10, z=0, is_anchor=True) d = network.add_node(x=0, y=10, z=10, is_anchor=True) e = network.add_node(x=5, y=5, z=0) network.add_edge(a, e) network.add_edge(b, e) network.add_edge(c, e) network.add_edge(d, e) # visualize the geometry artist = NetworkArtist(network) artist.layer = "ITA20::L5::FormFinding" node_color = { node: (255, 0, 0) for node in network.nodes_where({'is_anchor': True}) } artist.draw_nodes(color=node_color) artist.draw_edges() # visualize the forces lines = [] for node in network.nodes(): a = network.node_attributes(node, 'xyz')
def network_draw_vertex_labels(network, attr_name=None, layer=None, color=None, formatter=None): """Draw labels for the vertices of the network. Parameters ---------- network : compas.datastructures.Network A network object. attr_name : str (None) The name of the attribute value to display in the label. Default is is to display the vertex key. layer : str (None) The layer to draw in. Default is to draw in the current layer. color : str, tuple, list, dict (None) The color specififcation for the labels. Colors should be specified in the form of a string (hex colors) or as a tuple of RGB components. To apply the same color to all face labels, provide a single color specification. Individual colors can be assigned using a dictionary of key-color pairs. Missing keys will be assigned the default vertex color (``self.defaults['vertex.color']``). Default is to inherit color from the parent layer. formatter : callable (None) A formatting function. Defaults to the built-in ``str`` function. Notes ----- The labels are named using the following template: ``"{}.vertex.label.{}".format(self.network.name, key)``. This name is used afterwards to identify vertices of the networkin the Rhino model. Examples -------- >>> """ if not attr_name: attr_name = 'key' if formatter: assert callable(formatter), 'The provided formatter is not callable.' else: formatter = str text = {} for index, (key, attr) in enumerate(network.vertices(True)): if 'key' == attr_name: value = key elif 'index' == attr_name: value = index else: value = attr[attr_name] text[key] = formatter(value) artist = NetworkArtist(network) artist.layer = layer artist.clear_vertexlabels() artist.draw_vertexlabels(text=text, color=color) artist.redraw()
x = formdiagram.node_attribute(vkey, 'x') formdiagram.node_attribute(vkey, 'x', x + width * offset) # ------------------------------------------------------------------------------ # 3. reciprocate # ------------------------------------------------------------------------------ volmesh_reciprocate(forcediagram, formdiagram, kmax=500, weight=1, edge_min=0.5, edge_max=20, tolerance=0.01) force_artist = VolMeshArtist(forcediagram, layer=force_layer) form_artist = NetworkArtist(formdiagram, layer=form_layer) scaled_cell_artist = MeshArtist(None, layer=force_layer) prism_artist = MeshArtist(None, layer=force_layer) force_artist.draw_faces() form_artist.draw_edges() # ------------------------------------------------------------------------------ # 4. draw unified diagram # ------------------------------------------------------------------------------ while True: rs.EnableRedraw(True)
self.clear_edges() # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures import Network from compas_rhino.artists import NetworkArtist network = Network.from_obj(compas.get('grid_irregular.obj')) artist = NetworkArtist(network, layer='NetworkArtist') artist.clear_layer() artist.draw_vertices() artist.redraw(0.0) artist.draw_vertexlabels() artist.redraw(1.0) artist.draw_edges() artist.redraw(1.0) artist.draw_edgelabels() artist.redraw(1.0)
for node in network.nodes(): point = network.node_coordinates(node) for nbr in tree.nearest_neighbors(point, 4, distance_sort=True): if nbr[2] < 1e-6: continue if not network.has_edge(node, nbr[1], directed=False): network.add_edge(node, nbr[1]) start = network.get_any_node() goal = network.get_any_node() path = network.shortest_path(start, goal) edges = [(u, v) if network.has_edge(u, v) else (v, u) for u, v in pairwise(path)] artist = NetworkArtist(network, layer="ITA20::Network") artist.clear_layer() artist.draw_nodes(color={start: (255, 0, 0), goal: (0, 0, 255)}) artist.draw_edges(color={edge: (0, 255, 0) for edge in edges}) for u, v in edges: o = network.edge_midpoint(u, v) n = network.edge_direction(u, v) h = network.edge_length(u, v) cylinder = Cylinder([(o, n), 0.02], h) artist = CylinderArtist(cylinder, color=(0, 255, 0), layer="ITA20::Network") artist.draw(show_vertices=False)
def draw_vertex_labels(self, **kwattr): artist = NetworkArtist(self) artist.draw_vertexlabels(**kwattr)
def network_draw(network, layer=None, clear_layer=False, clear_vertices=True, clear_edges=True, vertexcolor=None, edgecolor=None): """Draw a network data structure in Rhino. Parameters ---------- network : compas.datastructures.Network A network object. layer : str (None) The layer to draw in. Default is the current layer. clear_layer : bool (False) Clear the layer. vertexcolor : list, tuple, str, dict (None) The color specification for the vertices. * list, tuple: rgb color, with color specs between 0 and 255 (e.g. ``(255, 0, 0)``). * str: hex color (e.g. ``'#ff0000'``). * dict: dictionary of hex or rgb colors. edgecolor : list, tuple, str, dict (None) The color specification for the edges. * list, tuple: rgb color, with color specs between 0 and 255 (e.g. ``(255, 0, 0)``). * str: hex color (e.g. ``'#ff0000'``). * dict: dictionary of hex or rgb color. Notes ----- * Any network objects with the same name that are already present in the model will be deleted by this function. * To also clear the entire layer the network will be drawn on, for example, if you have a dedicated network layer, use the ``clear_layer`` flag as well. See Also -------- * :func:`network_draw_vertices` * :func:`network_draw_edges` Examples -------- >>> """ artist = NetworkArtist(network) artist.layer = layer if clear_layer: artist.clear_layer() if clear_vertices: artist.clear_vertices() if clear_edges: artist.clear_edges() artist.draw_vertices(color=vertexcolor) artist.draw_edges(color=edgecolor) artist.redraw()
from itertools import combinations from compas.datastructures import Network from compas.utilities import linspace, meshgrid from compas_rhino.artists import NetworkArtist X, Y = meshgrid(linspace(0, 10, 10), linspace(0, 5, 5)) points = [] for z in linspace(0, 3, 3): for xs, ys in zip(X, Y): for x, y in zip(xs, ys): points.append([x, y, z]) network = Network() for point in points: network.add_node(x=point[0], y=point[1], z=point[2]) for a, b in combinations(network.nodes(), 2): if network.node_attribute(a, 'z') != network.node_attribute(b, 'z'): network.add_edge(a, b) artist = NetworkArtist(network, layer="ITA20::Network") artist.clear_layer() artist.draw_nodes() artist.draw_edges()
# Attributes network.update_default_edge_attributes({'E': E, 'A': A, 's0': s0}) # Pins gkey_key = network.gkey_key() pins = [] for i in rs.ObjectsByLayer('Pins'): gkey = geometric_key(rs.PointCoordinates(i)) key = gkey_key[gkey] network.set_vertex_attributes(key, 'B', [[0, 0, 0]]) pins.append(key) # Run XFunc X, f, l, network = drx_numpy(structure=network, factor=factor, tol=tol, steps=steps, refresh=10, update=True) # Draw Network artist = NetworkArtist(network=network, layer='Plot') artist.clear_layer() artist.draw_edges()