I'm working in a simple 2.5 Axis CNC simulator script, wich can generate a wire from a given list of points, and use it as a toolpath. It can even remove material from a raw block, but there is a problem: is very very slow.
The code:
Code: Select all
import Part
import math as mt
import time
from FreeCAD import Base
from FreeCAD import Gui
from PyQt4 import QtCore
#PROGRAM VARIABLES
raw_size=(50,30,10)
Tool_radius = 1.5 # mm
Tool_heigh = 10.0 #mm
feed_rate = 1.0 #mm/s
fast_movement = 10 mm/s
Program=((-5,0,20),
(-5,5,20),
(-5,5,7),
(10,5,7),
(20,15,7),
(30,5,7),
(40,5,7),
(45,10,7),
(45,20,7),
(-5,20,7),
(-5,20,4),
(25,20,4),
(10,20,4),
(10,20,2),
(-5,20,2),
(-5,0,20))
#######
##Internal Variables
ToolR = App.Rotation(App.Vector(0,0,1),0)
#######
#Create Trajectory Wire
Wire_done = False
i=0
while Wire_done == False:
i+=1
if i == len(Program):
Wire_done = True
break
if i == 1:
Line0 = Part.makeLine(Program[i-1], Program[i])
Wire0 = Part.Wire([Line0])
else:
Line1 = Part.makeLine(Program[i-1],Program[i])
Trajectory_Wire = Part.Wire([Wire0,Line1])
Wire0 = Trajectory_Wire
TjWire = App.ActiveDocument.addObject("Part::Feature", "Trajectory")
TjWire.Shape = Trajectory_Wire
TjWire_UserName = TjWire.Label
FreeCADGui.ActiveDocument.getObject(TjWire_UserName).LineColor = (1.00,0.67,0.00)
### Create raw
Raw = Part.makeBox(raw_size[0],raw_size[1],raw_size[2])
Raw_shape = App.ActiveDocument.addObject("Part::Feature", "Raw")
Raw_shape.Shape = Raw
### Create Tool
Tool = Part.makeBox(Tool_radius, Tool_radius, Tool_heigh)
Tool_shape = App.ActiveDocument.addObject("Part::Feature", "Tool")
Tool_shape.Shape = Tool
Tool_shape_gui = Tool_shape.Label
FreeCADGui.ActiveDocument.getObject(Tool_shape_gui).ShapeColor = (0.33,0.33,1.00)
Tool_shape.Placement = App.Placement(App.Vector(Program[0]),ToolR )
### Machining Function
L1 = Raw.cut(Tool_shape.Shape)
i=0
n=0.0
def Machining():
global n, i,feed_rate, L1
if i < len(Program):
Current_position = Tool_shape.Placement.Base
Vector_trajectory = App.Vector(Program[i+1])-App.Vector(Program[i])
Vector_direction = (App.Vector(Program[i+1])-App.Vector(Program[i])).normalize()
VT_modulus = Vector_trajectory.Length
VD_modulus = 1.0
if VT_modulus > VD_modulus*n:
Next_position = App.Vector(Program[i])+Vector_direction.multiply(n*feed_rate)
n+=0.1
else:
Next_Position = App.Vector(Program[i+1])
i+=1
n=0.0
Tool_shape.Placement = App.Placement(Next_position, ToolR)
L1 = L1.cut(Tool_shape.Shape)
else:
Part.show(L1)
timer = QtCore.QTimer()
timer.timeout.connect(Machining)
timer.start(1)
You will notice that, as soon as the tool starts to mill, it slows down dramatically.
When you get tired of waiting for it to finish, type "timer.stop()", and to see the milled part, "Part.show(L1)"
The tool is rectangular, it shouldn't create edges when is milling along one axis only, but if you look at the shape "L1", you can see how it creates edges across the tool corridor at every iteration. And this incredible number of non-sense edges is what slows down FreeCAD.
I've run out of ideas today, how would you improve this?
Thanks!
Javier.