#include "fiko/cam/gkod.hpp" #include #include #include namespace fiko::cam { namespace { struct Yazici { std::string s; int n = 10; void satir(const char* fmt, ...) { char tampon[256]; va_list args; va_start(args, fmt); std::vsnprintf(tampon, sizeof(tampon), fmt, args); va_end(args); char bas[32]; std::snprintf(bas, sizeof(bas), "N%d ", n); n += 10; s += bas; s += tampon; s += "\n"; } void ham(const std::string& metin) { s += metin + "\n"; } }; const Takim* takimBul(const std::vector& takimlar, int no) { for (const Takim& t : takimlar) if (t.numara == no) return &t; return nullptr; // bulunamadı → nullptr (SESSİZCE takimlar[0]'a düşme: yanlış takımla kesim!) } void delikDongusuYaz(Yazici& y, const CamIslem& op) { // Sabit dongu secimi + parametreleri (Fanuc). char dongu[96]; switch (op.dongu) { case DelikDongu::PeckDelme: std::snprintf(dongu, sizeof(dongu), "G83 Z%.3f R%.3f Q%.3f F%.1f", op.altZ, op.geriCekmeZ, op.peckDerinlik, op.dalmaIlerleme); break; case DelikDongu::Kilavuz: std::snprintf(dongu, sizeof(dongu), "G84 Z%.3f R%.3f F%.1f", op.altZ, op.geriCekmeZ, op.devir * op.disAdimi); break; case DelikDongu::Rayba: std::snprintf(dongu, sizeof(dongu), "G85 Z%.3f R%.3f F%.1f", op.altZ, op.geriCekmeZ, op.dalmaIlerleme); break; case DelikDongu::Havsa: std::snprintf(dongu, sizeof(dongu), "G82 Z%.3f R%.3f P%.0f F%.1f", op.altZ, op.geriCekmeZ, op.bekleme * 1000.0, op.dalmaIlerleme); break; default: // Punta + Delme std::snprintf(dongu, sizeof(dongu), "G81 Z%.3f R%.3f F%.1f", op.altZ, op.geriCekmeZ, op.dalmaIlerleme); break; } bool ilk = true; for (const Vec3d& m : op.yol.delikler) { if (ilk) { y.satir("G98 %s X%.3f Y%.3f", dongu, m.x, m.y); ilk = false; } else { y.satir("X%.3f Y%.3f", m.x, m.y); } } y.satir("G80"); } // CATIA axial op sabit döngü satırı (canned cycle). Nokta geometrisinden delik // merkezleri; strateji döngü/peck/dwell'den G81/G83/G84 seçimi. altZ = op derinliği. void catiaAxialYaz(Yazici& y, const CamOperasyon& op) { const f64 ustZ = op.geometri.ustZ; f64 altZ = op.geometri.altZ; if (op.geometri.derinlik > 1e-6) altZ = ustZ - op.geometri.derinlik; const f64 rZ = ustZ + 3.0; // R düzlemi (malzeme üstü + pay) const f64 f = op.hizlar.dalmaIlerleme > 1.0 ? op.hizlar.dalmaIlerleme : op.hizlar.ilerleme; char dongu[96]; const bool peck = op.tip == CamIslemTipCatia::DeepHole || op.tip == CamIslemTipCatia::BreakChips || op.strateji.dongu == AxialDongu::Gagali || op.strateji.dongu == AxialDongu::TalasKir; const bool tap = op.tip == CamIslemTipCatia::Tapping || op.tip == CamIslemTipCatia::ReverseTap; if (peck) { std::snprintf(dongu, sizeof(dongu), "G83 Z%.3f R%.3f Q%.3f F%.1f", altZ, rZ, std::max(0.1, op.strateji.peckDerinlik), f); } else if (tap) { std::snprintf(dongu, sizeof(dongu), "G84 Z%.3f R%.3f F%.1f", altZ, rZ, f); } else if (op.strateji.dwellMod != DwellMod::Kapali) { std::snprintf(dongu, sizeof(dongu), "G82 Z%.3f R%.3f P%.0f F%.1f", altZ, rZ, op.strateji.dwellDeger * 1000.0, f); } else { std::snprintf(dongu, sizeof(dongu), "G81 Z%.3f R%.3f F%.1f", altZ, rZ, f); } bool ilk = true; for (const CamNokta& n : op.geometri.noktalar) { if (!n.aktif) continue; if (ilk) { y.satir("G98 %s X%.3f Y%.3f", dongu, n.x, n.y); ilk = false; } else { y.satir("X%.3f Y%.3f", n.x, n.y); } } y.satir("G80"); } } // namespace std::string gkodCatiaUret(const std::vector& operasyonlar, const std::vector& takimlar, const std::string& programAdi) { Yazici y; y.ham("%"); y.ham("O0002 (" + programAdi + ")"); y.satir("G21 G90 G94 G17"); y.satir("G54"); int sonTakim = -1; f64 sonGuvenli = 25.0; for (const CamOperasyon& op : operasyonlar) { if (op.bastirilmis) continue; const bool axial = axialMi(op.tip); if (!axial && op.yol.bosMu()) continue; if (axial && op.geometri.noktalar.empty()) continue; const Takim* t = takimBul(takimlar, op.takimId); y.ham("(=== " + op.ad + " — " + islemTipCatiaAdi(op.tip) + " ===)"); if (!t) y.ham("(UYARI: takim #" + std::to_string(op.takimId) + " bulunamadi — takim degisimi atlandi)"); if (t && t->numara != sonTakim) { y.satir("M05"); y.satir("T%d M06 (%s)", t->numara, t->ad.c_str()); sonTakim = t->numara; } y.satir("S%.0f M03", op.hizlar.devir); y.satir("M08"); const f64 guvenli = op.makrolar.empty() ? op.geometri.ustZ + 25.0 : op.geometri.ustZ + 25.0; y.satir("G00 Z%.3f", guvenli); sonGuvenli = guvenli; if (axial) { catiaAxialYaz(y, op); } else { f64 sonIlerleme = -1.0; for (const Hamle& h : op.yol.hamleler) { if (h.tip == HamleTip::Hizli) { y.satir("G00 X%.3f Y%.3f Z%.3f", h.nokta.x, h.nokta.y, h.nokta.z); sonIlerleme = -1.0; } else { const f64 f = h.ilerleme > 1.0 ? h.ilerleme : 1000.0; if (std::abs(f - sonIlerleme) > 0.5) { y.satir("G01 X%.3f Y%.3f Z%.3f F%.1f", h.nokta.x, h.nokta.y, h.nokta.z, f); sonIlerleme = f; } else { y.satir("G01 X%.3f Y%.3f Z%.3f", h.nokta.x, h.nokta.y, h.nokta.z); } } } } y.satir("M09"); } y.satir("M05"); y.satir("G00 Z%.3f", sonGuvenli + 25.0); y.satir("M30"); y.ham("%"); return y.s; } std::string gkodUret(const std::vector& islemler, const std::vector& takimlar, const std::string& programAdi) { Yazici y; y.ham("%"); y.ham("O0001 (" + programAdi + ")"); y.satir("G21 G90 G94 G17"); y.satir("G54"); int sonTakim = -1; f64 sonGuvenli = 25.0; for (const CamIslem& op : islemler) { if (!op.etkin || op.yol.bosMu()) continue; const Takim* t = takimBul(takimlar, op.takimNo); y.ham("(=== " + op.ad + " — " + islemTipAdi(op.tip) + " ===)"); if (!t) y.ham("(UYARI: takim #" + std::to_string(op.takimNo) + " bulunamadi — takim degisimi atlandi)"); if (t && t->numara != sonTakim) { y.satir("M05"); y.satir("T%d M06 (%s)", t->numara, t->ad.c_str()); sonTakim = t->numara; } y.satir("S%.0f M03", op.devir); y.satir("M08"); y.satir("G00 Z%.3f", op.guvenliZ); sonGuvenli = op.guvenliZ; if (op.tip == CamIslemTip::Delik && !op.yol.delikler.empty()) { if (!op.yol.delikler.empty()) y.satir("G00 X%.3f Y%.3f", op.yol.delikler[0].x, op.yol.delikler[0].y); delikDongusuYaz(y, op); } else { f64 sonIlerleme = -1.0; for (size_t i = 0; i < op.yol.hamleler.size(); ++i) { const Hamle& h = op.yol.hamleler[i]; if (h.tip == HamleTip::Hizli) { y.satir("G00 X%.3f Y%.3f Z%.3f", h.nokta.x, h.nokta.y, h.nokta.z); sonIlerleme = -1.0; } else { const f64 f = h.ilerleme > 1.0 ? h.ilerleme : 1000.0; if (std::abs(f - sonIlerleme) > 0.5) { y.satir("G01 X%.3f Y%.3f Z%.3f F%.1f", h.nokta.x, h.nokta.y, h.nokta.z, f); sonIlerleme = f; } else { y.satir("G01 X%.3f Y%.3f Z%.3f", h.nokta.x, h.nokta.y, h.nokta.z); } } } } y.satir("M09"); } y.satir("M05"); y.satir("G00 Z%.3f", sonGuvenli + 25.0); y.satir("M30"); y.ham("%"); return y.s; } } // namespace fiko::cam