Back to SDS/2 Parametric Scripts

 

##  xmcdParse.py Version 1.01 (module macrolib.xmcdParse)

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

##  All rights reserved.

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

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

"""

Parse a Mathcad file, extract variable names and data

Look for:

<region

    <ml:id                  variable name, need to get subscript if present

    <ml:apply>              a value is applied to the variable name

        <ml:real>           in the sample file, all values are floats

           

Value units are not parsed

 

Version History:

Version 1.00 - Initial release (2/20/07)

Version 1.01 - Modify get_text()

               Eliminate function indexList()

"""

import re

 

def subscript(line):

    if 'subscript' in line:

        return line[line.find('subscript')+11:line.find('>')-1]

    else:

        return ""

   

def get_text(line):

    return re.split('[<>]', line)[2]+ subscript(line)

 

def formatXMCDdata(f):

    dataDict = {}

    in_region = False

    in_apply = False

    key = False

    value = False

    for line in f:

        line = line.strip()

       

        if in_region == False:

            if line.startswith('</region>'):

                in_region = False

            elif line.startswith('<region '):

                in_region = True

               

        elif in_region == True and in_apply == False:

 

            if line.startswith('<inlineAttr'):

                in_region = False

               

            elif line.startswith('</region'):

                in_region = False

               

            elif line.startswith('<ml:id'):

                key = get_text(line)

               

            elif key:

                if line.strip().startswith('<ml:apply>'):

                    in_apply = True

                elif line.startswith('<ml:real>'):

                    value = float(get_text(line))

           

        elif in_region == True and in_apply == True:

            if line.startswith('<ml:real>'):

                value = float(get_text(line))

               

        if key and value:

            dataDict[key] = value

            in_region = False

            in_apply = False

            key = False

            value = False

   

    return dataDict

 

def run_script():

    f = open('C:\\SDS2_7.0\\macro\\Work In Progress\\XML\AX-01.xmcd', 'r')

    dd = formatXMCDdata(f)

    f.close

    keyList = dd.keys()

    keyList.sort()

    for key in dd:

        exec "%s = %s" % (key, dd[key]) in None

        print "%s = %s" % (key, eval(key))

    print

    print dd

    print

    print Bolts*Shear

    print

    import pprint

    pprint.pprint(dd)

 

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

if __name__ == '__main__':

    try:

        run_script()

    finally:

        del run_script

 

"""

>>> Tension = 25.0

Bolts = 8.0

leg = 4.0

Fy = 36.0

kip = 1000.0

gage = 5.5

p = 3.0

tw = 0.305

tf = 0.5

ksi = 1.0

Shear = 95.0

d = 0.875

 

{'Tension': 25.0, 'Bolts': 8.0, 'leg': 4.0, 'Fy': 36.0, 'kip': 1000.0, 'gage': 5.5, 'p': 3.0, 'tw': 0.30499999999999999, 'tf': 0.5, 'ksi': 1.0, 'Shear': 95.0, 'd': 0.875}

 

760.0

 

{'Bolts': 8.0,

 'Fy': 36.0,

 'Shear': 95.0,

 'Tension': 25.0,

 'd': 0.875,

 'gage': 5.5,

 'kip': 1000.0,

 'ksi': 1.0,

 'leg': 4.0,

 'p': 3.0,

 'tf': 0.5,

 'tw': 0.30499999999999999}

>>>

"""