# -*- coding: UTF-8 -*-
# Copyright 2015-2022 - RoboDK Inc. - https://robodk.com/
#
# This file loads the compiled version of the RoboDK post processor for:
#   ABB RAPID S4C robot controllers
# 
# More information about RoboDK Post Processors and Offline Programming:
#     https://robodk.com/help#PostProcessor
#     https://robodk.com/doc/en/PythonAPI/postprocessor.html
# ----------------------------------------------------

import sys
import os

#Needed to make the robodk generated code work
import math
from robodk import *

# Detect Python version and post processor
print("Using Python version: " + str(sys.version_info))
path_app = os.path.dirname(__file__).replace(os.sep,"/")
print("RoboDK Post Processor: " + path_app)

# Check if the post is compatible with the Python version
version_str = str(sys.version_info[0]) + str(sys.version_info[1])
path_library = path_app + '/v' + version_str
if not os.path.isdir(path_library):
    msg = "Invalid Python version or post processor not found. Make sure you are using a supported Python version: " + path_library
    msg += "\nSelect Tools-Options-Python and select a supported Python version"
    print(msg)
    raise Exception(msg)

# Load the post processor
exec("from v" + version_str + ".ABB_RAPID_S4C import RobotPost as BasePost")

class RobotPost(BasePost):
    """Robot post object"""
    #------------------------ Customize your post using the following variables ----------------------
    # Set the program file extension:
    #PROG_EXT = 'mod'  # IRC5 (newer controllers)
    PROG_EXT = "prg"    # S4 (older controllers)

    # Set if we want to generate the main/first program as a Main() program. The name of the main/first program will be replaced by Main()
    # Example: PROC Main() instead of PROG Prog1()
    #FIRST_PROG_AS_MAIN = False # It will generate PROG Prog1() (or the name set in the RoboDK program)
    FIRST_PROG_AS_MAIN = True    # It will generate PROG Main()

    # Default maximum number of lines per program. If a program exceeds this value it will then generate multiple "pages" (files)
    # This value can also be set in Tools-Options-Program-Maximum number of lines per program.
    #MAX_LINES_X_PROG = 20000  # recommended for IRC5: 20000
    MAX_LINES_X_PROG = 5000    # recommended for S4:  5000

    # Include subprograms in the main module:
    # Set to True to include sub programs in the same module
    INCLUDE_SUB_PROGRAMS = True    

    # Specify the maximum lines of code allowed to include a subprogram in the main/first program
    # If a subprogram exceeds this number of lines of code it will be generated as a separate module
    MAX_SUBPROG_LINES = 500    

    # External dripfeed: set to True if you use an external tool to load the programs (such as RAPBOX):
    #EXTERNAL_DRIPFEEDER = True # It will not generate a main program to load subprograms. Each subprogram will be called Main(). This is suitable for RAPBOX.
    EXTERNAL_DRIPFEEDER = False    # It will generate a main program to load subprograms.

    # Remote path to place programs in the robot controller
    # When program splitting takes place we need this path to load programs on the fly
    RAPID_REMOTE_PATH = "/hd0a/Enter-Serial-Number/HOME/RoboDK"    

    # Set if you want to ignore the setup of the turntable (or external axis) on the controller
    # If you set it to True it means the controller will not be aware of the axis
    # (you can't move using a synchronized movement and the turntable will not hold the wobjdata)
    TURNTABLE_IGNORE = False    # default: False

    # Specify the mechanical unit name for linear track and/or turntable, if required.
    # This name will be added to the wobjdata variable
    #MECHANICAL_UNIT_NAME = 'T6003'
    #MECHANICAL_UNIT_NAME = 'STN1'
    MECHANICAL_UNIT_NAME = "Turntable_Mechanical_Unit_Name"    

    # Enter the axes ratio for external axes, if required
    #AXES_RATIO = [1,1,1,1,1,1,  -1,1,1]
    AXES_RATIO = None    

    # Specify the mechanical unit name for linear track and/or turntable, if required.
    # This name will be added to the wobjdata variable
    # MECHANICAL_UNIT_NAME = 'T6003'
    # MECHANICAL_UNIT_NAME = 'STN1'
    MECHANICAL_UNIT_NAME = "Turntable_Mechanical_Unit_Name"    

    # Enter the axes ratio for external axes, if required
    #AXES_RATIO = [1,1,1,1,1,1,  -1,1,1]
    AXES_RATIO = None    

    # Default speed
    SPEED_MMS = 500    

    # Default speed variable
    # Example: [500,500,5000,1000]
    SPEEDDATA = "v500"    

    # Default rounding
    ZONEDATA = "z1"    

    # Default tool
    TOOLDATA = "tool0"    

    # Default reference
    WOBJDATA = "wobj0"    
    
    def setDO(self, io_var, io_value):
        """Set a Digital Output"""
        if type(io_var) != str:  # set default variable name if io_var is a number
            io_var = 'DO_%02i' % io_var
        if type(io_value) != str: # set default variable value if io_value is a number            
            if io_value > 0:
                io_value = '1'
            else:
                io_value = '0'
        
        # at this point, io_var and io_value must be string values
        self.addline('SetDO %s, %s;' % (io_var, io_value))

    pass

if __name__== "__main__":
    exec("from v" + version_str + ".ABB_RAPID_S4C import test_post")
    test_post()

