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__ =""
__Author__ = "2cv001"
__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__
from PySide import QtGui
@@ -43,79 +43,85 @@ def recomputeAll() :
FreeCAD.ActiveDocument.recompute()
def set_constraint_expression(sketchName, constraint_name, expression):
# Obtenez le Sketch
sketch = App.ActiveDocument.getObject(sketchName)
def set_constraint_expression(sketchLabel, constraint_name, expression):
# Get all Sketches with this label
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
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
# Check if the constraint name matches the one we're looking for
if constraint.Name == constraint_name:
# Modify the constraint value
sketch.setExpression('Constraints[' + str(i)+ ']', expression)
break
def set_property_based_on_alias(sheet,cell):
# alias de la cellule
# alias of the cell
alias = sheet.getAlias(cell)
# Vérifier si l'alias est valide
# Check if the alias is valid
if sheet.getContents(cell)!='' :
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
# Séparer l'alias en nom du corps et nom de la propriété
parts = alias.split('_')
# Separate the alias into body name and property name
# ex 'body_Name_posX' -> body_Name for the object and posX for property name
parts = alias.rsplit('_', 1)
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
objName, property_name = parts
#obj = App.ActiveDocument.getObject(objName)
obj=App.ActiveDocument.getObjectsByLabel(objName)[0]
objLabel, property_name = parts
obj=App.ActiveDocument.getObjectsByLabel(objLabel)[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')
print ('property ' + property_name + ' not found in the dictionary. See at the top of the source code')
except :
pass
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
# 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}"
# Modifier la propriété de l'objet
# si c'est une contrainte
# Modify the object's property
# if it's a constraint
if obj.TypeId == 'Sketcher::SketchObject' :
set_constraint_expression(objName, property_name, expression)
set_constraint_expression(objLabel, 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}'.")
print(f"The body '{objLabel}' does not have a property '{property_name}'.")
def main() :
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':
print("Veuillez sélectionner des cellules dans une feuille de calcul.")
print("Please select cells in a spreadsheet.")
return
# cellule active
# active cell
#cell = Gui.activeView().currentIndex()
aw = Gui.getMainWindow().centralWidget().activeSubWindow() # Store the active window
@@ -128,6 +134,8 @@ def main() :
if str(sheet.getContents(cellName))!= '' :
set_property_based_on_alias(sheet, cellName)
if __name__ == '__main__':
main()