Skip to content

Instantly share code, notes, and snippets.

@jay3sh
Created June 27, 2011 04:49
Show Gist options
  • Save jay3sh/1048327 to your computer and use it in GitHub Desktop.
Save jay3sh/1048327 to your computer and use it in GitHub Desktop.
PythonOCC Shear transform testing
from OCC.BRepPrimAPI import *
from OCC.BRepBuilderAPI import *
from OCC.gp import *
from OCC import StlAPI
from OCC.Precision import *
from OCC.Utils.Topology import *
from OCC.TopoDS import *
from OCC.TopAbs import *
from OCC.BRepMesh import *
from OCC.BRep import *
from pprint import pprint
def vtxkey(vtx):
return '%.2f_%.2f_%.2f'%(vtx[0], vtx[1], vtx[2])
def printMesh(shape):
vtxmap = {}
BRepMesh_Mesh(shape, Precision_Confusion()) # TODO precision
faces_iterator = Topo(shape).faces()
for F in faces_iterator:
face_location = F.Location()
facing = BRep_Tool_Triangulation(F,face_location).GetObject()
tab = facing.Nodes()
tri = facing.Triangles()
for i in range(1,facing.NbTriangles()+1):
trian = tri.Value(i)
if F.Orientation() == TopAbs_REVERSED:
index1, index3, index2 = trian.Get()
else:
index1, index2, index3 = trian.Get()
P1 = tab.Value(index1).Transformed(face_location.Transformation())
P2 = tab.Value(index2).Transformed(face_location.Transformation())
P3 = tab.Value(index3).Transformed(face_location.Transformation())
c1 = P1.XYZ().Coord()
c2 = P2.XYZ().Coord()
c3 = P3.XYZ().Coord()
if not vtxmap.has_key(vtxkey(c1)): vtxmap[vtxkey(c1)] = c1
if not vtxmap.has_key(vtxkey(c2)): vtxmap[vtxkey(c2)] = c2
if not vtxmap.has_key(vtxkey(c3)): vtxmap[vtxkey(c3)] = c3
pprint(vtxmap.values())
def main():
box = BRepPrimAPI_MakeBox(1,1,1)
print 'Before Shear Transform'
printMesh(box.Shape())
xform = gp_Trsf()
xform.SetValues(
1, 0.5, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
Precision_Angular(), Precision_Confusion()
);
brep = BRepBuilderAPI_Transform(box.Shape(), xform, False)
brep.Build()
print 'After Shear Transform'
printMesh(brep.Shape())
#StlAPI.StlAPI_Write(brep.Shape(), 'shearbox2.stl')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment