Hi folks,
For quite a time now I've been receiving orders for machining parts in the form of simple .DXF files accompanied by a 3D model in .stp file format. CamBam, with some small exceptions, reads the .stp files and it is big bonus as I can see the real part on the screen and plan my work strategy. To make it easier for CamBam I normally 'Select by object type' all 3D surfaces and join them into a single 3D surface on a separate layer. This way I can see the 3D model, the wireframe geometry or both.
There are normally various pockets with different depths often one containing another, holes, posts, etc.
Instead of using the 2D .DXF I started by manually selecting elements from the wireframe, then copying to another layer and joining them them to form closed polylines. This way I have the shapes to be machined at their exact depth. Thus helping me with the creation of 2.5D simple MOPs. If the part is not very complex doing it manually does not consume much time.
Then I thought about automating the process for more complex parts where edges are difficult to separate and select all small segments one by one manually is a time consuming work. I wrote a simple and unsophisticated Python script. The latter brought even more pain to the fact that I am not quite familiar with CB API and methods to access and manipulate entities. Why Python? Because I run CamBam for Linux and Python is the only available language there.
The idea is as follows:
I select a single entity from the wireframe and run the script.
The script determines the Z-level (plane) the entity lies on and by iterating through all (visible) entities copies to a new layer all that lie on the same plane (for now I test only both ends by getting entity's extrema.
After a lot of struggle, reading the reference pages and looking into code snippets it started working with very satisfactory results.
But there is a problem I bumped into: Many of the entities in the wireframe are placed at their places by means of transformation matrix. For example an arc may lie at 10mm above zero but getting its extrema would read 0 if not in 'Identity' state. This leads to incorrect selections.
I tried using entity clones and 'ApplyTransformation' method so there positions read correctly but some of them, especially arcs that lie on X/Z or Y/Z plane cause errors. (I actually do not need them but the iteration process goes through them too).
I am starting this thread in the hope that some of you can give me a hand on how to analyze an entity with active transformation matrix.
If successful the script may come handy to those who use .step models in their work. Speaking in the terms of the expensive CAM packages this is a kind of semi-automatic determination of the 'features' of the model.
I can determine whether the matrix is in 'Identity' state or not but could not find a method to read the actual values and determine the Z component.
Here's the code in its present state
# Get the Z level of an object
# iterate through all objects and select objects with
# the same Z level within a tolerance
import sys
#from System import Math
#from System import Array
#from System.Collections.Generic import List
from CamBam.UI import CamBamUI
from CamBam.Geom import *
from CamBam.CAD import *
from CamBam.CAM import *
from CamBam.Geom import *
app.log("Begin")
p3max = Point3F()
p3min = Point3F()
p2 = Point2F(1,1)
tmat = Matrix4x4F
sel = list()
Tlrnc = 0.1 #Tolerance, not used currently
selZ = 0
selminZ = 0
selmaxZ = 0
# First we need one and only one entity selected as a guide
if (view.SelectedEntities.Length == 1):
for ent in view.Selection:
ne = ent.Clone()
tmat = ne.Transform
mtrx = ne.Transform.ToString()
if (mtrx != 'Identity'):
if (ne.ApplyTransformation(tmat)):
mtrx = ne.Transform.ToString()
else:
app.log("Could not transform selected ID=" + ne.ID.ToString())
sys.exit(1)
app.log("Object: "+ne.ID.ToString())
p3min, p3max = ne.GetExtrema(p3min, p3max,False)
app.log("Extrema: "+p3min.Z.ToString() + " : " +p3max.Z.ToString())
selZ = p3min.Z
selminZ = selZ - Tlrnc
selmaxZ = selZ + Tlrnc
app.log("Level: "+selZ.ToString() )
nlayer = Layer()
nlayer.Name = "SelLayer"
doc.Layers.Add(nlayer)
#Then iterate throu all
CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()
for ent in CamBamUI.MainUI.ActiveView.SelectedEntities:
ne = ent.Clone()
tmat = ne.Transform
mtrx = ne.Transform.ToString()
if (mtrx == 'Identity'):
p3min, p3max = ne.GetExtrema(p3min, p3max,False)
if ((p3min.Z >= selminZ) and (p3min.Z <= selmaxZ)) :
if ((p3max.Z >= selminZ) and (p3max.Z <= selmaxZ)) :
nlayer.Entities.Add(ne)
else:
sys.exit(1)
# Here must be code to look into the matrix values
# No success in doing so
else:
app.log("Error: Select a single poly.")