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); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { // TODO: complete command. MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance; if (p.if_painted_object_set_ == false) { RhinoApp.WriteLine("No mesh"); return(Result.Failure); } Mesh my_mesh = p.painted_object_; GetObject gbrep = new GetObject(); gbrep.SetCommandPrompt("get the brep"); gbrep.GeometryFilter = Rhino.DocObjects.ObjectType.Brep; gbrep.SubObjectSelect = false; gbrep.Get(); if (gbrep.CommandResult() != Rhino.Commands.Result.Success) { return(gbrep.CommandResult()); } ObjRef my_objref = gbrep.Object(0); RhinoObject my_obj = my_objref.Object(); if (my_obj == null) { return(Rhino.Commands.Result.Failure); } Brep brep = my_objref.Brep(); if (brep == null) { return(Result.Failure); } my_obj.Select(false); int brep_number = 0; for (int i = 0; i < p.my_objects_list.Count; i++) { Brep an_object = p.my_objects_list[i]; if (My_object_functions.GetComponentID(brep) == My_object_functions.GetComponentID(an_object)) { brep_number = i; break; } } Point3d position = My_object_functions.GetPosition(brep); Vector3d normal_direction = My_object_functions.GetZ(brep); Plane plane = new Plane(position, normal_direction); /* * GetPoint gp = new GetPoint(); * gp.Constrain(plane, false); * gp.Get(); * if (gp.CommandResult() != Result.Success) * return gp.CommandResult(); * var start_point = gp.Point(); * if (start_point == Point3d.Unset) * return Result.Failure; */ Vector3d horizontal_direction = My_object_functions.GetY(brep); Point3d start_point = position + 10 * horizontal_direction; GetRotationAngle gr = new GetRotationAngle(brep, my_mesh, start_point); gr.SetCommandPrompt("Get the rotation angle"); gr.Constrain(plane, false); gr.Get(); if (gr.CommandResult() != Result.Success) { return(gr.CommandResult()); } Point3d end_point = gr.Point(); Brep new_brep = GetRotationAngle.RotateBrep(brep, my_mesh, end_point, start_point); ObjectAttributes path_attributes = new ObjectAttributes(); path_attributes.ObjectColor = Color.Yellow; path_attributes.ColorSource = ObjectColorSource.ColorFromObject; path_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject; path_attributes.PlotWeight = 2.0; ObjectAttributes my_attributes = new ObjectAttributes(); my_attributes.ObjectColor = My_object_functions.GetColor(new_brep); my_attributes.ColorSource = ObjectColorSource.ColorFromObject; int new_pin_number = My_object_functions.GetPinQuantity(new_brep); List <NurbsCurve> new_path_list = new List <NurbsCurve>(); for (int i = 0; i < new_pin_number; i++) { Guid pin_id = My_object_functions.GetPinGuid(new_brep, i); Point3d pin_position = My_object_functions.GetPinPosition(new_brep, i); MeshPoint pin_on_mesh = my_mesh.ClosestMeshPoint(pin_position, 0); new_path_list = p.graph.DijkstraPath_Change(pin_id, pin_on_mesh); } IEnumerable <RhinoObject> path_objref = doc.Objects.GetObjectList(ObjectType.Curve); foreach (RhinoObject path in path_objref) { doc.Objects.Delete(path, true); } for (int i = 0; i < new_path_list.Count; i++) { Guid path_id = new_path_list[i].UserDictionary.GetGuid("PathID"); path_attributes.ObjectId = path_id; doc.Objects.AddCurve(new_path_list[i], path_attributes); } doc.Objects.Delete(my_objref, true); doc.Objects.AddBrep(new_brep, my_attributes); p.my_objects_list[brep_number] = new_brep; doc.Views.Redraw(); return(Result.Success); }
public static Brep MoveBrep(Brep brep, Mesh base_mesh, Point3d current_point, out List <Sphere> pin_ball_list) { Brep new_brep = brep.DuplicateBrep(); Point3d original_position = My_object_functions.GetPosition(new_brep); Vector3d original_direction = My_object_functions.GetZ(new_brep); Vector3d normal_on_mesh = base_mesh.NormalAt(base_mesh.ClosestMeshPoint(current_point, 0.1)); My_object_functions.RotateVerticallyTo(new_brep, normal_on_mesh); My_object_functions.TranslateTo(new_brep, current_point); 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>(); List <Sphere> pin_ball = new List <Sphere>(); for (int i = 0; i < pin_quantity; i++) { Point3d pin_position = My_object_functions.GetPinPosition(new_brep, i); //Sphere pin_ball_i = new Sphere(pin_position, 2); //pin_ball.Add(pin_ball_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; } } original_position = My_object_functions.GetPosition(new_brep); My_object_functions.TranslateTo(new_brep, original_position + pin_to_mesh_direction[min]); /* * for (int i = 0; i < pin_quantity; i ++) * { * Point3d pin_position = My_object_functions.GetPinPosition(new_brep, i); * Sphere pin_ball_i = new Sphere(pin_position, 2); * pin_ball.Add(pin_ball_i); * } */ pin_ball_list = pin_ball; return(new_brep); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance; if (p.if_painted_object_set_ == false) { RhinoApp.WriteLine("No mesh"); return(Result.Failure); } Mesh my_mesh = p.painted_object_; GetObject gbrep = new GetObject(); gbrep.SetCommandPrompt("get the brep"); gbrep.GeometryFilter = Rhino.DocObjects.ObjectType.Brep; gbrep.SubObjectSelect = false; gbrep.Get(); if (gbrep.CommandResult() != Rhino.Commands.Result.Success) { return(gbrep.CommandResult()); } ObjRef my_objref = gbrep.Object(0); RhinoObject my_obj = my_objref.Object(); if (my_obj == null) { return(Rhino.Commands.Result.Failure); } Brep brep = my_objref.Brep(); if (brep == null) { return(Result.Failure); } my_obj.Select(false); int brep_number = 0; for (int i = 0; i < p.my_objects_list.Count; i++) { Brep an_object = p.my_objects_list[i]; if (My_object_functions.GetComponentID(brep) == My_object_functions.GetComponentID(an_object)) { brep_number = i; break; } } GetComponentPosition gp = new GetComponentPosition(brep, my_mesh); gp.SetCommandPrompt("Get the object position on mesh: "); gp.Constrain(my_mesh, false); gp.Get(); if (gp.CommandResult() != Result.Success) { return(gp.CommandResult()); } Point3d n_p = gp.Point(); List <Sphere> pin_ball_list; Brep new_brep = GetComponentPosition.MoveBrep(brep, my_mesh, n_p, out pin_ball_list); /* * Point3d position = My_object_functions.GetPosition(new_brep); * Vector3d d_x = My_object_functions.GetX(new_brep); * Vector3d d_y = My_object_functions.GetY(new_brep); * Vector3d d_z = My_object_functions.GetZ(new_brep); * Line l_x = new Line(position, 10 * d_x); * Line l_y = new Line(position, 10 * d_y); * Line l_z = new Line(position, 10 * d_z); */ ObjectAttributes path_attributes = new ObjectAttributes(); path_attributes.ObjectColor = Color.Yellow; path_attributes.ColorSource = ObjectColorSource.ColorFromObject; path_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject; path_attributes.PlotWeight = 2.0; ObjectAttributes my_attributes = new ObjectAttributes(); my_attributes.ObjectColor = My_object_functions.GetColor(new_brep); my_attributes.ColorSource = ObjectColorSource.ColorFromObject; int new_pin_number = My_object_functions.GetPinQuantity(new_brep); List <NurbsCurve> new_path_list = new List <NurbsCurve>(); for (int i = 0; i < new_pin_number; i++) { Guid pin_id = My_object_functions.GetPinGuid(new_brep, i); Point3d pin_position = My_object_functions.GetPinPosition(new_brep, i); MeshPoint pin_on_mesh = my_mesh.ClosestMeshPoint(pin_position, 0); new_path_list = p.graph.DijkstraPath_Change(pin_id, pin_on_mesh); } IEnumerable <RhinoObject> path_objref = doc.Objects.GetObjectList(ObjectType.Curve); foreach (RhinoObject path in path_objref) { doc.Objects.Delete(path, true); } for (int i = 0; i < new_path_list.Count; i++) { Guid path_id = new_path_list[i].UserDictionary.GetGuid("PathID"); path_attributes.ObjectId = path_id; doc.Objects.AddCurve(new_path_list[i], path_attributes); } doc.Objects.Delete(my_objref, true); /* * IEnumerable<RhinoObject> rhino_objects = doc.Objects.GetObjectList(ObjectType.Brep); * foreach (RhinoObject r_o in rhino_objects) { doc.Objects.Delete(r_o, true); } * * IEnumerable<RhinoObject> lines = doc.Objects.GetObjectList(ObjectType.Curve); * foreach (RhinoObject r_o in lines) { doc.Objects.Delete(r_o, true); } */ doc.Objects.AddBrep(new_brep, my_attributes); p.my_objects_list[brep_number] = new_brep; /* * foreach (Sphere s in pin_ball_list) * { doc.Objects.AddSphere(s); } * doc.Objects.AddLine(l_x, path_attributes); * doc.Objects.AddLine(l_y, path_attributes); * doc.Objects.AddLine(l_z, path_attributes); */ doc.Views.Redraw(); return(Result.Success); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { // TODO: complete command. MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance; if (p.if_painted_object_set_ == false) { RhinoApp.WriteLine("No mesh"); return(Result.Failure); } Mesh my_mesh = p.painted_object_; DijkstraGraph my_graph = p.graph; GetObject gbrep = new GetObject(); gbrep.SetCommandPrompt("get the brep"); gbrep.GeometryFilter = Rhino.DocObjects.ObjectType.Brep; gbrep.SubObjectSelect = false; gbrep.Get(); if (gbrep.CommandResult() != Rhino.Commands.Result.Success) { return(gbrep.CommandResult()); } Rhino.DocObjects.ObjRef my_objref = gbrep.Object(0); Rhino.DocObjects.RhinoObject my_obj = my_objref.Object(); if (my_obj == null) { return(Rhino.Commands.Result.Failure); } Brep brep = my_objref.Brep(); if (brep == null) { return(Result.Failure); } my_obj.Select(false); int pin_number_1 = My_object_functions.GetPinQuantity(brep); List <Guid> pin_ball_guid_list_1 = new List <Guid>(); for (int i = 0; i < pin_number_1; i++) { ObjectAttributes green_ball_attributes = new ObjectAttributes(); green_ball_attributes.ObjectColor = Color.Green; green_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject; Point3d pin_position = My_object_functions.GetPinPosition(brep, i); Guid pin_id = My_object_functions.GetPinGuid(brep, i); green_ball_attributes.ObjectId = pin_id; green_ball_attributes.UserDictionary.Set("isPin", true); Sphere pin_ball = new Sphere(pin_position, 2); //Brep pin_ball_brep = pin_ball.ToBrep(); doc.Objects.AddSphere(pin_ball, green_ball_attributes); pin_ball_guid_list_1.Add(pin_id); } doc.Views.Redraw(); GetObject g_pinball = new GetObject(); g_pinball.SetCommandPrompt("choose the pin"); g_pinball.GeometryFilter = ObjectType.Surface; //g_pinball.SetCustomGeometryFilter(PinBallGeometryFilter); //g_pinball.DisablePreSelect(); g_pinball.SubObjectSelect = false; g_pinball.Get(); if (g_pinball.CommandResult() != Result.Success) { return(g_pinball.CommandResult()); } if (g_pinball.Object(0).Brep() == null) { return(Result.Failure); } RhinoObject selected_pin_ball = g_pinball.Object(0).Object(); Guid selected_pin_ball_id = selected_pin_ball.Id; for (int i = 0; i < pin_ball_guid_list_1.Count; i++) { doc.Objects.Delete(pin_ball_guid_list_1[i], true); } ObjectAttributes greenyellow_ball_attributes = new ObjectAttributes(); greenyellow_ball_attributes.ObjectColor = Color.GreenYellow; greenyellow_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject; greenyellow_ball_attributes.ObjectId = selected_pin_ball_id; greenyellow_ball_attributes.UserDictionary.Set("isPin", true); int pin_number = My_object_functions.FindPinNumber(brep, selected_pin_ball_id); Point3d selected_pin_position = My_object_functions.GetPinPosition(brep, pin_number); Sphere pin_ball_new = new Sphere(selected_pin_position, 2); doc.Objects.AddSphere(pin_ball_new, greenyellow_ball_attributes); doc.Views.Redraw(); //MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance; if (p.my_objects_list.Count <= 1) { RhinoApp.WriteLine("not enough objects"); return(Result.Failure); } List <Guid> pin_ball_guid_list_2 = new List <Guid>(); for (int i = 0; i < p.my_objects_list.Count; i++) { Brep an_object = p.my_objects_list[i]; if (My_object_functions.GetComponentID(brep) != My_object_functions.GetComponentID(an_object)) { int pin_quantity = My_object_functions.GetPinQuantity(an_object); for (int j = 0; j < pin_quantity; j++) { ObjectAttributes green_ball_attributes = new ObjectAttributes(); green_ball_attributes.ObjectColor = Color.Green; green_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject; Point3d pin_position = My_object_functions.GetPinPosition(an_object, j); Guid pin_id = My_object_functions.GetPinGuid(an_object, j); green_ball_attributes.ObjectId = pin_id; green_ball_attributes.UserDictionary.Set("isPin", true); Sphere pin_ball = new Sphere(pin_position, 2); doc.Objects.AddSphere(pin_ball, green_ball_attributes); pin_ball_guid_list_2.Add(pin_id); } } } doc.Views.Redraw(); GetObject g_pinball_2 = new GetObject(); g_pinball_2.SetCommandPrompt("choose the pin"); g_pinball_2.GeometryFilter = ObjectType.Surface; //g_pinball.SetCustomGeometryFilter(PinBallGeometryFilter); //g_pinball.DisablePreSelect(); g_pinball_2.SubObjectSelect = false; g_pinball_2.Get(); if (g_pinball_2.CommandResult() != Result.Success) { return(g_pinball_2.CommandResult()); } if (g_pinball_2.Object(0).Brep() == null) { return(Result.Failure); } RhinoObject selected_pin_ball_2 = g_pinball_2.Object(0).Object(); Guid selected_pin_ball_id_2 = selected_pin_ball_2.Id; Brep brep_2 = null; for (int i = 0; i < p.my_objects_list.Count; i++) { Brep an_object = p.my_objects_list[i]; int pin_quantity = My_object_functions.GetPinQuantity(an_object); bool mark = false; for (int j = 0; j < pin_quantity; j++) { if (My_object_functions.GetPinGuid(an_object, j) == selected_pin_ball_id_2) { mark = true; break; } } if (mark) { brep_2 = an_object; break; } } if (brep_2 == null) { return(Result.Failure); } int selected_pin_number_2 = My_object_functions.FindPinNumber(brep_2, selected_pin_ball_id_2); for (int i = 0; i < pin_ball_guid_list_2.Count; i++) { doc.Objects.Delete(pin_ball_guid_list_2[i], true); } Point3d selected_pin_position_2 = My_object_functions.GetPinPosition(brep_2, selected_pin_number_2); Sphere pin_ball_new_2 = new Sphere(selected_pin_position_2, 2); greenyellow_ball_attributes.ObjectId = selected_pin_ball_id_2; doc.Objects.AddSphere(pin_ball_new_2, greenyellow_ball_attributes); doc.Views.Redraw(); MeshPoint pin_1_meshpoint = my_mesh.ClosestMeshPoint(selected_pin_position, 0); MeshPoint pin_2_meshpoint = my_mesh.ClosestMeshPoint(selected_pin_position_2, 0); NurbsCurve d_path = my_graph.DijkstraPath_Add(pin_1_meshpoint, selected_pin_ball_id, pin_2_meshpoint, selected_pin_ball_id_2); if (d_path != null) { ObjectAttributes path_attributes = new ObjectAttributes(); path_attributes.ObjectColor = Color.Yellow; path_attributes.ColorSource = ObjectColorSource.ColorFromObject; path_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject; path_attributes.PlotWeight = 2.0; path_attributes.ObjectId = d_path.UserDictionary.GetGuid("PathID"); doc.Objects.AddCurve(d_path, path_attributes); } doc.Objects.Delete(selected_pin_ball_id, true); doc.Objects.Delete(selected_pin_ball_id_2, true); doc.Views.Redraw(); return(Result.Success); }