1 #include "footprint_generator_single.hpp"
2 #include "widgets/pool_browser_button.hpp"
3 #include "document/idocument_package.hpp"
4 #include "pool/ipool.hpp"
5 #include "pool/package.hpp"
6 
7 namespace horizon {
FootprintGeneratorSingle(IDocumentPackage & c)8 FootprintGeneratorSingle::FootprintGeneratorSingle(IDocumentPackage &c)
9     : Glib::ObjectBase(typeid(FootprintGeneratorSingle)), FootprintGeneratorBase(
10                                                                   "/org/horizon-eda/horizon/imp/footprint_generator/"
11                                                                   "single.svg",
12                                                                   c)
13 {
14 
15     {
16         auto tbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 4));
17         auto la = Gtk::manage(new Gtk::Label("Count:"));
18         tbox->pack_start(*la, false, false, 0);
19 
20         sp_count = Gtk::manage(new Gtk::SpinButton());
21         sp_count->set_range(2, 512);
22         sp_count->set_increments(2, 2);
23         tbox->pack_start(*sp_count, false, false, 0);
24 
25         box_top->pack_start(*tbox, false, false, 0);
26     }
27 
28     update_preview();
29 
30     sp_pitch = Gtk::manage(new SpinButtonDim());
31     sp_pitch->set_range(0, 50_mm);
32     sp_pitch->set_valign(Gtk::ALIGN_CENTER);
33     sp_pitch->set_halign(Gtk::ALIGN_START);
34     sp_pitch->set_value(1_mm);
35     overlay->add_at_sub(*sp_pitch, "#pitch");
36     sp_pitch->show();
37 
38     sp_pad_height = Gtk::manage(new SpinButtonDim());
39     sp_pad_height->set_range(0, 50_mm);
40     sp_pad_height->set_valign(Gtk::ALIGN_CENTER);
41     sp_pad_height->set_halign(Gtk::ALIGN_START);
42     sp_pad_height->set_value(.5_mm);
43     overlay->add_at_sub(*sp_pad_height, "#pad_height");
44     sp_pad_height->show();
45 
46     sp_pad_width = Gtk::manage(new SpinButtonDim());
47     sp_pad_width->set_range(0, 50_mm);
48     sp_pad_width->set_valign(Gtk::ALIGN_CENTER);
49     sp_pad_width->set_halign(Gtk::ALIGN_START);
50     sp_pad_width->set_value(.5_mm);
51     overlay->add_at_sub(*sp_pad_width, "#pad_width");
52     sp_pad_width->show();
53 
54     sp_count->signal_value_changed().connect([this] {
55         pad_count = sp_count->get_value_as_int();
56         update_preview();
57     });
58     sp_count->set_value(4);
59     sp_count->set_range(1, 512);
60     sp_count->set_increments(1, 1);
61 }
62 
generate()63 bool FootprintGeneratorSingle::generate()
64 {
65     if (!property_can_generate())
66         return false;
67     int64_t pitch = sp_pitch->get_value_as_int();
68     int64_t pad_width = sp_pad_width->get_value_as_int();
69     int64_t pad_height = sp_pad_height->get_value_as_int();
70     int64_t y0 = (pad_count - 1) * (pitch / 2);
71     auto padstack = core.get_pool().get_padstack(browser_button->property_selected_uuid());
72     for (unsigned int i = 0; i < pad_count; i++) {
73         auto uu = UUID::random();
74         auto &pad = package.pads.emplace(uu, Pad(uu, padstack)).first->second;
75         pad.placement.shift = {0, y0 - pitch * i};
76         pad.placement.set_angle_deg(270);
77         pad.name = std::to_string(i + 1);
78         update_pad_parameters(*padstack, pad, pad_width, pad_height);
79     }
80     return true;
81 }
82 
update_preview()83 void FootprintGeneratorSingle::update_preview()
84 {
85     auto n = pad_count;
86     if (n >= 4) {
87         overlay->sub_texts["#pad1"] = "1";
88         overlay->sub_texts["#pad2"] = "2";
89         overlay->sub_texts["#pad3"] = std::to_string(n - 1);
90         overlay->sub_texts["#pad4"] = std::to_string(n);
91     }
92     else if (n == 3) {
93         overlay->sub_texts["#pad1"] = "1";
94         overlay->sub_texts["#pad2"] = "2";
95         overlay->sub_texts["#pad3"] = "3";
96         overlay->sub_texts["#pad4"] = "X";
97     }
98     else if (n == 2) {
99         overlay->sub_texts["#pad1"] = "1";
100         overlay->sub_texts["#pad2"] = "2";
101         overlay->sub_texts["#pad3"] = "X";
102         overlay->sub_texts["#pad4"] = "X";
103     }
104     else if (n == 1) {
105         overlay->sub_texts["#pad1"] = "1";
106         overlay->sub_texts["#pad2"] = "X";
107         overlay->sub_texts["#pad3"] = "X";
108         overlay->sub_texts["#pad4"] = "X";
109     }
110     overlay->queue_draw();
111 }
112 } // namespace horizon
113