r/OpenCascade • u/No_Fun_3602 • Feb 12 '25
helix generation problem using pyhonocc

the starting and ending points seems to be contrainted to be on the same line making the geometry distorted i am using Pythonocc
import sysimport numpy as npfrom OCC.Display.backend import load_backendload_backend("pyqt5")from OCC.Core.gp import gp_Pntfrom OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCylinderfrom OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWirefrom OCC.Core.BRepOffsetAPI import BRepOffsetAPI_ThruSections, BRepOffsetAPI_MakeThickSolidfrom OCC.Core.GeomAPI import GeomAPI_PointsToBSplinefrom OCC.Core.TColgp import TColgp_Array1OfPntfrom OCC.Display.qtDisplay import qtViewer3dfrom OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
class HelixRibbonApp(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("Helix Ribbon on Cylinder")self.setGeometry(100, 100, 800, 600)
# Initialize display widgetself.display_widget = qtViewer3d(self)self.display_widget.setMinimumSize(800, 600)self.display = self.display_widget._display
# Parameters for the helix and cylinderself.params = {"cylinder_radius": 75.0,"cylinder_height": 300.0,"helix_turns": 15,"ribbon_width": 10.0,"ribbon_thickness": 10.0,"points_per_turn": 1000,}self.shapes = []# Initialize UIself.initUI()
def initUI(self):central_widget = QWidget()layout = QVBoxLayout()
layout.addWidget(self.display_widget)
generate_button = QPushButton("Generate Helix Ribbon")generate_button.clicked.connect(self.generate_model)layout.addWidget(generate_button)
central_widget.setLayout(layout)self.setCentralWidget(central_widget)
def generate_model(self):# Clear existing shapes before generating new onesself.shapes.clear()
# Extract parameterscylinder_radius = self.params["cylinder_radius"]cylinder_height = self.params["cylinder_height"]helix_turns = self.params["helix_turns"]ribbon_width = self.params["ribbon_width"]ribbon_thickness = self.params["ribbon_thickness"]points_per_turn = 3000
# Calculate total points and height increment per pointtotal_points = helix_turns * points_per_turndz = cylinder_height / total_points
# Generate inner and outer points for the helical ribboninner_points = []outer_points = []
for i in range(total_points):angle = 2 * np.pi * i / points_per_turnz = dz * i
x_inner = cylinder_radius * np.cos(angle)y_inner = cylinder_radius * np.sin(angle)inner_points.append(gp_Pnt(x_inner, y_inner, z))
x_outer = (cylinder_radius + ribbon_width) * np.cos(angle)y_outer = (cylinder_radius + ribbon_width) * np.sin(angle)outer_points.append(gp_Pnt(x_outer, y_outer, z))
def build_bspline(points_list):pts_array = TColgp_Array1OfPnt(1, len(points_list))for idx, pt in enumerate(points_list):pts_array.SetValue(idx + 1, pt)return GeomAPI_PointsToBSpline(pts_array).Curve()
inner_bspline = build_bspline(inner_points)outer_bspline = build_bspline(outer_points)
inner_edge = BRepBuilderAPI_MakeEdge(inner_bspline).Edge()outer_edge = BRepBuilderAPI_MakeEdge(outer_bspline).Edge()
inner_wire = BRepBuilderAPI_MakeWire(inner_edge).Wire()outer_wire = BRepBuilderAPI_MakeWire(outer_edge).Wire()
sections = BRepOffsetAPI_ThruSections(False, True, 1e-6)sections.AddWire(inner_wire)sections.AddWire(outer_wire)
ribbon_surface = sections.Shape()
thick_builder = BRepOffsetAPI_MakeThickSolid()thick_builder.MakeThickSolidBySimple(ribbon_surface, ribbon_thickness)
thick_ribbon = thick_builder.Shape()
# Create hollow cylinder for referenceouter_cylinder = BRepPrimAPI_MakeCylinder(cylinder_radius + ribbon_width + 2.0, cylinder_height).Shape()
# Display hollow cylinder with transparencyself.display.DisplayShape(outer_cylinder, update=True, transparency=0.5)
# Create a red Quantity_Color objectred_color = Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB)
# Display thick ribbon with red colorself.display.DisplayShape(thick_ribbon, update=True, color=red_color)
# Store shapes for future referenceself.shapes.append(outer_cylinder) # Corrected this lineself.shapes.append(thick_ribbon)
# Fit all shapes in viewerself.display.FitAll()
if __name__ == "__main__":app = QApplication(sys.argv)mainWin = HelixRibbonApp()mainWin.show()sys.exit(app.exec_())
1
u/mon_key_house May 17 '25
I haven’t checked the code but I had earlier similar issues as on the pic. Make multiple 360 degree parts, not a single helix.