##  TextDXF.py Version 1.02

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

##  All rights reserved.

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

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

'''

parseDXFtext(file_name) - 'file_name' is the name of the DXF file to parse.

                          The function returns a list of text strings.

sumStuds(textList) - Summarize the stud counts found in the argument list

                     Stud count string format: [36] or [14,2,2,14]

 

Example usage:

    file_name = r'C:\SDS2_7.0\macro\New Versions\Ref\plan.dxf'

    stud_count = sumStuds(parseDXFtext(file_name))

 

Version 1.01 (6/27/07)

Version 1.02 (12/21/07) Modify parseDXFtext to read 2000 version DXF files

 

*** WATCH OUT FOR MEMBER NUMBERS IN PLACE OF PIECEMARKS ***

Member numbers begin with an open bracket '['

'''

 

def parseDXFtext(fn):

    f = open(fn)

 

    # skip to entities section

    s = f.next()

    while s.strip() != 'ENTITIES':

        s = f.next()

 

    inText = False

 

    textList = []

   

    for line in f:

        # In ENTITIES section, iteration can cease when ENDSEC is reached

        if line.strip() == 'ENDSEC':

            break

       

        elif inText == True:

            dd = dict.fromkeys(['  1\n',], '0')

            while line != '  0\n':

                #if line.strip() in dd:

                if line in dd:

                    dd[line.strip()] = f.next().strip()

                line = f.next()

            textList.append(dd['1'])

            inText = False

 

        else:

            if line.strip() == 'TEXT':

                inText = True

 

    f.close()

 

    return textList

 

def sumStuds(textList):

    stud_count = 0

    errorList = []

    for text in textList:

        try:

            stud_count += sum([int(i) for i in text.lstrip('[').rstrip(']').split(',') if text.startswith('[')])

        except:

            errorList.append(text)

    if errorList:

        print 'Invalid text entr%s:\n%s' % (('y', 'ies')[len(errorList)>1 or 0], '\n'.join(errorList))

    return stud_count

 

if __name__ == '__main__':

    from macrolib.dirEntries import dirEntries

 

    # Calculate the total number of studs found in all the DXF files in a specific directory

    dirName = r'H:\Dxf\studcount'

    dirName = r'H:\Dxf'

    studCnt = 0

    for fn in dirEntries(dirName, False, 'dxf', 'DXF'):

        sheetCnt = sumStuds(parseDXFtext(fn))

        studCnt += sheetCnt

        print 'There were %d studs counted in file %s' % (sheetCnt, fn)

 

    print '\nTotal number of shear connector studs counted: %d' % studCnt

    '''

    fn = r'H:\Dxf\6TH_FLOOR_FRAMING_PLAN_2000.dxf'

    fn = r'H:\Dxf\6TH_FLOOR_FRAMING_PLAN.dxf'

    textList = parseDXFtext(fn)

    print '\n'.join(textList)

    '''

''' Toringdon VI

>>> There were 0 studs counted in file H:\Dxf\studcount\E1001.dxf

There were 0 studs counted in file H:\Dxf\studcount\E1002.dxf

There were 0 studs counted in file H:\Dxf\studcount\E1003.dxf

There were 0 studs counted in file H:\Dxf\studcount\E1004.dxf

There were 0 studs counted in file H:\Dxf\studcount\E1005.dxf

There were 4530 studs counted in file H:\Dxf\studcount\E101.dxf

There were 4284 studs counted in file H:\Dxf\studcount\E102.dxf

There were 4288 studs counted in file H:\Dxf\studcount\E201.dxf

There were 714 studs counted in file H:\Dxf\studcount\E202.dxf

There were 0 studs counted in file H:\Dxf\studcount\E203.dxf

There were 0 studs counted in file H:\Dxf\studcount\E901.dxf

There were 0 studs counted in file H:\Dxf\studcount\E902.dxf

 

Total number of shear connector studs counted: 13816

'''

 

''' Corporate Center IV

>>> There were 3770 studs counted in file H:\Dxf\studcount\E101.dxf

There were 4418 studs counted in file H:\Dxf\studcount\E102.dxf

There were 0 studs counted in file H:\Dxf\studcount\E103.dxf

There were 3974 studs counted in file H:\Dxf\studcount\E501.dxf

There were 3974 studs counted in file H:\Dxf\studcount\E502.dxf

There were 3974 studs counted in file H:\Dxf\studcount\E503.dxf

There were 3974 studs counted in file H:\Dxf\studcount\E901.dxf

There were 4157 studs counted in file H:\Dxf\studcount\E902.dxf

There were 137 studs counted in file H:\Dxf\studcount\E903.dxf

There were 128 studs counted in file H:\Dxf\studcount\E904.dxf

There were 0 studs counted in file H:\Dxf\studcount\E905.dxf

 

Total number of shear connector studs counted: 28506

>>>

'''

 

'''

fn = r'H:\Dxf\6TH_FLOOR_FRAMING_PLAN.dxf'

studCnt = sumStuds(parseDXFtext(fn))

print stud_count

'''

 

'''

>>> 3962

'''