Manque ou évolution (bug ?) : expression avec =Pad.Shape.BoundBox....

Forum destiné aux questions et discussions en français
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
2cv001
Posts: 484
Joined: Wed Jan 01, 2020 9:30 am

Re: Manque ou évolution (bug ?) : expression avec =Pad.Shape.BoundBox....

Post by 2cv001 »

Pour les curieux : la macro qui fonctionne avec tous les plans mais à condition que ce soit le même Support pour les différents sketch concernés (par exemple tous les deux sur XY_Plane).

Pour tester : sélectionnez un pad et un sketch. Lancez la macro et le sketch vient se placer sur le Pad. Le tout sans les problème de dénomination topologique.

Code: Select all


##########################################################################
# Select a pad and then select a sketch, the sketch will be on the pad
# Select a sketch and then a pad, the pad will be on the sketch
# sketch and the pad have to be selected altogether 
# (Ctrl to select the second object)
###########################################################################


import FreeCAD, FreeCADGui

def padOfSketch(sketch) :
    # get pad of selected Sketch if exist 
    for obj in FreeCAD.ActiveDocument.Objects :
        if obj.TypeId == 'PartDesign::Pad' :
            if obj.Profile[0] == sketch :
                return obj
    return None

##########################################
# Main proceddure
##########################################
def main():
    
    # get selected pad
    if len(FreeCADGui.Selection.getSelection())!= 2 :
        print('Select one sketch and one pad')
        return
        
    # have a look to first selected object   
    sel = FreeCADGui.Selection.getSelection()[0]
    if sel.TypeId == 'Sketcher::SketchObject' :
        selSketch = sel
        sketchOnPad = False # if True Sketch will be on the pad else under the pad

    elif sel.TypeId == 'PartDesign::Pad' :
        selPad = sel
        sketchOnPad = True # Sketch will be under the pad    
    else :
        print('- Select one sketch and one pad.')   

    # have a look to second selected object
    sel = FreeCADGui.Selection.getSelection()[1]
    if sel.TypeId == 'Sketcher::SketchObject' :
        selSketch = sel
    elif sel.TypeId == 'PartDesign::Pad' :
        selPad = sel 
    else :
        print('-- Select one sketch and one pad')   

    # get pad properties
    selectedPadName = selPad.Name
    selectedPadLength = selPad.Length
    selectedPadSketch = selPad.Profile[0]
    selectedPadSketchName = selectedPadSketch.Name 
    padSelectedSketch = padOfSketch(selSketch)
    # support and MapMode must be the same
    selSketch.Support = selectedPadSketch.Support
    selSketch.MapMode = selectedPadSketch.MapMode
       
    
    if sketchOnPad :
        expression = selectedPadName + '.Reversed == False ? ' + selectedPadSketchName \
        + '.AttachmentOffset.Base.z + ' + selectedPadName + '.Length : ' + selectedPadSketchName + '.AttachmentOffset.Base.z'
        padSelectedSketch.Reversed = False
    else :
        expression = selectedPadName + '.Reversed == True ? ' + selectedPadSketchName \
        + '.AttachmentOffset.Base.z + ' + selectedPadName + '.Length : ' + selectedPadSketchName + '.AttachmentOffset.Base.z' 
        padSelectedSketch.Reversed = True
       
    selSketch.setExpression('.AttachmentOffset.Base.z', expression )
        

if __name__ == '__main__':
    main()



Il faut que je regarde maintenant les cas où les support sont différents (un par exemple en XY_Plane et l'autre en XZ_Plane).
Macro Sketch Constraint From Spreadsheet :
https://wiki.freecad.org/Macro_Sketch_C ... adsheet/fr
Post Reply