pythonocc 求一条直线与一个梯形的交点的横坐标
坐标系为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)
本文地址:https://blog.csdn.net/reyyy/article/details/107319187
上一篇: 365日历如何显示透明农历
下一篇: 红酒用什么杯子最好?酒杯不止一种