PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/unittest/topology_building_unittest.py

http://pythonocc.googlecode.com/
Python | 296 lines | 219 code | 34 blank | 43 comment | 3 complexity | d4dcc576e254ed35f98149817f89b2cc MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, Apache-2.0
  1. ##Copyright 2010 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 OCC.gp import *
  18. from OCC.BRepBuilderAPI import *
  19. from OCC.GeomAPI import *
  20. from OCC.Geom import *
  21. from OCC.GeomAbs import *
  22. from OCC.TColgp import *
  23. from OCC.Geom2d import *
  24. from OCC.TColgp import *
  25. from OCC.BRepOffsetAPI import *
  26. from OCC.GeomAbs import *
  27. from OCC.BRepPrimAPI import *
  28. from OCC.Utils.Topology import Topo
  29. from OCC.BRep import *
  30. from OCC.Precision import *
  31. from OCC.BRepLib import *
  32. import math, sys
  33. import unittest
  34. class TestTopologyBuilding(unittest.TestCase):
  35. def test_edge(self):
  36. print 'Test: edge'
  37. # The blud edge
  38. BlueEdge = BRepBuilderAPI_MakeEdge(gp_Pnt(-80,-50,-20),gp_Pnt(-30,-60,-60))
  39. V1 = BRepBuilderAPI_MakeVertex(gp_Pnt(-20,10,-30))
  40. V2 = BRepBuilderAPI_MakeVertex(gp_Pnt(10,7,-25))
  41. YellowEdge = BRepBuilderAPI_MakeEdge(V1.Vertex(),V2.Vertex())
  42. #The white edge
  43. line = gp_Lin(gp_Ax1(gp_Pnt(10,10,10),gp_Dir(1,0,0)))
  44. WhiteEdge = BRepBuilderAPI_MakeEdge(line,-20,10)
  45. #The red edge
  46. Elips = gp_Elips(gp_Ax2(gp_Pnt(10,0,0),gp_Dir(1,1,1)),60,30)
  47. RedEdge = BRepBuilderAPI_MakeEdge(Elips,0,math.pi/2)
  48. # The green edge and the both extreme vertex
  49. P1=gp_Pnt(-15,200,10)
  50. P2=gp_Pnt(5,204,0)
  51. P3=gp_Pnt(15,200,0)
  52. P4=gp_Pnt(-15,20,15)
  53. P5=gp_Pnt(-5,20,0)
  54. P6=gp_Pnt(15,20,0)
  55. P7=gp_Pnt(24,120,0)
  56. P8=gp_Pnt(-24,120,12.5)
  57. array=TColgp_Array1OfPnt(1,8)
  58. array.SetValue(1,P1)
  59. array.SetValue(2,P2)
  60. array.SetValue(3,P3)
  61. array.SetValue(4,P4)
  62. array.SetValue(5,P5)
  63. array.SetValue(6,P6)
  64. array.SetValue(7,P7)
  65. array.SetValue(8,P8)
  66. curve = Geom_BezierCurve(array)
  67. ME = BRepBuilderAPI_MakeEdge(curve.GetHandle())
  68. GreenEdge = ME
  69. V3 = ME.Vertex1()
  70. V4 = ME.Vertex2()
  71. def test_wire(self):
  72. print 'Test: wire'
  73. # The red wire is build from a single edge
  74. Elips = gp_Elips(gp_Ax2(gp_Pnt(250,0,0),gp_Dir(1,1,1)),160,90)
  75. Edge1 = BRepBuilderAPI_MakeEdge(Elips,0,math.pi/2).Edge()
  76. RedWire = BRepBuilderAPI_MakeWire(Edge1).Wire()
  77. ### the yellow wire is build from an existing wire and an edge
  78. circle = gp_Circ(gp_Ax2(gp_Pnt(-300,0,0),gp_Dir(1,0,0)),80)
  79. Edge2 = BRepBuilderAPI_MakeEdge(circle,0,math.pi).Edge()
  80. ExistingWire = BRepBuilderAPI_MakeWire(Edge2).Wire()
  81. Edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(-300,0,-80),gp_Pnt(-90,20,-30)).Edge()
  82. MW1 = BRepBuilderAPI_MakeWire(ExistingWire,Edge3)
  83. self.assertTrue(MW1.IsDone())
  84. YellowWire = MW1.Wire()
  85. ### the white wire is built with an existing wire and 3 edges.
  86. ### we use the methods Add, Edge and Vertex from BRepBuilderAPI_MakeWire.
  87. circle2 = gp_Circ(gp_Ax2(gp_Pnt(0,0,0),gp_Dir(0,1,0)),200)
  88. Edge4 = BRepBuilderAPI_MakeEdge(circle2,0,math.pi).Edge()
  89. ExistingWire2 = BRepBuilderAPI_MakeWire(Edge4).Wire()
  90. P1 = gp_Pnt(0,0,-200)
  91. P2 = gp_Pnt(5,204,0)
  92. Edge5 = BRepBuilderAPI_MakeEdge(P1,P2).Edge()
  93. P3 = gp_Pnt(-15,20,15)
  94. Edge6 = BRepBuilderAPI_MakeEdge(P2,P3).Edge()
  95. P4 = gp_Pnt(15,20,0)
  96. Edge7 = BRepBuilderAPI_MakeEdge(P3,P4).Edge()
  97. MW = BRepBuilderAPI_MakeWire()
  98. MW.Add(ExistingWire2)
  99. MW.Add(Edge5)
  100. MW.Add(Edge6)
  101. MW.Add(Edge7)
  102. self.assertTrue(MW.IsDone())
  103. WhiteWire = MW.Wire()
  104. LastEdge = MW.Edge()
  105. LastVertex = MW.Vertex()
  106. def test_face(self):
  107. print 'Test: face'
  108. P1 = gp_Pnt()
  109. P2 = gp_Pnt()
  110. P3 = gp_Pnt()
  111. P4 = gp_Pnt()
  112. P5 = gp_Pnt()
  113. P6 = gp_Pnt()
  114. # The white Face
  115. sphere = gp_Sphere(gp_Ax3(gp_Pnt(0,0,0),gp_Dir(1,0,0)),150)
  116. WhiteFace = BRepBuilderAPI_MakeFace(sphere,0.1,0.7,0.2,0.9)
  117. # The red face
  118. P1.SetCoord(-15,200,10)
  119. P2.SetCoord(5,204,0)
  120. P3.SetCoord(15,200,0)
  121. P4.SetCoord(-15,20,15)
  122. P5.SetCoord(-5,20,0)
  123. P6.SetCoord(15,20,35)
  124. array = TColgp_Array2OfPnt(1,3,1,2)
  125. array.SetValue(1,1,P1)
  126. array.SetValue(2,1,P2)
  127. array.SetValue(3,1,P3)
  128. array.SetValue(1,2,P4)
  129. array.SetValue(2,2,P5)
  130. array.SetValue(3,2,P6)
  131. curve = GeomAPI_PointsToBSplineSurface(array,3,8,GeomAbs_C2,0.001).Surface()
  132. RedFace = BRepBuilderAPI_MakeFace(curve)
  133. #The brown face
  134. circle = gp_Circ(gp_Ax2(gp_Pnt(0,0,0),gp_Dir(1,0,0)),80)
  135. Edge1 = BRepBuilderAPI_MakeEdge(circle,0,math.pi)
  136. Edge2 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,0,-80),gp_Pnt(0,-10,40))
  137. Edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,-10,40),gp_Pnt(0,0,80))
  138. ##
  139. ##TopoDS_Wire YellowWire;
  140. MW1 = BRepBuilderAPI_MakeWire(Edge1.Edge(),Edge2.Edge(),Edge3.Edge())
  141. self.assertTrue(MW1.IsDone())
  142. YellowWire = MW1.Wire()
  143. BrownFace = BRepBuilderAPI_MakeFace(YellowWire)
  144. #The pink face
  145. P1.SetCoord(35,-200,40)
  146. P2.SetCoord(50,-204,30)
  147. P3.SetCoord(65,-200,30)
  148. P4.SetCoord(35,-20,45)
  149. P5.SetCoord(45,-20,30)
  150. P6.SetCoord(65,-20,65)
  151. array2 = TColgp_Array2OfPnt(1,3,1,2)
  152. array2.SetValue(1,1,P1)
  153. array2.SetValue(2,1,P2)
  154. array2.SetValue(3,1,P3)
  155. array2.SetValue(1,2,P4)
  156. array2.SetValue(2,2,P5)
  157. array2.SetValue(3,2,P6)
  158. ##
  159. BSplineSurf = GeomAPI_PointsToBSplineSurface(array2,3,8,GeomAbs_C2,0.001)
  160. ##
  161. aFace = BRepBuilderAPI_MakeFace(BSplineSurf.Surface()).Face()
  162. ##
  163. ##//2d lines
  164. P12d = gp_Pnt2d(0.9,0.1)
  165. P22d = gp_Pnt2d(0.2,0.7)
  166. P32d = gp_Pnt2d(0.02,0.1)
  167. ##
  168. line1 = Geom2d_Line(P12d,gp_Dir2d((0.2-0.9),(0.7-0.1)))
  169. line2 = Geom2d_Line(P22d,gp_Dir2d((0.02-0.2),(0.1-0.7)))
  170. line3 = Geom2d_Line(P32d,gp_Dir2d((0.9-0.02),(0.1-0.1)))
  171. ##
  172. ##//Edges are on the BSpline surface
  173. Edge1 = BRepBuilderAPI_MakeEdge(line1.GetHandle(),BSplineSurf.Surface(),0,P12d.Distance(P22d)).Edge()
  174. Edge2 = BRepBuilderAPI_MakeEdge(line2.GetHandle(),BSplineSurf.Surface(),0,P22d.Distance(P32d)).Edge()
  175. Edge3 = BRepBuilderAPI_MakeEdge(line3.GetHandle(),BSplineSurf.Surface(),0,P32d.Distance(P12d)).Edge()
  176. ##
  177. Wire1 = BRepBuilderAPI_MakeWire(Edge1,Edge2,Edge3).Wire()
  178. Wire1.Reverse()
  179. PinkFace = BRepBuilderAPI_MakeFace(aFace,Wire1).Face()
  180. BRepLib_BuildCurves3d(PinkFace)
  181. def test_evolved_shape(self):
  182. print 'Test: evolved shape'
  183. P = BRepBuilderAPI_MakePolygon()
  184. P.Add(gp_Pnt(0.,0.,0.))
  185. P.Add(gp_Pnt(200.,0.,0.))
  186. P.Add(gp_Pnt(200.,200.,0.))
  187. P.Add(gp_Pnt(0.,200.,0.))
  188. P.Add(gp_Pnt(0.,0.,0.))
  189. wprof = BRepBuilderAPI_MakePolygon(gp_Pnt(0.,0.,0.),gp_Pnt(-60.,-60.,-200.))
  190. S = BRepOffsetAPI_MakeEvolved(P.Wire(),wprof.Wire(),GeomAbs_Arc,True,False,True,0.0001)
  191. S.Build()
  192. self.assertTrue(S.IsDone())
  193. def test_draft_angle(self):
  194. print 'Test: draft angle'
  195. S = BRepPrimAPI_MakeBox(200.,300.,150.).Shape()
  196. adraft = BRepOffsetAPI_DraftAngle(S)
  197. topo = Topo(S)
  198. for f in topo.faces():
  199. surf = Handle_Geom_Plane_DownCast(BRep_Tool_Surface(f)).GetObject()
  200. dirf = surf.Pln().Axis().Direction()
  201. print 'direction',dirf.Coord()
  202. ddd = gp_Dir(0,0,1)
  203. if dirf.IsNormal(ddd, Precision_Angular()):
  204. adraft.Add(f, ddd, math.radians(15), gp_Pln(gp_Ax3(gp_XOY())))
  205. adraft.Build()
  206. self.assertTrue(adraft.IsDone())
  207. def test_through_sections(self):
  208. print 'Test: through sections'
  209. c1 = gp_Circ(gp_Ax2(gp_Pnt(-100.,0.,-100.),gp_Dir(0.,0.,1.)),40.)
  210. W1 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c1).Edge()).Wire()
  211. c2 = gp_Circ(gp_Ax2(gp_Pnt(-10.,0.,-0.),gp_Dir(0.,0.,1.)),40.)
  212. W2 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c2).Edge()).Wire()
  213. c3 = gp_Circ(gp_Ax2(gp_Pnt(-75.,0.,100.),gp_Dir(0.,0.,1.)),40.)
  214. W3 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c3).Edge()).Wire()
  215. c4= gp_Circ(gp_Ax2(gp_Pnt(0.,0.,200.),gp_Dir(0.,0.,1.)),40.)
  216. W4 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c4).Edge()).Wire()
  217. generatorA = BRepOffsetAPI_ThruSections(False, True)
  218. map(generatorA.AddWire, [W1,W2,W3,W4])
  219. generatorA.Build()
  220. self.assertTrue(generatorA.IsDone())
  221. self.assertFalse(generatorA.Shape().IsNull())
  222. #smooth
  223. c1b = gp_Circ(gp_Ax2(gp_Pnt(100.,0.,-100.),gp_Dir(0.,0.,1.)),40.)
  224. W1b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c1b).Edge()).Wire()
  225. c2b = gp_Circ(gp_Ax2(gp_Pnt(210.,0.,-0.),gp_Dir(0.,0.,1.)),40.)
  226. W2b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c2b).Edge()).Wire()
  227. c3b = gp_Circ(gp_Ax2(gp_Pnt(275.,0.,100.),gp_Dir(0.,0.,1.)),40.)
  228. W3b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c3b).Edge()).Wire()
  229. c4b= gp_Circ(gp_Ax2(gp_Pnt(200.,0.,200.),gp_Dir(0.,0.,1.)),40.)
  230. W4b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(c4b).Edge()).Wire()
  231. generatorB = BRepOffsetAPI_ThruSections(True, False)
  232. map(generatorB.AddWire, [W1b,W2b,W3b,W4b])
  233. generatorB.Build()
  234. self.assertTrue(generatorB.IsDone())
  235. self.assertFalse(generatorB.Shape().IsNull())
  236. def test_prism(self):
  237. print 'Test: prism'
  238. #Prism a vertex > result is an edge
  239. V1 = BRepBuilderAPI_MakeVertex(gp_Pnt(200.,200.,0.))
  240. S1 = BRepPrimAPI_MakePrism(V1.Shape(),gp_Vec(0.,0.,100.))
  241. #Prism an edge > result is a face
  242. E = BRepBuilderAPI_MakeEdge(gp_Pnt(150.,150,0.), gp_Pnt(50.,50,0.))
  243. S2 = BRepPrimAPI_MakePrism(E.Shape(),gp_Vec(0.,0.,100.))
  244. #Prism an wire > result is a shell
  245. E1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.,0.,0.), gp_Pnt(50.,0.,0.))
  246. E2 = BRepBuilderAPI_MakeEdge(gp_Pnt(50.,0.,0.), gp_Pnt(50.,50.,0.))
  247. E3 = BRepBuilderAPI_MakeEdge(gp_Pnt(50.,50.,0.), gp_Pnt(0.,0.,0.))
  248. W = BRepBuilderAPI_MakeWire(E1.Edge(),E2.Edge(),E3.Edge())
  249. S3 = BRepPrimAPI_MakePrism(W.Shape(),gp_Vec(0.,0.,100.))
  250. #Prism a face or a shell > result is a solid
  251. c = gp_Circ(gp_Ax2(gp_Pnt(200.,200.,0.),gp_Dir(0.,0.,1.)), 80.)
  252. Ec = BRepBuilderAPI_MakeEdge(c)
  253. Wc = BRepBuilderAPI_MakeWire(Ec.Edge())
  254. F = BRepBuilderAPI_MakeFace(gp_Pln(gp_Ax3(gp_XOY())),Wc.Wire())
  255. S4 = BRepPrimAPI_MakePrism(F.Shape(),gp_Vec(0.,0.,100.))
  256. self.assertTrue(S4.IsDone())
  257. def suite():
  258. suite = unittest.TestSuite()
  259. suite.addTest(unittest.makeSuite(TestTopologyBuilding))
  260. return suite
  261. if __name__== '__main__':
  262. unittest.main()