443 lines
16 KiB
C++
443 lines
16 KiB
C++
#include <pybind11/pybind11.h>
|
|
#include <pybind11/eigen.h>
|
|
#include <pybind11/stl.h>
|
|
#include <pybind11/functional.h>
|
|
|
|
#include "vde/brep/brep.h"
|
|
#include "vde/curves/nurbs_curve.h"
|
|
#include "vde_cam/cam_strategies.h"
|
|
#include "vde_cam/cam_toolpath.h"
|
|
|
|
namespace py = pybind11;
|
|
using namespace vde::cam;
|
|
|
|
void bind_cam(py::module& m) {
|
|
auto cam = m.def_submodule("cam",
|
|
R"pbdoc(
|
|
CAM (Computer-Aided Manufacturing) toolpath generation.
|
|
|
|
Provides toolpath generation strategies for CNC machining:
|
|
roughing_toolpath() — Z-layer roughing
|
|
finishing_toolpath() — parallel or spiral finishing
|
|
drilling_toolpath() — G81/G83/G84 drilling cycles
|
|
|
|
Classes:
|
|
Tool, ToolLibrary — tool definitions and management
|
|
RoughingParams, FinishingParams — machining parameters
|
|
PostProcessor, FanucPost, SiemensPost, HeidenhainPost — G-code postprocessing
|
|
)pbdoc");
|
|
|
|
// ── ToolType enum ──────────────────────────────────
|
|
py::enum_<ToolType>(cam, "ToolType",
|
|
R"pbdoc(
|
|
Tool type enumeration.
|
|
|
|
Values:
|
|
ENDMILL — flat end mill
|
|
BALLNOSE — ball nose cutter
|
|
DRILL — twist drill
|
|
FACEMILL — face mill
|
|
TAP — tap (threading)
|
|
)pbdoc")
|
|
.value("ENDMILL", ToolType::ENDMILL)
|
|
.value("BALLNOSE", ToolType::BALLNOSE)
|
|
.value("DRILL", ToolType::DRILL)
|
|
.value("FACEMILL", ToolType::FACEMILL)
|
|
.value("TAP", ToolType::TAP)
|
|
.export_values();
|
|
|
|
// ── Tool ───────────────────────────────────────────
|
|
py::class_<Tool>(cam, "Tool",
|
|
R"pbdoc(
|
|
CNC tool definition.
|
|
|
|
Attributes:
|
|
id: tool number (T-code)
|
|
name: tool name
|
|
type: ToolType enum
|
|
diameter: tool diameter (mm)
|
|
flutes: number of flutes
|
|
length: effective cutting length (mm)
|
|
overall_length: total tool length (mm)
|
|
shank_diameter: shank diameter (mm)
|
|
|
|
Methods:
|
|
spindle_rpm(cutting_speed): compute recommended RPM
|
|
feed_rate_mm_per_min(feed_per_tooth): compute feed rate
|
|
|
|
Example:
|
|
>>> tool = cam.Tool()
|
|
>>> tool.id = 1
|
|
>>> tool.name = "10mm Endmill"
|
|
>>> tool.type = cam.ToolType.ENDMILL
|
|
>>> tool.diameter = 10.0
|
|
>>> tool.flutes = 4
|
|
>>> rpm = tool.spindle_rpm(150) # 150 m/min cutting speed
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("id", &Tool::id, "Tool number (T-code)")
|
|
.def_readwrite("name", &Tool::name, "Tool name")
|
|
.def_readwrite("type", &Tool::type, "ToolType enum")
|
|
.def_readwrite("diameter", &Tool::diameter, "Tool diameter (mm)")
|
|
.def_readwrite("flutes", &Tool::flutes, "Number of flutes")
|
|
.def_readwrite("length", &Tool::length, "Effective cutting length (mm)")
|
|
.def_readwrite("overall_length", &Tool::overall_length, "Total length (mm)")
|
|
.def_readwrite("shank_diameter", &Tool::shank_diameter, "Shank diameter (mm)")
|
|
.def("spindle_rpm", &Tool::spindle_rpm,
|
|
py::arg("cutting_speed_m_per_min"),
|
|
"Compute recommended spindle RPM from cutting speed (m/min)")
|
|
.def("feed_rate_mm_per_min", &Tool::feed_rate_mm_per_min,
|
|
py::arg("feed_per_tooth"),
|
|
"Compute feed rate (mm/min) from feed per tooth (mm/tooth)")
|
|
.def("__repr__", [](const Tool& t) {
|
|
return "Tool(id=" + std::to_string(t.id)
|
|
+ ", name=\"" + t.name
|
|
+ "\", dia=" + std::to_string(t.diameter) + ")";
|
|
});
|
|
|
|
// ── ToolLibrary ────────────────────────────────────
|
|
py::class_<ToolLibrary>(cam, "ToolLibrary",
|
|
R"pbdoc(
|
|
Tool library managing a collection of CNC tools.
|
|
|
|
Example:
|
|
>>> lib = cam.ToolLibrary()
|
|
>>> t = cam.Tool()
|
|
>>> t.id, t.name, t.diameter = 1, "10mm Endmill", 10.0
|
|
>>> lib.add_tool(t)
|
|
>>> found = lib.find_tool(1)
|
|
>>> if found:
|
|
... print(found.name)
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def("add_tool", &ToolLibrary::add_tool,
|
|
py::arg("tool"),
|
|
"Add a tool, returns internal index")
|
|
.def("find_tool", py::overload_cast<int>(&ToolLibrary::find_tool, py::const_),
|
|
py::arg("id"),
|
|
"Find tool by ID, returns Optional[Tool]")
|
|
.def("find_tool", py::overload_cast<const std::string&>(&ToolLibrary::find_tool, py::const_),
|
|
py::arg("name"),
|
|
"Find tool by name, returns Optional[Tool]")
|
|
.def("list_tools", [](const ToolLibrary& lib) {
|
|
return lib.list_tools();
|
|
}, "List all tools")
|
|
.def_property_readonly("size", &ToolLibrary::size, "Number of tools")
|
|
.def("__len__", &ToolLibrary::size)
|
|
.def("__repr__", [](const ToolLibrary& lib) {
|
|
return "ToolLibrary(" + std::to_string(lib.size()) + " tools)";
|
|
});
|
|
|
|
// ── RoughingParams ─────────────────────────────────
|
|
py::class_<RoughingParams>(cam, "RoughingParams",
|
|
R"pbdoc(
|
|
Roughing machining parameters.
|
|
|
|
Attributes:
|
|
step_down: depth per layer (mm, default 1.0)
|
|
step_over: lateral step distance (mm, default 2.0)
|
|
stock_to_leave: material left for finishing (mm, default 0.5)
|
|
feed_rate: feed rate (mm/min, default 800)
|
|
safe_z: safe retract height (mm, default 10.0)
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("step_down", &RoughingParams::step_down)
|
|
.def_readwrite("step_over", &RoughingParams::step_over)
|
|
.def_readwrite("stock_to_leave", &RoughingParams::stock_to_leave)
|
|
.def_readwrite("feed_rate", &RoughingParams::feed_rate)
|
|
.def_readwrite("safe_z", &RoughingParams::safe_z)
|
|
.def("__repr__", [](const RoughingParams& p) {
|
|
return "RoughingParams(step_down=" + std::to_string(p.step_down) + ")";
|
|
});
|
|
|
|
// ── FinishingParams ────────────────────────────────
|
|
py::class_<FinishingParams>(cam, "FinishingParams",
|
|
R"pbdoc(
|
|
Finishing machining parameters.
|
|
|
|
Attributes:
|
|
step_over: line spacing (mm, default 0.5)
|
|
feed_rate: feed rate (mm/min, default 500)
|
|
safe_z: safe retract height (mm, default 10.0)
|
|
depth_of_cut: finishing Z depth (0 = final Z, negative = below)
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("step_over", &FinishingParams::step_over)
|
|
.def_readwrite("feed_rate", &FinishingParams::feed_rate)
|
|
.def_readwrite("safe_z", &FinishingParams::safe_z)
|
|
.def_readwrite("depth_of_cut", &FinishingParams::depth_of_cut)
|
|
.def("__repr__", [](const FinishingParams& p) {
|
|
return "FinishingParams(step_over=" + std::to_string(p.step_over) + ")";
|
|
});
|
|
|
|
// ── FinishingStrategy ──────────────────────────────
|
|
py::enum_<FinishingStrategy>(cam, "FinishingStrategy",
|
|
R"pbdoc(
|
|
Finishing toolpath strategy.
|
|
|
|
Values:
|
|
PARALLEL — equally spaced parallel lines
|
|
SPIRAL — inward offset from contour profile
|
|
)pbdoc")
|
|
.value("PARALLEL", FinishingStrategy::PARALLEL)
|
|
.value("SPIRAL", FinishingStrategy::SPIRAL)
|
|
.export_values();
|
|
|
|
// ── DrillingCycle ──────────────────────────────────
|
|
py::enum_<DrillingCycle>(cam, "DrillingCycle",
|
|
R"pbdoc(
|
|
Drilling cycle type (G-code canned cycle).
|
|
|
|
Values:
|
|
G81 — simple drill
|
|
G83 — deep hole peck drill
|
|
G84 — tapping
|
|
)pbdoc")
|
|
.value("G81", DrillingCycle::G81)
|
|
.value("G83", DrillingCycle::G83)
|
|
.value("G84", DrillingCycle::G84)
|
|
.export_values();
|
|
|
|
// ── DrillPoint ─────────────────────────────────────
|
|
py::class_<DrillPoint>(cam, "DrillPoint",
|
|
R"pbdoc(
|
|
Drill point definition.
|
|
|
|
Attributes:
|
|
position: hole location (Point3D)
|
|
depth: drill depth Z coordinate (default -10.0)
|
|
peck_depth: peck depth for G83 (mm, default 2.0)
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("position", &DrillPoint::position)
|
|
.def_readwrite("depth", &DrillPoint::depth)
|
|
.def_readwrite("peck_depth", &DrillPoint::peck_depth)
|
|
.def("__repr__", [](const DrillPoint& dp) {
|
|
return "DrillPoint(pos=" + std::to_string(dp.position.x())
|
|
+ ", depth=" + std::to_string(dp.depth) + ")";
|
|
});
|
|
|
|
// ── PathSegmentType ────────────────────────────────
|
|
py::enum_<PathSegmentType>(cam, "PathSegmentType")
|
|
.value("RAPID", PathSegmentType::Rapid, "G0 rapid traverse")
|
|
.value("LINEAR", PathSegmentType::Linear, "G1 linear feed")
|
|
.value("ARC_CW", PathSegmentType::ArcCW, "G2 clockwise arc")
|
|
.value("ARC_CCW", PathSegmentType::ArcCCW, "G3 counter-clockwise arc")
|
|
.export_values();
|
|
|
|
// ── PathSegment ────────────────────────────────────
|
|
py::class_<PathSegment>(cam, "PathSegment",
|
|
R"pbdoc(
|
|
Single toolpath segment.
|
|
|
|
Attributes:
|
|
start: start point (Point3D)
|
|
end: end point (Point3D)
|
|
center: arc center for G2/G3 (Point3D)
|
|
type: PathSegmentType
|
|
feed_rate: feed rate (mm/min)
|
|
z_depth: cutting Z depth
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("start", &PathSegment::start)
|
|
.def_readwrite("end", &PathSegment::end)
|
|
.def_readwrite("center", &PathSegment::center)
|
|
.def_readwrite("type", &PathSegment::type)
|
|
.def_readwrite("feed_rate", &PathSegment::feed_rate)
|
|
.def_readwrite("z_depth", &PathSegment::z_depth)
|
|
.def("__repr__", [](const PathSegment& s) {
|
|
return "PathSegment(" + std::to_string(s.start.x()) + " → "
|
|
+ std::to_string(s.end.x()) + ")";
|
|
});
|
|
|
|
// ── Toolpath ───────────────────────────────────────
|
|
py::class_<Toolpath>(cam, "Toolpath",
|
|
R"pbdoc(
|
|
Complete toolpath for one machining operation.
|
|
|
|
Attributes:
|
|
name: operation name
|
|
segments: list of PathSegment
|
|
safe_z: safe retract height
|
|
cut_z: cutting depth
|
|
step_down: depth per pass
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("name", &Toolpath::name)
|
|
.def_readwrite("segments", &Toolpath::segments)
|
|
.def_readwrite("safe_z", &Toolpath::safe_z)
|
|
.def_readwrite("cut_z", &Toolpath::cut_z)
|
|
.def_readwrite("step_down", &Toolpath::step_down)
|
|
.def("__repr__", [](const Toolpath& tp) {
|
|
return "Toolpath(name=\"" + tp.name
|
|
+ "\", segments=" + std::to_string(tp.segments.size()) + ")";
|
|
});
|
|
|
|
// ── Toolpath factory functions ─────────────────────
|
|
cam.def("roughing_toolpath", &roughing_toolpath,
|
|
py::arg("body"), py::arg("tool"), py::arg("params"),
|
|
R"pbdoc(
|
|
Generate Z-layer roughing toolpath.
|
|
|
|
Performs layer-by-layer rough material removal:
|
|
1. Compute Z range of the B-Rep model
|
|
2. Step down from safe Z in step_down increments
|
|
3. At each layer, project contours and offset by stock_to_leave
|
|
|
|
Args:
|
|
body: BrepModel solid
|
|
tool: Tool definition
|
|
params: RoughingParams
|
|
|
|
Returns:
|
|
Toolpath with roughing segments
|
|
|
|
Example:
|
|
>>> body = brep.make_box(100, 50, 20)
|
|
>>> tool = cam.Tool()
|
|
>>> tool.diameter = 10.0
|
|
>>> params = cam.RoughingParams()
|
|
>>> params.step_down = 2.0
|
|
>>> tp = cam.roughing_toolpath(body, tool, params)
|
|
)pbdoc");
|
|
|
|
cam.def("finishing_toolpath", &finishing_toolpath,
|
|
py::arg("body"), py::arg("tool"), py::arg("params"),
|
|
py::arg("strategy") = FinishingStrategy::PARALLEL,
|
|
R"pbdoc(
|
|
Generate finishing toolpath.
|
|
|
|
Supports two strategies:
|
|
- PARALLEL: equally spaced parallel lines covering the part surface
|
|
- SPIRAL: inward offset from part contour profile
|
|
|
|
Args:
|
|
body: BrepModel solid
|
|
tool: Tool definition
|
|
params: FinishingParams
|
|
strategy: FinishingStrategy (default PARALLEL)
|
|
|
|
Returns:
|
|
Toolpath with finishing segments
|
|
|
|
Example:
|
|
>>> params = cam.FinishingParams()
|
|
>>> params.step_over = 0.5
|
|
>>> tp = cam.finishing_toolpath(body, tool, params, cam.FinishingStrategy.SPIRAL)
|
|
)pbdoc");
|
|
|
|
cam.def("drilling_toolpath", &drilling_toolpath,
|
|
py::arg("points"), py::arg("tool"),
|
|
py::arg("cycle") = DrillingCycle::G81,
|
|
py::arg("safe_z") = 10.0,
|
|
py::arg("feed_rate") = 200.0,
|
|
R"pbdoc(
|
|
Generate drilling cycle toolpath.
|
|
|
|
Supports:
|
|
- G81: simple drill
|
|
- G83: deep hole peck drill (with Q parameter)
|
|
- G84: tapping
|
|
|
|
Args:
|
|
points: list of DrillPoint
|
|
tool: Tool definition (drill/tap)
|
|
cycle: DrillingCycle (default G81)
|
|
safe_z: safe retract height (mm)
|
|
feed_rate: feed rate (mm/min)
|
|
|
|
Returns:
|
|
Toolpath with drilling segments
|
|
|
|
Example:
|
|
>>> points = [cam.DrillPoint()]
|
|
>>> points[0].position = Point3D(10, 0, 0)
|
|
>>> points[0].depth = -15.0
|
|
>>> drill = cam.Tool()
|
|
>>> drill.type = cam.ToolType.DRILL
|
|
>>> tp = cam.drilling_toolpath(points, drill, cam.DrillingCycle.G83)
|
|
)pbdoc");
|
|
|
|
// ── PostProcessors ─────────────────────────────────
|
|
py::class_<PostProcessor, std::shared_ptr<PostProcessor>>(cam, "PostProcessor",
|
|
R"pbdoc(
|
|
Abstract base class for G-code postprocessors.
|
|
|
|
Concrete implementations: FanucPost, SiemensPost, HeidenhainPost.
|
|
|
|
Methods:
|
|
post_process(toolpath): generate complete G-code string
|
|
)pbdoc")
|
|
.def("post_process", &PostProcessor::post_process,
|
|
py::arg("toolpath"),
|
|
"Generate complete G-code from a toolpath");
|
|
|
|
py::class_<FanucPost, PostProcessor, std::shared_ptr<FanucPost>>(cam, "FanucPost",
|
|
R"pbdoc(
|
|
Fanuc controller G-code postprocessor.
|
|
|
|
Generates ISO G-code compatible with Fanuc CNC controllers.
|
|
|
|
Example:
|
|
>>> post = cam.FanucPost()
|
|
>>> gcode = post.post_process(toolpath)
|
|
>>> with open("output.nc", "w") as f:
|
|
... f.write(gcode)
|
|
)pbdoc")
|
|
.def(py::init<>());
|
|
|
|
py::class_<SiemensPost, PostProcessor, std::shared_ptr<SiemensPost>>(cam, "SiemensPost",
|
|
R"pbdoc(
|
|
Siemens Sinumerik controller G-code postprocessor.
|
|
)pbdoc")
|
|
.def(py::init<>());
|
|
|
|
py::class_<HeidenhainPost, PostProcessor, std::shared_ptr<HeidenhainPost>>(cam, "HeidenhainPost",
|
|
R"pbdoc(
|
|
Heidenhain controller G-code postprocessor.
|
|
)pbdoc")
|
|
.def(py::init<>());
|
|
|
|
// ── G-code export utilities ────────────────────────
|
|
cam.def("export_gcode", &export_gcode,
|
|
py::arg("toolpath"),
|
|
"Export toolpath to basic ISO G-code string");
|
|
|
|
cam.def("contour_toolpath", &contour_toolpath,
|
|
py::arg("curve"), py::arg("cut_depth"),
|
|
py::arg("safe_z") = 10.0, py::arg("step_down") = 1.0,
|
|
"Generate contour toolpath following a curve at depth");
|
|
|
|
cam.def("pocket_toolpath", &pocket_toolpath,
|
|
py::arg("boundary"), py::arg("islands"),
|
|
py::arg("cut_depth"), py::arg("step_over") = 0.5,
|
|
py::arg("angle") = 0.0,
|
|
"Generate zigzag pocket toolpath within a boundary");
|
|
|
|
// ── Material removal simulation ────────────────────
|
|
py::class_<SimulationResult>(cam, "SimulationResult",
|
|
R"pbdoc(
|
|
Result of material removal simulation.
|
|
|
|
Attributes:
|
|
remaining_mesh: HalfedgeMesh of remaining stock
|
|
removed_mesh: HalfedgeMesh of removed material
|
|
volume_removed: removed volume (mm³)
|
|
volume_remaining: remaining volume (mm³)
|
|
steps: simulation step count
|
|
)pbdoc")
|
|
.def_readonly("remaining_mesh", &SimulationResult::remaining_mesh)
|
|
.def_readonly("removed_mesh", &SimulationResult::removed_mesh)
|
|
.def_readonly("volume_removed", &SimulationResult::volume_removed)
|
|
.def_readonly("volume_remaining", &SimulationResult::volume_remaining)
|
|
.def_readonly("steps", &SimulationResult::steps)
|
|
.def("__repr__", [](const SimulationResult& r) {
|
|
return "SimulationResult(removed=" + std::to_string(r.volume_removed)
|
|
+ "mm³, steps=" + std::to_string(r.steps) + ")";
|
|
});
|
|
|
|
cam.def("material_removal_simulation", &material_removal_simulation,
|
|
py::arg("body"), py::arg("toolpath"), py::arg("tool"),
|
|
"Simulate material removal by sweeping a tool along a toolpath");
|
|
}
|