Importowanie punktów z pliku CSV, obracanie ich i łączenie w polilinię
Import points from a CSV file, rotate them and connect into a polyline
Ten skrypt wymaga modyfikacji, zanim będzie działać. Musisz zastąpić plik CSV i katalog własnymi. Skrypt zakłada, że jednostką są mm. Jeśli w obszarze roboczym ustawiono inną jednostkę, zaimportowany szkic może również być wyświetlany w innej skali.
Działanie skryptu:
- Otwiera plik CSV w formacie (x,y bez nagłówków)
| 0 | 0 |
|---|
| 0 | 4 |
|---|
| 3 | 5 |
|---|
| 6 | 0 |
|---|
| 0 | 0 |
|---|
-
Obraca punkty o zadaną wartość wokół punktu obrotu:
-
konfiguracja
Dział zatytułowany „konfiguracja”
angle = 45 rotationpoint = [15.0, 0.0]
plik do zaimportowania — zastąp własnym przykładem
Dział zatytułowany „plik do zaimportowania — zastąp własnym przykładem”csvfile = r’C:\temp\sample.csv’
- Tworzy nową część i szkic z polilinią przechodzącą przez obrócone punkty szkicu
# imports a set of 2D points from a csv file, rotates them and then# adds them to a 2D sketch with lines connecting the points
# specify which libraries we are going to useimport csvimport math
# configurationangle = 45rotationpoint = [15.0, 0.0]# file to import, replace with your own examplecsvfile = r'C:\temp\sample.csv'
# rotates a point around another point# passed is the angle, the point to rotate and the origin of the rotation# copied from http://ubuntuforums.org/showthread.php?t=975315&p=8618044#post8618044def rotate2d(degrees,point,origin): x = point[0] - origin[0] yorz = point[1] - origin[1] newx = (x*math.cos(math.radians(degrees))) - (yorz*math.sin(math.radians(degrees))) newyorz = (x*math.sin(math.radians(degrees))) + (yorz*math.cos(math.radians(degrees))) newx += origin[0] newyorz += origin[1] return newx,newyorz
# list of points, empty for now# points will be stored as [x1,y1, x2,y2, ... ,xn,yn]points = []
# open csv filef = open(csvfile)
# create csv reader and read in linesreader = csv.reader(f)for row in reader: # column 0 contains x, column 1 contains y x = float(row[0]) y = float(row[1])
# rotate point and add to list of points points.extend(rotate2d(angle, [x, y], rotationpoint))
# finished with csv filef.close()
# show number of points foundprint 'Found %d points' % (len(points) / 2)
# create partMyPart = Part('My Part')# add sketch on XY planePointSketch = MyPart.AddSketch('Point Sketch', MyPart.GetPlane('XY-Plane'))# add points with lines connecting themPointSketch.AddLines(points, False)Oryginał: Import points from a CSV file, rotate them and connect into a polyline