[PYTHON] 3D or D with Py

There are many opportunities to perform numerical calculations (mainly using Python) even when using 3D CAD, and there is a request to generate (must) data for 3D (STEP, IGES, STL) based on numerical calculations. It is a solution while the number is increasing.

Conclusion

In conclusion, use Open CASCADE.

OpenCASCADE is famous for C ++ and Deathly Hallows Open Cascade Technology, but the original is a library composed of 1 million lines in C ++, so if you try to use it It's very difficult. So I use the great code pythonocc-core, which is a Wrap of almost all OpenCASCADE to Python.

Install method

# Python 3.8
conda install -c conda-forge pythonocc-core
# Python 3.7 or 3.6
conda install -c dlr-sc pythonocc-core

It is a good code group to put in additionally.

How to use

This is a very simple usage example.

from OCC.Display.SimpleGui import init_display
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Dir
from OCC.Core.gp import gp_Ax1, gp_Ax2, gp_Ax3
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Extend.DataExchange import write_step_file, read_step_file

display, start_display, add_menu, add_function = init_display()


display.DisplayShape(gp_Pnt())

p1 = gp_Pnt(0, 0, 0)
p2 = gp_Pnt(0, 0, 10)
edge = BRepBuilderAPI_MakeEdge(p1, p2)
result = edge.Edge()
write_step_file(result, "pythonocc-core.stp")
display.DisplayShape(result)

display.FitAll()
display.View.Dump("pythonocc-core.png ")
start_display()

When you run it, you will see a photo of the 3D Viewer and its Viewer and a STEP file called pythonocc-core.stp.

pythonocc-core.png

How to use it is yet to come. After that, you can move the 3D Viewer around, spit out files other than STEP (IGES, STL, BRep), or create your favorite shape.

There are many samples at demo.

advantage

It may be reasonably good as a 3D Viewer (STEP, IGES, STL can be displayed), but the most useful point for work is that it can spit out STEP. With this, compatibility with commercial CAD is greatly improved.

In addition, Vector calculation, coordinate system conversion, Spilne and Boolean can be used, and the minimum required calculation functions as so-called CAD are implemented, so there is an aspect that it is excellent as a basic geometric calculation library rather than CAD. ..

Of course, if it's about conversion operations, it's not difficult to implement if you write code in various ways, but I'm very grateful that almost all geometric operations have already been implemented.

Digression

It's been a few years since I started using it, but honestly I don't know how it works with 3D Viewer. I wonder if the drawing function of OpenCASCADE is embedded in Qt (PyQt).

I usually use Windows mainly, but when 3D works with Qt just by running conda install in the native environment of Windows, it was quite shocking. (And I realized that it is impossible for me to do it in C ++ including drawing)

Recommended Posts

3D or D with Py
3D plot with matplotlib
3D or D in Py Thon de ~ Spines r Fase ~
Create 3d gif with python3
3D scatter plot with PyQtGraph
Interpolate 2D data with scipy.interpolate.griddata
3D skeleton structure analysis with Python
Solve ABC166 A ~ D with Python
Run mruby with Python or Blender
3D drawing with SceneKit in Pythonista
Optimize RF or lightGBM with Optuna
selenium: wait for element with AND / OR
Express Max Pooling 2D only with Conv2D
2D FEM stress analysis program with Python
Solve AtCoder ABC168 with python (A ~ D)
Working with 3D data structures in pandas
By linking Maya with Excel or spreadsheets ~
Try assigning or switching with Python: lambda
2D physics simulation with Box2d and wxPython