Macro Alias Manager

This macro helps managing aliases inside FreeCAD Spreadsheet workbench. It is able to:
 * create aliases based on first column (A column)
 * delete aliases placed on a column
 * move aliases from one column to another one
 * create a "part family", that's it, create different files for each column in a range. It will add to the original name a suffix based on first row (1)

More information might be found on FreeCAD forums:

How to use
- pending -

Limitations
- pending -

The script
QtCore.Qt.AlignTop)

okbox = QtGui.QDialogButtonBox(self.dialog) okbox.setOrientation(QtCore.Qt.Horizontal) okbox.setStandardButtons(QtGui.QDialogButtonBox.Close) okbox.setFocus

grid2 = QtGui.QGridLayout grid2.setSpacing(10) grid2.addWidget(info, 0, 0) grid2.addWidget(okbox, 1, 0)

self.dialog.setLayout(grid2)

QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected"), self.close) QtCore.QMetaObject.connectSlotsByName(self.dialog) self.dialog.show self.dialog.exec_

def close(self): self.dialog.close


 * 1) ===== Main code ========================================
 * 1) ===== Main code ========================================

alphabet_list = list(string.ascii_uppercase)
 * 1) ===== Global variables ==============================================

class p:   def aliasManager(self): try:

mode = self.d1.currentText column_from = self.d2.currentText column_to = self.d3.currentText row_from = self.d4.value row_to = self.d5.value
 * 1) ===== Variables ==============================================

if mode == "Set aliases": for i in range(row_from,row_to+1): cell_from = 'A' + str(i) cell_to = str(column_from) + str(i) App.ActiveDocument.Spreadsheet.setAlias(cell_to, '') App.ActiveDocument.Spreadsheet.setAlias(cell_to, App.ActiveDocument.Spreadsheet.getContents(cell_from)) App.ActiveDocument.recompute
 * 1) ===== Mode - Set ==============================================

FreeCAD.Console.PrintMessage("\nAliases set\n")

elif mode == "Clear aliases": for i in range(row_from,row_to+1): cell_to = str(column_from) + str(i) App.ActiveDocument.Spreadsheet.setAlias(cell_to, '') App.ActiveDocument.recompute
 * 1) ===== Mode - Clear ==============================================

FreeCAD.Console.PrintMessage("\nAliases cleared\n")

elif mode == "Move aliases":
 * 1) ===== Mode - Move ==============================================

self.d3.setDisabled(False) for i in range(row_from,row_to+1): cell_reference = 'A'+ str(i) cell_from = column_from + str(i) cell_to = column_to + str(i) App.ActiveDocument.Spreadsheet.setAlias(cell_from, '') App.ActiveDocument.recompute App.ActiveDocument.Spreadsheet.setAlias(cell_to, App.ActiveDocument.Spreadsheet.getContents(cell_reference)) App.ActiveDocument.recompute FreeCAD.Console.PrintMessage("\nAliases moved\n")

elif mode == "Generate part family": doc = FreeCAD.ActiveDocument if not doc.FileName: FreeCAD.Console.PrintError('\nMust save project first\n') docDir, docFilename = os.path.split(doc.FileName) filePrefix = os.path.splitext(docFilename)[0]
 * 1) ===== Mode - Generate part family ==============================================

def char_range(c1, c2): """Generates the characters from `c1` to `c2`, inclusive.""" for c in xrange(ord(c1), ord(c2)+1): yield str.capitalize(chr(c))

fam_range = [] for c in char_range(str(column_from), str(column_to)): fam_range.append(c)

for index in range(len(fam_range)): # set aliases for i in range(row_from,row_to+1): cell_reference = 'A' + str(i) cell_from = str(fam_range[index-1]) + str(i) cell_to = str(fam_range[index]) + str(i) App.ActiveDocument.Spreadsheet.setAlias(cell_from, '') App.ActiveDocument.recompute App.ActiveDocument.Spreadsheet.setAlias(cell_to, App.ActiveDocument.Spreadsheet.getContents(cell_reference)) App.ActiveDocument.recompute sfx = str(fam_range[index]) + '1'

# save file suffix = App.ActiveDocument.Spreadsheet.getContents(sfx) filename = filePrefix + '_' + suffix + '.fcstd' filePath = os.path.join(docDir, filename) FreeCAD.Console.PrintMessage("\nSaving view to %s\n" % filePath) App.getDocument(filePrefix).saveAs(filePath)

# Clear last aliases created: for j in range(row_from,row_to+1): cell_to = str(column_to) + str(j) App.ActiveDocument.Spreadsheet.setAlias(cell_to, '') App.ActiveDocument.recompute

# Turn working file to original naming: filename = filePrefix + '.fcstd' filePath = os.path.join(docDir, filename) FreeCAD.Console.PrintMessage("\nSaving original view to %s\n" % filePath) App.getDocument(filePrefix).saveAs(filePath) FreeCAD.Console.PrintMessage("\nPart family files generated\n")

else: FreeCAD.Console.PrintError("\nError or 'TODO'\n")
 * 1) ===== If errors ==============================================

except: FreeCAD.Console.PrintError("\nUnable to complete task\n") self.close

def close(self): self.dialog.hide

def __init__(self): infoIcon = ['16 16 3 1', '  c None', '+ c #444444', '. c #e6e6e6', '    ......    ',                '   ..........  ',                '  ......++.... ',                ' .......++.....',                ' ..............',                '.....+++++......',                '....+++++.......',                '.......++.......',                '.......++.......',                '.......+........',                '......++........',                ' .....++.+.....',                ' .....++++.....',                '  .....++..... ',                '   ..........  ',                '     ......    ']
 * 1) ===== GUI menu ========================================
 * 1) ===== GUI menu ========================================

# Hide/show "to column" label and spinbox based on mode type def disableWidget(currentIndex): if self.d1.currentText == "Set aliases": iN3.hide self.d3.setEnabled(False) self.d3.hide elif self.d1.currentText == "Clear aliases": iN3.hide self.d3.setEnabled(False) self.d3.hide else: iN3.show self.d3.setEnabled(True) self.d3.show

self.dialog = None self.dialog = QtGui.QDialog self.dialog.resize(400,140) self.dialog.setWindowTitle("Alias manager") iN1 = QtGui.QLabel("mode:") iN1.setAlignment(QtCore.Qt.AlignRight