Thursday, 10 September 2015

FreeCAD: Intersection Between Shapes

Hi!

This is a small script that will find intersection volumes between the selected shapes, for example, given two cubes and a sphere, this is the result:


It gives some transparency to the original shapes and also displays intersection volume at the report view.

The code is:

# JMG 2015
object_list = []
for obj in FreeCAD.Gui.Selection.getSelectionEx():
  obj = obj.Object
  object_list.append( obj )

for n in range( len(object_list) ):
  object_A = object_list[n]
  for i in range( len(object_list) ):
    if i <= n:
      pass
    
    else:
      object_B = object_list[i]
      common = object_A.Shape.common( object_B.Shape )
      if common.Volume > 0.0:
        FreeCAD.Console.PrintMessage( '-Intersection- ' + object_A.Name + ' with ' + object_B.Name + '\n')
        FreeCAD.Console.PrintMessage( 'Common volume: ' + str( common.Volume ) + '\n' + '\n' )
        
        intersection_object = FreeCAD.ActiveDocument.addObject( 'Part::Feature', 'Intersection_Volume' )
        intersection_object.Shape = common
        intersection_object.ViewObject.ShapeColor = ( 1.0,0.0,0.0,1.0 )
        object_A.ViewObject.Transparency = 80
        object_B.ViewObject.Transparency = 80

Thursday, 6 August 2015

FreeCAD: Double Slider Mechanism Animation

Since few days ago, this blog is two years old, and, also, last month it crossed the 4000 views/month barrier.
I was thinking about a way of celebrating this events, and last night I saw a .gif image about a flat mechanism at which I could be staring the whole day:


I don't know its exact name, but "double slider mechanism" seems appropriate. It belongs to the family of flat, four bar linkage mechanisms, and, possibly, there is no real use for this one. But it moves very smoothly, with the outer end of the rotating arm drawing some kind of ellipse.



The kinematics of this one are not too difficult (none of the family of four bar linkage mechanism are), and for the animation I have solved it in an analytical way.

The code:


# Javier Martinez Garcia    August 2015   GPL V2.0

from PySide import QtCore
from math import sin, cos, radians

# retrieve the objects from the document
slider_x = FreeCAD.ActiveDocument.getObject("Pad003002")
slider_y = FreeCAD.ActiveDocument.getObject("Pad003001")
arm = FreeCAD.ActiveDocument.getObject("Pad002001")


# store initial placement (needed to restore initial position)
slider_x_placement = slider_x.Placement
slider_y_placement = slider_y.Placement
arm_placement = arm.Placement

# store object placements in a new variable
r_slider_x_pl = slider_x.Placement
r_slider_y_pl = slider_y.Placement
r_arm_pl = arm.Placement


def reset():
  # function to restore initial position of the objects
  slider_x.Placement = r_slider_x_pl
  slider_y.Placement = r_slider_y_pl
  arm.Placement = r_arm_pl


# In this mechanism, "i" represents the angle of the rod in degrees
i = 0

# update function calculates object position as f(i) and increases i
def update():
  global i
  alpha = radians( i )
  x = 150.0*cos( alpha )
  y = 150.0*sin( alpha )
  slider_x.Placement = FreeCAD.Placement( slider_x_placement.Base + FreeCAD.Vector( 150-x, 0, 0 ),
                                          slider_x_placement.Rotation )
  
  slider_y.Placement = FreeCAD.Placement( slider_y_placement.Base + FreeCAD.Vector( 0, y, 0 ),
                                          slider_y_placement.Rotation )
  
  arm.Placement = FreeCAD.Placement( arm_placement.Base + FreeCAD.Vector( 0, y, 0 ),
                                     FreeCAD.Rotation( FreeCAD.Vector( 0,0,1), i))
  # update the scene
  FreeCAD.Gui.updateGui()
  # increase mechanism input position
  i += 1


# create a timer object
timer = QtCore.QTimer()
# connect timer event to function "update"
timer.timeout.connect( update )
# start the timer to trigger "update" every 10 ms
timer.start( 10 )



Download the .fcstd model and animation script here, on github.


Have fun!

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!