From a56ac75346fa0498f05871c00ab105269bfaa549 Mon Sep 17 00:00:00 2001 From: Simon Lefort Date: Mon, 5 Feb 2024 11:22:04 +0100 Subject: [PATCH] =?UTF-8?q?CreateAliasForTable=20:=20Une=20macro=20pour=20?= =?UTF-8?q?cr=C3=A9er=20facilement=20les=20alias=20dans=20un=20tableau=20?= =?UTF-8?q?=C3=A0=20deux=20dimensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- createAliasForTable.FCMacro | 143 ++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 createAliasForTable.FCMacro diff --git a/createAliasForTable.FCMacro b/createAliasForTable.FCMacro new file mode 100644 index 0000000..544e81c --- /dev/null +++ b/createAliasForTable.FCMacro @@ -0,0 +1,143 @@ +__Name__ = "Create alias for table" +__Comment__ ="" +__Author__ = "2cv001" +__Title__ = "Macro Create Alias For Table" +__Date__ = "2024/01/31" #YYYY/MM/DD 20:13 Béta +__Version__ = __Date__ +# https://forum.freecad.org/viewtopic.php?p=734928#p734928 + + + +import FreeCAD, FreeCADGui +from PySide import QtGui +import re + +addNumberIfOther=True + +def get_column(cell): + column = ''.join([c for c in cell if not c.isdigit()]) + return column + +def get_row(cell): + row = ''.join([c for c in cell if c.isdigit()]) + if row=='' : + row='1' + return int(row) + + + + +################################################################################# +# part code for alias +################################################################################# +# parameters for alias creation +separateur = " " # typically put " " so blanks will be replaced by nouveauCaract +nouveauCaract = '' #Put for example "_" to have the separators replaced by "_". Put "" to have no separator +majuscule = True #set to True if you want "Diametre du cercle" to be "DiametreDuCercle" +changeTexteCellule = False # the text will only be changed if changeCellText is True. + # This does not change anything for the allias itself +premierCaractereEnMinuscule = False # Force the first character to be in lower case + +# list of characters to be replaced by an equivalent. for example an @ will be replaced by 'a' +# if you add characters, please send me a private message. Il will eventually add it in my code. +caracEquivalents = [ ['é','e'],['è','e'],['ê','e'],['à','a'],['@','a'],['&','e'],['ç','c'], + ['²','2'],["'",''],['?',''],['"',''],['(',''],[')',''],['#',''],['.',''], + [',',''],[';',''],['$',''],['+',''],['-',''],['*',''],['/',''],['\\',''] , + ['[',''],[']',''], + ] + + + +def traitementChaineSource(chaineSource, separateur, nouveauCaract, majuscule): +# If separator is ' ' and nouveauCaract is '_', and majuscule is True +# transforms "Diametre du cylindre" into "Diametre_Du_Cylindre + + def remplaceCararcDansMot(mot): + + def remplaceCartatParEquivalent(caractere): + # replaces a character with its equivalent if it exists + caracResult = caractere + for couple in caracEquivalents: + if (couple[0] == caractere): + caracResult = couple[1] + break + return caracResult + + #replaces all characters of the word with its equivalent if it exists + motResult = mot + for caract in mot: + a = remplaceCartatParEquivalent(caract) + motResult = motResult.replace(caract, a) + return motResult + + + chaineResult = '' + first = True + carctDeSeparation = '' + for mots in chaineSource.split(separateur): + mots = remplaceCararcDansMot(mots) + if (not (first)): + carctDeSeparation = nouveauCaract + if (majuscule): + chaineResult = chaineResult + nouveauCaract + mots[:1].upper() + mots[1:] + # We use "[:1]" instead of "[0]", + # for no crash in case of an empty string (which happens if the cell is empty) + else: + chaineResult = chaineResult + nouveauCaract + mots + first=False + if premierCaractereEnMinuscule : + chaineResult = chaineResult[:1].lower() + chaineResult[1:] + return chaineResult + + +def create_alias_for_table_cells_Selected(): + mySpreadsheet = Gui.ActiveDocument.ActiveView.getSheet() + aw = Gui.getMainWindow().centralWidget().activeSubWindow() # Store the active window + # To get list of all selected cells + sel_items = aw.widget().findChild(QtGui.QTableView).selectedIndexes() + + getCellName = lambda r,c:'{}{}{}'.format(chr(c//26 + 64) if c//26 > 0 else '', chr(c%26 + 65), r + 1) + + + + for item in sel_items: # The selected cells are scanned + + if (sel_items[0].column()!=item.column() and sel_items[0].row()!=item.row()) : + cellName=getCellName(item.row(), item.column()) + firstcellName=getCellName(sel_items[0].row(), sel_items[0].column()) + cellTextLine=get_column(firstcellName)+str(get_row(cellName)) + cellTextColumn=get_column(cellName)+ str(get_row(firstcellName)) + try : + textLine = mySpreadsheet.get(cellTextLine) + textColumn = mySpreadsheet.get(cellTextColumn) + + except : + #print('Des cellules en première colonne ou en première ligne ne sont pas conformes') + continue + textAlias = str(textLine) + '_' + str(textColumn) + textAlias=traitementChaineSource(textAlias, separateur, nouveauCaract, majuscule) + if addNumberIfOther : + startIndexForTextAlias=1 + for i in range(startIndexForTextAlias, 201): + try: + if i == 1: + mySpreadsheet.setAlias(cellName, textAlias) + else: + mySpreadsheet.setAlias(cellName, textAlias + '_' + str(i-1)) + startIndexForTextAlias=i+1 + break # Arrête la boucle si aucune exception n'est levée + except: + continue # Passe à l'itération suivante si une exception est levée + + + + + + + +def main(): + create_alias_for_table_cells_Selected() + +if __name__ == '__main__': + main() +