Friday, November 5, 2010

Creating Cross Sections of Parts in HeeksPython

Ok, I worked out a scheme for creating a cross sectional view of a simple assembly in HeeksPython. I altered my dxf_to_heekspython script slightly to let the sketches have unique IDs. Then it's possible to create objects out of them and do CSG operations to the unique objects.
Here is a sample script:

import HeeksPython as cad
import sys
sys.path.insert(0,'/home/dan/heeks/heekspython2/examples')
import dxf_to_heekspython
from math import pi

a1 = 90*(pi/180)


units = 25.4 #inch units
cad.setcolor(255,255,255)
r1='/home/dan/Documents/drawings/revolve1.dxf'
part1 = "cad.setcolor(0,0,0)\n"
part1 = dxf_to_heekspython.gen_heekspython_entities(r1,1)
part1 = part1 +"cad.scale(sketch1,0,0,0,units)\n"
part1 = part1 +"cad.setcolor(255,255,255)\n"
part1 = part1 +"cad.revolve(sketch1,360)\n"
part1 = part1 +"cup = cad.getlastobj()\n"
exec(part1)
cad.setcolor(0,255,0)
r2='/home/dan/Documents/drawings/revolve2.dxf'
part2 = "cad.setcolor(0,0,0)\n"
part2 = dxf_to_heekspython.gen_heekspython_entities(r2,2)
part2 = part2 +"cad.scale(sketch2,0,0,0,units)\n"
part2 = part2 +"cad.setcolor(0,255,0)\n"
part2 = part2 +"cad.revolve(sketch2,360)\n"
part2 = part2 +"sleeve = cad.getlastobj()\n"
exec(part2)

cad.view_extents()

c1= '/home/dan/Documents/drawings/cutaway.dxf'
cut = dxf_to_heekspython.gen_heekspython_entities(c1,3)
cut = cut + "cad.scale(sketch3,0,0,0,units)\n"
cut = cut + "cad.revolve(sketch3,90)\n"
cut = cut + "cutter = cad.getlastobj()\n"
cut = cut + "cad.rotate(cutter,0,0,0,1,0,0,a1)"
exec(cut)

cad.cut(cup,cutter)
cad.cut(sleeve,cutter)

Here is the assembly:


Just prior to importing these dxf files into HeeksPython I had also added fillets to all the corners in Caduntu (yes, this works well now Ries!). So now I can work in my favorite 2D CAD program and my favorite 3D CADCAM program to create assemblies with python.


It's easy to alter the design of the parts in this assembly by simply changing them in the dxf files:



Then re run the python script in HeeksPython:



One major improvement that I would like to do is to make the dxf_to_heekspython script deal with layers. Then I could do all the dxf editing in one file in Caduntu.
As I learn more about C++ programming, I might be able to utilize the functions in dxf.cpp from HeeksCAD itself. Importing would run much faster and I could take advantage of the things that already work well, like layers and importing polylines (which isn't currently implemented in the python script).

More DXF to HeeksPython Experiments

I created a python script to import a dxf file into HeeksPython, using parts of Doug Blanding's excellent python program 'Cadvas'. Doug and I have emailed back and forth over the years and he is happy to see his creation used for different things. He's not supporting it anymore, but in it's present state, it's a very good program to study. It's very simple and well laid out.
Included with Cadvas is the dxf.py module. I have copied some of it and altered it to suite my needs for dxf to heekspython conversion and uploaded the script into the /examples directory in the HeeksPython project. This script is called dxf_to_heekspython.py. Here are two profiles created in Qcad, that I want to manipulate in HeeksPython:





Here is a script to bring them into HeeksPython as separate revolved solid parts:

import HeeksPython as cad
import sys
sys.path.insert(0,'/home/dan/heeks/heekspython2/examples')
import dxf_to_heekspython


units = 25.4 #inch units
cad.setcolor(255,255,255)
r1='/home/dan/Documents/drawings/revolve1.dxf'
part1 = "cad.setcolor(0,0,0)\n"
part1 = dxf_to_heekspython.gen_heekspython_entities(r1)
part1 = part1 +"cad.scale(sketch,0,0,0,units)\n"
part1 = part1 +"cad.setcolor(255,255,255)\n"
part1 = part1 +"cad.revolve(sketch,360)"
exec(part1)
cad.setcolor(0,255,0)
r2='/home/dan/Documents/drawings/revolve2.dxf'
part2 = "cad.setcolor(0,0,0)\n"
part2 = dxf_to_heekspython.gen_heekspython_entities(r2)
part2 = part2 +"cad.scale(sketch,0,0,0,units)\n"
part2 = part2 +"cad.setcolor(0,255,0)\n"
part2 = part2 +"cad.revolve(sketch,360)\n"
part2 = part2 +"p1 = cad.getlastobj()\n"
exec(part2)

cad.view_extents()

Here is a screenshot of the resulting parts generated with this script:



The advantage of using this dxf_to_heekspython is that the dxf entities can be manipulated/transformed as they are brought in. I have some things that I would like to do with some dxf files - create solids by revolving them around the X axis. Eventually I will create cutaway views using this technique.