Skip to content

Installation with conda

karmapuzan edited this page May 9, 2025 · 1 revision

Required: pythonocc-core

Install with: pip install pythonocc-core

import math from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Vec, gp_Trsf from OCC.Core.BRepBuilderAPI import ( BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform, ) from OCC.Core.TopoDS import TopoDS_Compound from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism from OCC.Core.TCollection import TCollection_AsciiString from OCC.Core.Font import Font_BRepTextBuilder, Font_FontMgr from OCC.Core.GC import GC_MakeCircle from OCC.Core.BRep import BRep_Builder from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_AsIs from OCC.Core.StlAPI import StlAPI_Writer

========== SHAPE FUNCTIONS ==========

def make_star(radius_outer=50, radius_inner=20, z_height=5): points = [] for i in range(10): angle = math.radians(i * 36) r = radius_outer if i % 2 == 0 else radius_inner x = r * math.cos(angle) y = r * math.sin(angle) points.append(gp_Pnt(x, y, 0))

polygon = BRepBuilderAPI_MakePolygon()
for p in points:
    polygon.Add(p)
polygon.Close()
wire = polygon.Wire()
face = BRepBuilderAPI_MakeFace(wire).Face()
return BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, z_height)).Shape()

def make_heart(z_height=5): from OCC.Core.Geom import Geom_BezierCurve from OCC.Core.TColgp import TColgp_Array1OfPnt from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire

left_curve_pts = TColgp_Array1OfPnt(1, 4)
left_curve_pts.SetValue(1, gp_Pnt(0, 0, 0))
left_curve_pts.SetValue(2, gp_Pnt(-50, 50, 0))
left_curve_pts.SetValue(3, gp_Pnt(-50, 100, 0))
left_curve_pts.SetValue(4, gp_Pnt(0, 120, 0))

right_curve_pts = TColgp_Array1OfPnt(1, 4)
right_curve_pts.SetValue(1, gp_Pnt(0, 120, 0))
right_curve_pts.SetValue(2, gp_Pnt(50, 100, 0))
right_curve_pts.SetValue(3, gp_Pnt(50, 50, 0))
right_curve_pts.SetValue(4, gp_Pnt(0, 0, 0))

edge1 = BRepBuilderAPI_MakeEdge(Geom_BezierCurve(left_curve_pts)).Edge()
edge2 = BRepBuilderAPI_MakeEdge(Geom_BezierCurve(right_curve_pts)).Edge()

wire = BRepBuilderAPI_MakeWire(edge1, edge2).Wire()
face = BRepBuilderAPI_MakeFace(wire).Face()
return BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, z_height)).Shape()

def make_ring(outer_radius=450, inner_radius=400, height=2): outer_circle = GC_MakeCircle(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), outer_radius).Value() inner_circle = GC_MakeCircle(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), inner_radius).Value()

outer_edge = BRepBuilderAPI_MakeEdge(outer_circle).Edge()
inner_edge = BRepBuilderAPI_MakeEdge(inner_circle).Edge()

from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeWire
outer_wire = BRepBuilderAPI_MakeWire(outer_edge).Wire()
inner_wire = BRepBuilderAPI_MakeWire(inner_edge).Wire()

face = BRepBuilderAPI_MakeFace(outer_wire, inner_wire).Face()
return BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, height)).Shape()

def make_extruded_text(text="Mid9ight", height=10, font_size=50): font_mgr = Font_FontMgr.GetInstance() font_name = font_mgr.GetAvailableFonts().Value(1) builder = Font_BRepTextBuilder() label = TCollection_AsciiString(text) ax2 = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) shape2d = builder.Perform(label, font_name, ax2, font_size, False, False) return BRepPrimAPI_MakePrism(shape2d, gp_Vec(0, 0, height)).Shape()

========== ASSEMBLY ==========

shapes = []

ring = make_ring() shapes.append(ring)

Stars around ring

for angle in [30, 90, 150, 210, 270, 330]: star = make_star() angle_rad = math.radians(angle) x = 430 * math.cos(angle_rad) y = 430 * math.sin(angle_rad) trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(0, 0, 0), gp_Vec(x, y, 2)) star_trans = BRepBuilderAPI_Transform(star, trsf).Shape() shapes.append(star_trans)

Heart

heart = make_heart() trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(0, 500, 0)) heart_trans = BRepBuilderAPI_Transform(heart, trsf).Shape() shapes.append(heart_trans)

Mid9ight text

text1 = make_extruded_text("Mid9ight", height=10, font_size=80) trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(-250, 0, 3)) text1_trans = BRepBuilderAPI_Transform(text1, trsf).Shape() shapes.append(text1_trans)

CASINO text

text2 = make_extruded_text("CASINO", height=5, font_size=50) trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(-150, -100, 2)) text2_trans = BRepBuilderAPI_Transform(text2, trsf).Shape() shapes.append(text2_trans)

========== EXPORT ==========

def export_to_files(shapes, step_name="logo_model.step", stl_name="logo_model.stl"): builder = BRep_Builder() compound = TopoDS_Compound() builder.MakeCompound(compound) for s in shapes: builder.Add(compound, s)

# STEP
step_writer = STEPControl_Writer()
step_writer.Transfer(compound, STEPControl_AsIs)
if step_writer.Write(step_name) == 0:
    print(f"✅ STEP exported: {step_name}")
else:
    print("❌ STEP export failed.")

# STL
stl_writer = StlAPI_Writer()
stl_writer.Write(compound, stl_name)
print(f"✅ STL exported: {stl_name}")

export_to_files(shapes)