This is the sixth in-class practice saving created objects into files and then importing the file back using file functions in Rhinoceros.
Here is the Python Code for creating random points:
import rhinoscriptsyntax as rs import random rs.DeleteObjects (rs.AllObjects ()) nrPoints = rs.GetInteger ("Number of points") cubeSize = 10 rs.EnableRedraw (False) for i in range (0, nrPoints): x = random.uniform (0, cubeSize) y = random.uniform (0, cubeSize) z = random.uniform (0, cubeSize) point = rs.AddPoint (x, y, z) #we can assign specific colors to rgb by giving the exact number. e.g.(for red, r=255,g=0,b=0) r = random.randint (0, 255) g = random.randint (0, 255) b = random.randint (0, 255) rs.ObjectColor (point, (r, g, b)) rs.EnableRedraw (True)
Here is the Python Code for saving the created points into a file:
import rhinoscriptsyntax as rs points = rs.GetObjects ("Please select points") #get gilename and location from user #saved this to file named "savedfile", and the file has a list of coordinates filename = rs.SaveFileName () #opening the file for access myfile = open(filename,"w") for point in points: coord = rs.PointCoordinates (point) x = coord[0] y = coord[1] z = coord[2] #r = rs.ObjectColor (point)[0] #g = rs.ObjectColor (point)[1] #b = rs.ObjectColor (point)[2] #print (str(x) + "," + str(y) + "," + str(z)) #below means what is x/y/z is replaced by %.2f/%.2f/%.2f, .2 makes the printed coordinates only show 2 digits #print ("coord X: %.2f, coord Y: %.2f, coord Z: %.2f" %(x,y,z)) color = rs.ObjectColor (point) print color r = color.R g = color.G b = color.B #print (str(x) + "," + str(y) + "," + str(z)) newLine = ("%.2f,%.2f,%.2f,%d,%d,%d\n" %(x,y,z,r,g,b)) myfile.write(newLine) myfile.close()
Here is the Python Code for importing the saved file back:
import rhinoscriptsyntax as rs rs.DeleteObjects (rs.AllObjects ()) filename = rs.OpenFileName () #open the saved file file = open (filename, "r") text = file.readlines () file.close() #above process was running at background, so we print it to see if it is working #for line in text: #normalLine = line.strip() #print normalLine #it will read the file and bring the values(x,y,z)(r,g,b) in for line in text: normalLine = line.strip() values = line.split(",") x = values[0] y = values[1] z = values[2] #we need to cast the rgb into integer r = int(values[3]) g = int(values[4]) b = int(values[5]) coord = (x,y,z) color = (r,g,b) point = rs.AddPoint (coord) rs.ObjectColor (point, color) #coord = (values[0], values[1], values[2]) #coord = (values[0], values[1], values[2]) #print coord