public static Brep RotateBrep(Brep brep, Mesh base_mesh, Point3d current_point, Point3d start_point) { Brep new_brep = brep.DuplicateBrep(); Point3d position = My_object_functions.GetPosition(new_brep); Vector3d start_direction = start_point - position; Vector3d end_direction = current_point - position; double rotate_angle = Vector3d.VectorAngle(end_direction, start_direction); //My_object_functions.RotateHorizontally(new_brep, rotate_angle); My_object_functions.RotateHorizontallyTo(new_brep, end_direction); Vector3d normal_on_mesh = My_object_functions.GetZ(new_brep); int pin_quantity = My_object_functions.GetPinQuantity(new_brep); List <double> pin_to_mesh_distance = new List <double>(); List <Vector3d> pin_to_mesh_direction = new List <Vector3d>(); for (int i = 0; i < pin_quantity; i++) { Point3d pin_position = My_object_functions.GetPinPosition(new_brep, i); Line line1 = new Line(pin_position, -20 * normal_on_mesh); Line line2 = new Line(pin_position, 10 * normal_on_mesh); int[] faceids; Point3d[] i_p_1 = Intersection.MeshLine(base_mesh, line1, out faceids); Point3d[] i_p_2 = Intersection.MeshLine(base_mesh, line2, out faceids); foreach (Point3d p_1 in i_p_1) { pin_to_mesh_distance.Add(pin_position.DistanceTo(p_1)); pin_to_mesh_direction.Add(p_1 - pin_position); } foreach (Point3d p_2 in i_p_2) { pin_to_mesh_distance.Add(pin_position.DistanceTo(p_2)); pin_to_mesh_direction.Add(p_2 - pin_position); } } int min = 0; double min_d = double.MaxValue; for (int i = 0; i < pin_to_mesh_distance.Count; i++) { if (min_d > pin_to_mesh_distance[i]) { min_d = pin_to_mesh_distance[i]; min = i; } } position = My_object_functions.GetPosition(new_brep); My_object_functions.TranslateTo(new_brep, position + pin_to_mesh_direction[min]); return(new_brep); }