def plot_data(self): hatching = plot_data.HatchingSet(0.5, 3) edge_style = plot_data.EdgeStyle(line_width=1, color_stroke=BLUE, dashline=[]) surface_style = plot_data.SurfaceStyle(color_fill=WHITE, opacity=1, hatching=hatching) pt1 = vm.Point2D(0, 0) pt2 = vm.Point2D(0, self.ly) pt3 = vm.Point2D(self.lx, self.ly) pt4 = vm.Point2D(self.lx, 0) c1 = vm.wires.Contour2D([ vm.edges.LineSegment2D(pt1, pt2), vm.edges.LineSegment2D(pt2, pt3), vm.edges.LineSegment2D(pt3, pt4), vm.edges.LineSegment2D(pt4, pt1) ]) contours = [c1] for i in range(5): vector = vm.Vector2D(random.uniform(0, 2), random.uniform(0, 2)) c2 = c1.translation(vector, copy=True) contours.append(c2) plot_datas = [ c.plot_data(edge_style=edge_style, surface_style=surface_style) for c in contours ] return plot_data.PrimitiveGroup(primitives=plot_datas)
def contour(self): p0 = vm.Point2D(-self.length / 2, -self.height / 2) p1 = vm.Point2D(-self.length / 2, self.height / 2) p2 = vm.Point2D(self.length / 2, self.height / 2) p3 = vm.Point2D(self.length / 2, -self.height / 2) b1 = p2d.ClosedRoundedLineSegments2D([p0, p1, p2, p3], {}) return vm.wires.Contour2D(b1.primitives)
def contour(self, full_contour=False): if full_contour: p0 = vm.Point2D(0, 0) vectors = [vm.Vector2D(self.rivet_diameter / 2, 0), vm.Vector2D(self.head_diameter / 2 - self.rivet_diameter / 2, 0), vm.Vector2D(0, self.head_length), vm.Vector2D(-self.head_diameter, 0), vm.Vector2D(0, -self.head_length), vm.Vector2D(self.head_diameter / 2 - self.rivet_diameter / 2, 0), vm.Vector2D(0, -self.rivet_length), vm.Vector2D(self.rivet_diameter, 0), vm.Vector2D(0, self.rivet_length), ] else: p0 = vm.Point2D(0, 0) vectors = [vm.Vector2D(0, self.rivet_diameter / 2), vm.Vector2D(0, self.head_diameter / 2 - self.rivet_diameter / 2), vm.Vector2D(self.head_length, 0), vm.Vector2D(0, -self.head_diameter / 2), vm.Vector2D(-self.head_length - self.rivet_length, 0), vm.Vector2D(0, self.rivet_diameter / 2), vm.Vector2D(self.rivet_length, 0), ] points = [] p_init = p0 for v in vectors: p1 = p_init.translation(v, copy=True) points.append(p1) p_init = p1 return vm.wires.ClosedPolygon2D(points)
def plot(plot_datas, ax=None): if ax is None: fig, ax = plt.subplots() ax.set_aspect('equal') else: fig = None for plot_data in plot_datas: if plot_data['type'] == 'line': style = '' if plot_data['dash']: style += '--' else: style += '-' style += color[plot_data['color']] p1, p2 = plot_data['data'][0: 2], plot_data['data'][2:] if plot_data['arrow']: ax.plot([p1[0], p2[0]], [p1[1], p2[1]], style) length = ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5 if width is None: width = length / 1000. head_length = length/20. head_width = head_length/2. else: head_width = 2*width head_length = head_width ax.arrow(p1[0], p1[1], (p2[0] - p1[0])/length*(length - head_length), (p2[1] - p1[1])/length*(length - head_length), head_width = head_width, fc = 'b', linewidth = 0, head_length = head_length, width = width, alpha = 0.3) else: ax.plot([p1[0], p2[0]], [p1[1], p2[1]], style, linewidth=plot_data['size']) elif plot_data['type'] == 'point': p1 = plot_data['data'] style = '' style += color[plot_data['color']] style += plot_data['marker'] ax.plot(p1[0], p1[1], style, linewidth=plot_data['size']) elif plot_data['type'] == 'contour': plot(plot_data['plot_data'], ax) elif plot_data['type'] == 'arc': pc = vm.Point2D(( plot_data['cx'], plot_data['cy'])) ax.add_patch(Arc(pc, 2*plot_data['r'], 2*plot_data['r'], angle=0, theta1=plot_data['angle1']*0.5/math.pi*360, theta2=plot_data['angle2']*0.5/math.pi*360, color=color[plot_data['color']], linewidth=plot_data['size'])) elif plot_data['type'] == 'circle': pc = vm.Point2D(( plot_data['cx'], plot_data['cy'])) ax.add_patch(Arc(pc, 2*plot_data['r'], 2*plot_data['r'], angle=0, theta1=0, theta2=360, color=color[plot_data['color']], linewidth=plot_data['size'])) return fig, ax
def contour(self): points = [ vm.Point2D(0, 0), vm.Point2D(0, 1), vm.Point2D(1, 1), vm.Point2D(1, 0) ] crls = p2d.ClosedRoundedLineSegments2D(points=points, radius={}) return vm.wires.Contour2D(crls.primitives)
def plot(self, ax=None, x_setting=0., z_setting=0.): ax = self.cushion_contour().translation( vm.Point2D(x_setting, z_setting)).plot(ax=ax) self.backrest_contour().translation(vm.Point2D(x_setting, z_setting)).plot(ax=ax) self.headrest_contour().translation(vm.Point2D(x_setting, z_setting)).plot(ax=ax) ax.axvline(0.) ax.axhline(0.) return ax
def Geometry(self): pas = 2 * npy.pi / self.Z O = vm.Point2D((self.Ox, self.Oy)) A = O.Translation(vm.Point2D((0, self.DE / 2.))) Bp = O.Translation(vm.Point2D((0, self.DI / 2.))) B = Bp.Rotation(O, -self.alpha1, copy=True) Bi = B.Rotation(O, -self.alpha2 / 2., copy=True) C = B.Rotation(O, -self.alpha2, copy=True) D = A.Rotation(O, -self.alpha1 - self.alpha2 - self.alpha3, copy=True) Di = D.Rotation(O, (-pas + self.alpha1 + self.alpha2 + self.alpha3) / 2., copy=True) Ap = A.Rotation(O, -pas, copy=True) dir1 = (B[1] - A[1]) / (B[0] - A[0]) ord1 = B[1] - dir1 * B[0] def fonct(x, *data): Ox, Oy, R, coeff_dir, ord_origine = data f = (ord_origine + coeff_dir * x - Oy)**2 + (x - Ox)**2 - R**2 return f x1 = fsolve(fonct, 0, args=(self.Ox, self.Oy, self.DF / 2., dir1, ord1)) R1 = vm.Point2D((x1, ord1 + x1 * dir1)) dir2 = (D[1] - C[1]) / (D[0] - C[0]) ord2 = C[1] - dir2 * C[0] x2 = fsolve(fonct, 1, args=(self.Ox, self.Oy, self.DF / 2., dir2, ord2)) R2 = vm.Point2D((x2, ord2 + x2 * dir2)) li = [B, Bi, C, D, Di, Ap] AB = vm.LineSegment2D(A, B) BC = vm.Arc2D(B, Bi, C) CD = vm.LineSegment2D(C, D) list_seg = [] Am = A for z in range(self.Z): li_point = [] for pt in li: li_point.append(pt.Rotation(O, -pas * z, copy=True)) AB = vm.LineSegment2D(Am, li_point[0]) BC = vm.Arc2D(li_point[0], li_point[1], li_point[2]) CD = vm.LineSegment2D(li_point[2], li_point[3]) DA = vm.Arc2D(li_point[3], li_point[4], li_point[5]) list_seg.extend([AB, BC, CD, DA]) Am = li_point[5] c = vm.CompositePrimitive2D(list_seg) return c
def chest_contour(self, origin=vm.O2D, chest_angle=0.): p1 = origin + vm.Point2D(0.8 * self.chest_width, 0.) p2 = origin + vm.Point2D(self.chest_width, self.sitting_shoulder_height) p3 = origin + vm.Point2D(0, self.sitting_shoulder_height) p4 = origin + vm.Point2D(0, 0.) contour = p2d.ClosedRoundedLineSegments2D([p1, p2, p3, p4], {1: 0.3 * self.chest_width}) return contour.rotation(origin, chest_angle)
def upper_leg_contour(self, upper_leg_angle: float, origin=vm.O2D): p1 = origin p2 = origin + vm.Point2D(self.upper_leg_length, 0.) p3 = origin + vm.Point2D(self.upper_leg_length, self.upper_leg_width) p4 = origin + vm.Point2D(0., self.upper_leg_width) contour = p2d.ClosedRoundedLineSegments2D([p1, p2, p3, p4], { 1: 0.3 * self.upper_leg_width, 2: 0.3 * self.upper_leg_width }) return contour.rotation(origin, upper_leg_angle)
def lower_arm_contour(self, elbow_postion: vm.Point2D, lower_arm_angle: float): p1 = elbow_postion + vm.Point2D(0., -0.5 * self.lower_arm_width) p2 = elbow_postion + vm.Point2D(self.elbow_grip_center_length, -0.5 * self.lower_arm_width) p3 = elbow_postion + vm.Point2D(self.elbow_grip_center_length, 0.5 * self.lower_arm_width) p4 = elbow_postion + vm.Point2D(0., 0.5 * self.lower_arm_width) contour = p2d.ClosedRoundedLineSegments2D([p1, p2, p3, p4], { 1: 0.3 * self.lower_arm_width, 2: 0.3 * self.lower_arm_width }) return contour.rotation(elbow_postion, lower_arm_angle)
def contour(self, full_contour=False): p0 = vm.Point2D(0, 0) vectors = [ vm.Vector2D(self.rivet_diameter / 2, 0), vm.Vector2D(self.head_diameter / 2 - self.rivet_diameter / 2, 0), vm.Vector2D(0, self.head_length), vm.Vector2D(-self.head_diameter, 0), vm.Vector2D(0, -self.head_length), vm.Vector2D(self.head_diameter / 2 - self.rivet_diameter / 2, 0), vm.Vector2D(0, -self.rivet_length), vm.Vector2D(self.rivet_diameter, 0), vm.Vector2D(0, self.rivet_length), ] points = [] p_init = p0 for v in vectors: p1 = p_init.translation(v, copy=True) points.append(p1) p_init = p1 c = p2d.ClosedRoundedLineSegments2D(points, {}) if full_contour: return vm.wires.Contour2D(c.primitives) else: line = vm.edges.Line2D( p0, p0.translation(vm.Vector2D(0, -self.rivet_length), copy=True)) contour = vm.wires.Contour2D(c.primitives) return contour.cut_by_line(line)[0]
def plot_data(self): plot_datas = [] center = vm.Point2D(self.pos_x, self.pos_y) circle = vm.wires.Circle2D(center=center, radius=self.diameter / 2) plot_datas.append(circle.plot_data()) return [plot_data.PrimitiveGroup(primitives=plot_datas)]
def PointProjectionPlane(point, plane_origin, plane_normal): """ Plane is defined by (plane_point,plane_normal) :returns : coordinates in global coordinate system """ return vm.Point2D(point - dot(point - plane_origin, plane_normal) * plane_normal)
def optimize(self) -> List[Component]: solutions = [] for i in range(10): x0a = [random() for i in range(8)] xra, fx = cma.fmin(self.objective, x0a, 0.1, options={ 'bounds': [0, 1], 'tolfun': 1e-8, 'verbose': 10, 'ftarget': 1e-8, 'maxiter': 100 })[0:2] x_sol = xra points = [ vm.Point2D(x_sol[2 * i], x_sol[2 * i + 1]) for i in range(int(len(x_sol) / 2.)) ] self.component.update(*points) length = self.component.length() if abs(length - 0.2) < 1e-3: solutions.append(self.component.copy()) return solutions
def Geometry(self): theta = 2*npy.pi/Z #theta=theta2 +theta3 R = O.Translation(vm.point2D((self.L8*npy.cos(alphaR),-self.L8*npy.sin(alphaR)))) K = R.Translation(vm.point2D((0,DF/2))) H = K.Rotation(R,theta2) M = K.Rotation(R,theta2+theta3) a1 = vm.Arc2D(K,H,M) J1 = R.Translation(vm.Point2D((0,DI/2))) J=J1.Rotation(R,(theta2-theta1)/2) #est ce un probleme de ne pas controler directement la pente du créneau ? (on ne se sert pas de alphaJ) #sinon on pourrait remplacer les deux lignes précédentes par:J=K.Translation(vm.point2D((-self.L9*npy.sin(alphaJ),-self.L9*npy.cos(alphaJ)))) #Dans ce cas il faudrait peut être paramétrer H de la même façon I=J.Rotation(R,theta1) l1=primitives2D.RoundedLineSegments2D([K,J,I,H],{'''je ne sais pas trop quoi mettre ici'''}) L=[a1,l1] for i in range(Z-1): thetar=(i+1)*theta L.append(a1.Rotation(R,thetar,True)) L.append(l1.Rotation(R,thetar,True)) c1=vm.Contour2D(L) c1.MPLPlot()
def fonct(alpha, *data): line, d = data x = R.Translation( vm.Point2D((d / 2. * npy.cos(alpha + npy.pi / 2.), d / 2. * npy.sin(alpha + npy.pi / 2.)))) p = line.PointProjection(x) return p.PointDistance(x)
def lower_leg_contour(self, knee_postion: vm.Point2D, lower_leg_angle: float): # width = 0.25*self.lower_leg_length p1 = knee_postion + vm.Point2D(-0.5 * self.lower_leg_width, 0.) p2 = knee_postion + vm.Point2D(-0.5 * self.lower_leg_width, -self.lower_leg_length) p3 = knee_postion + vm.Point2D(0.5 * self.lower_leg_width, -self.lower_leg_length) p4 = knee_postion + vm.Point2D(0.5 * self.lower_leg_width, 0.) contour = p2d.ClosedRoundedLineSegments2D([p1, p2, p3, p4], { 1: 0.3 * self.lower_leg_width, 2: 0.3 * self.lower_leg_width }) return contour.rotation(knee_postion, lower_leg_angle)
def babylon(self, xy_position, z_position): primitives = [] ## AFFICHAGE EN CYLINDRE ## # pos = vm.Point3D((xy_position[0], xy_position[1], z_position)) # axis = vm.Vector3D((0,0,1)) # radius = self.diameter/2 # length = self.length # cylinder = p3d.Cylinder(pos, axis, radius, length) # primitives.append(cylinder) ## AFFICHAGE EN ROUE DENTEE ## plane_origin = vm.Point3D( (xy_position[0], xy_position[1], z_position - self.length / 2)) x = vm.x3D y = vm.y3D vector = vm.Vector3D((0, 0, self.length)) if self.n_teeth is None: n_teeth = int(round(self.diameter * 100, 0)) else: n_teeth = self.n_teeth angle = 2 * math.pi / n_teeth inner_diameter = 0.90 * self.diameter teeth_l = (2 * math.pi * self.diameter / 2) / (1.5 * n_teeth) pt1 = vm.Point2D( (-teeth_l / 2, (inner_diameter**2 / 4 - teeth_l**2 / 4)**0.5)) pt5 = vm.Point2D( (teeth_l / 2, (inner_diameter**2 / 4 - teeth_l**2 / 4)**0.5)) pt2 = vm.Point2D((pt1.vector[0] / 2, self.diameter / 2)) pt4 = vm.Point2D((pt5.vector[0] / 2, self.diameter / 2)) rls_points = [pt1, pt2, pt4, pt5] for i in range(n_teeth - 1): new_pt1 = rls_points[-4].Rotation(vm.o2D, -angle) new_pt2 = rls_points[-3].Rotation(vm.o2D, -angle) new_pt4 = rls_points[-2].Rotation(vm.o2D, -angle) new_pt5 = rls_points[-1].Rotation(vm.o2D, -angle) rls_points.extend([new_pt1, new_pt2, new_pt4, new_pt5]) rls_points.reverse() rls = p2d.OpenedRoundedLineSegments2D(rls_points, {}) outer_contour2d = vm.Contour2D([rls]) extrusion = p3d.ExtrudedProfile(plane_origin, x, y, outer_contour2d, [], vector) primitives.append(extrusion) return primitives
def get_elipse_interpolation(self): """ Calculates the interpolation of an elipse """ point1 = volmdlr.Point2D(self.minor_axis_size_in_mm / 2, self.piece_diameter + self.clearence) point2 = volmdlr.Point2D(0, self.major_axis / 2) point3 = volmdlr.Point2D(-self.minor_axis_size_in_mm / 2, self.piece_diameter + self.clearence) bspline1 = vme.BSplineCurve2D.from_points_interpolation( [point1, point2, point3], 2) edges = [] points = bspline1.polygon_points(40) for i in range(0, len(points) - 1): edges.append(volmdlr.edges.LineSegment2D(points[i], points[i + 1])) contour2 = vmw.Contour2D(edges) return contour2
def upper_arm_contour(self, shoulder_position: vm.Point2D, upper_arm_angle: float): p1 = shoulder_position + vm.Point2D(-0.5 * self.upper_arm_width, 0.) p2 = shoulder_position + vm.Point2D(-0.5 * self.upper_arm_width, -self.shoulder_elbow_length) p3 = shoulder_position + vm.Point2D( 0.5 * self.upper_arm_width, -self.shoulder_elbow_length, ) p4 = shoulder_position + vm.Point2D(0.5 * self.upper_arm_width, 0.) contour = p2d.ClosedRoundedLineSegments2D([p1, p2, p3, p4], { 1: 0.3 * self.upper_arm_width, 2: 0.3 * self.upper_arm_width }) return contour.rotation(shoulder_position, upper_arm_angle)
def objective(self, x): objective = 0 points = [ vm.Point2D(x[2 * i], x[2 * i + 1]) for i in range(int(len(x) / 2.)) ] self.component.update(*points) length = self.component.length() length_obj = 0.2 return (length - length_obj)**2
def generate_sweep(self, points: List[vm.Point3D], color=(248 / 255, 205 / 255, 70 / 255), alpha=0.8): contour = vm.wires.Circle2D(vm.Point2D(0, 0), self.diameter / 2) rl = self.genere_neutral_fiber(points) # contour = vm.wires.Contour2D([c]) sweep = p3d.Sweep(contour, rl, color=color, alpha=alpha, name='piping') return [sweep]
def volmdlr_primitives(self, frame=vm.OXYZ, x_setting=0., z_setting=0): cushion_contour = self.cushion_contour().translation( vm.Point2D(x_setting, z_setting)) cushion = p3d.ExtrudedProfile(frame.origin - 0.5 * self.width * frame.v, frame.u, frame.w, cushion_contour, [], self.width * frame.v, name='cushion', color=(0.2, 0.2, 0.2)) backrest_contour = self.backrest_contour().translation( vm.Point2D(x_setting, z_setting)) backrest = p3d.ExtrudedProfile(frame.origin - 0.5 * self.width * frame.v, frame.u, frame.w, backrest_contour, [], self.width * frame.v, color=(0.2, 0.2, 0.2), name='backrest') headrest_contour = self.headrest_contour().translation( vm.Point2D(x_setting, z_setting)) headrest = p3d.ExtrudedProfile(frame.origin - 0.25 * self.width * frame.v, frame.u, frame.w, headrest_contour, [], 0.5 * self.width * frame.v, color=(0.2, 0.2, 0.2), name='headrest') floor = faces.Plane3D(vm.Frame3D(frame.origin-self.floor_height*vm.Z3D, vm.X3D, vm.Y3D, vm.Z3D))\ .rectangular_cut(0.25, 1, -0.5*self.width, 0.5*self.width) roof = faces.Plane3D(vm.Frame3D(frame.origin+self.roof_height*vm.Z3D, vm.X3D, vm.Y3D, vm.Z3D))\ .rectangular_cut(-0.15, 0.15, -0.5*self.width, 0.5*self.width) primitives = [cushion, backrest, headrest, floor, roof] return primitives
def head_contour(self, position): p1 = position + vm.Point2D(-0.5 * self.head_width, -0.3 * self.head_height) p2 = position + vm.Point2D(-0.5 * self.head_width, 0.5 * self.head_height) p3 = position + vm.Point2D(0.3 * self.head_width, 0.5 * self.head_height) p4 = position + vm.Point2D(0.7 * self.head_width, 0.15 * self.head_height) p5 = position + vm.Point2D(0.2 * self.head_width, -0.3 * self.head_height) contour = p2d.ClosedRoundedLineSegments2D( [p1, p2, p3, p4, p5], { 1: 0.3 * self.head_width, 2: 0.6 * self.head_width, 3: 0.1 * self.head_width }) return contour
def bolt_circles(self): circles = [] delta_angle = 2 * math.pi / self.number_bolts inner_circle = vm.wires.Circle2D(vm.O2D, 0.5 * self.hub_diameter) first_circle = vm.wires.Circle2D( vm.Point2D(0, 0.5 * self.bolt_circle_diameter), 0.5 * self.bolt_diameter) circles = [inner_circle, first_circle] for i in range(1, self.number_bolts): circles.append(first_circle.rotation(vm.O2D, i * delta_angle)) return circles
def generate_sweep(self, color=(248 / 255, 205 / 255, 70 / 255), alpha=0.8): contour = vm.wires.Circle2D(vm.Point2D(0, 0), self.diameter / 2) points = [] for l in self.routes: if l.start not in points: points.append(l.start) if l.end not in points: points.append(l.end) rl = self.genere_neutral_fiber(points) sweep = p3d.Sweep(contour, rl, color=color, alpha=alpha, name='piping') return [sweep]
def define_grid(self, contour: vm.wires.Contour2D, number_rivet1: int, number_rivet2: int): xmin, xmax, ymin, ymax = contour.bounding_rectangle() dir1 = xmax - xmin dir2 = ymax - ymin ratio1 = dir1 / (number_rivet1 + 1) ratio2 = dir2 / (number_rivet2 + 1) grids = [] for n1 in range(number_rivet1): for n2 in range(number_rivet2): grids.append(vm.Point2D(xmin + ratio1 * (n1 + 1), ymin + ratio2 * (n2 + 1))) return grids
def mirror(self, axis: volmdlr.Vector2D): """ calculates the mirrored object to a given axis """ if axis == volmdlr.X2D: new_x = self.position[0] new_y = -self.position[1] elif axis == volmdlr.Y2D: new_x = -self.position[0] new_y = self.position[1] else: raise NotImplementedError return Piece(volmdlr.Point2D(new_x, new_y), self.diameter)
def cushion_contour(self): cushion_p1 = vm.O2D cushion_p2 = self.cushion_length * vm.X2D.rotation( vm.O2D, self.cushion_angle) cushion_p3 = cushion_p2 - vm.Y2D * 0.2 cushion_p5 = cushion_p1 - vm.X2D * self.backrest_base_width cushion_p4 = vm.Point2D(cushion_p5[0], cushion_p3[1]) cushion_points = [ cushion_p1, cushion_p2, cushion_p3, cushion_p4, cushion_p5 ] cushion_contour = p2d.ClosedRoundedLineSegments2D( cushion_points, {1: 0.05}) return cushion_contour
def generate_param_component(xmin, xmax, ymin, ymax, c_min, c_max, h_min, h_max): x, y = random.randrange(xmin * 100, xmax * 100, 1) / 100, random.randrange( ymin * 100, ymax * 100, 1) / 100 c = random.randrange(c_min * 100, c_max * 100, 1) / 100 x_vec, y_vec = random.randrange(xmin * 100, xmax * 100, 1) / 100, random.randrange( ymin * 100, ymax * 100, 1) / 100 vec1 = vm.Vector2D((x_vec, y_vec)) vec1.Normalize() vec2 = vec1.deterministic_unit_normal_vector() center = vm.Point2D((x, y)) height = random.randrange(h_min * 100, h_max * 100, 1) / 100 return center, c, vec1, vec2, height