Description

Crée des calques à partir d’un fichier .csv.

Voir aussi le script ExportLayerTree et la Hiérarchie de modélisation.

Le script suivant fonctionne sur :

  • Rhino pour Windows ;
  • Rhino pour macOS.

Script par MLAV.LAND, sous licence GNU GPL 3.

Code

import csv
import rhinoscriptsyntax as rs
 
def open_csv(file_path):  # Function to open the CSV file and read layer data
    with open(file_path, 'rb') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            full_layer_name = row['FullLayerName']
            color = rs.CreateColor(int(row['R']), int(row['G']), int(row['B']))
            print_color = parse_color(row['PrintColor'])
            linetype = row['Linetype']
            print_width = parse_print_width(row['PrintWidth'])
            create_layer(full_layer_name, color, print_color, linetype, print_width)
 
def parse_color(color_string):
    # Parse a color from the CSV, which could be an RGB string or 'ByLayer'
    if color_string.lower() == 'bylayer':
        return None  # Use None to signify the default 'ByLayer' color
    else:
        r, g, b = map(int, color_string.split())
        return rs.CreateColor(r, g, b)
 
def parse_print_width(print_width_string):
    # Parse a print width from the CSV, which could be a number or 'ByLayer/Default'
    if print_width_string.lower() in ['bylayer', 'bylayer/default', 'default']:
        return None  # Use None to signify the default print width
    else:
        return float(print_width_string)
 
def create_layer(full_layer_name, color, print_color, linetype, print_width):
    # Check if the layer exists
    if rs.IsLayer(full_layer_name):
        rs.LayerColor(full_layer_name, color)
        if print_color:
            rs.LayerPrintColor(full_layer_name, print_color)
        if print_width is not None:
            rs.LayerPrintWidth(full_layer_name, print_width)
        if linetype and rs.IsLinetype(linetype):
            rs.LayerLinetype(full_layer_name, linetype)
    else:
        # Create the layer with the specified color
        new_layer = rs.AddLayer(full_layer_name, color)
        if new_layer:
            if print_color:
                rs.LayerPrintColor(new_layer, print_color)
            if print_width is not None:
                rs.LayerPrintWidth(new_layer, print_width)
            if linetype and not rs.IsLinetype(linetype):
                rs.AddLinetype(linetype, [(0.0, "Solid")])  # Default pattern, modify as needed
            if linetype and rs.IsLinetype(linetype):
                rs.LayerLinetype(new_layer, linetype)
 
if __name__ == "__main__":
    file_path = rs.OpenFileName("Open CSV File", "CSV Files (*.csv)|*.csv||")
    if file_path:
        open_csv(file_path)