Files
freecad-macros/tableToObject.FCMacro

142 lines
4.7 KiB
Plaintext

__Name__ = "tableToObject"
__Comment__ =""
__Author__ = "2cv001"
__Title__ = "Macro Table to object"
__Date__ = "2024/02/03" #YYYY/MM/DD 17:38 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(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]
# 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 of the cell
alias = sheet.getAlias(cell)
# Check if the alias is valid
if sheet.getContents(cell)!='' :
if alias is None:
print("The selected cell " + cell + " does not have an alias.")
return
# 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("The alias must be in the form 'BodyName_PropertyName'.")
return
objLabel, property_name = parts
obj=App.ActiveDocument.getObjectsByLabel(objLabel)[0]
try :
property_name=dico[property_name]
except :
try :
if obj.TypeId != 'Sketcher::SketchObject' :
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"There is no object named '{objLabel}' in the document.")
return
# Create an expression that refers to the cell's alias
expression = f"<<{sheet.Label}>>.{alias}"
# Modify the object's property
# if it's a constraint
if obj.TypeId == 'Sketcher::SketchObject' :
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"The body '{objLabel}' does not have a property '{property_name}'.")
def main() :
sheet = Gui.ActiveDocument.ActiveView.getSheet()
# Check if the spreadsheet is active
if sheet.TypeId != 'Spreadsheet::Sheet':
print("Please select cells in a spreadsheet.")
return
# active cell
#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()