#
# Copyright (c) 2009 Matteo Boscolo
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the termscl_bo of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#

import os.path
#import fastcreat.py
import Tools

class ExtFormat:
    """
        This class provide base class for hendly different drawing format in pythoncad
    """
    def __init__(self,gtkImage):
        """
            Default Constructor
        """
        self.__Tool=gtkImage.getTool()
        self.__GtkImage=gtkImage
    def openFile(self,fileName):
        """
           Open a generic file 
        """
        path,exte=os.path.splitext( fileName )
        if( exte.upper()==".dxf".upper()):
            dxf=Dxf(gtkImage,fileName)

class DrawingFile:
    """
        This Class provide base capability to read write a  file
    """
    def __init__(self,fileName):
        """
            Base Constructor
        """
        self.__fn=fileName
        self.__fb=None
    def ReadAsci(self):
        """
            Read a generic file 
        """
        self.__fb=open(self.__fn,'r')
    def FileObject(self):
        """
            Return the file opened 
        """
        if(self.__fb!=None): return self.__fb
        else: return None

class Dxf(DrawingFile):
    """
        this class provide dxf reading/writing capability 
    """
    def __init__(self,gtkImage):
        """
            Default Constructor
        """
        self.__gtkI=gtkImage
    def OpenFile(fileName):
        """
            Open The file and create The entity in pythonCad
        """
        self.ReadAsci();
        fo=self.FileObject()
        if(fo!=None):
            line = fo.readline()
            """Implement here your methon"""
#           if not line : break
#           else:
            k = line
            if k[0:4] == 'LINE':
                # indicates the data of lines are there in next block of 19 lines
                g = 0 # start counter to read lines
                while g < 19:
                    line = fo.readline()
                    k = line
                    """if g == 5:# this line contains color property
                    we can get the color presently not implemented
                    color=(float(k[0:-1]))"""
                    if g == 7:
                       # this line of file contains start point"X" co ordinate
                        x1 = (float(k[0:-1]))
                    if g == 9:# this line of file contains start point "Y" co ordinate
                        y1 = (float(k[0:-1]))
                    if g == 11:# this line of file contains start point "Z" co ordinate
                        z1 = (float(k[0:-1])) 
                            # Z co ordinates are not used in PythonCAD we can live without this line
                    if g == 15:# this line of file contains End point "X" co ordinate
                        x2 = (float(k[0:-1]))
                    if g == 17:# this line of file contains End point "Y" co ordinate
                        y2 = (float(k[0:-1]))
                    if g == 18:# this line of file contains End point "Z" co ordinate
                        z2 = (float(k[0:-1]))
                            #Z co ordinates are not used in PythonCAD we can live without this line
                        'I need a "create segment code" here to append the segment to image'
                g = g + 1# Search for next line in the dxf file
# Similar subroutine can be return for collecting data of circle/polyline/arc
    
