Repetitively cut solids

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
JMG
Posts: 288
Joined: Wed Dec 25, 2013 9:32 am
Location: Spain
Contact:

Repetitively cut solids

Post by JMG »

Hi all.

Image

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)


To use it, copy and paste inside the terminal.
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.
FreeCAD scripts, animations, experiments and more: http://linuxforanengineer.blogspot.com.es/
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Repetitively cut solids

Post by shoogen »

JMG wrote:I've run out of ideas today, how would you improve this?
I would use a voxel representation, maybe even using octrees.
Trying to use booleans in real time won't scale well.
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Repetitively cut solids

Post by ulrich1a »

You could call

Code: Select all

L1 = L1.removeSplitter()
from time to time.
This may reduce the number of not needed edges.

Ulrich
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Repetitively cut solids

Post by ickby »

If you only use straigth edges the best would be to calculate the used crosssection of the tool for every straight path segment, build a sweep of the crosssection with the path segment and make a boolean cut. Or even better a extrude of the crosssection wth the path segment as direction and length vector. If you have many path segments maybe fusing all sweeps/extrudes together and then cutting just once may be better.

But as shoogen said, booleans are not well suited for such simulations and were never intended for it. Their high preciscion and topological correctness just take time, and as you don't need that much information you can use other techniques named by shoogen.

the problem with remove splitter is that it only works if the tool is moved parallel to its faces. If not, no coplanar faces are created on the material block but zick-zack ones and therefore removesplitter can't join anything.
JMG
Posts: 288
Joined: Wed Dec 25, 2013 9:32 am
Location: Spain
Contact:

Re: Repetitively cut solids

Post by JMG »

Thanks for your answers :)

I've added ulrich1a's idea to the script, it looks better and works a bit faster, but as ickby and shoogen said, this is not the adequate way.

And thanks for that info, I've never heard of voxels and octrees before, I will do some research.

I've uploaded a video (before adding the idea of ulrich1a), the link is:

https://www.youtube.com/watch?v=ljunVAoo89o


Greetings, Javier.
FreeCAD scripts, animations, experiments and more: http://linuxforanengineer.blogspot.com.es/
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
Post Reply