Sunday, 27 April 2014

FreeCAD: Work Points

Sometimes when using the FreeCAD part-design workbench I feel the need of placing a sketch in a position that can't be achieved with the current sketch placing options.
I haven't seen any object similar to work-points, work-axis, work-planes... in FreeCAD, but I'm used to them in Autodesk Inventor.

I'm trying (from time to time) to implement this functionality in FreeCAD, and this is the first post about it.

The idea is that work-points and work-axis serve to determine the position of a work-plane, and a work-plane serves to place a sketch.

For example with tree points, or one point and an axis, or two axis, we can place a work-plane, then a sketch on it and later build more complex things easily and faster.

These work-objects can be created from existing objects or added as new objects.

Let's start with something very basic:

Workpoints: Midpoint


A midpoint is a point in the middle of a line, a vertex in FreeCAD.

At the moment this can be used as a macro, but without the ability to create work-planes, it is useless.


# -*- coding: utf-8 -*-
import Part
from FreeCAD import Console

try:
  MouseSel = Gui.Selection.getSelectionEx() #Gets Current Mouse Selection
  for s in range(len(MouseSel)):                          # Explores the objects selected
    Sel = MouseSel[s]
    Edge_List = Sel.SubObjects   # Retrieves the edge list of the current object

    for i in range(len(Edge_List)):   # For every edge in the list
      Vector_A=Edge_List[i].valueAt(0.0)  # initial point of the edge
      Vector_B=Edge_List[i].valueAt(Edge_List[i].Length)  # endpoint of the edge
      
      Vector_MidPoint = Vector_B+Vector_A
      Vector_MidPoint = Vector_MidPoint.multiply(0.5)
      
      MidPoint_Vertex = Part.Vertex(Vector_MidPoint)  #Creates the point
      
      #this conditional creates a folder that will contain all workpoints
      if not(App.ActiveDocument.getObject("WorkPoints")):
        App.ActiveDocument.addObject("App::DocumentObjectGroup","WorkPoints")
      
      
      name = "Midpoint" #name of the feature we are going to add to FreeCAD
      MidPoint = App.ActiveDocument.addObject("Part::Feature",name) #add the feature
      MidPoint.Shape = MidPoint_Vertex #links feature with the point created in the block above
      MidPoint = App.ActiveDocument.getObject("WorkPoints").addObject(MidPoint) 
      #moves everything to the folder "Workpoints"

except: #Something funny happened
  Console.PrintError("Select only edges")



This is how it works:

Create something or open a file with some shape at it, like this one:


Select some random edges (non circular):


Copy-paste the code above at the python terminal of FreeCAD.

Result:


And in the tree-view, all points have been place inside the "WorkPoints" folder:



And this is all it does. 

The next work-point feature will be the center of a circular edge. 

I need to improve things, for example, move the folder "workpoints" to the top of the tree-view (I have found absolutely nothing about that), create a new feature called workpoint, with its own symbol in the tree-view and predetermined colors for beter visibility, and more... 

Greetings!

Sunday, 13 April 2014

Another crappy machine

I have been out of the blog for a while because I'm busy with other projects, but I think that I can show here this one, about parallel port, python and floppy drives:




My conclusion is that stepper motors (from floppy and DVD drives) haven't enough torque to do anything useful, because the will simply jump several steps when applied a bit of load.

It has been a good exercise about machine control, I was trying to slice a part on FreeCAD, get the trajectories and then carve foam with this machine, but I do not have time to continue with this at the moment.

I will post here a bit about programming this things using python.


Bye!

Tuesday, 11 March 2014

FreeCAD: Simple Engine Animation.


A very simple animation to show the basics:


I do not have the time needed to go on detail plus this information will be obsolete once FreeCAD has an assembly module. So I'm posting just what I have.

Steps to animate something inside FreeCAD:


-Create parts

-Export them to STEP

-Create new document and import parts

-Determine how FreeCAD names the parts (in my script you can see "Part_Feature003", or "Part_Feature")

-Create a function that defines mathematically the mechanism (crank-slider type in mine, very simple)

-Create a timer that calls the function to make it move (look at the end of the script)


This is the python script, you can download parts here:


from PyQt4 import QtCore, QtGui

import Part
from FreeCAD import Base
import math as mt

i=0
def engine():
  global i
  
  i+=0.1
  
  cig=mt.degrees(i)
  
  
  B=mt.asin((mt.cos(i)*40.0)/140.0)
  AB=mt.asin((mt.sin(i)*(40.0/140.0)))
  PP=mt.cos(i)*40.0+mt.cos(AB)*140.0
  alphaB=mt.radians(90.0)-B
  
  br=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),alphaB)
  bd=App.Vector(mt.sin(i)*40,mt.cos(i)*40.0,20.0)
  bdgc=FreeCAD.Vector(mt.sin(i)*40,mt.cos(i)*40.0,20.0)
  
  App.getDocument("montadoanimacion").Part__Feature002.Placement=App.Placement(App.Vector(0,0,0),App.Rotation(FreeCAD.Vector(0,0,1),-cig))
  
  #biela
  
  App.getDocument("montadoanimacion").Part__Feature003.Placement=App.Placement(bd,App.Rotation(FreeCAD.Vector(0,0,1),(mt.degrees(AB))))
  
  #piston
  
  App.getDocument("montadoanimacion").Part__Feature.Placement=App.Placement(App.Vector(0,PP-35,30),App.Rotation(-0.5,0.5,0.5,0.5))

timer=QtCore.QTimer()
timer.timeout.connect(engine)
timer.start(10)



Open the file and then copy-paste this script. It should move. 

That's all.

Now I hope to see how people creates new and more complex animations :)


Bye!