OCC: interface to open-cascade
Preamble
OCC performs CAD operations and surface meshing using open-cascade.
To use the module with the Converter.array interface:
import OCC
To use the module with the CGNS/Python interface:
import OCC.PyTree as OCC
List of functions
– CAD to surface mesh
Get a first TRI meshed tree linked to CAD. |
|
Project t on CAD. |
|
|
– CAD to surface mesh (legacy functions)
Convert a CAD (IGES or STEP) file to arrays. |
|
Convert a CAD (IGES or STEP) file to pyTree. |
– CAD manipulation
Read CAD file and return a CAD hook. |
|
Write CAD file. |
|
Return the number of edges in CAD hook. |
|
Return the number of faces in CAD hook. |
|
Return the area of given faces. |
|
Translate all or given faces. |
|
Rotate all or given faces. |
|
Scale all or given faces. |
|
Split all faces to be less than area. |
|
Merge some faces. |
|
Sew some faces (suppress redundant edges). |
|
Remove given faces. |
|
Trim a set of faces with another set of faces. |
Contents
CAD to surface mesh
- OCC.PyTree.meshAll(hook, hmin=-1, hmax=-1., hausd=-1.)
Mesh a CAD with triangles. If hmin=hmax, mesh with a regular h. If hmin, hmax and hausd are set, mesh with isotropic triangles with local size adapted to curvature. hausd is the max chordal error of mesh to CAD. This function returns a tree with two bases (EDGES and FACES). EDGES contains discretized edges with a link to the global edge number in CAD. FACES contains discretized faces with a link to the global face number in CAD.
- Parameters:
hook (CAD hook) – CAD hook
hmin (float) – minimum step size on output mesh.
hmax (float) – maximum step size on output mesh.
hausd (float) – maximum chordal error.
- Return type:
mesh
Example of use:
# - meshAll (pyTree) - import OCC.PyTree as OCC import Converter.PyTree as C hook = OCC.readCAD("cube.step", "fmt_step") # constant h m = OCC.meshAll(hook, hmin=1., hmax=1.) # varying h between hmin and hmax with deflection hausd m = OCC.meshAll(hook, hmin=1., hmax=5., hausd=1.) # get components in final mesh cs = OCC.getComponents(m) # check if components are watertight for c in cs: ret = OCC.isWatertight(c) print("Component watertight: ", ret) C.convertPyTree2File(m, 'out.cgns')
- OCC.PyTree._projectOnFaces(hook, t, faceList=None)
Project mesh on given CAD faces (in place).
- Parameters:
hook (CAD hook) – CAD hook
t (zone, list of zones or tree) – mesh to project
faceList (list of face index (starts 1)) – list of faces number to calculate the area
Example of use:
# - projectOnFaces (PyTree) - import OCC.PyTree as OCC import Converter.PyTree as C import Generator.PyTree as G hook = OCC.readCAD("cube.step", "fmt_step") # project on all CAD faces a = G.cart((-27,-17,-33), (1,1,1), (1,10,10)) OCC._projectOnFaces(hook, a, None) C.convertPyTree2File(a, 'out.cgns') # project on certain CAD faces a = G.cart((-27,-17,-33), (1,1,1), (1,10,10)) OCC._projectOnFaces(hook, a, [1,2,3,4,5,6]) C.convertPyTree2File(a, 'out.cgns')
- OCC.PyTree.meshDeviation(hook, t)
Mesaure deviation of mesh from CAD. Project the center of mesh triangles on CAD and return the projection distance in a field.
- Parameters:
hook (CAD hook) – CAD hook
t (zone, list of zones or tree) – mesh
Example of use:
# - meshDeviation (pyTree) - import OCC.PyTree as OCC import Converter.PyTree as C hook = OCC.readCAD("cube.step", "fmt_step") m = OCC.meshAll(hook, hmin=1., hmax=1.) OCC._meshDeviation(hook, m) C.convertPyTree2File(m, 'out.cgns')
CAD to surface mesh (legacy functions)
- OCC.convertCAD2Arrays(fileName, format='fmt_iges', h=0., chordal_err=0., growth_ratio=0., algo=1)
Read a CAD and return arrays.
- Parameters:
fileName (string) – CAD file name
format (string) – file format (‘fmt_iges’ or ‘fmt_step’)
h (float) – step size on output mesh. If 0., automatic setting [algo=1,2].
chordal_error (float) – max error between CAD and mesh. Result in curvature adaptation. If 0., automatic setting.
growth_ratio (float) – max growth ratio between adjacent triangles [algo=1,2].
algo (int) – algo=0: mesh with only respect to curvature, algo=1 or algo=2: mesh with regular triangles.
- Return type:
a list of TRI arrays
Example of use:
# - convertCAD2Arrays (arrays) - import Converter as C import OCC # IGES avec T3Mesher A = OCC.convertCAD2Arrays('hammer.iges', format='fmt_iges', h=0., chordal_err=0., growth_ratio=0.8, algo=1) C.convertArrays2File(A, 'hammer1.plt') # IGES avec OCC A = OCC.convertCAD2Arrays('hammer.iges', format='fmt_iges', chordal_err=1, algo=0) C.convertArrays2File(A, 'hammer2.plt') # STEP avec T3Mesher A = OCC.convertCAD2Arrays('as1-oc-214.stp', format='fmt_step', h=0., chordal_err=0., growth_ratio=0.8, algo=1) C.convertArrays2File(A, 'as1.plt') # STEP avec OCC A = OCC.convertCAD2Arrays('as1-oc-214.stp', format='fmt_step', chordal_err=1, algo=0) C.convertArrays2File(A, 'as2.plt')
- OCC.PyTree.convertCAD2PyTree(fileName, format='fmt_iges', h=0., chordal_err=0., growth_ratio=0., algo=1)
Read a CAD and return a zone.
- Parameters:
fileName (string) – CAD file name
format (string) – file format (‘fmt_iges’ or ‘fmt_step’)
h (float) – step size on output mesh. If 0., automatic setting [algo=1,2].
chordal_error (float) – max error between CAD and mesh. Result in curvature adaptation. If 0., automatic setting.
growth_ratio (float) – max growth ratio between adjacent triangles [algo=1,2].
algo (int) – algo=0: mesh with only respect to curvature, algo=1 or algo=2: mesh with regular triangles.
- Return type:
CGNS pyTree
Example of use:
# - convertIGES2PyTree (PyTree) - import OCC.PyTree as OCC import Converter.PyTree as C t = OCC.convertCAD2PyTree('hammer.iges', h=0., chordal_err=0., algo=1) C.convertPyTree2File(t, 'out.cgns')
CAD manipulation
- OCC.readCAD(fileName, format='fmt_step')
Read a CAD file and return a CAD hook.
- Parameters:
fileName (string) – CAD file name
format (string) – file format (‘fmt_iges’ or ‘fmt_step’)
- Return type:
CAD hook
Example of use:
# - readCAD (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step")
- OCC.writeCAD(hook, fileName, format='fmt_step')
Write a CAD hook to a file.
- Parameters:
hook (CAD hook) – CAD hook
fileName (string) – CAD file name
format (string) – file format (‘fmt_iges’ or ‘fmt_step’)
Example of use:
# - writeCAD (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC.writeCAD(hook, "out.step", "fmt_step")
- OCC.getNbEdges(hook)
Return the total number of edges in a CAD hook.
- Parameters:
hook (CAD hook) – CAD hook
- Return type:
int
Example of use:
# - getNbEdges (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") print(OCC.getNbEdges(hook)) #> 12
- OCC.getNbFaces(hook)
Return the number of faces in a CAD hook.
- Parameters:
hook (CAD hook) – CAD hook
- Return type:
int
Example of use:
# - getNbFaces (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") print(OCC.getNbFaces(hook)) #> 6
- OCC.getFaceArea(hook, faceList=[])
Return the area of given faces.
- Parameters:
hook (CAD hook) – CAD hook
faceList (list of face index (starts 1)) – list of faces number to calculate the area
- Return type:
float
Example of use:
# - getFaceArea (pyTree) - import OCC.PyTree as OCC h = OCC.readCAD('cube.step', 'fmt_step') # Area of all model print(OCC.getFaceArea(h)) # Area of seome faces print(OCC.getFaceArea(h, [1,2]))
- OCC._translate(hook, vector, listOfFaces=None)
Translate a CAD hook by a given vector.
- Parameters:
hook (CAD hook) – CAD hook
vector (tuple of floats) – translation vector (dx, dy, dz)
listOfFaces (list of face indices (starts 1)) – if None, translate all else translate only given faces
Example of use:
# - translate (pyTre) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._translate(hook, (1,0,0)) OCC.writeCAD(hook, 'out.step', 'fmt_step')
- OCC._rotate(hook, Xc, axis, angle, listOfFaces=None)
Rotate a CAD hook around a given axis by a given angle.
- Parameters:
hook (CAD hook) – CAD hook
Xc (tuple of floats) – rotation center (x, y, z)
axis (tuple of floats) – rotation axis
angle (float) – rotation angle in degrees
listOfFaces (list of face indices (starts 1)) – if None, rotate all else rotate only given faces
Example of use:
# - rotate (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._rotate(hook, (0,0,0), (0,0,1), 30.) OCC.writeCAD(hook, 'out.step', 'fmt_step')
- OCC._scale(hook, factor, X, listOfFaces=None)
Scale a CAD hook by a given factor.
- Parameters:
hook (CAD hook) – CAD hook
factor (float) – scale factor
X (tuple of floats) – invariant point (x, y, z)
listOfFaces (list of face indices (starts 1)) – if None, scale all else scale only given faces
Example of use:
# - scale (pyTre) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._scale(hook, 0.5, (0,0,0)) OCC.writeCAD(hook, 'out.step', 'fmt_step')
- OCC._splitFaces(hook, area)
Split faces in a CAD hook.
- Parameters:
hook (CAD hook) – CAD hook
area (float) – split each face if area greater than this value
Example of use:
# - splitFaces (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._splitFaces(hook, 20.) OCC.writeCAD(hook, "out.step", "fmt_step")
- OCC._mergeFaces(hook, faceList=None)
Merge faces in a CAD hook.
- Parameters:
hook (CAD hook) – CAD hook
faceList (list of face indices (starts 1)) – if None, merge all faces else list of faces indices to merge
Example of use:
# - mergeFaces (array) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._splitFaces(hook, 200.) # merge given faces OCC._mergeFaces(hook, [1,2,3]) # merge all faces OCC._mergeFaces(hook) OCC.writeCAD(hook, "out.step", "fmt_step")
- OCC._sewing(hook, faceList=None, tol=1.e-6)
Sew faces. Supress redundant edges.
- Parameters:
hook (CAD hook) – CAD hook
faceList (list of face indices (starts 1)) – if None, merge all faces else list of faces indices to merge
tol (float) – tolerance for sewing
Example of use:
# - sewing (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cube.step", "fmt_step") OCC._sewing(hook, tol=1.e-6, faceList=[1,2]) OCC._sewing(hook, tol=1.) OCC.writeCAD(hook, 'out.step', 'fmt_step')
- OCC._removeFaces(hook, faceList)
Remove given list of faces from CAD.
- Parameters:
hook (CAD hook) – CAD hook
faceList (list of face indices (starts 1)) – list of faces
Example of use:
# - removeFaces (pyTree) - import OCC hook = OCC.readCAD("cubeNoTrim.step", "fmt_step") ret = OCC.getFaceNameInOCAF(hook) cube1 = ret[3] OCC._removeFaces(hook, cube1) OCC.writeCAD(hook, 'out.step', 'fmt_step')
- OCC._trimFaces(hook, faces1, faces2)
Trim set of faces1 with set of faces2.
- Parameters:
hook (CAD hook) – CAD hook
faces1 (list of face indices (starts 1)) – first set of faces
faces2 (list of face indices (starts 1)) – second set of faces
Example of use:
# - trimFaces (pyTree) - import OCC.PyTree as OCC hook = OCC.readCAD("cubeNoTrim.step", "fmt_step") ret = OCC.getFaceNameInOCAF(hook) cube1 = ret[3] cube2 = ret[5] #OCC._translate(hook, (+300.,0,0), cube2) OCC._trimFaces(hook, cube1, cube2) OCC._sewing(hook, tol=1.e-2) OCC.writeCAD(hook, 'out.step', 'fmt_step')