MathCAD Interface (pyfacade.pymcad)
Module pyfacade.pymcad provides two Classes working with MathCAD on implement level in both statical and
interactive way.
Class
Xmcd- .xmcd file parser.Class
Mathcad- MathCAD API.
- class pyfacade.pymcad.Xmcd(file_path)
Operation interface parsing and modifying xmcd file.
- Parameters:
file_path – str, full path of target xmcd file.
- property worksheet
Root element of the xmcd file. Read-only.
- property namespace
Name space used by the xmcd file. Read-only.
- property regions
Element <regions>, the main part of the xmcd file. Read-only.
- show_fulltree(levels=3, show_namespace=True)
Show the entire structure of xmcd file.
- Parameters:
levels – int, max. levels to be shown, count from root as level 0
show_namespace – bool, include name space as initial of element tag
- Returns:
None
- show_region(levels=3, show_namespace=True)
Show the sub-structure of element <regions>.
- Parameters:
levels – int, max. levels to be shown, count from element <regions> as level 0
show_namespace – bool, include name space as initial of element tag
- Returns:
None
- classmethod read_region(file_path)
Read all the <region> sub-elements from specified xmcd file to a list.
- Parameters:
file_path – str, full path of target xmcd file.
- Returns:
list of <region> sub-elements.
- static dfupdate(dataframe, repls, columns=None, remove=True)
A quick tool to replace string content in specified columns of data frame surrounded by Curly Braces
{}according to provided dict.- Parameters:
dataframe – a pandas.DateFrame
repls – dict, {‘replace_mark_1’: new_1, ‘replace_mark_2’: new_2,…}
columns – list of str, column names to be update, None for all columns.
remove – bool, remove the row which contains invalid replace mark.
- Returns:
tuple, (list of index of replaced row, list of index of unreplaced row)
- copyfrom(file_path, row_offset=24)
Copy all <region> sub-elements from a specified file to the end of current file.
- Parameters:
file_path – str, full path of target xmcd file.
row_offset – float, offset from the the last line of existing part to the first line of pasted part when presented in MathCAD Worksheet.
- Returns:
None
- addtext(text, row, col=30, width=164, border=False, highlight=False, bgc='inherit', tag='', lock_width=True, style='Normal', bold=False, italic=False, underline=False, color=None)
Insert a text region to current xmcd file.
- Parameters:
text – str, content to be added into xmcd file.
id – int, id of new text region.
row – float, target row location.
col – float, target column location.
width – float, width of text region, only valid when lock_width = True.
border – bool, show border of text region.
highlight – bool, highlight text region.
bgc – str, color name or hex code in lowercase of background, only valid when highlight = True.
tag – str, tag of region.
lock_width – bool, fix the width of text region.
style – str, name of pre-defined text style.
bold – bool, use bold font style.
italic – bool, use italic font style.
underline – bool, text with underline.
color – str, color name or hex code in lowercase for text.
- Returns:
A copy of inserted <region> element.
- addmath(var_name, row, col=204, border=False, highlight=False, bgc='inherit', tag='', expression=None, evaluate=False, unit=None)
Insert a math region to current xmcd file.
- Parameters:
var_name – str, name of variable.
id – int, id of new text region.
row – float, target row location.
col – float, target column location.
border – bool, show border of math region.
highlight – bool. highlight math region
bgc – str, color name or hex code in lowercase of background, only valid when highlight = True.
tag – str, tag of region.
expression – str, expression for variable definition. When expression is not provided, insert a math region of evaluation (if evaluate = True) or a individual math region of variable only (if evaluate = False)
evaluate – bool, in-line evaluate the variable.
unit – str, unit use to overwrite default unit in evaluating result.
- Returns:
A copy of inserted <region> element.
- addcompare(row, csign='<=', var_name1=None, var_name2=None, col=204, border=False, highlight=False, bgc='inherit', tag='', expression1=None, evaluate1=False, unit1=None, expression2=None, evaluate2=False, unit2=None)
Insert a special fusion region with 2 math sub-regions connected by a comparing symbol to current xmcd file.
- Parameters:
row – float, target row location.
csign – str, comparing operation.
var_name1 – str, name of variable at left-hand.
var_name2 – str, name of variable at right-hand.
col – float, target column location.
border – bool, show border of text region.
highlight – bool, highlight text region.
bgc – str, color name or hex code in lowercase for background, only valid when highlight = True.
tag – str, tag of region.
expression1 – str, expression for left-hand variable definition.
evaluate1 – bool, in-line evaluate the variable at left-hand.
unit1 – str, unit use to overwrite default unit in evaluating result at left-hand.
expression2 – str, expression for right-hand variable definition.
evaluate2 – bool. in-line evaluate the variable at right-hand.
unit2 – unit use to overwrite default unit in evaluating result at right-hand.
- Returns:
A copy of inserted <region> element.
- addsolve(conditionset, unknown_guess, row, spacing=24, txt_col=30, math_col=204, math_border=False, math_highlight=False, math_bgc='inherit', tag='', unit=None, txt_border=False, txt_highlight=False, txt_bgc='inherit', txt_bold=False, txt_italic=False, txt_underline=False, txt_color=None)
Insert a Solve Block to current xmcd file.
- Parameters:
conditionset – nested list, conditions of solving in the form of [[‘left-hand expression, ‘symbol’, right-hand expression], […],…] ‘symbol’ can be
'==','<','<=','>','>=','!='unknown_guess – dict, guesses of unknown variables. in the form of {‘variable_1: guess_1, ‘variable_2: guess_2, …}
row – float, start row location.
spacing – float, line spacing.
txt_col – float, target column location of text.
math_col – float, target column location of math expression.
math_border – bool, show border of math region.
math_highlight – bool, highlight math region.
math_bgc – str., color name or hex code in lowercase for math background, only valid when highlight = True.
tag – str, tag of region.
unit – str, unit use to overwrite default unit in evaluating result.
txt_border – bool, show border of text region.
txt_highlight – bool, highlight text region
txt_bgc – str, color name or hex code in lowercase for text background, only valid when highlight = True.
txt_bold – bool, use bold font style.
txt_italic – bool, use italic font style.
txt_underline – bool, text with underline.
txt_color – str. color name or hex code in lowercase for text.
- Returns:
None
- write(file_path)
save current xmcd as specified file
- Parameters:
file_path – str, path and the file name.
- Returns:
- make_contents(file_name, title_style, init_page=None, page_space=None, indent=0, prefix=None)
Create table of contents for current xmcd file and save it to specified file.
- Parameters:
file_name – str, path and name of target file to save the contents as.
title_style – list of str, style names of titles to be included in the contents.
init_page – int, start number of first page. If not given, will be auto-fetched from page settings of file.
page_space – float, height of one page. If not given, will be auto-fetched from page settings of file.
indent – int, numbers of ‘Space’ as indentations of sub-level contents. The level of a content is according to the index of its style in title_style list.
prefix – str, prefix of page numbers. If not given, will be auto-fetched from page settings of file.
- Returns:
None
- addblock_fst(fst_layout, loads, eccentricity, ply_mat, ply_thk, ply_edg=[0, 0], hole_type=['standard', 'standard'], pryedge=[0, 0], loadsubs=None, fastener='bolt', name='M6', grade='A4-70', packing=0, grib=0, alum_code=True, sec_num='1.1', sec_title=None, template=None)
Insert a group of regions for fastener checking to current xmcd file.
- Parameters:
fst_layout – 2d list of float, coordinates of fasteners in the form of [[u1, v1], [u2, v2],…], unit=mm.
loads – list of float, external load in form as N.mm [Fu, Fv, Fn, Mu, Mv, Mn], unit=N, mm.
eccentricity – list of float, distance from loading point to reference origin [du, dv, dn], unit=mm.
ply_mat – list of str, material name of connected parts [part1, part2].
ply_thk – list of float, thickness of connected parts, unit=mm.
ply_edg – list of float, edge distance on connected parts, unit=mm.
hole_type – list of str, hole type on connected parts.
pryedge – list of float, nominal lever arm for prying force about u and v, unit =mm.
loadsubs – list of float, external load for substitution if argument loads includes algebra.
fastener – str, general name of fastener to be shown in calculations.
name – str, fastener name/code.
grade – str, grade code of fastener.
packing – float, packing thickness, unit=mm.
grib – float, grib length, unit=mm.
alum_code – bool, verify fasteners according to BS8118 as fastener on aluminum member when True, or according to BS5950 as fastener on steel member when False.
sec_num – str, section number to be shown in title of inserted part.
sec_title – str, section title of inserted part.
template – str, file path of the template used to create the regions. If no template is specified, a default one will be searched from the package folder.
- Returns:
None
- addblock_lbcheck(section_elem, material='6063-T6', full_detail=False, avg_thk=True, sec_num='2.1.1', sec_title='Local Buckling Check', template=None)
Insert a group of regions for local buckling checking of aluminum member.
- Parameters:
section_elem – dict, elements data of a section in the form of {‘1’: {‘length’: .., ‘thks’: [..], ‘rp’: [..], ‘slope’: .., ‘bx’: [..], ‘by’: [..], ‘Ie’: .., ‘type’: ..}, ‘2’:{…} …}
material – str: material name.
full_detail – bool, show full calculation detail.
avg_thk – bool, get element thickness from average thickness of intersections when element is internal.
sec_num – str, section number to be shown in title of inserted part.
sec_title – str, section title of inserted part.
template – str, file path of the template used to create the regions. If no template is specified, a default one will be searched from the package folder.
- Returns:
None
- class pyfacade.pymcad.Mathcad(visible=True)
MathCAD Automation API
- Parameters:
visible – bool, show MathCAD Application UI after launching.
- property visible
bool, visibility of application.
- property filepath
str, default file path.
- worksheet(file_name=None)
Request a worksheet object with specified file name.
- Parameters:
file_name – str, name (and path) of requested xmcd file. When specified, the target worksheet will be firstly searched from files currently opened by application. If no valid worksheet is found, the function then try to load requested file according to specified name and path. Otherwise, if file_name is left None as default, the current active worksheet will be returned.
- Returns:
pymcad.Worksheetobject.
- sheetslist(fullname=False)
Get a name list of worksheet opened by application.
- Parameters:
fullname – bool, show full path of the worksheet.
- Returns:
list of str.
- closeall(quit=False, save=True, mute=True)
Close all the worksheets.
- Parameters:
quit – bool, quit the application after closing worksheets.
save – bool, save the worksheets before closing.
mute – bool, save automatically without asking for user confirmation.
- Returns:
None.
- class pyfacade.pymcad.Worksheet(mcsheet)
Interface with MathCAD worksheet.
Create a instance by calling
worksheetmethod ofpymcad.Mathcad.- property name
Name of the worksheet. Read-only.
- property path
Path of the worksheet file. Read-only.
- property fullname
The fully-qualified path to the worksheet. Read-only.
- property windowstate
State of the worksheet window.
0- The window is maximized.1- The window is minimized.2- The window is resizable.
- region(tag)
Request a region object in the worksheet by its tag name.
- Parameters:
tag – str, tag name of requested region.
- Returns:
pymcad.Regionobject.
- activate()
Activate the worksheet and bring it to the top of the application UI.
- Returns:
None.
- save(full_file_name=None)
Save the worksheet.
- Parameters:
full_file_name – str, path and full name to save the file as. If it is not provided, the file will be saved in-place.
- Returns:
None.
- getvalue(var_name)
Read the last value of a variable in worksheet.
- Parameters:
var_name – str, name of variable.
- Returns:
- str if the value of variable is a string.float if the value of variable is a real number.complex if the value of variable is a complex.numpy.ndarray if the value of variable is a matrix.
- setvalue(var_name, value)
Set Initial value of a variable.
Warning
value set by this way could NOT be saved.
- Parameters:
var_name – str, name of variable.
value – float, string, or array-like.
- Returns:
None.
- recalculate()
Re-calculate the worksheet.
- Returns:
None.
- scrollto(coordinate_x, coordinate_y)
Scroll the window of worksheet to specified location
- Parameters:
coordinate_x – float, X-coordinate to scroll the window to.
coordinate_y – float, y-coordinate to scroll the window to.
- Returns:
None.
- printout()
Print the work sheet by default print setting.
- Returns:
None.
- close(save=True, mute=True)
Close the worksheet.
- Parameters:
save – bool, save the worksheet before closing.
mute – bool, save automatically without asking for user confirmation.
- Returns:
None.
- class pyfacade.pymcad.Region(mcregion, wnd)
Interface with region in MathCAD worksheet.
Create a instance by calling
regionmethod ofpymcad.Worksheet.’- property x
X-coordinate of the region. Read-only.
- property y
Y-coordinate of the region. Read-only.
- property tag
tag name of the region. Read-only.
- property type
Type of the region. Read-only.
0 - Text region.1 - Math region.2 - Bitmap region.3 - Metafile region.4 - OLE object region.
- property xml
XML data of the region. Only applicable to Math Region
- property errmsg
Error message of the region. Read-only.
- locate()
Locate window to the region.
- Returns:
None
Table of Color Name
Below are strings of Color Name recognized by method addtext, addmath, addcompare, addsolve of
class pymcad.Xmcd, and the corresponding Hex Code .
Color Name |
Hex Code |
|---|---|
‘red’ |
#ff0000 |
‘maroon’ |
#800000 |
‘pink’ |
#ff8080 |
‘violet’ |
#ff80ff |
‘magenta’ |
#ff00ff |
‘orange’ |
#ff8000 |
‘yellow’ |
#ffff80 |
‘lime’ |
#80ff80 |
‘green’ |
#00ff00 |
‘aqua’ |
#80ffff |
‘blue’ |
#0000ff |
‘gray’ |
#c0c0c0 |