Skip to content

NC part programs (bt.nc)

A machine tool's axes as a mechanism, and a part program read the way the machine's control runs it: against its work offsets and its tool table, timed by its rates, refused by line where it cannot be read — and, with the tools entered as bodies, cut: the stock carved, what a tool met named by line, the part measured against its design, the setup written down. A program that works the cell with M codes is the source of the machine's own program (MCode, sequence). See Machine tending and NC machining.

vf2 = bt.parts.machine_tool(scene, "vf2", catalog="haas/vf/vf-2", axes=True)
bt.nc.set_work_offset(scene, vf2, "G54", at=part_top)
holder = bt.nc.Holder(diameter=0.040, length=0.075)
bt.nc.set_tool(scene, vf2, 1, bt.nc.end_mill(0.010, flutes=0.022, stickout=0.035, holder=holder))
bt.nc.program(scene, "op10", Path("op10.nc"))
run = bt.nc.check(scene, vf2, "op10", stock="part", design="part.stl")     # the dry run
print(run.summary(), run.machining.summary(), sep="\n")
hs = bt.tending.haas_mfin(scene, vf2, "op10", parts=["part", "blank"])  # the program runs the cell
tl = scene.simulate_sequences(["tend", hs.program])
shown, cut = bt.nc.animate(scene, tl, vf2, "part", design="part.stl")      # … cut, in the cell

nc

NC part programs on a machine tool's axes.

A machining centre built with its axes (bt.parts.machine_tool(axes=)) is a mechanism whose joints are the machine coordinates, declared as an NC machine: a control with travels and rates, and the two tables an operator fills in at setup — work offsets and tools. A part program — the text a CAM post writes — is read against those the way the control would run it, and what the axes do and how long it takes come out of the program, not out of a typed-in cycle time:

vf2 = bt.parts.machine_tool(scene, "vf2", catalog="haas/vf/vf-2", axes=True)
bt.nc.set_work_offset(scene, vf2, "G54", at=part_top)   # derived from the cell
bt.nc.set_tool(scene, vf2, 1, length=0.110, diameter=0.010)
bt.nc.program(scene, "op10", Path("op10.nc"))
print(bt.nc.check(scene, vf2, "op10").summary())        # a dry run
sq.step("machining", actions=[bt.seq.run_program(vf2.axes, "op10")])

A tool entered as a body (end_mill, drill … in a Holder) cuts: the dry run, and a baked cycle, carve the stock that rides the table and say what went wrong on which line — a cutter in the material at rapid, a holder in the part, a tool in the vise — and, against the design model, what is left over and what is missing:

bt.nc.set_tool(scene, vf2, 1, bt.nc.end_mill(0.010, flutes=0.022, stickout=0.035,
                                             holder=bt.nc.Holder(0.040, 0.075)))
run = bt.nc.check(scene, vf2, "op10", stock="part", fixtures=vise.obstacles,
                  design="part.stl")
print(run.machining.summary())
tl, cut = bt.nc.animate(scene, tl, vf2, "part", design="part.stl")   # … and the picture
Path("setup.md").write_text(bt.nc.setup_sheet(scene, vf2, "op10", stock="part"))

The table's length is what the axes are positioned by; the body's is where the tip is. They agree — the entry is taken from the body — unless you enter another, which is how a setup that was measured wrong is tried.

A program that runs the cell — opens the window, clamps the vise, calls the robot through a relay and waits for its M-FIN, and starts over on M99 — does it with M codes that are the machine's to carry out, not the control's. They are the machine's M-code table (MCode), and sequence writes the machine's own program from the part program: its blocks in order, and at every such code the step that answers it.

hs = bt.tending.haas_mfin(scene, vf2, "o1000", parts=["part", "blank"])   # the Haas table

What the control cannot read is refused with the line number rather than skipped — a word it does not know, a work offset or a tool nobody entered, a point past an axis's travel, an M code that is in nobody's table — at check, and again when the step fires in a bake.

What is read (the Haas dialect a CAM post writes for 3-axis milling): G0/G1, G2/G3 (IJK or R, helical), G4, G17–G19, G20/G21, G28, G40, G43/G49, G53, G54–G59 and G154 P, G73/G81/G82/G83 with G80 and G98/G99, G90/G91, G94; M1, M2/M30, M3/M4/M5, M6, M8/M9, M99 (the program starts over), and the M codes of the machine's table. Not read: cutter radius compensation (post the path compensated), macros, subprograms, probing.

Timing is a control's look-ahead in outline: a rapid runs straight from rest to rest at the axes' rapid rates; a run of cutting moves is one continuous path at the programmed feed, slowed for corners and bounded by the axes' accelerations. It is an estimate of the cycle, not a servo model.

The cut is mesh booleans (cutrail): each tool's sweep subtracted from the stock in the stock's own frame, exact to the chord tolerance. A drill is cut as a flat end of its diameter — wall and depth, not the cone.

Holder

A tool holder, from the gauge line down: its diameter and how far it reaches (length, m) — to the nose the cutter sticks out of.

Issue

One thing a tool met that it should not have, on the source line it happened on. kind is "rapid_into_stock", "spindle_stopped", "holder_into_stock" or "into_fixture"; what the stock or the fixture's obstacle; volume the material in the way (m³); time the first contact on the cycle's clock (s).

MCode

One row of a machine's M-code table: an M code that is the machine's to carry out, and how it does it.

The control stops at such a code and waits. actions fire as it gets there (bt.seq actions: a door sent to its end, a relay set) and the program goes on once until holds (a bt.seq condition: the door's end lane, the M-FIN, a time; at once without one). then fire at that moment, and settle is what the control still waits for after them — a relay dropped once its M-FIN came, and the M-FIN gone again before the next call can mistake it for its own.

needs is what must hold as the control gets to the code: not a wait but an alarm. Where it does not hold the program stops there for good, with alarm in the step's name — the way a control refuses a door command in a cell that is not safe, instead of opening the door whenever the cell gets round to being safe.

at is where the axes must stand when the code is called (machine coordinates, m) — checked as the machine's program is written: a relay that calls an arm calls it to where the arm was taught.

actions class-attribute

actions = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

alarm class-attribute

alarm = ''

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

then class-attribute

then = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

MachineTool

Bases: botrail.parts.Built

What machine_tool built, plus the names a tending program addresses: the side door's axis (door, None for a manual door or none), what rides on that door (door_objects — the leaf and its trim, for a robot that slides it by hand), its end-of-travel lanes (door_lanes = closed, open — the axis's stop lanes, or two zone sensors on a loose leaf), the stroke and the world direction it opens along (door_travel, door_axis), the front door's closed switch and the E-stop lane the machine's program is guarded by (front_door_lane, estop), the operator panel's Built and the button sensors on it.

door_axis class-attribute

door_axis = (0.0, 1.0, 0.0)

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

door_travel class-attribute

door_travel = 0.0

Convert a string or number to a floating-point number, if possible.

front_closed class-attribute

front_closed = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

front_door_axes class-attribute

front_door_axes = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

front_door_objects class-attribute

front_door_objects = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

front_door_travel class-attribute

front_door_travel = 0.0

Convert a string or number to a floating-point number, if possible.

front_doors class-attribute

front_doors = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

front_open class-attribute

front_open = ()

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

head_link = 'head'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

table_link = 'table'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

Machining

What a machine's programs cut out of one stock — check(stock=), cut and animate return it. Volumes are m³.

ok property

ok

Nothing met anything it should not have, every tool that was called is one that cuts, and — where a design was given — the part is the design within the tolerance.

by_tool

by_tool() -> dict[int, float]

Removed volume by tool number.

summary

summary() -> str

A few lines: what was removed, what went wrong, how the part compares.

Run

A part program read and timed for one machine — what check returns. Times are seconds, lengths metres, positions machine coordinates (X, Y, Z).

functions

functions() -> list[tuple[int, int]]

(line, M code) of every machine function the program calls, in order — the codes of the machine's M-code table. Here nothing answers them, so they take no time: what the cell takes to answer is in the bake.

line_at

line_at(t: float) -> Optional[int]

The source line running at t.

summary

summary() -> str

One paragraph: the cycle and what it is made of.

tools

tools() -> list[int]

The tools the program puts in the spindle, in order.

Tool

A tool as it stands in the spindle (m). length is gauge line to tip — the figure a presetter reads, and what the tool table's entry is taken from. Build one with end_mill, ball_mill, bull_nose or drill.

describe

describe() -> str

10 mm flat end mill, the way a setup sheet names it.

animate

animate(
    scene,
    timeline,
    machine: Union[MachineTool, str],
    stock: str,
    *,
    fixtures: Sequence[str] = (),
    design: Optional[Union[str, Path]] = None,
    tolerance: float = 5e-05,
    tools: bool = True,
    shape: Union[Machining, object, str, Path, None] = None,
    window: Optional[tuple[float, float]] = None,
    **options,
)

cut with the picture: returns (timeline, Machining), the timeline showing the stock as it is cut — on the table that carries it, and out of the machine in the gripper as the part — and, with tools, each tool in the spindle while it is the one in it; where the machine is drawn with a tool changer (machine_tool(shape=)), a tool that is not in the spindle rides its pocket on the carousel.

Everything added is display (<stock>_cut/…, <machine>/tool/T<n>/…), out of collision: the stock itself goes on being what is gripped and checked, unseen. Play the returned timeline (scene.show_timeline), or export it. options also take stage_period (s — the display lags the cutter by at most this) and max_stages.

shape and window as in cut. A part made on two machines is animated in two calls on the one timeline — the second on the timeline the first returned, with shape= the first one's part: the first operation's picture then stays until the second machine's first cut and gives way to its stages there.

ball_mill

ball_mill(
    diameter: float,
    *,
    flutes: float,
    stickout: Optional[float] = None,
    holder: Optional[Holder] = None,
    length: Optional[float] = None,
    shank: Optional[float] = None,
) -> Tool

A ball end mill: end_mill with a hemispherical end.

bull_nose

bull_nose(
    diameter: float,
    corner_radius: float,
    *,
    flutes: float,
    stickout: Optional[float] = None,
    holder: Optional[Holder] = None,
    length: Optional[float] = None,
    shank: Optional[float] = None,
) -> Tool

A bull-nose end mill: end_mill with corner_radius on its end.

check

check(
    scene,
    machine: Union[MachineTool, str],
    program: str,
    *,
    start: Optional[Sequence[float]] = None,
    stock: Optional[str] = None,
    fixtures: Sequence[str] = (),
    design: Optional[Union[str, Path]] = None,
    tolerance: float = 5e-05,
    shape: Union[Machining, object, str, Path, None] = None,
    **options,
) -> Run

A dry run: reads program the way the machine's control would — from where its axes stand, or from the machine coordinates start — and times it. Nothing in the scene moves. A ValueError names the line of whatever the control cannot read.

With stock — the obstacle on the table that is to be machined — the run also cuts: the stock and the fixtures (the vise and its jaws, by obstacle name) ride the table from where they stand, the tools with bodies are swept through it, and Run.machining says what came off on which line, what a tool met that it should not have, and — with design, the finished part as a closed mesh file in the stock's own frame — what is left over or missing beyond tolerance (m). options go to the cut (chord_tolerance, max_deviation, dt, tile_size).

shape is what is cut where that is not the obstacle stock as it stands: the part an earlier operation left (its Machining, or the StockCarve of a robot's carve) or a mesh file in the stock's own frame. A second operation is dry-run on the first one's part this way — and the obstacle may then be no more than a box of the part's size, there to be clamped and gripped.

cut

cut(
    timeline,
    machine: Union[MachineTool, str],
    stock: str,
    *,
    fixtures: Sequence[str] = (),
    design: Optional[Union[str, Path]] = None,
    tolerance: float = 5e-05,
    shape: Union[Machining, object, str, Path, None] = None,
    window: Optional[tuple[float, float]] = None,
    **options,
) -> Machining

What the machine's programs cut out of stock over a baked cycle.

Where check assumes the stock sits where it sits, this reads the bake: the stock is wherever its own track has it — where the arm really put it, clamped to a table that really moved — so the work offset, the tool table, the program and the cell's handling all meet in the one result. fixtures, design, tolerance, shape and options as in check.

A part made in two setups is cut in two calls, the second starting from the first one's part: cut(tl, op20_machine, "part", shape=cut(tl, op10_machine, "part")). And where the cycle brings the machine one part after another, window = (t0, t1) takes the program runs that begin in that stretch of the bake — the time this part is clamped on it — and leaves the others' to them.

drill

drill(
    diameter: float,
    *,
    flutes: float,
    stickout: Optional[float] = None,
    holder: Optional[Holder] = None,
    length: Optional[float] = None,
) -> Tool

A twist drill. It is cut as a flat end of its diameter: the hole's wall and its programmed depth are right, the cone at the bottom is not drawn — measure a drilled depth to the drill's corner, not its point.

end_mill

end_mill(
    diameter: float,
    *,
    flutes: float,
    stickout: Optional[float] = None,
    holder: Optional[Holder] = None,
    length: Optional[float] = None,
    shank: Optional[float] = None,
) -> Tool

A flat end mill diameter across, cutting flutes up from its tip. Its length is derived from how it is held — holder and the stickout out of the holder's nose — or given as length (gauge line to tip) for a tool with no holder to collide. shank is the shank's diameter where it differs from the cutter's. Metres.

machine_coordinates

machine_coordinates(
    scene,
    machine: Union[MachineTool, str],
    point: Sequence[float],
) -> tuple

The machine coordinates at which the spindle nose stands on point — a world point on something that rides the table (a corner of the part, a frame on the fixture), where the table stands now.

This is what an operator finds with an edge finder and a gauge block: jog until the nose is on the point, read the position page. Here it is worked out from the mechanism, so a fixture moved in the cell moves its work offset with it.

program

program(scene, name: str, source: Union[str, Path]) -> str

Adds (or replaces) part program name and returns the name. source is the program's text, or a Path to read it from.

sequence

sequence(
    scene,
    machine: Union[MachineTool, str],
    program: str,
    m_codes: Mapping[int, MCode],
    *,
    name: Optional[str] = None,
    running: Optional[str] = None,
    alarm: Optional[str] = None,
    spindle_needs: Optional[dict] = None,
    spindle_alarm: str = "the spindle may not start",
) -> list[dict]

Writes the machine's own program from a part program that runs the cell: the sequence name (the program's, by default), a chain of

  • the part program's blocks — what the control runs on its own between two machine functions (bt.seq.run_program(block=)), each step named after its lines (N25-N135), and
  • at every machine function, the step that answers it out of m_codes (N150 M180 window open), with its alarm where the row has a needs.

On M99 the chain starts over (gates(cycle=True)) — the program that keeps running, beside which a cell's own programs are baked; on M30 it ends. The rows are entered in the machine's M-code table (scene.set_nc_m_code), which is what lets the control read them.

running and alarm are signals to keep: running on while the program runs, alarm on — and running off — where it stopped on an alarm. spindle_needs is what must hold for a block that turns the spindle to start (the doors' closed switches, no E-stop), an alarm (spindle_alarm) otherwise.

The program is read here once, whole: what the control cannot read — an M code that is in neither table included — is a ValueError with the line, and so is a function called with the axes away from the row's at. Returns the blocks (scene.nc_blocks).

set_tool

set_tool(
    scene,
    machine: Union[MachineTool, str],
    number: int,
    tool: Optional[Tool] = None,
    *,
    length: Optional[float] = None,
    diameter: Optional[float] = None,
) -> None

Enters tool number in the machine's tool table.

With a tool (end_mill, drill …) the pocket holds that body — it is what cuts the stock and what must not touch it — and the table's entry is taken from it: the length a presetter would read, the cutter's diameter. Giving length as well enters that in the table while the tool stays as long as it is: an operator's mistake, to see what the program then does.

Without a tool, length is the entry — gauge line to tip (m), what G43 H<number> adds to Z — and the moves are timed but cut nothing.

set_work_offset

set_work_offset(
    scene,
    machine: Union[MachineTool, str],
    name: str = "G54",
    *,
    at: Optional[Sequence[float]] = None,
    xyz: Optional[Sequence[float]] = None,
) -> tuple

Enters a work offset in the machine's table and returns it.

at is the work zero as a world point riding the table — the offset is derived from where the axes stand (machine_coordinates), for a spindle nose with no tool in it: tool lengths go in the tool table (set_tool) and are added by G43. Or give the machine coordinates themselves as xyz, the figures off a real machine's offset page. name is "G54"…"G59" or "G154 P<n>".

setup_sheet

setup_sheet(
    scene,
    machine: Union[MachineTool, str],
    program: str,
    *,
    stock: Optional[str] = None,
    fixtures: Sequence[str] = (),
    design: Optional[Union[str, Path]] = None,
    tolerance: float = 5e-05,
    title: Optional[str] = None,
    shape: Union[Machining, object, str, Path, None] = None,
    run: Optional[Run] = None,
    **options,
) -> str

The setup sheet of program on machine, as Markdown: what an operator needs to stand the same setup again, and what a dry run of it says — derived from the cell, so it cannot disagree with it.

The stock and where it stands on the table; the fixtures; the work offsets, as machine coordinates and as where the work zero is on the stock; the tools — what is in each pocket and what its table entry says, flagged where they differ; the speeds the program runs each at; the cycle; and the dry run: the travel, what each tool removes, what a tool met that it should not have, and the part against its design. stock, fixtures, design, tolerance, shape and options as in check, which this runs — or give it the dry run already made (run=, what check returned for the same setup) and it is written up as it is.