def _add_first_rule(cls, layer_name, rule_spec): """Receives: layer_name str. The name of the layer containing the rule rule_spec (labeled_shape_spec, labeled_shape_spec) Adds a new layer named layer_name. Inserts left and right frame instances. Draws the left and right labeled shapes. Returns: layer_name_out str. If successful None otherwise """ set_up_okay = g.Grammar._set_up_first_rule() rs.RenameLayer(s.Settings.first_rule_layer_name, layer_name) left_frame_position = s.Settings.first_rule_left_frame_position draw_okay = cls._draw_rule(rule_spec, layer_name, left_frame_position) if set_up_okay and draw_okay: return_value = layer_name else: return_value = None return return_value
def _add_first_initial_shape(cls, layer_name, initial_shape_spec): """Receives: layer_name str. The name of the layer containing the initial shape initial_shape_spec (line_specs, labeled_point_specs) Adds a new layer named layer_name. Inserts an initial shape frame. Draws the initial shape. Returns: layer_name str. If successful None otherwise """ set_up_okay = g.Grammar._set_up_first_initial_shape() rs.RenameLayer(s.Settings.first_initial_shape_layer_name, layer_name) frame_position = s.Settings.first_initial_shape_frame_position draw_okay = cls._draw_initial_shape(initial_shape_spec, layer_name, frame_position) if set_up_okay and draw_okay: return_value = layer_name else: return_value = None return return_value
import rhinoscriptsyntax as rs import Rhino import scriptcontext as sc import math import re import time import os #add to rhino as an alias if not rs.IsAlias("TNM_renameLayers"): rs.AddAlias("TNM_renameLayers", "'_-runPythonScript \"%s\"" % __file__) print 'addAlias' else: print 'alias could not be added' layers = rs.LayerIds() for layer_id in layers: if rs.IsLayer(layer_id): old_name = rs.LayerName(layer_id, False) if (re.search('0.0.0', old_name)): new_name = old_name.replace('0.0.0', '0.0.160.0') rs.RenameLayer(old_name, new_name) else: print layer_id
def translateLayer(layer): """ translates from Rhino Name to CAD Name input: one layer returns: new layer name """ rhinoLayerFilePath = "C:\\Users\\Tim\\Desktop\\temp\\RhinoLayersV2.csv" #Read the CSV file = open(rhinoLayerFilePath, "r") contents = file.readlines() file.close() #Variables CategoryCol = 1 RhinoLayerCol = 2 CADNameCol = 9 CADColorCol = 10 CADLineweightCol = 11 found = False layerShort = rs.LayerName(layer, False) newLayerName = "" #Check the CSV for row in contents: rowParts = row.split(",") if row.split(",")[RhinoLayerCol] == str( layerShort): #if layer name exists in CSV CADName = row.split(",")[CADNameCol] CADColor = translateColor(layer, row.split(",")[CADColorCol]) CADLineweight = row.split(",")[CADLineweightCol] #if Rhino name found but no CAD name associated with it if not CADName: CADName = "A-RHINO-" + layerShort newLayerName = CADName if len(CADLineweight) < 2: CADLineweight = 0 #Check if layer already exists. parent = rs.ParentLayer(layer) existLayers = rs.LayerChildren(parent) isExisting = False for existLayer in existLayers: #if new name already exists as a layer if rs.LayerName(existLayer, False) == CADName: layersObjs = rs.ObjectsByLayer(layer) for layersObj in layersObjs: rs.ObjectLayer(layersObj, existLayer) rs.DeleteLayer(layer) newLayerName = rs.LayerName(existLayer, False) print "Layer {} merged with existing layer {}.".format( layerShort, rs.LayerName(layer, False)) isExisting = True break #if layer does not already exist if isExisting == False: rs.LayerColor(layer, CADColor) rs.LayerPrintWidth(layer, float(CADLineweight)) newLayerName = CADName rs.RenameLayer(layer, CADName) print "Layer {} changed to layer {}.".format( layerShort, CADName) found = True break if not found: layerShort = rs.LayerName(layer, False) CADName = "A-RHINO-" + layerShort newLayerName = CADName rs.RenameLayer(layer, CADName) print "Layer {} has no matching CAD layer.".format(layerShort) return newLayerName return newLayerName
import rhinoscriptsyntax as rs obj_get = rs.GetObject("select object on layer to rename:",0,True) lay_name = rs.ObjectLayer(obj_get) lay_new_name = rs.GetString('Layer name is currently: "' + lay_name + '". Enter new name or Esc to cancel') if obj_get: if lay_new_name: rs.RenameLayer(lay_name,lay_new_name) print 'layer name change changed to "'+ lay_new_name + '".' else: print "layer name change cancelled."
def flush_data(self): print 'Adding polylines to CAD environment ... wait' _FILE = self.corePath + r'\Mesh.gcode' pl = [] # polyline list pc = [] # pointcloud list poly_fail = 0 _add_point_cloud = False _z_level_change = False first_z_move_in_layer = True # it is possible to define a layer range that is displayed in CAD # default is layer 1 to end -> [1, -1] layer_start = 0 layer_end = -1 _from_to_layer = [layer_start, layer_end] _layer = 1 LayerPoints = {} line_in_file = 0 end_of_file = False # check if a previous calculation exists if rs.IsLayer('MW 3D Printer PointCloud'): if rs.IsLayer('MW 3D Printer PointCloud_OLD'): rs.PurgeLayer('MW 3D Printer PointCloud_OLD') rs.RenameLayer('MW 3D Printer PointCloud', 'MW 3D Printer PointCloud_OLD') rs.LayerVisible('MW 3D Printer PointCloud_OLD', visible=False) else: rs.RenameLayer('MW 3D Printer PointCloud', 'MW 3D Printer PointCloud_OLD') rs.LayerVisible('MW 3D Printer PointCloud_OLD', visible=False) rs.AddLayer(name='MW 3D Printer PointCloud', visible=False) else: rs.AddLayer(name='MW 3D Printer PointCloud', visible=False) if rs.IsLayer('MW 3D Printer Perimeter'): if rs.IsLayer('MW 3D Printer Perimeter_OLD'): rs.PurgeLayer('MW 3D Printer Perimeter_OLD') rs.RenameLayer('MW 3D Printer Perimeter', 'MW 3D Printer Perimeter_OLD') rs.LayerVisible('MW 3D Printer Perimeter_OLD', visible=False) else: rs.RenameLayer('MW 3D Printer Perimeter', 'MW 3D Printer Perimeter_OLD') rs.LayerVisible('MW 3D Printer Perimeter_OLD', visible=False) rs.AddLayer(name='MW 3D Printer Perimeter', visible=True) else: rs.AddLayer(name='MW 3D Printer Perimeter', visible=True) # open G-Code fid = open(_FILE, 'r') # Begin G-Code processing last_pos = [] last_pos.append(fid.tell()) for line in fid: last_pos.append(fid.tell()) line_in_file += 1 if len(line) == 1: continue if self.runstat: # external functions can inject a command to stop threading # proof if there is a comment with layer information if line.strip()[0] == ';': if line.find('gCode file finished') != -1: end_of_file = True if line.find( 'WARNING') == -1: # if we do NOT find a WARNING split_line = line.split(',') _segment = split_line[0][1:].strip().rstrip() _backplot_width = split_line[0][1:].strip().rstrip() if _segment not in self.segmentIdxDict: self.segmentIdxDict[_segment] = 0 segment = _segment + str(self.segmentIdxDict[_segment]) else: pass # print 'WARNING' # coordinates of G1 and G0 moves are processed if line[0:3] == 'G1 ' or line[0:3] == 'G0 ': # filter z coordinates and proof if its the first inital or the next layer pos_Z = line.find('Z') if pos_Z != -1: pos_ws = line[pos_Z:].find(' ') if pos_ws == -1: Z = float(line[pos_Z + 1:]) else: try: Z = float(line[pos_Z + 1:pos_ws + pos_Z + 1]) except: Z = float(line[pos_Z + 1:pos_ws + pos_Z + 1]) if first_z_move_in_layer: first_z_move_in_layer = False else: _z_level_change = True fid.seek(last_pos[-3]) # revert file position last_pos.pop() last_pos.pop() last_pos.pop() line_in_file = line_in_file - 2 if not _z_level_change: # filter coordinates from G1 move if line[0:3] == 'G1 ': pos_X = line.find('X') if pos_X != -1: pos_ws = line[pos_X:].find(' ') X = float(line[pos_X + 1:pos_ws + pos_X + 1]) pos_Y = line.find('Y') if pos_Y != -1: pos_ws = line[pos_Y:].find(' ') if pos_ws == -1: Y = float(line[pos_Y + 1:]) else: Y = float(line[pos_Y + 1:pos_ws + pos_Y + 1]) # filter coordinates from G0 move elif line[0:3] == 'G0 ': pos_X = line.find('X') if pos_X != -1: pos_ws = line[pos_X:].find(' ') X_G0 = float(line[pos_X + 1:pos_ws + pos_X + 1]) pos_Y = line.find('Y') if pos_Y != -1: pos_ws = line[pos_Y:].find(' ') if pos_ws == -1: Y_G0 = float(line[pos_Y + 1:]) else: Y_G0 = float(line[pos_Y + 1:pos_ws + pos_Y + 1]) # add coordinates to layer dictionary if _layer >= _from_to_layer[0]: if line[0:3] == 'G0 ': if _segment in self.segmentIdxDict: self.segmentIdxDict[_segment] += 1 else: self.segmentIdxDict[_segment] = 0 segment = _segment + str( self.segmentIdxDict[_segment]) LayerPoints[segment] = [[ X_G0, Y_G0, Z ]] # first point of next segment else: if len(LayerPoints) == 0: LayerPoints[segment] = [[X, Y, Z]] else: if segment in LayerPoints: LayerPoints[segment].append([X, Y, Z]) # add lines and points to Rhino environment if _z_level_change and self.runstat or end_of_file: try: if _layer >= _from_to_layer[0]: if len(LayerPoints) > 1: for segment, points in LayerPoints.iteritems(): if self.runstat: if len(points) > 1: try: # .NET try: int(segment[-1]) lenIdx = 1 int(segment[-2:]) lenIdx = 2 int(segment[-3:]) lenIdx = 3 int(segment[-4:]) lenIdx = 4 except: pass if segment[: -lenIdx] in self.colorDict: pl.append( AddPolyline( points, _layer, objColor=self. colorDict[ segment[: -lenIdx]], segment=segment)) else: pl.append( AddPolyline( points, _layer, objColor=(255, 0, 0), segment=segment)) # print 'Segment not found' # add point cloud if _add_point_cloud: pc.append( rs.AddPointCloud( points)) except: raise poly_fail += 1 LayerPoints = {} # clear point information from dict self.segmentIdxDict = {} # clear segment counter dict _z_level_change = False _layer += 1 first_z_move_in_layer = True except: raise pass if _from_to_layer[1] != -1: if _layer == _from_to_layer[1]: break scriptcontext.doc.Views.Redraw() # rs.ObjectLayer(pl, layer='MW 3D Printer Perimeter') print 'Flushing path data finished.' print 'Polylines ignored: ' + str(poly_fail)