##  GridSave.py Version 1.02

##  Copyright (c) 2006 Bruce Vaughan, BV Detailing & Design, Inc.

##  All rights reserved.

##  NOT FOR SALE. The software is provided "as is" without any warranty.

############################################################################

"""

    Save construction lines and circles to disk - compatible with GridLayout_v1.04 or gretaer

"""

def run_script():

    from macrolib.FileDefaults import job_Defaults_path, export_data, import_data, check_Defaults_dir

    from param import ResponseNotOK, Units, Dialog, Warning, dim, dim_print, Prompt, yes_or_no

    from macrolib.ExceptWarn import formatExceptionInfo

    from point import Point, PointLocate

    from job import JobName

    from cons_line import ConsLine

    from cons_circle import ConsCircle

    import os

    Units("feet")

    #####################################   

    def gridAdd(pt, dir, cons_color="Cyan"):

        # construction line begin

        cl2 = ConsLine()

        cl2.pt1 = pt

        cl2.angle = dir

        cl2.pen = cons_color

        cl2.add()

        # construction line end

    ################################

    def gridAddExplicit(pt1, pt2, cons_color="Cyan"):

        # construction line begin

        cl2 = ConsLine()

        cl2.pt1 = pt1

        cl2.pt2 = pt2

        cl2.pen = cons_color

        cl2.add()

        # construction line end

    ################################

    def arcAdd(pt, rad, cons_color="Red"):

        cc1 = ConsCircle()

        cc1.pt1 = pt

        cc1.radius = rad

        cc1.pen = cons_color

        cc1.add()

    ## Variables section

    # user save path

    job_default_file_path = os.path.join(job_Defaults_path(), "GridSave")

    # default values file name

    def_file = "GridSave.txt"

    # default to enable or disable the automatic importing and exporting of dialog dictionary variables

    # ("Enable", "Disable")

    enable_default_import_export = "Enable"

           

    # check if user defaults directory exists

    if not check_Defaults_dir(job_default_file_path):

        Warning("Job defaults directory does not exist.")

   

    ## Defaults section

    # ['Write', 'Append']

    write_or_append = 'Append'

    cons_color = "Cyan"

    conc_color = "Red"

    # ['Lines-2P', 'Lines-1P+Angle', 'Circles-2P', 'Circles-1P+Radius']

    lines_or_circles = "Lines-2P"

    circle_rad = 120.0

    line_angle = 45.0

    ########################################

    # Import default values file if enabled

    if enable_default_import_export == "Enable":

        dd0 = import_data(os.path.join(job_default_file_path, def_file))

        if dd0:

            for key, value in dd0.items():

                exec "%s = %s" % (key, repr(value)) in None

    ########################################  

    try:

        ## Dialog Box ##################

        dlg1 = Dialog("Add/Save Cons Lines and Circles")

        dlg1.group_title("File Name")

        dlg1.file_save('export_file', os.path.join(os.getcwd(), "jobs", JobName(), "macro", "Save1.txt"), "Enter file name or browse       ")

        dlg1.menu('write_or_append', ["Write", "Append"], write_or_append, "If file is existing, over'Write' or 'Append'")

       

        try:

            dd1 = dlg1.done()

        except ResponseNotOK:

            raise StandardError, 'User Cancel'

 

        try:

            if dlg1.write_or_append == "Write":

                f = open(dlg1.export_file, "w")

            else:

                f = open(dlg1.export_file, "a")

        except IOError, e:

            # unable to open file

            Warning("Unable to open file: %s" % (e))

            raise IOError

 

        while 1:           

            ## Dialog Box ######################

            dlg2 = Dialog("Create Cons Lines and Circles and Save")

            dlg2.group_title("Lines or Circles")

            dlg2.menu('lines_or_circles', ['Lines-2P', 'Lines-1P+Angle', 'Circles-2P', 'Circles-1P+Radius'], lines_or_circles, 'Add/Save cons lines or circles')

            dlg2.group_title("Line/Circle Options")

            dlg2.entry('circle_rad', dim_print(circle_rad), 'Initial Circle Radius                  ')

            dlg2.entry('line_angle', line_angle, 'Initial Angle of Construction Line')

            dlg2.menu('cons_color', ("Blue", "Green", "Yellow", "Magenta", "Red", "White", "Cyan"), "Cyan", "Construction line color")

            dlg2.menu('conc_color', ("Blue", "Green", "Yellow", "Magenta", "Red", "White", "Cyan"), "Red", "Construction circle color")

 

            try:

                dd2 = dlg2.done()

            except ResponseNotOK:

                f.close()

                break

 

            # Update local namespace

            for key, value in dd2.items():

                exec "%s = %s" % (key, repr(value)) in None

           

            # Auto export default values to disk if enabled

            if enable_default_import_export == "Enable":

                export_data(os.path.join(job_default_file_path, def_file), [dd1, dd2])

            #####################################

            if lines_or_circles == 'Lines-2P':

                while 1:

                    pt1 = PointLocate('Save Construction Line - Pick Point 1')

                    if pt1:

                        pt2 = PointLocate('Pick Point 2')

                        gridAddExplicit(pt1, pt2, cons_color)

                        f.write('ExplicitL: %0.4f, %0.4f, %0.4f : %0.4f, %0.4f, %0.4f\n' % (pt1.x, pt1.y, pt1.z, pt2.x, pt2.y, pt2.z))

                    else:

                        break

            elif lines_or_circles == 'Lines-1P+Angle':

                while 1:

                    pt1 = PointLocate('Save Construction Line - Pick Point 1')

                    if pt1:

                        line_angle = Prompt(line_angle, "Construction Line Angle")

                        gridAdd(pt1, line_angle, cons_color)

                        f.write('ExplicitA: %0.4f, %0.4f, %0.4f : %0.4f\n' % (pt1.x, pt1.y, pt1.z, line_angle))

                    else:

                        break

            elif lines_or_circles == 'Circles-2P':

                while 1:

                    pt1 = PointLocate('Save Construction Circle - Pick Center Point')

                    if not pt1:

                        break

                    else:

                        pt2 = PointLocate('Pick Radius Point')

                        circle_rad = pt1.dist(pt2)

                        arcAdd(pt1, circle_rad, conc_color)

                        f.write('ExplicitR: %0.4f, %0.4f, %0.4f : %0.4f\n' % (pt1.x, pt1.y, pt1.z, circle_rad))               

            else:

                while 1:

                    pt1 = PointLocate('Save Construction Circle - Pick Center Point')

                    if not pt1:

                        break

                    else:

                        circle_rad = Prompt(dim_print(circle_rad), "Circle Radius")

                        arcAdd(pt1, circle_rad, conc_color)

                        f.write('ExplicitR: %0.4f, %0.4f, %0.4f : %0.4f\n' % (pt1.x, pt1.y, pt1.z, circle_rad))

                       

            if not yes_or_no("Continue?"):

                f.close()

                break

       

    except:

        Warning(formatExceptionInfo())

## END run_script() ########################

if __name__ == '__main__':

    try:

        run_script()

    finally:

        del run_script