PythonOcc/elbow: elbow.py

File elbow.py, 2.9 KB (added by Leon Kos, 10 years ago)

Končnni program

Line 
1# -*- coding: utf-8 -*-
2from OCC.gp import *
3from OCC.BRepPrimAPI import *
4from OCC.TopExp import *
5from OCC.TopAbs import *
6from OCC.BRep import *
7from OCC.Geom import *
8from OCC.GCE2d import *
9from OCC.GC import *
10from OCC.Geom2d import *
11from OCC.BRepLib import *
12from OCC.BRepFeat import *
13from OCC.Utils.Topology import Topo
14from OCC.BRepBuilderAPI import *
15from OCC.BRepAlgoAPI import *
16from OCC.BRepFilletAPI import *
17from OCC.TopoDS import *
18from OCC.StlAPI import *
19from math import radians
20
21from OCC.Display.SimpleGui import *
22from OCC.Graphic3d import *
23from OCC.BRepFeat import *
24
25R = 31
26L = 120
27p = gp_Pnt(0.,0.,0)
28d = gp_Dir(1. ,0., 0.) 
29myAxes1 = gp_Ax2(p,d) 
30myPrim1 = BRepPrimAPI_MakeCylinder(myAxes1, R, 100)
31
32mkFillet = BRepFilletAPI_MakeChamfer(myPrim1.Shape())
33ex = TopExp_Explorer(myPrim1.Shape(), TopAbs_EDGE)
34ex2 = TopExp_Explorer(myPrim1.Shape(), TopAbs_FACE)
35edge = TopoDS_edge(ex.Current())
36face = TopoDS_face(ex2.Current())
37mkFillet.Add(15.0, 3.0, edge, face)
38
39myAxes2 = gp_Ax2(p, gp_Dir(0,-1,0))
40myPrim2 = BRepPrimAPI_MakeCylinder(myAxes1, R-5, L)
41myShp1 = BRepAlgoAPI_Cut(mkFillet.Shape(),myPrim2.Shape())
42
43mySphere =  BRepPrimAPI_MakeSphere(p, R)
44
45rotateAxis = gp_Ax1(p, gp_Dir(0.0,0.0,1.0)) #Os rotacije
46transfRot1 = gp_Trsf()
47transfRot1.SetRotation(rotateAxis, radians(90.0))
48myShp2 = BRepBuilderAPI_Transform(myShp1.Shape(), transfRot1)
49myShp3 = BRepAlgoAPI_Fuse(myShp1.Shape(),myShp2.Shape())
50myShp4 = BRepAlgoAPI_Fuse(myShp3.Shape(), mySphere.Shape())
51
52
53# Izdelamo navadno rebro
54rib_thickness = 6.0 # polovica debeline rebra
55rib1 = gp_Pnt(2*R,R,0)
56rib2 = gp_Pnt(R,2*R,0)
57aPlane = Geom_Plane(0,0,1.0,0.0)
58edge1 = BRepBuilderAPI_MakeEdge(rib1, rib2).Edge()
59aWire = BRepBuilderAPI_MakeWire(edge1)
60rib = BRepFeat_MakeLinearForm( myShp4.Shape(), aWire.Wire(), aPlane.GetHandle(), \
61                gp_Vec(0.,0.,rib_thickness), gp_Vec(0.,0.,-rib_thickness), 1, True )
62rib.Perform()
63
64# Poiščemo vse štiri robove na obeh debelinah rebra, ki imajo začetno in končno Z isto
65# in so vzporedni z osjo X ali Y ter jim dodamo spremenljivo debelino zaokrožitve
66TOL=1E-6
67with_fillet = BRepFilletAPI_MakeFillet(rib.Shape())
68topology_traverser = Topo(rib.Shape())
69for aEdge in topology_traverser.edges(): 
70    first, last = TopExp().FirstVertex(aEdge), TopExp().LastVertex(aEdge)
71    first_vert, last_vert = BRep_Tool().Pnt(first), BRep_Tool().Pnt(last)
72    if abs(abs(first_vert.Z())-rib_thickness) < TOL and \
73       abs(first_vert.Z() - last_vert.Z()) < TOL and \
74      (abs(first_vert.X()-last_vert.X())<TOL or abs(first_vert.Y()-last_vert.Y())<TOL):
75        with_fillet.Add(25, 0.0, aEdge)
76
77myPart = with_fillet
78
79stl_writer = StlAPI()
80stl_writer.Write(myPart.Shape(), "elbow.stl", False)
81
82display, start_display, add_menu, add_function_to_menu = init_display()
83material = Graphic3d_MaterialAspect(Graphic3d_NOM_SILVER)
84display.DisplayShape(myPart.Shape(), material)
85start_display()