1 # Test KindOfShape method for Edges
5 from inspect import getfile
6 from os.path import abspath, dirname, join
7 salome.salome_init_without_session()
10 from salome.geom import geomBuilder
11 geompy = geomBuilder.New()
14 def isEqual(v1, v2, tol=1.e-5):
15 return abs(v1 - v2) < tol
17 def isEqualPoint(p1, p2, tol=1.e-5):
18 return isEqual(p1[0], p2[0], tol) and isEqual(p1[1], p2[1], tol) and isEqual(p1[2], p2[2], tol)
20 def assertEqualType(props, t):
21 assert (props[0]==t), f"Expected type {t}, but was {props[0]}"
23 def checkSegment(props, p1, p2):
24 assertEqualType(props, geompy.kind.SEGMENT)
26 assert isEqualPoint(p, p1), f"Expected SEGMENT start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
28 assert isEqualPoint(p, p2), f"Expected SEGMENT end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
30 def checkCircle(props, c, d, r):
31 assertEqualType(props, geompy.kind.CIRCLE)
33 assert isEqualPoint(p, c), f"Expected CIRCLE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
35 assert isEqualPoint(p, d), f"Expected CIRCLE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
36 assert isEqual(props[7], r), f"Expected CIRCLE radius {r:.4f}, but was {props[7]:.4f}"
38 def checkArcCircle(props, c, d, r, p1, p2):
39 assertEqualType(props, geompy.kind.ARC_CIRCLE)
41 assert isEqualPoint(p, c), f"Expected ARC_CIRCLE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
43 assert isEqualPoint(p, d), f"Expected ARC_CIRCLE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
44 assert isEqual(props[7], r), f"Expected ARC_CIRCLE radius {r:.4f}, but was {props[7]:.4f}"
46 assert isEqualPoint(p, p1), f"Expected ARC_CIRCLE start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
48 assert isEqualPoint(p, p2), f"Expected ARC_CIRCLE end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
50 def checkEllipse(props, c, d, r1, r2, vx, vy):
51 assertEqualType(props, geompy.kind.ELLIPSE)
53 assert isEqualPoint(p, c), f"Expected ELLIPSE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
55 assert isEqualPoint(p, d), f"Expected ELLIPSE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
56 assert isEqual(props[7], r1), f"Expected ELLIPSE major radius {r1:.4f}, but was {props[7]:.4f}"
57 assert isEqual(props[8], r2), f"Expected ELLIPSE minor radius {r2:.4f}, but was {props[8]:.4f}"
59 assert isEqualPoint(p, vx), f"Expected ELLIPSE x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
61 assert isEqualPoint(p, vy), f"Expected ELLIPSE y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
63 def checkArcEllipse(props, c, d, r1, r2, p1, p2, vx, vy):
64 assertEqualType(props, geompy.kind.ARC_ELLIPSE)
66 assert isEqualPoint(p, c), f"Expected ARC_ELLIPSE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
68 assert isEqualPoint(props[4:7], d), f"Expected ARC_ELLIPSE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
69 assert isEqual(props[7], r1), f"Expected ARC_ELLIPSE major radius {r1:.4f}, but was {props[7]:.4f}"
70 assert isEqual(props[8], r2), f"Expected ARC_ELLIPSE minor radius {r2:.4f}, but was {props[8]:.4f}"
72 assert isEqualPoint(props[9:12], p1), f"Expected ARC_ELLIPSE start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
74 assert isEqualPoint(props[12:15], p2), f"Expected ARC_ELLIPSE end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
76 assert isEqualPoint(props[15:18], vx), f"Expected ARC_ELLIPSE x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
78 assert isEqualPoint(props[18:21], vy), f"Expected ARC_ELLIPSE y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
80 def checkHyperbola(props, c, d, r1, r2, vx, vy):
81 assertEqualType(props, geompy.kind.HYPERBOLA)
83 assert isEqualPoint(p, c), f"Expected HYPERBOLA center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
85 assert isEqualPoint(p, d), f"Expected HYPERBOLA direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
86 assert isEqual(props[7], r1), f"Expected HYPERBOLA major radius {r1:.4f}, but was {props[7]:.4f}"
87 assert isEqual(props[8], r2), f"Expected HYPERBOLA minor radius {r2:.4f}, but was {props[8]:.4f}"
89 assert isEqualPoint(p, vx), f"Expected HYPERBOLA x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
91 assert isEqualPoint(p, vy), f"Expected HYPERBOLA y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
93 def checkParabola(props, c, d, f, vx, vy):
94 assertEqualType(props, geompy.kind.PARABOLA)
96 assert isEqualPoint(p, c), f"Expected PARABOLA center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
98 assert isEqualPoint(p, d), f"Expected PARABOLA direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
99 assert isEqual(props[7], f), f"Expected PARABOLA focal length {f:.4f}, but was {props[7]:.4f}"
101 assert isEqualPoint(p, vx), f"Expected PARABOLA x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
103 assert isEqualPoint(p, vy), f"Expected PARABOLA y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
105 def assertPoles(props, np, pfirst, poles):
107 assert (n<=np), f"Too much poles given ({n}), but should not be more than {np}"
108 for i in range(0,n-1):
110 p2 = props[pfirst+i*3:pfirst+(i+1)*3]
111 assert isEqualPoint(p1, p2), f"Expected pole #{i+1} ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f})"
113 def assertKnots(props, nk, kfirst, knots):
115 assert (n<=nk), f"Too much knots given ({n}), but should not be more than {nk}"
117 for i in range(0,n-1):
118 assert isEqual(knots[i], props[kfirst+i]), f"Expected knot #{i+1} {knots[i]:.4f}, but was {props[kfirst+i]:.4f}"
120 assert("Number of knots does not match expected number")
122 def assertWeights(props, nw, wfirst, weights):
124 assert (n<=nw), f"Too much weights given ({n}), but should not be more than {nw}"
126 for i in range(0,n-1):
127 assert isEqual(weights[i], props[wfirst+i]), f"Expected weight #{i+1} {weights[i]:.4f}, but was {props[wfirst+i]:.4f}"
129 assert("Number of weights does not match expected number")
131 def assertMultiplicities(props, nm, mfirst, multis):
133 assert (n<=nm), f"Too much multiplicities given ({n}), but should not be more than {nm}"
135 for i in range(0,n-1):
136 assert isEqual(multis[i], props[mfirst+i]), f"Expected multiplicity #{i+1} {multis[i]:.4f}, but was {props[mfirst+i]:.4f}"
138 assert("Number of multiplicities does not match expected number")
140 def checkBSpline(props, period, deg, np, nk, nw, nm, poles=[], knots=[], weights=[], multis=[]):
141 assertEqualType(props, geompy.kind.CRV_BSPLINE)
142 assert (period==props[1]), f"Expected CRV_BSPLINE periodicity ({period}), but was ({props[1]})"
143 assert (deg==props[2]), f"Expected CRV_BSPLINE degree ({deg}), but was ({props[2]})"
144 assert (np==props[3]), f"Expected CRV_BSPLINE number of poles ({np}), but was ({props[3]})"
145 assert (nk==props[4]), f"Expected CRV_BSPLINE number of knots ({nk}), but was ({props[4]})"
146 assert (nw==props[5]), f"Expected CRV_BSPLINE number of weights ({nw}), but was ({props[5]})"
147 assert (nm==props[6]), f"Expected CRV_BSPLINE number of multiplicities ({nm}), but was ({props[6]})"
148 if poles and len(poles):
149 assertPoles(props, np, 7, poles)
150 if knots and len(knots):
151 assertKnots(props, nk, 7+3*np, knots)
152 if weights and len(weights):
153 assertWeights(props, nw, 7+3*np+nk, weights)
155 def checkBezier(props, np, nw, poles=[]):
156 assertEqualType(props, geompy.kind.CRV_BEZIER)
157 assert (np==props[1]), f"Expected CRV_BEZIER number of poles ({np}), but was ({props[1]})"
158 assert (nw==props[2]), f"Expected CRV_BEZIER number of weights ({nw}), but was ({props[2]})"
159 if poles and len(poles):
160 assertPoles(props, np, 3, poles)
163 data_dir = abspath(join(dirname(getfile(lambda: None)), 'data'))
165 O = geompy.MakeVertex(0, 0, 0)
166 OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
167 OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
168 OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
169 Vertex_1 = geompy.MakeVertex(0, 0, 0)
170 Vertex_2 = geompy.MakeVertex(50, 100, 0)
171 Vertex_3 = geompy.MakeVertex(-10, 60, 0)
172 Vertex_4 = geompy.MakeVertex(0, 100, 0)
173 Vertex_5 = geompy.MakeVertex(-100, 100, 0)
174 Vertex_6 = geompy.MakeVertex(-100, 0, 0)
175 Vertex_7 = geompy.MakeVertex(-200, 0, 0)
176 Vertex_8 = geompy.MakeVertex(-200, 100, 0)
178 # create or import some curves
179 Line_1 = geompy.MakeLineTwoPnt(Vertex_1, Vertex_2)
180 Circle_1 = geompy.MakeCircle(Vertex_2, OZ, 50)
181 Ellipse_1 = geompy.MakeEllipse(Vertex_1, OZ, 200, 100, Line_1)
182 Arc_1 = geompy.MakeArc(Vertex_2, Vertex_3, Vertex_1)
183 Curve_1 = geompy.MakeCurveParametric("t", "50*sin(t)", "0", 0, 360, 30, GEOM.Interpolation, True)
184 Curve_2 = geompy.MakeCurveParametric("-t", "50*cos(t)", "t", 0, 360, 14, GEOM.Bezier, True)
185 Curve_3 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve3.brep"))
186 Curve_4 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve4.brep"))
187 Curve_5 = geompy.MakeInterpol([Vertex_1, Vertex_4, Vertex_5, Vertex_6, Vertex_7, Vertex_8], False, False)
188 Curve_6 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve6.brep"))
189 Curve_7 = geompy.MakeBezier([Vertex_5, Vertex_6, Vertex_7, Vertex_8], True)
190 Curve_8 = geompy.MakeBezier([Vertex_5, Vertex_6, Vertex_1, Vertex_4], False)
191 Curve_9 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve9.brep"))
192 Curve_10 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve10.brep"))
195 props = geompy.KindOfShape(Line_1)
196 # [SEGMENT, 0.,0.,0., 50.,100.,0.]
197 checkSegment(props, [0,0,0], [50,100,0])
199 props = geompy.KindOfShape(Circle_1)
200 # [CIRCLE, 50.,100.,0., 0.,0.,1., 50.]
201 checkCircle(props, [50,100,0], [0,0,1], 50)
203 props = geompy.KindOfShape(Ellipse_1)
204 # [ELLIPSE, 0.,0.,0., 0.,0.,1., 200., 100., 0.44721,0.89443,0., 0.44721,0.89443,0.]
205 checkEllipse(props, [0,0,0], [0,0,1], 200, 100, [0.4472136,0.8944272,0], [-0.8944272,0.4472136,0])
207 props = geompy.KindOfShape(Arc_1)
208 # [ARC_CIRCLE, 47.5,38.75,0., 0.,0.,1., 61.301, 50.,100.,0., 0.,0.,0.]
209 checkArcCircle(props, [47.5,38.75,0], [0,0,1], 61.301, [50,100,0], [0,0,0])
211 props = geompy.KindOfShape(Curve_1)
212 # [CRV_BSPLINE, 0, 3, 33, 31, 0, 31, 0.,0.,0.,..., 4,1,...,1,4]
213 checkBSpline(props, 0, 3, 33, 31, 0, 31, [[0,0,0],[5.246092,-6.374961,0],[10.613646,-25.338811,0],[19.662636,-44.299221,0]], [0,29.39007,51.399444,64.149986], [], [4,1,1,1,1])
215 props = geompy.KindOfShape(Curve_2)
216 # [CRV_BEZIER, 15, 0, 0.,50.,0.,...,-360.,-14.18455,360.]
217 checkBezier(props, 15, 0, [[0,50,0],[-25.714286,41.780762,25.714286],[-51.428571,19.825283,51.428571],[-77.142857,-8.648146,77.142857]])
219 props = geompy.KindOfShape(Curve_3)
220 # [CRV_BSPLINE, 1, 3, 8, 9, 0, 0, -100.,0.,0.,..., 0.,1.,2.,3.,4.,5.,6.,7.,8.]
221 checkBSpline(props, 1, 3, 8, 9, 0, 0, [[-100,0,0],[-200,200,0],[0,100,0],[200,200,0],[100,0,0],[200,-200,0],[0,-100,0],[-200,-200,0]], [0,1,2,3,4,5,6,7,8])
223 props = geompy.KindOfShape(Curve_4)
224 # [CRV_BSPLINE, 1, 2, 3, 4, 3, 0, 0.,0.,0., 100.,200.,0., 200.,0.,0., 0,1,2,3, 200.,100.,100.]
225 checkBSpline(props, 1, 2, 3, 4, 3, 0, [[0,0,0],[100,200,0],[200,0,0]], [0,1,2,3], [200,100,100])
227 props = geompy.KindOfShape(Curve_5)
228 # [CRV_BSPLINE, 0, 3, 8, 6, 0, 6, 0.,0.,0.,..., 100.,0.,0.,100.,200.,300.,400.,500., 4,1,1,1,1,4]
229 checkBSpline(props, 0, 3, 8, 6, 0, 6, [[0,0,0],[38.888889,50,0],[23.684211,113.157895,0]], [0,100,200,300,400,500], [], [4,1,1,1,1,4])
231 props = geompy.KindOfShape(Curve_6)
232 # [ARC_ELLIPSE, -18.46154,0.,64.61538, 0.57668,0.,-0.81697, 96.03993, 76.25867, 50.,-37.25439,112.94118, 50.,37.25439,112.94118, -0.81697,0.,-0.57668, 0.,1.,0.]
233 checkArcEllipse(props, [-18.46154,0,64.61538], [0.57668,0,-0.81697], 96.03993, 76.25867, [50,-37.25439,112.94118], [50,37.25439,112.94118], [-0.81697,0,-0.57668], [0,1,0])
235 props = geompy.KindOfShape(Curve_7)
236 # [CRV_BEZIER, 5, 0, -100.,100.,0., -100.,0.,0., -200.,0.,0., -200.,100.,0., -100.,100.,0.]
237 checkBezier(props, 5, 0, [[-100,100,0],[-100,0,0],[-200,0,0],[-200,100,0],[-100,100,0]])
239 props = geompy.KindOfShape(Curve_8)
240 # [CRV_BEZIER, 4, 0, -100.,100.,0., -100.,0.,0., 0.,0.,0., 0.,100.,0.]
241 checkBezier(props, 4, 0, [[-100,100,0],[-100,0,0],[0,0,0],[0,100,0]])
243 props = geompy.KindOfShape(Curve_9)
244 # [HYPERBOLA, -50., 0., 300., 1., 0., 0., 75., 50., 0., 0., -1., 0., 1., 0.]
245 checkHyperbola(props, [-50,0,300], [1,0,0], 75, 50, [0,0,-1], [0,1,0])
247 props = geompy.KindOfShape(Curve_10)
248 # [PARABOLA, -25.,0.,262.5, -0.83205,0.,-0.5547, 13.8675, 0.5547,0.,-0.83205, 0.,-1.,0.]
249 checkParabola(props, [-25,0,262.5], [-0.83205,0,-0.5547], 13.867505, [0.5547,0,-0.83205], [0,-1,0])