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!

No comments:

Post a Comment