-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEasyAlias.FCMacro
175 lines (149 loc) · 4.87 KB
/
EasyAlias.FCMacro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
import FreeCAD
import re
from PySide import QtGui
"""
EasyAlias.FCMacro.py
This macro can be used to easily create aliases based on the contents of selected spreadsheet
cells in the previous column. As an example, suppose you wish to have the following:
A1: content = 'radius', B1: content = '5', alias = 'radius'
A2: content = 'height', B1: content = '15', alias = 'height'
The traditional way to set this up would be:
Select A1
Enter radius
Select B1
Enter 5
Right-click B1
Select properties
Select Alias
Enter radius
click OK
Select A2
Enter height
Select B2
Enter 15
Right-click B2
Select Properties
Select Alias
Enter height
Click OK
Using this macro, the work flow becomes:
Select A1
Enter radius
Select B1
Enter 5
Select A2
Enter height
Select B2
Enter 15
Select A1 through A2
Run the EasyAlias macro
Done
"""
__title__ = "EasyAlias"
__author__ = "TheMarkster"
__url__ = "https://wiki.freecadweb.org/Macro_EasyAlias"
__Wiki__ = "https://wiki.freecadweb.org/Macro_EasyAlias"
__date__ = "2022.07.31" #year.month.date
__version__ = __date__
CELL_ADDR_RE = re.compile(r"([A-Za-z]+)([1-9]\d*)")
CUSTOM_ALIAS_RE = re.compile(r".*\((.*)\)")
MAGIC_NUMBER = 64
REPLACEMENTS = {
" ": "_",
".": "_",
"ä": "ae",
"ö": "oe",
"ü": "ue",
"Ä": "Ae",
"Ö": "Oe",
"Ü": "Ue",
"ß": "ss"
}
def getSpreadsheets():
"""
Returns a set of selected spreadsheets in the active document or None if none is selected.
:returns: a set of selected spreadsheets in the active document or None if none is selected
:rtype: set
"""
spreadsheets = set()
for selectedObject in Gui.Selection.getSelection():
if selectedObject.TypeId == 'Spreadsheet::Sheet':
spreadsheets.add(selectedObject)
elif selectedObject.TypeId == "App::Link":
linkedObject = selectedObject.LinkedObject
if linkedObject.TypeId == 'Spreadsheet::Sheet':
spreadsheets.add(linkedObject)
return spreadsheets
# The original implementatin of a1_to_rowcol and rowcol_to_a1 can be found here:
# https://github.com/burnash/gspread/blob/master/gspread/utils.py
def a1_to_rowcol(label:str):
"""Translates a cell's address in A1 notation to a tuple of integers.
:param str label: A cell label in A1 notation, e.g. 'B1'. Letter case is ignored.
:returns: a tuple containing `row` and `column` numbers. Both indexed from 1 (one).
:rtype: tuple
Example:
>>> a1_to_rowcol('A1')
(1, 1)
"""
match = CELL_ADDR_RE.match(label)
row = int(match.group(2))
column_label = match.group(1).upper()
column = 0
for i, c in enumerate(reversed(column_label)):
column += (ord(c) - MAGIC_NUMBER) * (26**i)
return (row, column)
def rowcol_to_a1(row:int, column:int):
"""Translates a row and column cell address to A1 notation.
:param row: The row of the cell to be converted. Rows start at index 1.
:type row: int, str
:param col: The column of the cell to be converted. Columns start at index 1.
:type row: int, str
:returns: a string containing the cell's coordinates in A1 notation.
Example:
>>> rowcol_to_a1(1, 1)
A1
"""
row = int(row)
column = int(column)
dividend = column
column_label = ""
while dividend:
(dividend, mod) = divmod(dividend, 26)
if mod == 0:
mod = 26
dividend -= 1
column_label = chr(mod + MAGIC_NUMBER) + column_label
label = "{}{}".format(column_label, row)
return label
def textToAlias(text:str):
# support for custom aliases between parentheses
match = CUSTOM_ALIAS_RE.match(text)
if match:
return match.group(1)
for character in REPLACEMENTS:
text = text.replace(character,REPLACEMENTS.get(character))
return text
def main():
spreadsheets = getSpreadsheets()
if not spreadsheets:
QtGui.QMessageBox.critical(None, "Error",
"No spreadsheet selected.\nPlease select a spreadsheet in the tree view.")
return
for spreadsheet in spreadsheets:
for selectedCell in spreadsheet.ViewObject.getView().selectedCells():
contents = spreadsheet.getContents(selectedCell)
if contents:
alias = textToAlias(contents)
row, column = a1_to_rowcol(selectedCell)
nextCell = rowcol_to_a1(row, column + 1)
try:
spreadsheet.setAlias(nextCell, alias)
except:
QtGui.QMessageBox.critical(None, "Error",
"Unable to set alias <i>" + alias + "</i> at cell " + nextCell +
"<br>in spreadsheet <i>" + spreadsheet.FullName + "</i>." +
"<br><br><b>Remember, aliases cannot begin with a numeral or an " +
"underscore or contain any invalid characters.</b>")
App.ActiveDocument.recompute()
main()