This commit is contained in:
2024-02-05 11:45:55 +01:00
parent eb5c9560b2
commit 882a5a2c6c

View File

@@ -2,7 +2,7 @@ __Name__ = "tableToObject"
__Comment__ ="" __Comment__ =""
__Author__ = "2cv001" __Author__ = "2cv001"
__Title__ = "Macro Table to object" __Title__ = "Macro Table to object"
__Date__ = "2024/02/01" #YYYY/MM/DD 18:55 Béta __Date__ = "2024/02/03" #YYYY/MM/DD 17:38 Béta
__Version__ = __Date__ __Version__ = __Date__
from PySide import QtGui from PySide import QtGui
@@ -43,79 +43,85 @@ def recomputeAll() :
FreeCAD.ActiveDocument.recompute() FreeCAD.ActiveDocument.recompute()
def set_constraint_expression(sketchName, constraint_name, expression): def set_constraint_expression(sketchLabel, constraint_name, expression):
# Obtenez le Sketch # Get all Sketches with this label
sketch = App.ActiveDocument.getObject(sketchName) sketches = App.ActiveDocument.getObjectsByLabel(sketchLabel)
if len(sketches)>1 :
print("Warning, multiple sketches have the same label. "
+ " All these sketches will be processed. "
+ "This may cause problems if different sketches with the same label "
+ "have constraints with the same name.")
# Go through all Sketches
for sketch in sketches:
# Go through all constraints in the Sketch
for i in range(sketch.ConstraintCount):
# Get the constraint
constraint = sketch.Constraints[i]
# Parcourez toutes les contraintes dans le Sketch # Check if the constraint name matches the one we're looking for
for i in range(sketch.ConstraintCount): if constraint.Name == constraint_name:
# Obtenez la contrainte # Modify the constraint value
constraint = sketch.Constraints[i] sketch.setExpression('Constraints[' + str(i)+ ']', expression)
break
# 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): def set_property_based_on_alias(sheet,cell):
# alias de la cellule # alias of the cell
alias = sheet.getAlias(cell) alias = sheet.getAlias(cell)
# Vérifier si l'alias est valide # Check if the alias is valid
if sheet.getContents(cell)!='' : if sheet.getContents(cell)!='' :
if alias is None: if alias is None:
print("La cellule sélectionnée " + cell + " n'a pas d'alias.") print("The selected cell " + cell + " does not have an alias.")
return return
# Séparer l'alias en nom du corps et nom de la propriété # Separate the alias into body name and property name
parts = alias.split('_') # ex 'body_Name_posX' -> body_Name for the object and posX for property name
parts = alias.rsplit('_', 1)
if len(parts) != 2: if len(parts) != 2:
print("L'alias doit être sous la forme 'BodyName_PropertyName'.") print("The alias must be in the form 'BodyName_PropertyName'.")
return return
objName, property_name = parts objLabel, property_name = parts
#obj = App.ActiveDocument.getObject(objName) obj=App.ActiveDocument.getObjectsByLabel(objLabel)[0]
obj=App.ActiveDocument.getObjectsByLabel(objName)[0]
try : try :
property_name=dico[property_name] property_name=dico[property_name]
except : except :
try : try :
if obj.TypeId != 'Sketcher::SketchObject' : if obj.TypeId != 'Sketcher::SketchObject' :
print ('propriété ' + property_name + ' non trouvé dans le dico. Voir en haut du code source') print ('property ' + property_name + ' not found in the dictionary. See at the top of the source code')
except : except :
pass pass
if obj is None: if obj is None:
print(f"Il n'y a pas d objet nommé '{objName}' dans le document.") print(f"There is no object named '{objLabel}' in the document.")
return return
# Créer une expression qui fait référence à l'alias de la cellule # Create an expression that refers to the cell's alias
expression = f"<<{sheet.Label}>>.{alias}" expression = f"<<{sheet.Label}>>.{alias}"
# Modifier la propriété de l'objet # Modify the object's property
# si c'est une contrainte # if it's a constraint
if obj.TypeId == 'Sketcher::SketchObject' : if obj.TypeId == 'Sketcher::SketchObject' :
set_constraint_expression(objName, property_name, expression) set_constraint_expression(objLabel, property_name, expression)
return return
if hasattr(obj, property_name.split('.')[0]): # Placement.Base.x -> Placement if hasattr(obj, property_name.split('.')[0]): # Placement.Base.x -> Placement
obj.setExpression(property_name, expression) obj.setExpression(property_name, expression)
else: else:
print(f"Le corps '{objName}' n'a pas de propriété '{property_name}'.") print(f"The body '{objLabel}' does not have a property '{property_name}'.")
def main() : def main() :
sheet = Gui.ActiveDocument.ActiveView.getSheet() sheet = Gui.ActiveDocument.ActiveView.getSheet()
# Vérifiez si la feuille de calcul est active # Check if the spreadsheet is active
if sheet.TypeId != 'Spreadsheet::Sheet': if sheet.TypeId != 'Spreadsheet::Sheet':
print("Veuillez sélectionner des cellules dans une feuille de calcul.") print("Please select cells in a spreadsheet.")
return return
# cellule active # active cell
#cell = Gui.activeView().currentIndex() #cell = Gui.activeView().currentIndex()
aw = Gui.getMainWindow().centralWidget().activeSubWindow() # Store the active window aw = Gui.getMainWindow().centralWidget().activeSubWindow() # Store the active window
@@ -128,6 +134,8 @@ def main() :
if str(sheet.getContents(cellName))!= '' : if str(sheet.getContents(cellName))!= '' :
set_property_based_on_alias(sheet, cellName) set_property_based_on_alias(sheet, cellName)
if __name__ == '__main__': if __name__ == '__main__':
main() main()