Saturday 18 July 2015

FreeCAD: Drill macro

I am working on a project that has a lot of drill operations. Being an "assembly", it can get quite tedious to create an sketch or part cylinder, place it and then do a boolean cut to make the drill. To speed it up, I have written a short macro that drills existing holes into the selected shapes:

 

Selection:


 

Script:

"""
Drill Macro
JMG 2015 GPL

Select a set of circles and then the shapes that you want to drill
"""

#get user selection of edges
SelEdges = Gui.Selection.getSelectionEx()[0].SubObjects  

# get user selection for shapes
SelectedShapes = Gui.Selection.getSelectionEx() 

# create the faces that will be extruded to create the drill
cutFaces = []
for edge in SelEdges:
  cutFace = Part.Face(Part.Wire(edge)) # create face from edge
  cutFaces.append( cutFace ) 

# drill all selected shapes minus first one (selected edges)
for i in range(len(SelectedShapes)-1): 
  #retrieve object from selection list
  SelShape = SelectedShapes[i+1].Object 

  #create an empty part object in the document
  drilledShape=FreeCAD.ActiveDocument.addObject("Part::Feature",'Drilled'+SelShape.Name)
  drilledShape.Shape = SelShape.Shape
  #cut selected shape and assign shape to "drilledShape"
  for f in cutFaces:
    drilledShape.Shape=drilledShape.Shape.cut(f.extrude(f.normalAt(0,0)*1000000))
    drilledShape.Shape=drilledShape.Shape.cut(f.extrude(f.normalAt(0,0)*-1000000))
  
  #turn off visibility of the selected shape and set same color for the new object
  SelShape.ViewObject.Visibility = False
  drilledShape.ViewObject.ShapeColor = SelShape.ViewObject.ShapeColor

Output:



Also, if you want to drill something more complex, like a slot, a variation of the script can do it:

Selection:

Script:
"""
Drill Edges Macro
JMG 2015 GPL

Select a set of edges that form a closed wire and then the shapes
that you want to drill
"""

#get user selection of edges
SelEdges = Gui.Selection.getSelectionEx()[0].SubObjects  

# get user selection for shapes
SelectedShapes = Gui.Selection.getSelectionEx() 

# create the face that will be extruded to create the drill
cutFace = Part.Face(Part.Wire( SelEdges )) 

# drill all selected shapes minus first one (selected edges)
for i in range(len(SelectedShapes)-1): 
    #retrieve object from selection list
    SelShape = SelectedShapes[i+1].Object 
    
    #create an empty part object in the document
    drilledShape=FreeCAD.ActiveDocument.addObject("Part::Feature",'Drilled'+SelShape.Name)
    
    #cut selected shape and assign shape to "drilledShape"
    drilledShape.Shape=SelShape.Shape.cut(cutFace.extrude(cutFace.normalAt(0,0)*1000000))
    drilledShape.Shape=drilledShape.Shape.cut(cutFace.extrude(cutFace.normalAt(0,0)*-1000000))
    
    #turn off visibility of the selected shape and set same color for the new object
    SelShape.ViewObject.Visibility = False
    drilledShape.ViewObject.ShapeColor = SelShape.ViewObject.ShapeColor

Result:




I have them in a custom toolbar and they have helped me to do things faster.

Maybe it helps you too :)

Bye!

No comments:

Post a Comment