Exemple #1
0
def export(objectslist, filename, argstring):
    "called when freecad exports a list of objects"
    # pylint: disable=unused-argument

    output = '''(This output produced with the dump post processor)
(Dump is useful for inspecting the raw commands in your paths)
(but is not useful for driving machines.)
(Consider setting a default postprocessor in your project or )
(exporting your paths using a specific post that matches your machine)

'''

    for obj in objectslist:

        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return
        print("postprocessing...")
        output += parse(obj)

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(output)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = output
    else:
        final = output

    print("done postprocessing.")
    return final
def export(obj, filename, argstring):
    modal = True
    gcode = ""
    safetyblock1 = "G90G40G49\n"
    gcode += safetyblock1

    units = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units")
    if units.GetInt("UserSchema") == 0:
        firstcommand = Path.Command("G21")  # metric mode
    else:
        firstcommand = Path.Command("G20")  # inch mode
    oldvals = saveVals(firstcommand)  # save first command for modal use
    fp = obj[0]
    gcode += firstcommand.Name

    if hasattr(fp, "Path"):
        for c in fp.Path.Commands:
            gcode += lineout(c, oldvals, modal) + "\n"
            oldvals = saveVals(c)
        gcode += "M2\n"
        gfile = open(filename, "w")
        gfile.write(gcode)
        gfile.close()
    else:
        FreeCAD.Console.PrintError("Select a path object and try again\n")

    if SHOW_EDITOR:
        FreeCAD.Console.PrintMessage("Editor Activated\n")
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        dia.exec_()
Exemple #3
0
def export(objectslist, filename, argstring):
    """Export the list of objects into a filename.

    Parameters
    ----------
    objectslists: list
        List of objects.

    filename: str
        Name of the output file ending in `'.knc'`.
    """
    gcode = HEADER

    for obj in objectslist:
        for command in obj.Path.Commands:
            # Manipulate tool change commands
            if "M6" == command.Name:
                gcode += TOOL_CHANGE.replace("TOOL",
                                             str(int(command.Parameters["T"])))
            elif "M3" == command.Name:
                # Convert spindle speed (rpm) command to comment
                gcode += ("M01 Set spindle speed to " +
                          str(int(command.Parameters["S"])) +
                          " rounds per minute")
            else:
                # Add other commands
                gcode += command.Name

                # Loop through command parameters
                for parameter, value in command.Parameters.items():
                    # Multiply F parameter value by 10,
                    # FreeCAD = mm/s, nccad = 1/10 mm/s
                    if "F" == parameter:
                        value *= 10
                    # Add command parameters and values and round float
                    # as nccad9 does not support exponents
                    gcode += " " + parameter + str(round(value, 5))

            gcode += "\n"

    gcode += POSTAMBLE + "\n"

    # Open editor window
    if FreeCAD.GuiUp:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            gcode = dia.editor.toPlainText()

    # Save to file
    if filename != "-":
        gfile = open(filename, "w")
        gfile.write(gcode)
        gfile.close()

    return filename
Exemple #4
0
def parse(inputstring):
    "parse(inputstring): returns a parsed output string"
    print "postprocessing..."

    output = ""
    params = ['X', 'Y', 'Z', 'A', 'B', 'I', 'J', 'K', 'F', 'S',
              'T']  #This list control the order of parameters

    # write some stuff first
    if OUTPUT_HEADER:
        print "outputting header"
        output += "'Exported by FreeCAD\n"
        output += "'Post Processor: " + __name__ + "\n"
        output += "'Output Time:" + str(now) + "\n"

    #Write the preamble
    if OUTPUT_COMMENTS: output += "'begin preamble\n"
    for line in PREAMBLE.splitlines(True):
        output += line

    # treat the input line by line
    lines = inputstring.splitlines(True)

    for line in lines:
        commandline = PostUtils.stringsplit(line)
        command = commandline['command']
        try:
            print commandline
            print "command: " + command
            print command in scommands
            output += scommands[command](commandline)
        except:
            print "I don't know what the hell the command:  " + command + " means.  Maybe I should support it."

    print "finished"
    # write some more stuff at the end
    if OUTPUT_COMMENTS: output += "'begin postamble\n"
    for line in POSTAMBLE.splitlines(True):
        output += line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(output)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = output
    else:
        final = output

    print "done postprocessing."
    return final
Exemple #5
0
def export(objectslist, filename, argstring):

    gcode = HEADER

    for obj in objectslist:

        for command in obj.Path.Commands:

            # Manipulate tool change commands
            if 'M6' == command.Name:
                gcode += TOOL_CHANGE.replace('TOOL',
                                             str(int(command.Parameters['T'])))

            # Convert spindle speed (rpm) command to comment
            elif 'M3' == command.Name:
                gcode += 'M01 Set spindle speed to ' + str(
                    int(command.Parameters['S'])) + ' rounds per minute'

            # Add other commands
            else:
                gcode += command.Name

                # Loop through command parameters
                for parameter, value in command.Parameters.items():

                    # Multiply F parameter value by 10 (FreeCAD = mm/s, nccad = 1/10 mm/s)
                    if 'F' == parameter:
                        value *= 10

                    # Add command parameters and values and round float as nccad9 does not support exponents
                    gcode += ' ' + parameter + str(round(value, 5))

            gcode += '\n'

    gcode += POSTAMBLE + '\n'

    # Open editor window
    if FreeCAD.GuiUp:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            gcode = dia.editor.toPlainText()

    # Save to file
    if filename != '-':
        gfile = open(filename, "w")
        gfile.write(gcode)
        gfile.close()

    return filename
Exemple #6
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + "\n"
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: %s)\n" % obj.Label
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: %s)\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "wb")
        gfile.write(final)
        gfile.close()

    return final
Exemple #7
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None

    global UNITS
    global UNIT_FORMAT
    global UNIT_FEED_FORMAT
    global MOTION_MODE
    global SUPPRESS_COMMANDS

    print('Post Processor: ' + __name__ + ' postprocessing...')
    gcode = ''

    # Write header:
    if OUTPUT_HEADER:
        gcode += linenumber() + '(Exported by FreeCAD)\n'
        gcode += linenumber() + '(Post Processor: ' + __name__
        gcode += '.py, version: ' + Revised + ')\n'
        gcode += linenumber() + '(Output Time:' + str(datetime.now()) + ')\n'

    # Suppress drill-cycle commands:
    if TRANSLATE_DRILL_CYCLES:
        SUPPRESS_COMMANDS += ['G80', 'G98', 'G99']

    # Write the preamble:
    if OUTPUT_COMMENTS:
        gcode += linenumber() + '(Begin preamble)\n'
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line

    # Write these settings AFTER the preamble,
    # to prevent the preamble from changing these:
    if OUTPUT_COMMENTS:
        gcode += linenumber() + '(Default Configuration)\n'
    gcode += linenumber() + MOTION_MODE + '\n'
    gcode += linenumber() + UNITS + '\n'
    gcode += linenumber() + WORK_PLANE + '\n'

    for obj in objectslist:
        # Debug...
        # print('\n' + '*'*70 + '\n')
        # dump(obj)
        # print('\n' + '*'*70 + '\n')
        if not hasattr(obj, 'Path'):
            print('The object ' + obj.Name +
                  ' is not a path. Please select only path and Compounds.')
            return

        # Skip inactive operations:
        if PathUtil.opProperty(obj, 'Active') is False:
            continue

        # Do the pre_op:
        if OUTPUT_BCNC:
            gcode += linenumber() + '(Block-name: ' + obj.Label + ')\n'
            gcode += linenumber() + '(Block-expand: 0)\n'
            gcode += linenumber() + '(Block-enable: 1)\n'
        if OUTPUT_COMMENTS:
            gcode += linenumber() + '(Begin operation: ' + obj.Label + ')\n'
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # Get coolant mode:
        coolantMode = 'None'  # None is the word returned from the operation
        if hasattr(obj, 'CoolantMode') or hasattr(obj, 'Base') and \
           hasattr(obj.Base, 'CoolantMode'):
            if hasattr(obj, 'CoolantMode'):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # Turn coolant on if required:
        if OUTPUT_COMMENTS:
            if not coolantMode == 'None':
                gcode += linenumber() + '(Coolant On:' + coolantMode + ')\n'
        if coolantMode == 'Flood':
            gcode += linenumber() + 'M8\n'
        if coolantMode == 'Mist':
            gcode += linenumber() + 'M7\n'

        # Parse the op:
        gcode += parse(obj)

        # Do the post_op:
        if OUTPUT_COMMENTS and OUTPUT_FINISH:
            gcode += linenumber() + '(Finish operation: ' + obj.Label + ')\n'
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # Turn coolant off if previously enabled:
        if not coolantMode == 'None':
            if OUTPUT_COMMENTS:
                gcode += linenumber() + '(Coolant Off:' + coolantMode + ')\n'
            gcode += linenumber() + 'M9\n'

    # Do the post_amble:
    if OUTPUT_BCNC:
        gcode += linenumber() + '(Block-name: post_amble)\n'
        gcode += linenumber() + '(Block-expand: 0)\n'
        gcode += linenumber() + '(Block-enable: 1)\n'
    if OUTPUT_COMMENTS:
        gcode += linenumber() + '(Begin postamble)\n'
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    # Optionally add a final XYZ position to the end of the gcode:
    if RETURN_TO:
        first_comma = RETURN_TO.find(',')
        last_comma = RETURN_TO.rfind(',')  # == first_comma if only one comma
        ref_X = ' X' + RETURN_TO[0:first_comma].strip()

        # Z is optional:
        if last_comma != first_comma:
            ref_Z = ' Z' + RETURN_TO[last_comma + 1:].strip()
            ref_Y = ' Y' + RETURN_TO[first_comma + 1:last_comma].strip()
        else:
            ref_Z = ''
            ref_Y = ' Y' + RETURN_TO[first_comma + 1:].strip()

        gcode += linenumber() + 'G0' + ref_X + ref_Y + ref_Z + '\n'

    # Optionally add recommended Marlin 2.x configuration to gcode file:
    if OUTPUT_MARLIN_CONFIG:
        gcode += linenumber() + '(Marlin 2.x Configuration)\n'
        gcode += linenumber() + '(The following should be enabled in)\n'
        gcode += linenumber() + '(the configuration files of Marlin 2.x)\n'
        gcode += linenumber() + '(#define ARC_SUPPORT)\n'
        gcode += linenumber() + '(#define CNC_COORDINATE_SYSTEMS)\n'
        gcode += linenumber() + '(#define PAREN_COMMENTS)\n'
        gcode += linenumber() + '(#define GCODE_MOTION_MODES)\n'
        gcode += linenumber() + '(#define G0_FEEDRATE)\n'
        gcode += linenumber() + '(define VARIABLE_G0_FEEDRATE)\n'

    # Show the gcode result dialog:
    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print('Done postprocessing.')

    # Write the file:
    with open(filename, 'w') as fp:
        fp.write(final)
Exemple #8
0
def export(objectslist, filename, argstring):
    global OUTPUT_COMMENTS
    global OUTPUT_HEADER
    global SHOW_EDITOR
    global CurrentState
    global GetValue

    for arg in argstring.split():
        if arg == '--comments':
            OUTPUT_COMMENTS = True
        if arg == '--inches':
            GetValue = getImperialValue
        if arg == '--no-header':
            OUTPUT_HEADER = False
        if arg == '--no-show-editor':
            SHOW_EDITOR = False

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            s = "the object " + obj.Name
            s += " is not a path. Please select only path and Compounds."
            print(s)
            return

    CurrentState = {
        'X': 0,
        'Y': 0,
        'Z': 0,
        'F': 0,
        'S': 0,
        'JSXY': 0,
        'JSZ': 0,
        'MSXY': 0,
        'MSZ': 0
    }
    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "'Exported by FreeCAD\n"
        gcode += linenumber() + "'Post Processor: " + __name__ + "\n"
        gcode += linenumber() + "'Output Time:" + str(now) + "\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line

    for obj in objectslist:

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    # Write the output
    gfile = pythonopen(filename, "wb")
    gfile.write(final)
    gfile.close()
Exemple #9
0
def export(objectslist, filename, argstring):
    # pylint: disable=global-statement
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name + " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + "\n"
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # Skip inactive operations
        if hasattr(obj, 'Active'): 
            if not obj.Active:
                continue
        if hasattr(obj, 'Base') and hasattr(obj.Base, 'Active'):
            if not obj.Base.Active:
                continue

        # fetch machine details
        job = PathUtils.findParentJob(obj)

        myMachine = 'not set'

        if hasattr(job, "MachineName"):
            myMachine = job.MachineName

        if hasattr(job, "MachineUnits"):
            if job.MachineUnits == "Metric":
                UNITS = "G21"
                UNIT_FORMAT = 'mm'
                UNIT_SPEED_FORMAT = 'mm/min'
            else:
                UNITS = "G20"
                UNIT_FORMAT = 'in'
                UNIT_SPEED_FORMAT = 'in/min'

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: %s)\n" % obj.Label
            gcode += linenumber() + "(machine: %s, %s)\n" % (myMachine, UNIT_SPEED_FORMAT)
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # get coolant mode
        coolantMode = 'None'
        if hasattr(obj, "CoolantMode") or hasattr(obj, 'Base') and  hasattr(obj.Base, "CoolantMode"):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # turn coolant on if required
        if OUTPUT_COMMENTS:
            if not coolantMode == 'None':
                gcode += linenumber() + '(Coolant On:' + coolantMode + ')\n'
        if coolantMode == 'Flood':
            gcode  += linenumber() + 'M8' + '\n'
        if coolantMode == 'Mist':
            gcode += linenumber() + 'M7' + '\n'

        # process the operation gcode
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: %s)\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # turn coolant off if required
        if not coolantMode == 'None':
            if OUTPUT_COMMENTS:
                gcode += linenumber() + '(Coolant Off:' + coolantMode + ')\n'    
            gcode  += linenumber() +'M9' + '\n'

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
def export(objectslist, filename, argstring):
    # pylint: disable=global-statement
    processArguments(argstring)
    for i in objectslist:
        print(i.Name)
    global UNITS
    global UNIT_FORMAT

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += HEADER

    gcode += SAFETYBLOCK

    # Write the preamble
    if OUTPUT_COMMENTS:
        for item in objectslist:
            if isinstance(item.Proxy,
                          PathScripts.PathToolController.ToolController):
                gcode += ";T{}={}\n".format(item.ToolNumber, item.Name)
        gcode += linenumber() + ";begin preamble\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line

    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:
        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";begin operation\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";end operation: %s\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble

    if OUTPUT_COMMENTS:
        gcode += ";begin postamble\n"
    for line in TOOLRETURN.splitlines(True):
        gcode += linenumber() + line
    for line in SAFETYBLOCK.splitlines(True):
        gcode += linenumber() + line
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + "\n"
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # Skip inactive operations
        if hasattr(obj, "Active"):
            if not obj.Active:
                continue
        if hasattr(obj, "Base") and hasattr(obj.Base, "Active"):
            if not obj.Base.Active:
                continue

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: %s)\n" % obj.Label
            gcode += linenumber() + "(machine: %s, %s)\n" % (
                MACHINE_NAME,
                UNIT_SPEED_FORMAT,
            )
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # get coolant mode
        coolantMode = "None"
        if (hasattr(obj, "CoolantMode")
                or hasattr(obj, "Base") and hasattr(obj.Base, "CoolantMode")):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # turn coolant on if required
        if OUTPUT_COMMENTS:
            if not coolantMode == "None":
                gcode += linenumber() + "(Coolant On:" + coolantMode + ")\n"
        if coolantMode == "Flood":
            gcode += linenumber() + "M8" + "\n"
        if coolantMode == "Mist":
            gcode += linenumber() + "M7" + "\n"

        # process the operation gcode
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: %s)\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # turn coolant off if required
        if not coolantMode == "None":
            if OUTPUT_COMMENTS:
                gcode += linenumber() + "(Coolant Off:" + coolantMode + ")\n"
            gcode += linenumber() + "M9" + "\n"

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == "-":
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
Exemple #12
0
def export_common(values, objectslist, filename):
    """Do the common parts of postprocessing the objects in objectslist to filename."""
    #
    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("The object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    # for obj in objectslist:
    #    print(obj.Name)

    print("PostProcessor:  " + values["POSTPROCESSOR_FILE_NAME"] +
          " postprocessing...")
    gcode = ""

    # write header
    if values["OUTPUT_HEADER"]:
        comment = PostUtilsParse.create_comment("Exported by FreeCAD",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        comment = PostUtilsParse.create_comment(
            "Post Processor: " + values["POSTPROCESSOR_FILE_NAME"],
            values["COMMENT_SYMBOL"],
        )
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        if FreeCAD.ActiveDocument:
            cam_file = os.path.basename(FreeCAD.ActiveDocument.FileName)
        else:
            cam_file = "<None>"
        comment = PostUtilsParse.create_comment("Cam File: " + cam_file,
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        comment = PostUtilsParse.create_comment(
            "Output Time: " + str(datetime.datetime.now()),
            values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"

    # Check canned cycles for drilling
    if values["TRANSLATE_DRILL_CYCLES"]:
        if len(values["SUPPRESS_COMMANDS"]) == 0:
            values["SUPPRESS_COMMANDS"] = ["G99", "G98", "G80"]
        else:
            values["SUPPRESS_COMMANDS"] += ["G99", "G98", "G80"]

    for line in values["SAFETYBLOCK"].splitlines(False):
        gcode += PostUtilsParse.linenumber(values) + line + "\n"

    # Write the preamble
    if values["OUTPUT_COMMENTS"]:
        if values["LIST_TOOLS_IN_PREAMBLE"]:
            for item in objectslist:
                if hasattr(item, "Proxy") and isinstance(
                        item.Proxy, PathToolController.ToolController):
                    comment = PostUtilsParse.create_comment(
                        "T{}={}".format(item.ToolNumber, item.Name),
                        values["COMMENT_SYMBOL"],
                    )
                    gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        comment = PostUtilsParse.create_comment("Begin preamble",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
    for line in values["PREAMBLE"].splitlines(False):
        gcode += PostUtilsParse.linenumber(values) + line + "\n"
    # verify if PREAMBLE or SAFETYBLOCK have changed MOTION_MODE or UNITS
    if "G90" in values["PREAMBLE"] or "G90" in values["SAFETYBLOCK"]:
        values["MOTION_MODE"] = "G90"
    elif "G91" in values["PREAMBLE"] or "G91" in values["SAFETYBLOCK"]:
        values["MOTION_MODE"] = "G91"
    else:
        gcode += PostUtilsParse.linenumber(
            values) + values["MOTION_MODE"] + "\n"
    if "G21" in values["PREAMBLE"] or "G21" in values["SAFETYBLOCK"]:
        values["UNITS"] = "G21"
        values["UNIT_FORMAT"] = "mm"
        values["UNIT_SPEED_FORMAT"] = "mm/min"
    elif "G20" in values["PREAMBLE"] or "G20" in values["SAFETYBLOCK"]:
        values["UNITS"] = "G20"
        values["UNIT_FORMAT"] = "in"
        values["UNIT_SPEED_FORMAT"] = "in/min"
    else:
        gcode += PostUtilsParse.linenumber(values) + values["UNITS"] + "\n"

    for obj in objectslist:

        # Debug...
        # print("\n" + "*"*70)
        # dump(obj)
        # print("*"*70 + "\n")

        # Skip inactive operations
        if hasattr(obj, "Active"):
            if not obj.Active:
                continue
        if hasattr(obj, "Base") and hasattr(obj.Base, "Active"):
            if not obj.Base.Active:
                continue

        # do the pre_op
        if values["OUTPUT_BCNC"]:
            comment = PostUtilsParse.create_comment("Block-name: " + obj.Label,
                                                    values["COMMENT_SYMBOL"])
            gcode += PostUtilsParse.linenumber(values) + comment + "\n"
            comment = PostUtilsParse.create_comment("Block-expand: 0",
                                                    values["COMMENT_SYMBOL"])
            gcode += PostUtilsParse.linenumber(values) + comment + "\n"
            comment = PostUtilsParse.create_comment("Block-enable: 1",
                                                    values["COMMENT_SYMBOL"])
            gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        if values["OUTPUT_COMMENTS"]:
            if values["SHOW_OPERATION_LABELS"]:
                comment = PostUtilsParse.create_comment(
                    "Begin operation: %s" % obj.Label,
                    values["COMMENT_SYMBOL"])
            else:
                comment = PostUtilsParse.create_comment(
                    "Begin operation", values["COMMENT_SYMBOL"])
            gcode += PostUtilsParse.linenumber(values) + comment + "\n"
            if values["SHOW_MACHINE_UNITS"]:
                comment = PostUtilsParse.create_comment(
                    "Machine units: %s" % values["UNIT_SPEED_FORMAT"],
                    values["COMMENT_SYMBOL"],
                )
                gcode += PostUtilsParse.linenumber(values) + comment + "\n"
            if values["OUTPUT_MACHINE_NAME"]:
                comment = PostUtilsParse.create_comment(
                    "Machine: %s, %s" %
                    (values["MACHINE_NAME"], values["UNIT_SPEED_FORMAT"]),
                    values["COMMENT_SYMBOL"],
                )
                gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        for line in values["PRE_OPERATION"].splitlines(False):
            gcode += PostUtilsParse.linenumber(values) + line + "\n"

        # get coolant mode
        coolantMode = "None"
        if (hasattr(obj, "CoolantMode")
                or hasattr(obj, "Base") and hasattr(obj.Base, "CoolantMode")):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # turn coolant on if required
        if values["ENABLE_COOLANT"]:
            if values["OUTPUT_COMMENTS"]:
                if not coolantMode == "None":
                    comment = PostUtilsParse.create_comment(
                        "Coolant On:" + coolantMode, values["COMMENT_SYMBOL"])
                    gcode += PostUtilsParse.linenumber(values) + comment + "\n"
            if coolantMode == "Flood":
                gcode += PostUtilsParse.linenumber(values) + "M8" + "\n"
            if coolantMode == "Mist":
                gcode += PostUtilsParse.linenumber(values) + "M7" + "\n"

        # process the operation gcode
        gcode += PostUtilsParse.parse(values, obj)

        # do the post_op
        if values["OUTPUT_COMMENTS"]:
            comment = PostUtilsParse.create_comment(
                "%s operation: %s" % (values["FINISH_LABEL"], obj.Label),
                values["COMMENT_SYMBOL"],
            )
            gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        for line in values["POST_OPERATION"].splitlines(False):
            gcode += PostUtilsParse.linenumber(values) + line + "\n"

        # turn coolant off if required
        if values["ENABLE_COOLANT"]:
            if not coolantMode == "None":
                if values["OUTPUT_COMMENTS"]:
                    comment = PostUtilsParse.create_comment(
                        "Coolant Off:" + coolantMode, values["COMMENT_SYMBOL"])
                    gcode += PostUtilsParse.linenumber(values) + comment + "\n"
                gcode += PostUtilsParse.linenumber(values) + "M9" + "\n"

    if values["RETURN_TO"]:
        gcode += PostUtilsParse.linenumber(
            values) + "G0 X%s Y%s Z%s\n" % tuple(values["RETURN_TO"])

    # do the post_amble
    if values["OUTPUT_BCNC"]:
        comment = PostUtilsParse.create_comment("Block-name: post_amble",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        comment = PostUtilsParse.create_comment("Block-expand: 0",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
        comment = PostUtilsParse.create_comment("Block-enable: 1",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
    if values["OUTPUT_COMMENTS"]:
        comment = PostUtilsParse.create_comment("Begin postamble",
                                                values["COMMENT_SYMBOL"])
        gcode += PostUtilsParse.linenumber(values) + comment + "\n"
    for line in values["TOOLRETURN"].splitlines(False):
        gcode += PostUtilsParse.linenumber(values) + line + "\n"
    for line in values["SAFETYBLOCK"].splitlines(False):
        gcode += PostUtilsParse.linenumber(values) + line + "\n"
    for line in values["POSTAMBLE"].splitlines(False):
        gcode += PostUtilsParse.linenumber(values) + line + "\n"

    if FreeCAD.GuiUp and values["SHOW_EDITOR"]:
        final = gcode
        if len(gcode) > 100000:
            print("Skipping editor since output is greater than 100kb")
        else:
            dia = PostUtils.GCodeEditorDialog()
            dia.editor.setText(gcode)
            result = dia.exec_()
            if result:
                final = dia.editor.toPlainText()
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == "-":
        gfile = pythonopen(filename,
                           "w",
                           newline=values["END_OF_LINE_CHARACTERS"])
        gfile.write(final)
        gfile.close()

    return final
Exemple #13
0
def export(objectslist, filename, argstring):

    if not processArguments(argstring):
        print("export: process arguments failed, '{}'".format(argstring))
        return None

    global warnings_count
    global problems_count

    warnings_count = 0
    problems_count = 0

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("export: postprocessing...")
    gcode = append0("%" + PROG_NAME + "\n")
    if not argstring:
        gcode += append("(" + __name__ + " with default settings)\n")
    else:
        gcode += append("({} {})\n".format(__name__, argstring))

    # write header
    if OUTPUT_HEADER:
        for line in HEADER.format(
                GCODE_PROCESSOR,
                __name__,
                VERSION,
                FreeCAD.ActiveDocument.FileName,
                str(now),
        ).splitlines(False):
            if line:
                gcode += append(line + "\n")

    # Write the preamble
    # G20/G21 not supported by UC-CNC, *always* report the configured units.
    gcode += append("(Units: '" + UNIT_FORMAT + "' and '" + UNIT_SPEED_FORMAT +
                    "')\n")
    if UNIT_DEFAULT_CHANGED:
        gcode += append(
            "(WARNING: Units default changed, check your UC-CNC profile)\n")
        warnings_count += 1

    if OUTPUT_COMMENTS:
        gcode += append("(preamble: begin)\n")
        # for obj in objectslist:
        #    if isinstance(obj.Proxy, PathScripts.PathToolController.ToolController):
        #        gcode += append("(T{}={})\n".format(obj.ToolNumber, item.Name))
        # error: global name 'PathScripts' is not defined
    for line in PREAMBLE.splitlines(False):
        gcode += append(line + "\n")
    if OUTPUT_COMMENTS:
        gcode += append("(preamble: done)\n")

    # write the code body
    for obj in objectslist:

        # pre_op
        if OUTPUT_COMMENTS:
            gcode += append("(operation initialise: %s)\n" % obj.Label)
        for line in PRE_OPERATION.splitlines(True):
            gcode += append(line)

        # turn coolant on if required
        if hasattr(obj, "CoolantMode"):
            coolantMode = obj.CoolantMode
            if coolantMode == "Mist":
                if OUTPUT_COMMENTS:
                    gcode += append("M7 (coolant: mist on)\n")
                else:
                    gcode += append("M7\n")
            if coolantMode == "Flood":
                if OUTPUT_COMMENTS:
                    gcode += append("M8 (coolant: flood on)\n")
                else:
                    gcode += append("M8\n")

        # process the operation gcode
        if OUTPUT_COMMENTS:
            gcode += append("(operation start: %s)\n" % obj.Label)
        gcode += parse(obj)
        if OUTPUT_COMMENTS:
            gcode += append("(operation done: %s)\n" % obj.Label)

        # post_op
        for line in POST_OPERATION.splitlines(True):
            gcode += append(line)

        # turn coolant off if required
        if hasattr(obj, "CoolantMode"):
            coolantMode = obj.CoolantMode
            if not coolantMode == "None":
                if OUTPUT_COMMENTS:
                    gcode += append("M9 (coolant: off)\n")
                else:
                    gcode += append("M9\n")
        if OUTPUT_COMMENTS:
            gcode += append("(operation finalised: %s)\n" % obj.Label)

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += append("(postamble: begin)\n")
    for line in POSTAMBLE.splitlines(True):
        gcode += append(line)
    if OUTPUT_COMMENTS:
        gcode += append("(postamble: done)\n")

    # Show the results
    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    if (0 < problems_count) or (0 < warnings_count):
        print(
            "export: postprocessing: done, warnings: {}, problems: {}, see GCode for details."
            .format(warnings_count, problems_count))
    else:
        print("export: postprocessing: done (none of the problems detected).")

    if not filename == "-":
        print("export: writing to '{}'".format(filename))
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
Exemple #14
0
def export(objectslist, filename, argstring):
    processArguments(argstring)
    for i in objectslist:
        print(i.Name)
    global UNITS
    global UNIT_FORMAT

    # ISJOB = (len(objectslist) == 1) and isinstance(objectslist[0].Proxy, PathScripts.PathJob.ObjectJob)
    # print("isjob: {} {}".format(ISJOB, len(objectslist)))

    # if len(objectslist) > 1:
    #     for obj in objectslist:
    #         if not hasattr(obj, "Path"):
    #             print("the object " + obj.Name + " is not a path. Please select only path and Compounds.")
    #             return

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += HEADER

    gcode += SAFETYBLOCK

    # Write the preamble
    if OUTPUT_COMMENTS:
        for item in objectslist:
            if isinstance(item.Proxy,
                          PathScripts.PathToolController.ToolController):
                gcode += ";T{}={}\n".format(item.ToolNumber, item.Name)
        gcode += linenumber() + ";begin preamble\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line

    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:
        #skip postprocessing tools
        # if isinstance (obj.Proxy, PathScripts.PathToolController.ToolController):
        #     continue

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";begin operation\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";end operation: %s\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble

    if OUTPUT_COMMENTS:
        gcode += ";begin postamble\n"
    for line in TOOLRETURN.splitlines(True):
        gcode += linenumber() + line
    for line in SAFETYBLOCK.splitlines(True):
        gcode += linenumber() + line
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "wb")
        gfile.write(final)
        gfile.close()

    return final
def export(objectslist, filename, argstring):
    # pylint: disable=global-statement
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # prepare to take picture
    FreeCAD.Gui.activeDocument().activeView().viewIsometric()
    FreeCAD.Gui.SendMsgToActiveView("ViewFit")
    imagePath = FreeCAD.Gui.activeDocument().Document.FileName + ".png"
    FreeCAD.Gui.activeDocument().activeView().saveImage(
        imagePath, 720, 480, 'White')

    imageBase64 = ""
    with open(imagePath, "rb") as image_file:
        imageBase64 = base64.b64encode(image_file.read())

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + ";Exported for Snapmaker 2\n"
        gcode += linenumber() + ";Post Processor: " + __name__ + "\n"
        gcode += linenumber() + ";Output Time:" + str(now) + "\n"
        if not imageBase64 == "":
            gcode += linenumber(
            ) + ";Header Start\n;header_type: cnc\n;thumbnail: data:image/png;base64," + imageBase64.decode(
            ) + "\n;Header End\n"
        gcode += linenumber() + PREAMBLE + "\n"
        gcode += linenumber() + "G0 Z10.00 F300" + "\n"
        # gcode += linenumber() + "G0 Z0.50 F120" + "\n"
        PathLog.debug(
            "===== Post-process for Snapmaker 2 (export linear moves only) =====\n"
        )

    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # Skip inactive operations
        if hasattr(obj, 'Active'):
            if not obj.Active:
                continue
        if hasattr(obj, 'Base') and hasattr(obj.Base, 'Active'):
            if not obj.Base.Active:
                continue

        # fetch machine details
        job = PathUtils.findParentJob(obj)

        myMachine = 'not set'

        if hasattr(job, "MachineName"):
            myMachine = job.MachineName

        if hasattr(job, "MachineUnits"):
            if job.MachineUnits == "Metric":
                UNITS = "G21"
                UNIT_FORMAT = 'mm'
                UNIT_SPEED_FORMAT = 'mm/min'
            else:
                UNITS = "G20"
                UNIT_FORMAT = 'in'
                UNIT_SPEED_FORMAT = 'in/min'

        # get coolant mode
        coolantMode = 'None'
        if hasattr(obj, "CoolantMode") or hasattr(obj, 'Base') and hasattr(
                obj.Base, "CoolantMode"):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # process the operation gcode
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";finish operation: %s\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += ";begin postamble\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
Exemple #16
0
def export(objectslist, filename, argstring):
    # pylint: disable=unused-argument
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return

    print("postprocessing...")
    gcode = ""

    # Find the machine.
    # The user my have overridden post processor defaults in the GUI.  Make sure we're using the current values in the Machine Def.
    myMachine = None
    for pathobj in objectslist:
        if hasattr(pathobj, "MachineName"):
            myMachine = pathobj.MachineName
        if hasattr(pathobj, "MachineUnits"):
            if pathobj.MachineUnits == "Metric":
                UNITS = "G21"
            else:
                UNITS = "G20"
    if myMachine is None:
        print("No machine found in this selection")

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble

    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    print('show editor: {}'.format(SHOW_EDITOR))
    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    gfile = pythonopen(filename, "w")
    gfile.write(final)
    gfile.close()
Exemple #17
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = linenumber() + 'IMF_PBL_V1.0\n'

    # write header
    now = datetime.datetime.now()
    gcode += linenumber() + ";- Exported by FreeCAD\n"
    gcode += linenumber() + ";- Post Processor: " + __name__ + '\n'
    gcode += linenumber() + ";- Output Time:" + str(now) + '\n'

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + ";- begin preamble\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + '\n'
    if OUTPUT_COMMENTS:
        gcode += linenumber() + ";- finish preamble\n"

    for obj in objectslist:

        # fetch machine details
        job = PathUtils.findParentJob(obj)

        myMachine = job.Machine if hasattr(job, 'Machine') else MACHINE_NAME

        # FIXME: we only operate using ISEL units (µm and µm/s)
        if hasattr(job, "MachineUnits"):
            print("Unknown parameter 'MachineUnits: "
                  "{}".format(job.MachineUnits))

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";- begin operation: %s\n" % obj.Label
            gcode += linenumber() + ";- machine: %s\n" % myMachine
            gcode += linenumber() + ";- unit system: %s\n" % UNIT_SPEED_FORMAT
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + ";- finish operation: %s\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + ";- begin postamble\n"
    for line in POSTAMBLE.splitlines(False):
        gcode += linenumber() + line + '\n'
    if OUTPUT_COMMENTS:
        gcode += linenumber() + ";- finish postamble\n"

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        # Clear read-only flag set by Isel Remote
        try:
            chmod(filename, S_IWRITE | S_IREAD)
        except:
            pass  # file did not exist ?
        with pythonopen(filename, "w") as f:
            f.write(final)

    return final
Exemple #18
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None

    global UNITS
    global UNIT_FORMAT
    global UNIT_FEED_FORMAT
    global MOTION_MODE
    global SUPPRESS_COMMANDS

    print("Post Processor: " + __name__ + " postprocessing...")
    gcode = ""

    # Write header:
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__
        gcode += ".py, version: " + Revised + ")\n"
        gcode += linenumber() + "(Output Time:" + str(datetime.now()) + ")\n"

    # Suppress drill-cycle commands:
    if TRANSLATE_DRILL_CYCLES:
        SUPPRESS_COMMANDS += ["G80", "G98", "G99"]

    # Write the preamble:
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(Begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line

    # Write these settings AFTER the preamble,
    # to prevent the preamble from changing these:
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(Default Configuration)\n"
    gcode += linenumber() + MOTION_MODE + "\n"
    gcode += linenumber() + UNITS + "\n"
    gcode += linenumber() + WORK_PLANE + "\n"

    for obj in objectslist:
        # Debug...
        # print('\n' + '*'*70 + '\n')
        # dump(obj)
        # print('\n' + '*'*70 + '\n')
        if not hasattr(obj, "Path"):
            print(
                "The object "
                + obj.Name
                + " is not a path. Please select only path and Compounds."
            )
            return

        # Skip inactive operations:
        if PathUtil.opProperty(obj, "Active") is False:
            continue

        # Do the pre_op:
        if OUTPUT_BCNC:
            gcode += linenumber() + "(Block-name: " + obj.Label + ")\n"
            gcode += linenumber() + "(Block-expand: 0)\n"
            gcode += linenumber() + "(Block-enable: 1)\n"
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(Begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # Get coolant mode:
        coolantMode = "None"  # None is the word returned from the operation
        if (
            hasattr(obj, "CoolantMode")
            or hasattr(obj, "Base")
            and hasattr(obj.Base, "CoolantMode")
        ):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # Turn coolant on if required:
        if OUTPUT_COMMENTS:
            if not coolantMode == "None":
                gcode += linenumber() + "(Coolant On:" + coolantMode + ")\n"
        if coolantMode == "Flood":
            gcode += linenumber() + "M8\n"
        if coolantMode == "Mist":
            gcode += linenumber() + "M7\n"

        # Parse the op:
        gcode += parse(obj)

        # Do the post_op:
        if OUTPUT_COMMENTS and OUTPUT_FINISH:
            gcode += linenumber() + "(Finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # Turn coolant off if previously enabled:
        if not coolantMode == "None":
            if OUTPUT_COMMENTS:
                gcode += linenumber() + "(Coolant Off:" + coolantMode + ")\n"
            gcode += linenumber() + "M9\n"

    # Do the post_amble:
    if OUTPUT_BCNC:
        gcode += linenumber() + "(Block-name: post_amble)\n"
        gcode += linenumber() + "(Block-expand: 0)\n"
        gcode += linenumber() + "(Block-enable: 1)\n"
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(Begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    # Optionally add a final XYZ position to the end of the gcode:
    if RETURN_TO:
        first_comma = RETURN_TO.find(",")
        last_comma = RETURN_TO.rfind(",")  # == first_comma if only one comma
        ref_X = " X" + RETURN_TO[0:first_comma].strip()

        # Z is optional:
        if last_comma != first_comma:
            ref_Z = " Z" + RETURN_TO[last_comma + 1 :].strip()
            ref_Y = " Y" + RETURN_TO[first_comma + 1 : last_comma].strip()
        else:
            ref_Z = ""
            ref_Y = " Y" + RETURN_TO[first_comma + 1 :].strip()

        gcode += linenumber() + "G0" + ref_X + ref_Y + ref_Z + "\n"

    # Optionally add recommended Marlin 2.x configuration to gcode file:
    if OUTPUT_MARLIN_CONFIG:
        gcode += linenumber() + "(Marlin 2.x Configuration)\n"
        gcode += linenumber() + "(The following should be enabled in)\n"
        gcode += linenumber() + "(the configuration files of Marlin 2.x)\n"
        gcode += linenumber() + "(#define ARC_SUPPORT)\n"
        gcode += linenumber() + "(#define CNC_COORDINATE_SYSTEMS)\n"
        gcode += linenumber() + "(#define PAREN_COMMENTS)\n"
        gcode += linenumber() + "(#define GCODE_MOTION_MODES)\n"
        gcode += linenumber() + "(#define G0_FEEDRATE)\n"
        gcode += linenumber() + "(define VARIABLE_G0_FEEDRATE)\n"

    # Show the gcode result dialog:
    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("Done postprocessing.")

    # Write the file:
    with open(filename, "w") as fp:
        fp.write(final)
Exemple #19
0
def export(objectslist, filename, argstring):

  if not processArguments(argstring):
    return None

  global UNITS
  global UNIT_FORMAT
  global UNIT_SPEED_FORMAT
  global MOTION_MODE
  global SUPPRESS_COMMANDS

  print("Post Processor: " + __name__ + " postprocessing...")
  gcode = ""

  # write header
  if OUTPUT_HEADER:
    gcode += linenumber() + "(Exported by FreeCAD)\n"
    gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
    gcode += linenumber() + "(Output Time:" + str(datetime.datetime.now()) + ")\n"

  # Check canned cycles for drilling
  if TRANSLATE_DRILL_CYCLES:
    if len(SUPPRESS_COMMANDS) == 0:
      SUPPRESS_COMMANDS = ['G99', 'G98', 'G80']
    else:
      SUPPRESS_COMMANDS += ['G99', 'G98', 'G80']

  # Write the preamble
  if OUTPUT_COMMENTS:
    gcode += linenumber() + "(Begin preamble)\n"
  for line in PREAMBLE.splitlines(True):
    gcode += linenumber() + line
  # verify if PREAMBLE have changed MOTION_MODE or UNITS
  if 'G90' in PREAMBLE:
    MOTION_MODE = 'G90'
  elif 'G91' in PREAMBLE:
    MOTION_MODE = 'G91'
  else:
    gcode += linenumber() + MOTION_MODE + "\n"
  if 'G21' in PREAMBLE:
    UNITS = 'G21'
    UNIT_FORMAT = 'mm'
    UNIT_SPEED_FORMAT = 'mm/min'
  elif 'G20' in PREAMBLE:
    UNITS = 'G20'
    UNIT_FORMAT = 'in'
    UNIT_SPEED_FORMAT = 'in/min'
  else:
    gcode += linenumber() + UNITS + "\n"

  for obj in objectslist:
    # Debug...
    # print("\n" + "*"*70)
    # dump(obj)
    # print("*"*70 + "\n")
    if not hasattr(obj, "Path"):
      print("The object " + obj.Name + " is not a path. Please select only path and Compounds.")
      return

    # Skip inactive operations
    if PathUtil.opProperty(obj, 'Active') is False:
        continue

    # do the pre_op
    if OUTPUT_BCNC:
      gcode += linenumber() + "(Block-name: " + obj.Label + ")\n"
      gcode += linenumber() + "(Block-expand: 0)\n"
      gcode += linenumber() + "(Block-enable: 1)\n"
    if OUTPUT_COMMENTS:
      gcode += linenumber() + "(Begin operation: " + obj.Label + ")\n"
    for line in PRE_OPERATION.splitlines(True):
      gcode += linenumber() + line

    # get coolant mode
    coolantMode = 'None'
    if hasattr(obj, "CoolantMode") or hasattr(obj, 'Base') and  hasattr(obj.Base, "CoolantMode"):
        if hasattr(obj, "CoolantMode"):
            coolantMode = obj.CoolantMode
        else:
            coolantMode = obj.Base.CoolantMode

    # turn coolant on if required
    if OUTPUT_COMMENTS:
        if not coolantMode == 'None':
            gcode += linenumber() + '(Coolant On:' + coolantMode + ')\n'
    if coolantMode == 'Flood':
        gcode  += linenumber() + 'M8' + '\n'
    if coolantMode == 'Mist':
        gcode += linenumber() + 'M7' + '\n'

    # Parse the op
    gcode += parse(obj)

    # do the post_op
    if OUTPUT_COMMENTS:
      gcode += linenumber() + "(Finish operation: " + obj.Label + ")\n"
    for line in POST_OPERATION.splitlines(True):
      gcode += linenumber() + line

    # turn coolant off if required
    if not coolantMode == 'None':
        if OUTPUT_COMMENTS:
            gcode += linenumber() + '(Coolant Off:' + coolantMode + ')\n'
        gcode += linenumber() +'M9' + '\n'

  # do the post_amble
  if OUTPUT_BCNC:
    gcode += linenumber() + "(Block-name: post_amble)\n"
    gcode += linenumber() + "(Block-expand: 0)\n"
    gcode += linenumber() + "(Block-enable: 1)\n"
  if OUTPUT_COMMENTS:
    gcode += linenumber() + "(Begin postamble)\n"
  for line in POSTAMBLE.splitlines(True):
    gcode += linenumber() + line

  if RETURN_TO:
    gcode += linenumber() + "G0 X%s Y%s" % tuple(RETURN_TO)

  # show the gCode result dialog
  if FreeCAD.GuiUp and SHOW_EDITOR:
    dia = PostUtils.GCodeEditorDialog()
    dia.editor.setText(gcode)
    result = dia.exec_()
    if result:
      final = dia.editor.toPlainText()
    else:
      final = gcode
  else:
    final = gcode

  print("Done postprocessing.")

  # write the file
  gfile = pythonopen(filename, "w")
  gfile.write(final)
  gfile.close()
Exemple #20
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT
    global HORIZRAPID
    global VERTRAPID

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += "%\n"
        gcode += ";\n"
        gcode += (os.path.split(filename)[-1] + " (" +
                  "FREECAD-FILENAME-GOES-HERE" + ", " + "JOB-NAME-GOES-HERE" +
                  ")\n")
        gcode += linenumber() + "(" + filename.upper(
        ) + ",EXPORTED BY FREECAD!)\n"
        gcode += linenumber() + "(POST PROCESSOR: " + __name__.upper() + ")\n"
        gcode += linenumber() + "(OUTPUT TIME:" + str(now).upper() + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(BEGIN PREAMBLE)\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + "\n"
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # Skip inactive operations
        if hasattr(obj, "Active"):
            if not obj.Active:
                continue
        if hasattr(obj, "Base") and hasattr(obj.Base, "Active"):
            if not obj.Base.Active:
                continue

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber(
            ) + "(BEGIN OPERATION: %s)\n" % obj.Label.upper()
            gcode += linenumber() + "(MACHINE UNITS: %s)\n" % (
                UNIT_SPEED_FORMAT.upper())
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # get coolant mode
        coolantMode = "None"
        if (hasattr(obj, "CoolantMode")
                or hasattr(obj, "Base") and hasattr(obj.Base, "CoolantMode")):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # turn coolant on if required
        if OUTPUT_COMMENTS:
            if not coolantMode == "None":
                gcode += linenumber() + "(COOLANT ON:" + coolantMode.upper(
                ) + ")\n"
        if coolantMode == "Flood":
            gcode += linenumber() + "M8" + "\n"
        if coolantMode == "Mist":
            gcode += linenumber() + "M7" + "\n"

        # process the operation gcode
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber(
            ) + "(FINISH OPERATION: %s)\n" % obj.Label.upper()
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # turn coolant off if required
        if not coolantMode == "None":
            if OUTPUT_COMMENTS:
                gcode += linenumber() + "(COOLANT OFF:" + coolantMode.upper(
                ) + ")\n"
            gcode += linenumber() + "M9" + "\n"

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(BEGIN POSTAMBLE)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += "%\n"

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == "-":
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final
Exemple #21
0
def export(objectslist, filename, argstring):

    if not processArguments(argstring):
        return None

    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT
    global MOTION_MODE

    print("Post Processor: " + __name__ + " postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(
            datetime.datetime.now()) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    # verify if PREAMBLE have changed MOTION_MODE or UNITS
    if 'G90' in PREAMBLE:
        MOTION_MODE = 'G90'
    elif 'G91' in PREAMBLE:
        MOTION_MODE = 'G91'
    else:
        gcode += linenumber() + MOTION_MODE + "\n"
    if 'G21' in PREAMBLE:
        UNITS = 'G21'
        UNIT_FORMAT = 'mm'
        UNIT_SPEED_FORMAT = 'mm/min'
    elif 'G20' in PREAMBLE:
        UNITS = 'G20'
        UNIT_FORMAT = 'in'
        UNIT_SPEED_FORMAT = 'in/min'
    else:
        gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:
        # Debug...
        # print("\n" + "*"*70)
        # dump(obj)
        # print("*"*70 + "\n")
        if not hasattr(obj, "Path"):
            print("The object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # Parse the op
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    # show the gCode result dialog
    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    # write the file
    gfile = pythonopen(filename, "w")
    gfile.write(final)
    gfile.close()
Exemple #22
0
def export(objectslist, filename, argstring):
    if not processArguments(argstring):
        return None

    global UNITS

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return

    if SPINDLE_CONTROL:
        ALLOWED_COMMANDS.extend(['M3', 'M4', 'M5'])

    print("postprocessing...")
    gcode = ""

    #Find the machine.
    #The user my have overridden post processor defaults in the GUI.  Make sure we're using the current values in the Machine Def.
    myMachine = None
    for pathobj in objectslist:
        if hasattr(pathobj, "Group"):  #We have a compound or project.
            for p in pathobj.Group:
                if p.Name == "Machine":
                    myMachine = p
    if myMachine is None:
        print("No machine found in this project")
    else:
        if myMachine.MachineUnits == "Metric":
            UNITS = "G21"
        else:
            UNITS = "G20"

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + ";Exported by FreeCAD\n"
        gcode += linenumber() + ";Post Processor: " + __name__ + "\n"
        gcode += linenumber() + ";Output Time:" + str(now) + "\n"

    #Write the preamble
    if OUTPUT_COMMENTS: gcode += linenumber() + ";Begin preamble\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += linenumber() + UNITS + "\n;End preamble\n\n"

    data_stats = {
        "Xmin": 10000,
        "Xmax": 0,
        "Ymin": 10000,
        "Ymax": 0,
        "Zmin": 10000,
        "Zmax": 0,
        "Xmin'": 10000,
        "Xmax'": 0,
        "Ymin'": 10000,
        "Ymax'": 0,
        "Zmin'": 10000,
        "Zmax'": 0
    }

    gcodebody = ""

    for obj in objectslist:
        parse(obj, data_stats, True)

    for obj in objectslist:

        #do the pre_op
        if OUTPUT_COMMENTS:
            gcodebody += linenumber() + ";Begin operation: " + obj.Label + "\n"
        for line in PRE_OPERATION.splitlines(True):
            gcodebody += linenumber() + line

        gcodebody += parse(obj, data_stats, False)

        #do the post_op
        if OUTPUT_COMMENTS:
            gcodebody += linenumber(
            ) + ";finish operation: " + obj.Label + "\n"

        for line in POST_OPERATION.splitlines(True):
            gcodebody += linenumber() + line

    precision_string = '.' + str(PRECISION) + 'f'

    if OUTPUT_COMMENTS:
        wassup = 'Origin: ' + (
            'Bottom Left' if not (CENTER_ORIGIN_X or CENTER_ORIGIN_Y) else
            'Centered') + (' (Swap X and Y)' if SWAP_XY else '') + (
                '\n;' +
                ('x offset: ' + format(OFFSET_X, '0.2f')) if OFFSET_X > 0 else
                '') + ('\n;' + ('y offset: ' + format(OFFSET_Y, '0.2f'))
                       if OFFSET_Y > 0 else '')
        print(wassup)

        gcode += ';' + wassup + '\n'

        for key in data_stats:
            if len(key) == 4:
                tkey = key
                if SWAP_XY:
                    if tkey[0] == 'X': tkey = 'Y' + tkey[1:4]
                    elif tkey[0] == 'Y': tkey = 'X' + tkey[1:4]
                wassup = tkey + ' is ' + format(
                    data_stats[key], precision_string) + ' ==> ' + format(
                        data_stats[key + "'"], precision_string)
                print(wassup)
                gcode += ";" + wassup + '\n'
            else:
                break

    if OUTPUT_COMMENTS: gcode += '\n;GCode Commands detected:\n\n'

    if OUTPUT_COMMENTS:
        for key in data_stats:
            if len(key) < 4:
                nottoolate = key + ' detected, count is ' + str(
                    data_stats[key])
                print(nottoolate)
                gcode += ";" + nottoolate + "\n"

    gcode += '\n'
    gcode += gcodebody
    gcodebody = ''

    #do the post_amble

    if OUTPUT_COMMENTS: gcode += ";Begin postamble\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    gfile = pythonopen(filename, "w")
    gfile.write(gcode)
    gfile.close()
Exemple #23
0
def export(objectslist, filename, argstring):
    processArguments(argstring)
    global UNITS
    global IP_ADDR
    for obj in objectslist:
        if not hasattr(obj, "Path"):
            FreeCAD.Console.PrintError("the object " + obj.Name + " is not a path. Please select only path and Compounds.\n")
            return

    FreeCAD.Console.PrintMessage("postprocessing...\n")
    gcode = ""

    # Find the machine.
    # The user my have overridden post processor defaults in the GUI.  Make
    # sure we're using the current values in the Machine Def.
    myMachine = None
    for pathobj in objectslist:
        if hasattr(pathobj,"MachineName"):
            myMachine = pathobj.MachineName
        if hasattr(pathobj, "MachineUnits"):
            if pathobj.MachineUnits == "Metric":
               UNITS = "G21"
            else:
               UNITS = "G20"
    if myMachine is None:
        FreeCAD.Console.PrintWarning("No machine found in this selection\n")

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble

    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    if IP_ADDR is not None:
        sendToSmoothie(IP_ADDR, final, filename)
    else:

        if not filename == '-':
            gfile = pythonopen(filename, "wb")
            gfile.write(final)
            gfile.close()

    FreeCAD.Console.PrintMessage("done postprocessing.\n")
    return final
Exemple #24
0
def export(objectslist, filename, argstring):
    processArguments(argstring)
    global UNITS
    global UNIT_FORMAT

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # fetch machine details
        job = PathUtils.findParentJob(obj)

        myMachine = 'not set'

        if hasattr(job, "MachineName"):
            myMachine = job.MachineName

        if hasattr(job, "MachineUnits"):
            if job.MachineUnits == "Metric":
                UNITS = "G21"
                UNIT_FORMAT = 'mm/min'
            else:
                UNITS = "G20"
                UNIT_FORMAT = 'in/min'

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: %s)\n" % obj.Label
            gcode += linenumber() + "(machine: %s, %s)\n" % (myMachine,
                                                             UNIT_FORMAT)
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: %s)\n" % obj.Label
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    # do the post_amble

    if OUTPUT_COMMENTS:
        gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "wb")
        gfile.write(final)
        gfile.close()

    return final
Exemple #25
0
def export(objectslist, filename, args):
    global UNITS
    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print "the object " + obj.Name + " is not a path. Please select only path and Compounds."
            return

    print "postprocessing..."
    gcode = ""

    #Find the machine.
    #The user my have overridden post processor defaults in the GUI.  Make sure we're using the current values in the Machine Def.
    myMachine = None
    for pathobj in objectslist:
        if hasattr(pathobj, "Group"):  #We have a compound or project.
            for p in pathobj.Group:
                if p.Name == "Machine":
                    myMachine = p
    if myMachine is None:
        print "No machine found in this project"
    else:
        if myMachine.MachineUnits == "Metric":
            UNITS = "G21"
        else:
            UNITS = "G20"

    # write header
    if OUTPUT_HEADER:
        gcode += linenumber() + "(Exported by FreeCAD)\n"
        gcode += linenumber() + "(Post Processor: " + __name__ + ")\n"
        gcode += linenumber() + "(Output Time:" + str(now) + ")\n"

    #Write the preamble
    if OUTPUT_COMMENTS: gcode += linenumber() + "(begin preamble)\n"
    for line in PREAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        #do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(begin operation: " + obj.Label + ")\n"
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        gcode += parse(obj)

        #do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber() + "(finish operation: " + obj.Label + ")\n"
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

    #do the post_amble

    if OUTPUT_COMMENTS: gcode += "(begin postamble)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line

    if SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print "done postprocessing."

    gfile = pythonopen(filename, "wb")
    gfile.write(gcode)
    gfile.close()
Exemple #26
0
def export(objectslist, filename, argstring):
    # pylint: disable=global-statement
    if not processArguments(argstring):
        return None
    global UNITS
    global UNIT_FORMAT
    global UNIT_SPEED_FORMAT
    global HORIZRAPID
    global VERTRAPID

    for obj in objectslist:
        if not hasattr(obj, "Path"):
            print("the object " + obj.Name +
                  " is not a path. Please select only path and Compounds.")
            return None

    print("postprocessing...")
    gcode = ""

    # write header
    if OUTPUT_HEADER:
        gcode += "%\n"
        gcode += ";\n"
        gcode += os.path.split(
            filename
        )[-1] + " (" + "FREECAD-FILENAME-GOES-HERE" + ", " + "JOB-NAME-GOES-HERE" + ")\n"
        gcode += linenumber() + "(" + filename.upper(
        ) + ",EXPORTED BY FREECAD!)\n"
        gcode += linenumber() + "(POST PROCESSOR: " + __name__.upper() + ")\n"
        gcode += linenumber() + "(OUTPUT TIME:" + str(now).upper() + ")\n"

    # Write the preamble
    if OUTPUT_COMMENTS:
        gcode += linenumber() + "(BEGIN PREAMBLE)\n"
    for line in PREAMBLE.splitlines(False):
        gcode += linenumber() + line + "\n"
    gcode += linenumber() + UNITS + "\n"

    for obj in objectslist:

        # Skip inactive operations
        if hasattr(obj, 'Active'):
            if not obj.Active:
                continue
        if hasattr(obj, 'Base') and hasattr(obj.Base, 'Active'):
            if not obj.Base.Active:
                continue

        # fetch machine details
        job = PathUtils.findParentJob(obj)

        myMachine = 'not set'

        if hasattr(job, "MachineName"):
            myMachine = job.MachineName

        if hasattr(job, "MachineUnits"):
            if job.MachineUnits == "Metric":
                UNITS = "G21"
                UNIT_FORMAT = 'mm'
                UNIT_SPEED_FORMAT = 'mm/min'
            else:
                UNITS = "G20"
                UNIT_FORMAT = 'in'
                UNIT_SPEED_FORMAT = 'in/min'

        if hasattr(job, "SetupSheet"):
            if hasattr(job.SetupSheet, "HorizRapid"):
                HORIZRAPID = Units.Quantity(job.SetupSheet.HorizRapid,
                                            FreeCAD.Units.Velocity)
            if hasattr(job.SetupSheet, "VertRapid"):
                VERTRAPID = Units.Quantity(job.SetupSheet.HorizRapid,
                                           FreeCAD.Units.Velocity)

        # do the pre_op
        if OUTPUT_COMMENTS:
            gcode += linenumber(
            ) + "(BEGIN OPERATION: %s)\n" % obj.Label.upper()
            gcode += linenumber() + "(MACHINE: %s, %s)\n" % (
                myMachine.upper(), UNIT_SPEED_FORMAT.upper())
        for line in PRE_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # get coolant mode
        coolantMode = 'None'
        if hasattr(obj, "CoolantMode") or hasattr(obj, 'Base') and hasattr(
                obj.Base, "CoolantMode"):
            if hasattr(obj, "CoolantMode"):
                coolantMode = obj.CoolantMode
            else:
                coolantMode = obj.Base.CoolantMode

        # turn coolant on if required
        if OUTPUT_COMMENTS:
            if not coolantMode == 'None':
                gcode += linenumber() + '(COOLANT ON:' + coolantMode.upper(
                ) + ')\n'
        if coolantMode == 'Flood':
            gcode += linenumber() + 'M8' + '\n'
        if coolantMode == 'Mist':
            gcode += linenumber() + 'M7' + '\n'

        # process the operation gcode
        gcode += parse(obj)

        # do the post_op
        if OUTPUT_COMMENTS:
            gcode += linenumber(
            ) + "(FINISH OPERATION: %s)\n" % obj.Label.upper()
        for line in POST_OPERATION.splitlines(True):
            gcode += linenumber() + line

        # turn coolant off if required
        if not coolantMode == 'None':
            if OUTPUT_COMMENTS:
                gcode += linenumber() + '(COOLANT OFF:' + coolantMode.upper(
                ) + ')\n'
            gcode += linenumber() + 'M9' + '\n'

    # do the post_amble
    if OUTPUT_COMMENTS:
        gcode += "(BEGIN POSTAMBLE)\n"
    for line in POSTAMBLE.splitlines(True):
        gcode += linenumber() + line
    gcode += "%\n"

    if FreeCAD.GuiUp and SHOW_EDITOR:
        dia = PostUtils.GCodeEditorDialog()
        dia.editor.setText(gcode)
        result = dia.exec_()
        if result:
            final = dia.editor.toPlainText()
        else:
            final = gcode
    else:
        final = gcode

    print("done postprocessing.")

    if not filename == '-':
        gfile = pythonopen(filename, "w")
        gfile.write(final)
        gfile.close()

    return final