import geometry to gmsh

482 views
Skip to first unread message

Zoli Takács

unread,
Jun 22, 2022, 8:14:11 AM6/22/22
to CadQuery
hi, i would like to import geometry to gmsh from cq 

import cadquery as cq
import gmsh
import math
import os
import sys

#create cadquery geometry

geometry = cq.Workplane("front").box(2.0, 2.0, 0.5)
shape = geometry.toOCC()

#import geometry to gmsh
gmsh.initialize()
gmsh.model.add("box")
gmsh.model.occ.importShapesNativePointer(shape, highestDimOnly=True)
gmsh.model.occ.synchronize()

#generate mesh
gmsh.model.mesh.generate(3)

#export mesh
gmsh.write("box.msh")

# Launch the GUI to see the results:
if '-nopopup' not in sys.argv:
    gmsh.fltk.run()

gmsh.finalize()


my problem is that 
geometry.toOCC() gives back OCP.TopoDS.TopoDS_Solid type and as per gmsh doc 

def importShapesNativePointer(shape, highestDimOnly=True): """ gmsh.model.occ.importShapesNativePointer(shape, highestDimOnly=True) Imports an OpenCASCADE `shape' by providing a pointer to a native OpenCASCADE `TopoDS_Shape' object (passed as a pointer to void). The imported entities are returned in `outDimTags'. If the optional argument `highestDimOnly' is set, only import the highest dimensional entities in `shape'. In Python, this function can be used for integration with PythonOCC, in which the SwigPyObject pointer of `TopoDS_Shape' must be passed as an int to `shape', i.e., `shape = int(pythonocc_shape.this)'. Warning: this function is unsafe, as providing an invalid pointer will lead to undefined behavior. Return `outDimTags'. """

I need a TopoDS_Shape . Do anyone has an idea, how could i bypass this problem? 

Adam Urbanczyk

unread,
Jun 24, 2022, 4:45:48 PM6/24/22
to CadQuery
I don't think it'll work. Sounds like gmsh is using PythonOCC (SWIG) and cq is definitely using OCP (pybind11). I don't know if gmsh supports it, but you could serialize in-memory as brep (cq.Shape.exportBrep) and read that in gmsh. Worst case you'll need to store to disk and import (again likely the best option is brep).

Zoli Takács

unread,
Jun 25, 2022, 6:07:57 AM6/25/22
to CadQuery
I let this issue here. Has a lot of useful information about this topic. Thanks for the help Adam Urbanczyk. 
https://github.com/CadQuery/cadquery/issues/449

drshi...@gmail.com

unread,
Jun 27, 2022, 11:19:31 AM6/27/22
to CadQuery
The code in that issue will do the job :-)

However, serialise in memory suggestion sounds like a better solution compared to the Brep file exporting and then reading in the file in Gmsh. I guess the in memory method would be quicker as there is less IO.

On a slightly related question.

Does anyone know if it is possible to access the vertices/coordinate values of the STL geometry without writing the file and reading in the contents.

Currently I've saving STL files then reading them back in and accessing the STL vertices and connections for each triangle.

Ideally I wouldn't actually save the STL files but I would be able to obtain the vertices and connectivity data in memory.

All the best

Jon


Adam Urbanczyk

unread,
Jun 28, 2022, 5:21:29 AM6/28/22
to CadQuery
Reply all
Reply to author
Forward
0 new messages