|
| 1 | +##Copyright 2009-2016 Thomas Paviot (tpaviot@gmail.com) |
| 2 | +## |
| 3 | +##This file is part of pythonOCC. |
| 4 | +## |
| 5 | +##pythonOCC is free software: you can redistribute it and/or modify |
| 6 | +##it under the terms of the GNU Lesser General Public License as published by |
| 7 | +##the Free Software Foundation, either version 3 of the License, or |
| 8 | +##(at your option) any later version. |
| 9 | +## |
| 10 | +##pythonOCC is distributed in the hope that it will be useful, |
| 11 | +##but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +##GNU Lesser General Public License for more details. |
| 14 | +## |
| 15 | +##You should have received a copy of the GNU Lesser General Public License |
| 16 | +##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import sys |
| 20 | +import time |
| 21 | + |
| 22 | +from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, \ |
| 23 | + BRepBuilderAPI_MakeFace |
| 24 | +from OCC.Display.SimpleGui import init_display |
| 25 | +from OCC.GEOMAlgo import GEOMAlgo_Splitter |
| 26 | +from OCC.TopAbs import TopAbs_EDGE |
| 27 | +from OCC.TopExp import TopExp_Explorer |
| 28 | +from OCC.gp import gp_Dir, gp_Pln, gp_Pnt |
| 29 | + |
| 30 | +display, start_display, add_menu, add_function_to_menu = init_display() |
| 31 | + |
| 32 | + |
| 33 | +def split_face_and_edge(event=None): |
| 34 | + display.EraseAll() |
| 35 | + p0 = gp_Pnt() |
| 36 | + vnorm = gp_Dir(1, 0, 0) |
| 37 | + pln = gp_Pln(p0, vnorm) |
| 38 | + face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() |
| 39 | + p1 = gp_Pnt(0, 0, 15) |
| 40 | + p2 = gp_Pnt(0, 0, -15) |
| 41 | + edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() |
| 42 | + # Initialize splitter |
| 43 | + splitter = GEOMAlgo_Splitter() |
| 44 | + # Add both the face and edge as arguments. This will split both of the |
| 45 | + # shapes. |
| 46 | + splitter.AddArgument(face) |
| 47 | + splitter.AddArgument(edge) |
| 48 | + splitter.Perform() |
| 49 | + display.DisplayShape(splitter.Shape()) |
| 50 | + display.FitAll() |
| 51 | + |
| 52 | + |
| 53 | +def split_face_with_edge(event=None): |
| 54 | + display.EraseAll() |
| 55 | + p0 = gp_Pnt() |
| 56 | + vnorm = gp_Dir(1, 0, 0) |
| 57 | + pln = gp_Pln(p0, vnorm) |
| 58 | + face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() |
| 59 | + p1 = gp_Pnt(0, 0, 15) |
| 60 | + p2 = gp_Pnt(0, 0, -15) |
| 61 | + edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() |
| 62 | + # Initialize splitter |
| 63 | + splitter = GEOMAlgo_Splitter() |
| 64 | + # Add the face as an argument and the edge as a tool. This will split |
| 65 | + # the face with the edge. |
| 66 | + splitter.AddArgument(face) |
| 67 | + splitter.AddTool(edge) |
| 68 | + splitter.Perform() |
| 69 | + display.DisplayShape(splitter.Shape()) |
| 70 | + display.FitAll() |
| 71 | + |
| 72 | + |
| 73 | +def split_edge_with_face(event=None): |
| 74 | + display.EraseAll() |
| 75 | + p0 = gp_Pnt() |
| 76 | + vnorm = gp_Dir(1, 0, 0) |
| 77 | + pln = gp_Pln(p0, vnorm) |
| 78 | + face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() |
| 79 | + p1 = gp_Pnt(0, 0, 15) |
| 80 | + p2 = gp_Pnt(0, 0, -15) |
| 81 | + edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() |
| 82 | + # Initialize splitter |
| 83 | + splitter = GEOMAlgo_Splitter() |
| 84 | + # Add the edge as an argument and the face as a tool. This will split |
| 85 | + # the edge with the face. |
| 86 | + splitter.AddArgument(edge) |
| 87 | + splitter.AddTool(face) |
| 88 | + splitter.Perform() |
| 89 | + |
| 90 | + edges = [] |
| 91 | + exp = TopExp_Explorer(splitter.Shape(), TopAbs_EDGE) |
| 92 | + while exp.More(): |
| 93 | + edges.append(exp.Current()) |
| 94 | + exp.Next() |
| 95 | + print('Number of edges in split shape: ', len(edges)) |
| 96 | + display.DisplayShape(edges[0], color='red') |
| 97 | + display.DisplayShape(edges[1], color='green') |
| 98 | + display.DisplayShape(edges[2], color='yellow') |
| 99 | + display.FitAll() |
| 100 | + |
| 101 | + |
| 102 | +def exit(event=None): |
| 103 | + sys.exit() |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + add_menu('GEOMAlgo Splitter Example') |
| 108 | + add_function_to_menu('GEOMAlgo Splitter Example', split_face_and_edge) |
| 109 | + add_function_to_menu('GEOMAlgo Splitter Example', split_face_with_edge) |
| 110 | + add_function_to_menu('GEOMAlgo Splitter Example', split_edge_with_face) |
| 111 | + add_function_to_menu('GEOMAlgo Splitter Example', exit) |
| 112 | + start_display() |
0 commit comments