-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGrasshopper.py
223 lines (191 loc) · 7.68 KB
/
Grasshopper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Grasshopper Module Version 0.0.1.7
# Developed by Mahmoud Abdelrahman
"""
This Module is part of GH_CPython
BSD 2-Clause License
This project has been started by: Mahmoud M. Abdelrahman
<arch.mahmoud.ouf111[at]gmail.com>
Copyright (c) 2017, Mahmoud AbdelRahman
All rights reserved.
https://github.com/MahmoudAbdelRahman/GH_CPython/blob/master/LICENSE
"""
import sys
version = sys.version_info[0]
class Line:
def __init__(self, *args):
"""Adds new line using two input points, or two input lists or 6 input doubles
:param args:
"""
if len(args) == 2:
if isinstance(args[0], Point) and isinstance(args[1], Point):
self.X1 = args[0].X
self.Y1 = args[0].Y
self.Z1 = args[0].X
self.X2 = args[1].Z
self.Y2 = args[1].Y
self.Z2 = args[1].Z
elif isinstance(args[0], list) and isinstance(args[1], list):
self.X1 = args[0][0]
self.Y1 = args[0][1]
self.Z1 = args[0][2]
self.X2 = args[1][0]
self.Y2 = args[1][1]
self.Z2 = args[1][2]
elif version == 2:
if isinstance(args[0], basestring) and isinstance(args[1], basestring):
pointa = Point(args[0])
pointb = Point(args[1])
self.X1 = pointa.X
self.Y1 = pointa.Y
self.Z1 = pointa.Z
self.X2 = pointb.X
self.Y2 = pointb.Y
self.Z2 = pointb.Z
elif version == 3:
if isinstance(args[0], str) and isinstance(args[1], str):
pointa = Point(args[0])
pointb = Point(args[1])
self.X1 = pointa.X
self.Y1 = pointa.Y
self.Z1 = pointa.Z
self.X2 = pointb.X
self.Y2 = pointb.Y
self.Z2 = pointb.Z
elif len(args) == 6:
self.X1 = args[0]
self.Y1 = args[1]
self.Z1 = args[2]
self.X2 = args[3]
self.Y2 = args[4]
self.Z2 = args[5]
'''def addLine(self):
return "gCPy.Line(" + str(self.X1) + ", " \
+ str(self.Y1) + ", " \
+ str(self.Z1) + ", " \
+ str(self.X2) + ", " \
+ str(self.Y2) + ", " \
+ str(self.Z2) + ")"'''
def addLine(self):
return ["<Line>", self.X1, self.Y1, self.Z1, self.X2, self.Y2, self.Z2]
def __repr__(self):
return str(["<Line>",self.X1,self.Y1,self.Z1,self.X2,self.Y2,self.Z2,"</line>"])
def length(self):
return ((self.X2 - self.X1) ** 2 + (self.Y2 - self.Y1) ** 2 + (self.Z2 - self.Z1) ** 2) ** 0.5
def pointOnLine(self, parameter=0.5):
return Point((self.X2 - self.X1) * parameter + self.X1, \
(self.Y2 - self.Y1) * parameter + self.Y1, \
(self.Z2 - self.Z1) * parameter + self.Z1)
def __str__(self):
return str(["<Line>", self.X1, self.Y1, self.Z1, self.X2, self.Y2, self.Z2, "</Line>"])
allLines = []
for i in range(200):
l = Line(i, i+1, i+2, i-1, i-2, i-5)
allLines.append(l)
print(allLines[5])
class Point:
"""Adds new point in cartesian coordinates using 3 doubles x, y, z as input
:param:
x (double): is the x coordinate
y (double): is the y coordinate
z (double): is the z coordinate
:return:
representation of 3d point
:Example:
import Grasshopper as gh
point1 = gh.Point(0, 0, 0)
point2 = gh.Point(1., 1., 0.)
"""
def __init__(self, x=0., y=0., z=0.):
if version == 2:
if isinstance(x, list):
self.X = x[0]
self.Y = x[1]
self.Z = x[2]
elif isinstance(x, basestring):
new_vars = []
x = x.replace("gCPy.Point(", "").replace(")", "").lstrip().rstrip()
variables = x.split(",")
for i in variables:
new_vars.append(float(i))
self.X = new_vars[0]
self.Y = new_vars[1]
self.Z = new_vars[2]
else:
self.X = x
self.Y = y
self.Z = z
elif version == 3:
if isinstance(x, list):
self.X = x[0]
self.Y = x[1]
self.Z = x[2]
elif isinstance(x, str):
new_vars = []
x = x.replace("gCPy.Point(", "").replace(")", "").lstrip().rstrip()
variables = x.split(",")
for i in variables:
new_vars.append(float(i))
self.X = new_vars[0]
self.Y = new_vars[1]
self.Z = new_vars[2]
else:
self.X = x
self.Y = y
self.Z = z
self.addPoint = "gCPy.Point(" + str(x) + "," + str(y) + "," + str(z) + ")"
def __repr__(self):
return "gCPy.Point(" + str(self.X) + "," + str(self.Y) + "," + str(self.Z) + ")"
def __str__(self):
return "gCPy.Point(" + str(self.X) + "," + str(self.Y) + "," + str(self.Z) + ")"
class Surface:
def __init__(self, *args):
if len(args) == 4:
if isinstance(args[0], Point) and isinstance(args[1], Point) and isinstance(args[2], Point) and isinstance(args[3], Point):
self.P1 = args[0]
self.P2 = args[1]
self.P3 = args[2]
self.P4 = args[3]
self.addSurface = "gCPy.Surface("+ str(args[0].X) + "," \
+ str(args[0].Y) + "," \
+ str(args[0].Z) + "," \
+ str(args[1].X) + "," \
+ str(args[1].Y) + "," \
+ str(args[1].Z) + "," \
+ str(args[2].X) + "," \
+ str(args[2].Y) + "," \
+ str(args[2].Z) + "," \
+ str(args[3].X) + "," \
+ str(args[3].Y) + "," \
+ str(args[3].Z) + "," \
+ ")"
elif len(args) == 2:
if isinstance(args[0],Line) and isinstance(args[1], Line):
pass
else:
print("you have to create surface from 4 points")
########################### DEFINE METHODS ################################
def addLine(*args):
"""Adds new line between two points
:param: args
pointa, pointb (Point3): instance of points
[x1, y1, z1], [x2, y2, z2]: two points as lists
x1, y1, z1, x2, y2, z2 : 6 doubles represent coordinates of line end points.
:return:
"""
if len(args) == 2:
return Line(args[0], args[1])
elif len(args) == 6:
return Line(args[0], args[1], args[2], args[3], args[4], args[5])
def addPoint(*args):
"""
:param args:
:return:
"""
if len(args) == 1:
return Point(args[0])
elif len(args) == 3:
return Point(args[0], args[1], args[2])
def addSurface(*args):
return Surface(args[0], args[1], args[2], args[3]).addSurface
if __name__ == '__main__':
print(__name__)