134 lines
4.4 KiB
Plaintext
134 lines
4.4 KiB
Plaintext
__Name__ = "tableToObject"
|
|
__Comment__ =""
|
|
__Author__ = "2cv001"
|
|
__Title__ = "Macro Table to object"
|
|
__Date__ = "2024/02/01" #YYYY/MM/DD 18:55 Béta
|
|
__Version__ = __Date__
|
|
|
|
from PySide import QtGui
|
|
import FreeCAD as App
|
|
import FreeCADGui as Gui
|
|
|
|
|
|
|
|
dico={
|
|
"posx" : "Placement.Base.x",
|
|
"posy" : "Placement.Base.y",
|
|
"posz" : "Placement.Base.z",
|
|
"posX" : "Placement.Base.x",
|
|
"posY" : "Placement.Base.y",
|
|
"posZ" : "Placement.Base.z",
|
|
"angle": "Placement.Rotation.Angle",
|
|
"ang": "Placement.Rotation.Angle",
|
|
"axisx": "Placement.Rotation.Axis.x",
|
|
"axisy": "Placement.Rotation.Axis.y",
|
|
"axisz": "Placement.Rotation.Axis.z",
|
|
"axeX": "Placement.Rotation.Axis.x",
|
|
"axeY": "Placement.Rotation.Axis.y",
|
|
"axeZ": "Placement.Rotation.Axis.z",
|
|
"Length" : "Length",
|
|
"Length2" : "Length2",
|
|
"Radius" : "Radius",
|
|
"Height" : "Height",
|
|
"FirstAngle" : "FirstAngle",
|
|
"SecondAngle" : "SecondAngle",
|
|
"Angle1" : "Angle1",
|
|
"Angle2" : "Angle2",
|
|
"Angle3" : "Angle3",
|
|
}
|
|
|
|
def recomputeAll() :
|
|
for obj in FreeCAD.ActiveDocument.Objects:
|
|
obj.touch()
|
|
FreeCAD.ActiveDocument.recompute()
|
|
|
|
|
|
def set_constraint_expression(sketchName, constraint_name, expression):
|
|
# Obtenez le Sketch
|
|
sketch = App.ActiveDocument.getObject(sketchName)
|
|
|
|
# Parcourez toutes les contraintes dans le Sketch
|
|
for i in range(sketch.ConstraintCount):
|
|
# Obtenez la contrainte
|
|
constraint = sketch.Constraints[i]
|
|
|
|
# Vérifiez si le nom de la contrainte correspond à celui recherché
|
|
if constraint.Name == constraint_name:
|
|
# Modifiez la valeur de la contrainte
|
|
sketch.setExpression('Constraints[' + str(i)+ ']', expression)
|
|
break
|
|
|
|
|
|
def set_property_based_on_alias(sheet,cell):
|
|
|
|
# alias de la cellule
|
|
alias = sheet.getAlias(cell)
|
|
# Vérifier si l'alias est valide
|
|
if sheet.getContents(cell)!='' :
|
|
if alias is None:
|
|
print("La cellule sélectionnée " + cell + " n'a pas d'alias.")
|
|
return
|
|
|
|
# Séparer l'alias en nom du corps et nom de la propriété
|
|
parts = alias.split('_')
|
|
if len(parts) != 2:
|
|
print("L'alias doit être sous la forme 'BodyName_PropertyName'.")
|
|
return
|
|
|
|
objName, property_name = parts
|
|
#obj = App.ActiveDocument.getObject(objName)
|
|
obj=App.ActiveDocument.getObjectsByLabel(objName)[0]
|
|
|
|
try :
|
|
property_name=dico[property_name]
|
|
except :
|
|
try :
|
|
if obj.TypeId != 'Sketcher::SketchObject' :
|
|
print ('propriété ' + property_name + ' non trouvé dans le dico. Voir en haut du code source')
|
|
except :
|
|
pass
|
|
|
|
|
|
if obj is None:
|
|
print(f"Il n'y a pas d objet nommé '{objName}' dans le document.")
|
|
return
|
|
|
|
# Créer une expression qui fait référence à l'alias de la cellule
|
|
expression = f"<<{sheet.Label}>>.{alias}"
|
|
|
|
# Modifier la propriété de l'objet
|
|
# si c'est une contrainte
|
|
if obj.TypeId == 'Sketcher::SketchObject' :
|
|
set_constraint_expression(objName, property_name, expression)
|
|
return
|
|
|
|
if hasattr(obj, property_name.split('.')[0]): # Placement.Base.x -> Placement
|
|
obj.setExpression(property_name, expression)
|
|
else:
|
|
print(f"Le corps '{objName}' n'a pas de propriété '{property_name}'.")
|
|
|
|
|
|
def main() :
|
|
sheet = Gui.ActiveDocument.ActiveView.getSheet()
|
|
# Vérifiez si la feuille de calcul est active
|
|
if sheet.TypeId != 'Spreadsheet::Sheet':
|
|
print("Veuillez sélectionner des cellules dans une feuille de calcul.")
|
|
return
|
|
|
|
# cellule active
|
|
#cell = Gui.activeView().currentIndex()
|
|
|
|
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 cell in sel_items: # The selected cells are scanned
|
|
cellName=getCellName(cell.row(), cell.column())
|
|
|
|
if str(sheet.getContents(cellName))!= '' :
|
|
set_property_based_on_alias(sheet, cellName)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|