# -*- coding: utf-8 -*-
"""
Created on Fri Jul 24 16:24:29 2020
@author: JC0101
"""
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeFace
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Display.SimpleGui import init_display
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_VERTEX
from OCC.Core.TopoDS import topods
from OCC.Core.gp import gp
from OCC.Core.BRep import BRep_Tool
def GetTrapezoidShape(x0, x1, x2, x3, z0, z1, aPlane):
# ZOX plane
# x0 is bottom left, x1 is bottom right, x2 is top right, x3 is top left
# z0 is bottom, z1 is top
aP1 = gp_Pnt(x0, 0.0, z0)
aP2 = gp_Pnt(x1, 0.0, z0)
aP3 = gp_Pnt(x2, 0.0, z1)
aP4 = gp_Pnt(x3, 0.0, z1)
aPolygon = BRepBuilderAPI_MakePolygon(aP1, aP2, aP3, aP4, True)
return BRepBuilderAPI_MakeFace(aPlane, aPolygon.Wire()).Shape()
if __name__ == '__main__':
display, start_display, add_menu, add_function_to_menu = init_display()
p0 = gp_Pnt()
vnorm = gp_Dir(0, 1, 0)
aPlane = gp_Pln(p0, vnorm)
bcd1 = 30
tcd1 = 20
ht1 = 40
x0 = -0.5 * bcd1
x1 = 0.5 * bcd1
x2 = 0.5 * tcd1
x3 = -0.5 * tcd1
z0 = 0 # bottom line
z1 = ht1 # top line
aTrapezoid1Shape = GetTrapezoidShape(x0, x1, x2, x3, z0, z1, aPlane)
anVertexExplorer = TopExp_Explorer(aTrapezoid1Shape, TopAbs_VERTEX)
vertex = []
while anVertexExplorer.More(): # 有更多子形状去挖掘
anVertex = topods.Vertex(anVertexExplorer.Current()) # 当前被探索到的子形状是哪一个, topods_Vertex
aPnt = BRep_Tool.Pnt(anVertex) # 转换成gp_Pnt
vertex.append(aPnt)
display.DisplayShape(anVertex, update = True)
anVertexExplorer.Next() # 下一个子形状
pnts = []
for v in vertex:
coordinate = (v.X(), v.Y(), v.Z())
if coordinate not in pnts:
pnts.append(coordinate)
print(pnts)
display.FitAll()
start_display()