Conditional Gcode Syntax Spec

Joseph Lenox edited this page Aug 10, 2018 · 22 revisions

These features are planned for all custom G-Code fields.

The Slic3r Manual contains everything that has been implemented: http://manual.slic3r.org/advanced/conditional-gcode

PRUSA3D FORK USES A DIFFERENT SYNTAX

Related Feature Request/Issue: https://github.com/alexrj/Slic3r/issues/3390

All Slic3r variables referenced must be known by PlaceholderParser (eg [])

Conditional expressions

Conditional expressions has the following syntax:

  • {if _expression_} gcode line

expression may take one of the following forms:

  • VARIABLE OPERATOR CONSTANT
  • CONSTANT OPERATOR VARIABLE
  • {subexpression} OPERATOR {subexpression}
  • OPERATOR subexpression

subexpression has the same forms as expression.

If _subexpression_s exist, they are evaluated before any other operators.

Available operators:

  • && or and - logical AND
  • || or or - logical OR
  • ! or not - logical NOT (inversion)
  • ^ or xor - XOR (exclusive-OR)
  • == or equals - equality comparison.
  • != - inequality comparison. To use words, use a subexpression.
  • < - less than (valid for numbers only)
  • <= - less than or equal to (valid for numbers only)
  • > - greater than (valid for numbers only)
  • >= - greater than or equal to (valid for numbers only)

Available keywords:

  • 'false'
  • 'true'

To apply a single condition to multiple lines, repeat it once for each line.

{if [layer_num] == 10}M104 S210
{if [layer_num] == 10}M600
  • If a conditional expression evaluates to false, all the characters until the end of the line are removed.
  • If a conditional expression fails to parse, it's left in place.
  • Expressions may be chained for an implicit AND.
{if [layer_num] == 10}{if [temperature_1] != 210}M104 S210

Conventions

  • Numerical value of 0 is equivalent to logical false. Any nonzero value is considered to be logical true.
    • {if { 1 - 1 } } would result in 0 and thus false.

Comments

Outside of an expression { and } may be escaped with a single \.

Value expressions

Any expression enclosed in two curly brackets, but not starting with {if is evaluated as an arithmetic expression: {foo - bar}. If evaluation fails, the expression is silently left untouched. If any float variables are used, return value will have decimals. If string variables are mixed with numeric variables, they are parsed as floats if they have a dot, or integers otherwise.

Additional available operators:

  • + - Addition
  • - - Subtraction
  • / - Division
  • * - Multiplication

Value expressions can be used in conditional expressions by nesting them: {if {foo - bar} > 10}.

Comments

Should we also support modulo division and/or power syntax? I can see a couple use cases for modulo, but not for power - @lordofhyphens

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.