坐标系為ZOX坐标系,X軸為橫軸,Y軸為豎軸。直線為平行于X軸的一條直線。梯形為以Z軸為對稱軸的梯形。附上代碼。
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 13 09:58:09 2020
@author: zxl
"""
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeFace
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_EDGE
from OCC.Core.TopoDS import topods
from OCC.Core.Geom import Geom_Line
from OCC.Core.gp import gp
from OCC.Core.BRep import BRep_Tool
from OCC.Core.GeomAPI import GeomAPI_ExtremaCurveCurve
p0 = gp_Pnt()
vnorm = gp_Dir(0, 1, 0) # 法向量,平行于Y軸的機關向量
aPlane = gp_Pln(p0, vnorm) #ZOX平面
bcd = 150 #梯形的下底邊長
tcd = 50 #梯形的上底邊長
ht = 50 #梯形的高
offset = 0
z_of_trapezoid = 0 #梯形的底邊的Z的坐标
x0 = -0.5 * bcd + offset #梯形的左下角頂點的X坐标
x1 = 0.5 * bcd + offset #梯形的右下角頂點的X坐标
x2 = 0.5 * tcd + offset #梯形的右上角頂點的X坐标
x3 = -0.5 * tcd + offset #梯形的左上角頂點的X坐标
z0 = z_of_trapezoid # 梯形的下底的Z坐标
z1 = z_of_trapezoid + ht # 梯形的上底的Z坐标
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)
aTrapezoid = BRepBuilderAPI_MakeFace(aPlane, aPolygon.Wire()).Shape()
#梯形面,類型為TopoDS_Face
aLine = Geom_Line(gp_Pnt(0.0, 0.0, 25), gp.DX()) # 梯形的中位線
print("type of aLine: ", type(aLine))
anEdgeExplorer = TopExp_Explorer(aTrapezoid, TopAbs_EDGE)
start_pnts = [] #交點的X坐标的清單
while anEdgeExplorer.More(): # 有更多子形狀去挖掘
anEdge = topods.Edge(anEdgeExplorer.Current()) # 目前被探索到的子形狀是哪一個
#print("current edge: ", anEdge)
aCurve = BRep_Tool.Curve(anEdge)[0]
aFirst = BRep_Tool.Curve(anEdge)[1] # 這條邊的起始點
aLast = BRep_Tool.Curve(anEdge)[2] # 這條邊的終止點
#print("type of aCurve: ", type(aCurve))
#print("type of sec: ", type(sec), "sec: ", sec)
aExtrema = GeomAPI_ExtremaCurveCurve(aLine, aCurve, -1e308, 1e308, aFirst, aLast)
#GeomAPI_ExtremaCurveCurve(C1, C2, U1min, U1max, U2min, U2max)
#C1 為曲線1,C2為曲線2,U1min, U1max是曲線1的起始點和終止點,U2min, U2max是曲線2的起始點和終止點,
# 這裡因為C1是一條無限長的直線,是以是-1e308, 1e308。而C2的起始點和終止點是由BRep_Tool.Curve(anEdge)[1]和#BRep_Tool.Curve(anEdge)[2]得到
#print(type(aExtrema))
print("number of aExtrema: ", aExtrema.NbExtrema())
print("aExtrema.LowerDistance(): ", aExtrema.LowerDistance())
if aExtrema.NbExtrema() > 0 and aExtrema.LowerDistance() < 1e-10 : #兩曲線有交點,且兩曲線的最小距離為0,即有交點
print("if is in ")
for iPnt in range(aExtrema.NbExtrema()):
print("for is in ")
aPnt1 = gp_Pnt()
aPnt2 = gp_Pnt()
aExtrema.Points(iPnt + 1, aPnt1, aPnt2) #兩個點的坐标
print("aPnt1: ", aPnt1)
if aPnt1.SquareDistance(aPnt2) < 1e-15 :#兩個點是同一個點,即交點
start_pnts.append(aPnt1.X())
anEdgeExplorer.Next() # 下一個子形狀
print("start_pnts: ", start_pnts)