|
| 1 | +##Copyright 2009-2017 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 | + |
| 18 | +import sys |
| 19 | + |
| 20 | +from OCC.gp import gp_Pnt |
| 21 | +from OCC.TopAbs import TopAbs_FACE |
| 22 | +from OCC.TopExp import TopExp_Explorer |
| 23 | +from OCC.TopoDS import TopoDS_Compound, topods_Face |
| 24 | +from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace |
| 25 | +from OCC.GeomAPI import GeomAPI_PointsToBSpline |
| 26 | +from OCC.GeomFill import GeomFill_ConstrainedFilling, GeomFill_SimpleBound |
| 27 | +from OCC.GeomAdaptor import GeomAdaptor_HCurve |
| 28 | +from OCC.TColgp import TColgp_Array1OfPnt |
| 29 | +from OCC.BRep import BRep_Builder, BRep_Tool |
| 30 | +from OCC.BRepMesh import BRepMesh_IncrementalMesh |
| 31 | +from OCC.TopLoc import TopLoc_Location |
| 32 | +from OCC.MeshVS import MeshVS_Mesh, MeshVS_BP_Mesh, MeshVS_MeshPrsBuilder, MeshVS_DMF_NodalColorDataPrs |
| 33 | + |
| 34 | +# SMESH wrappers |
| 35 | +from OCC.SMESH import SMESH_Gen, SMESH_MeshVSLink |
| 36 | +from OCC.StdMeshers import (StdMeshers_Arithmetic1D, StdMeshers_TrianglePreference, |
| 37 | + StdMeshers_Regular_1D, StdMeshers_Quadrangle_2D, |
| 38 | + StdMeshers_MEFISTO_2D) |
| 39 | + |
| 40 | +from OCC.Display.SimpleGui import init_display |
| 41 | +display, start_display, add_menu, add_function_to_menu = init_display() |
| 42 | + |
| 43 | +def point_list_to_TColgp_Array1OfPnt(li): |
| 44 | + pts = TColgp_Array1OfPnt(0, len(li)-1) |
| 45 | + for n, i in enumerate(li): |
| 46 | + pts.SetValue(n, i) |
| 47 | + return pts |
| 48 | + |
| 49 | +def get_simple_bound(pts): |
| 50 | + spl1 = GeomAPI_PointsToBSpline(pts).Curve() |
| 51 | + spl1_adap_h = GeomAdaptor_HCurve(spl1).GetHandle() |
| 52 | + bound1_h = GeomFill_SimpleBound(spl1_adap_h, 0.001, 0.001).GetHandle() |
| 53 | + return spl1, bound1_h |
| 54 | + |
| 55 | +def constrained_filling(event=None): |
| 56 | + |
| 57 | + # left |
| 58 | + pts1 = point_list_to_TColgp_Array1OfPnt((gp_Pnt(0, 0, 0.0), |
| 59 | + gp_Pnt(0, 1, 0.3), |
| 60 | + gp_Pnt(0, 2, -0.3), |
| 61 | + gp_Pnt(0, 3, 0.15), |
| 62 | + gp_Pnt(0, 4, 0))) |
| 63 | + # front |
| 64 | + pts2 = point_list_to_TColgp_Array1OfPnt((gp_Pnt(0, 0, 0.0), |
| 65 | + gp_Pnt(1, 0, -0.3), |
| 66 | + gp_Pnt(2, 0, 0.15), |
| 67 | + gp_Pnt(3, 0, 0), |
| 68 | + gp_Pnt(4, 0, 0))) |
| 69 | + # back |
| 70 | + pts3 = point_list_to_TColgp_Array1OfPnt((gp_Pnt(0, 4, 0), |
| 71 | + gp_Pnt(1, 4, 0.3), |
| 72 | + gp_Pnt(2, 4, -0.15), |
| 73 | + gp_Pnt(3, 4, 0), |
| 74 | + gp_Pnt(4, 4, 1))) |
| 75 | + # rechts |
| 76 | + pts4 = point_list_to_TColgp_Array1OfPnt((gp_Pnt(4, 0, 0), |
| 77 | + gp_Pnt(4, 1, 0), |
| 78 | + gp_Pnt(4, 2, 2), |
| 79 | + gp_Pnt(4, 3, -0.15), |
| 80 | + gp_Pnt(4, 4, 1))) |
| 81 | + |
| 82 | + spl1, b1 = get_simple_bound(pts1) |
| 83 | + spl2, b2 = get_simple_bound(pts2) |
| 84 | + spl3, b3 = get_simple_bound(pts3) |
| 85 | + spl4, b4 = get_simple_bound(pts4) |
| 86 | + |
| 87 | + # build the constrained surface |
| 88 | + bConstrainedFilling = GeomFill_ConstrainedFilling(8, 2) |
| 89 | + bConstrainedFilling.Init(b1, b2, b3, b4, False) |
| 90 | + srf1 = bConstrainedFilling.Surface() |
| 91 | + |
| 92 | + display.EraseAll() |
| 93 | + for i in [spl1, spl2, spl3, spl4]: |
| 94 | + edg = BRepBuilderAPI_MakeEdge(i) |
| 95 | + edg.Build() |
| 96 | + _edg = edg.Shape() |
| 97 | + display.DisplayShape(_edg) |
| 98 | + |
| 99 | + f = BRepBuilderAPI_MakeFace(srf1, 1e-6) |
| 100 | + f.Build() |
| 101 | + shp = f.Shape() |
| 102 | + return shp |
| 103 | + |
| 104 | +def exit(event=None): |
| 105 | + sys.exit(0) |
| 106 | + |
| 107 | +def occ_triangle_mesh(event=None): |
| 108 | + # |
| 109 | + # Mesh the shape |
| 110 | + # |
| 111 | + BRepMesh_IncrementalMesh(aShape, 0.1) |
| 112 | + builder = BRep_Builder() |
| 113 | + Comp = TopoDS_Compound() |
| 114 | + builder.MakeCompound(Comp) |
| 115 | + |
| 116 | + ex = TopExp_Explorer(aShape, TopAbs_FACE) |
| 117 | + while ex.More(): |
| 118 | + F = topods_Face(ex.Current()) |
| 119 | + L = TopLoc_Location() |
| 120 | + facing = (BRep_Tool().Triangulation(F, L)).GetObject() |
| 121 | + tab = facing.Nodes() |
| 122 | + tri = facing.Triangles() |
| 123 | + for i in range(1, facing.NbTriangles()+1): |
| 124 | + trian = tri.Value(i) |
| 125 | + #print trian |
| 126 | + index1, index2, index3 = trian.Get() |
| 127 | + for j in range(1, 4): |
| 128 | + if j == 1: |
| 129 | + M = index1 |
| 130 | + N = index2 |
| 131 | + elif j == 2: |
| 132 | + N = index3 |
| 133 | + elif j == 3: |
| 134 | + M = index2 |
| 135 | + ME = BRepBuilderAPI_MakeEdge(tab.Value(M), tab.Value(N)) |
| 136 | + if ME.IsDone(): |
| 137 | + builder.Add(Comp, ME.Edge()) |
| 138 | + ex.Next() |
| 139 | + display.DisplayShape(Comp, update=True) |
| 140 | + |
| 141 | +def smesh_quadrangle_mesh(event=None): |
| 142 | + # Create the Mesh |
| 143 | + aMeshGen = SMESH_Gen() |
| 144 | + aMesh = aMeshGen.CreateMesh(0, True) |
| 145 | + # 1D |
| 146 | + an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen)#discretization of the wire |
| 147 | + an1DHypothesis.SetLength(0.01, False) # the smallest distance between 2 points |
| 148 | + an1DHypothesis.SetLength(0.3, True) # the longest distance between 2 points |
| 149 | + an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation |
| 150 | + # 2D |
| 151 | + a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) # define the boundary |
| 152 | + a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh |
| 153 | + #Calculate mesh |
| 154 | + aMesh.ShapeToMesh(aShape) |
| 155 | + #Assign hyptothesis to mesh |
| 156 | + aMesh.AddHypothesis(aShape, 0) |
| 157 | + aMesh.AddHypothesis(aShape, 1) |
| 158 | + aMesh.AddHypothesis(aShape, 2) |
| 159 | + aMesh.AddHypothesis(aShape, 3) |
| 160 | + #Compute the data |
| 161 | + aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) |
| 162 | + # Display the data |
| 163 | + display_mesh(aMesh) |
| 164 | + |
| 165 | +def smesh_MEFISTO2D(event=None): |
| 166 | + # Create the Mesh |
| 167 | + aMeshGen = SMESH_Gen() |
| 168 | + aMesh = aMeshGen.CreateMesh(0, True) |
| 169 | + # 1D |
| 170 | + an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) # discretization of the wire |
| 171 | + an1DHypothesis.SetLength(0.1, False) # the smallest distance between 2 points |
| 172 | + an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points |
| 173 | + an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation |
| 174 | + # 2D |
| 175 | + a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) # define the boundary |
| 176 | + a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) |
| 177 | + # alculate mesh |
| 178 | + aMesh.ShapeToMesh(aShape) |
| 179 | + # Assign hyptothesis to mesh |
| 180 | + aMesh.AddHypothesis(aShape, 0) |
| 181 | + aMesh.AddHypothesis(aShape, 1) |
| 182 | + aMesh.AddHypothesis(aShape, 2) |
| 183 | + aMesh.AddHypothesis(aShape, 3) |
| 184 | + # Compute the data |
| 185 | + aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) |
| 186 | + # Display the data |
| 187 | + display_mesh(aMesh) |
| 188 | + |
| 189 | +def display_mesh(the_mesh): |
| 190 | + # First, erase all |
| 191 | + display.EraseAll() |
| 192 | + # then redisplay the shape |
| 193 | + display.DisplayShape(aShape) |
| 194 | + # then the mesh |
| 195 | + aDS = SMESH_MeshVSLink(the_mesh) |
| 196 | + aMeshVS = MeshVS_Mesh(True) |
| 197 | + DMF = 1 # to wrap! |
| 198 | + aPrsBuilder = MeshVS_MeshPrsBuilder(aMeshVS.GetHandle(), |
| 199 | + DMF, |
| 200 | + aDS.GetHandle(), |
| 201 | + 0, |
| 202 | + MeshVS_BP_Mesh) |
| 203 | + aMeshVS.SetDataSource(aDS.GetHandle()) |
| 204 | + aMeshVS.AddBuilder(aPrsBuilder.GetHandle(), True) |
| 205 | + #Create the graphic window and display the mesh |
| 206 | + context = display.Context |
| 207 | + context.Display(aMeshVS.GetHandle()) |
| 208 | + context.Deactivate(aMeshVS.GetHandle()) |
| 209 | + |
| 210 | + display.FitAll() |
| 211 | + |
| 212 | +aShape = constrained_filling() |
| 213 | + |
| 214 | +if __name__ == '__main__': |
| 215 | + add_menu('surfacic mesh') |
| 216 | + add_function_to_menu('surfacic mesh', occ_triangle_mesh) |
| 217 | + add_function_to_menu('surfacic mesh', smesh_quadrangle_mesh) |
| 218 | + add_function_to_menu('surfacic mesh', smesh_MEFISTO2D) |
| 219 | + add_function_to_menu('surfacic mesh', exit) |
| 220 | + display.DisplayShape(aShape, update=True) |
| 221 | + start_display() |
0 commit comments