Files

132 lines
6.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// \file 10_flange/main.cpp
/// \brief Flange manufacturing part — B-Rep boolean + STEP/GLB export
///
/// Demonstrates a typical design-for-manufacturing workflow for a flange:
/// base cylinder → center through-hole → bolt clearance holes → edge fillets.
/// Exports to STEP (AP214, for CNC) and GLB (3D preview).
#include <vde/brep/modeling.h>
#include <vde/brep/brep_boolean.h>
#include <vde/brep/step_export.h>
#include <vde/foundation/io_gltf.h>
#include <vde/core/aabb.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace vde::brep;
using namespace vde::foundation;
using namespace vde::core;
// ═══════════════════════════════════════════════════════════════════════
// Main — Flange Manufacturing
// ═══════════════════════════════════════════════════════════════════════
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << "╔══════════════════════════════════╗\n"
<< "║ Flange — Manufacturing Demo ║\n"
<< "╚══════════════════════════════════╝\n\n";
// ── Step 1: Base cylinder (外径 80mm, 厚度 10mm) ─────────────────
//
// make_cylinder creates a Y-axis-aligned cylinder centered at origin.
// Radius 40mm → Ø80mm outer diameter, height 10mm.
auto flange = make_cylinder(40.0, 10.0, 64);
AABB3D bb = flange.bounds();
std::cout << "Step 1: Base cylinder Ø80×10\n"
<< " faces: " << flange.num_faces()
<< " edges: " << flange.num_edges() << "\n"
<< " bbox: (" << bb.min().x() << ", " << bb.min().y()
<< ", " << bb.min().z() << ") → ("
<< bb.max().x() << ", " << bb.max().y()
<< ", " << bb.max().z() << ")\n";
// ── Step 2: Center through-hole (内径 30mm) ──────────────────────
//
// Subtract a smaller cylinder to create the center bore.
// Height 12mm ensures the subtraction fully penetrates the 10mm flange.
auto center_hole = make_cylinder(15.0, 12.0, 64);
flange = brep_difference(flange, center_hole);
std::cout << "\nStep 2: Center through-hole Ø30\n"
<< " faces: " << flange.num_faces()
<< " edges: " << flange.num_edges() << "\n";
// ── Step 3: Bolt clearance holes (4× M8 on Ø60 PCD) ─────────────
//
// PCD radius = 30mm, bolt clearance hole radius = 4.5mm (M8).
// Each bolt cylinder is translated onto the bolt circle before subtraction.
const double pcd = 30.0; // PCD radius
const double bolt_r = 4.5; // M8 clearance hole radius
for (int i = 0; i < 4; ++i) {
double angle = i * 2.0 * M_PI / 4.0;
// Create bolt hole cylinder and place it on the bolt circle (XZ plane).
auto bolt = make_cylinder(bolt_r, 12.0, 32)
.transformed(translate({pcd * std::cos(angle), 0.0,
pcd * std::sin(angle)}));
flange = brep_difference(flange, bolt);
}
std::cout << "\nStep 3: 4× bolt clearance holes (M8, Ø60 PCD)\n"
<< " faces: " << flange.num_faces()
<< " edges: " << flange.num_edges() << "\n";
// ── Step 4: Edge fillets (圆角) ──────────────────────────────────
//
// Apply 1mm fillets to all edges for stress relief and deburring.
const double fillet_r = 1.0;
int num_edges = flange.num_edges();
for (int e = 0; e < num_edges && e < 8; ++e) {
flange = fillet(flange, e, fillet_r);
}
std::cout << "\nStep 4: Edge fillets (R" << fillet_r << ")\n"
<< " faces: " << flange.num_faces()
<< " edges: " << flange.num_edges() << "\n";
// ── Step 5: Export to STEP for CNC ───────────────────────────────
std::cout << "\nStep 5: Export STEP (AP214)\n";
const char* step_path = "flange.stp";
export_step_file(step_path, {flange});
std::string step_str = export_step({flange});
std::cout << " → " << step_path << " (" << step_str.size() << " chars)\n";
// ── Step 6: Export to GLB for 3D preview ────────────────────────
std::cout << "\nStep 6: Export GLB for visualization\n";
const char* glb_path = "flange.glb";
const int tess_res = 48;
if (write_brep_gltf(glb_path, flange, tess_res)) {
std::cout << " → " << glb_path << " (tessellation level "
<< tess_res << ")\n";
} else {
std::cerr << " ✗ Failed to write " << glb_path << "\n";
return 1;
}
// ── Summary ──────────────────────────────────────────────────────
std::cout << "\n╔══════════════════════════════════════════╗\n"
<< "║ Flange Export Summary ║\n"
<< "╠══════════════════════════════════════════╣\n"
<< "║ B-Rep → STEP (CNC/CAM) ✓ ║\n"
<< "║ B-Rep → GLB (Preview) ✓ ║\n"
<< "╚══════════════════════════════════════════╝\n";
std::cout << "\nDone! Flange ready for manufacturing.\n";
return 0;
}