pythonocc 偏移曲面offset,一个shape求它的包络offset,二维情形

以梯形为例,求它的包络。

BRepOffsetAPI_MakeOffset的参数GeomAbs_Intersection的作用是让边角是尖的,而GeomAbs_Arc的作用是让边角是弧状。

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 13:44:43 2020

@author: JC0101
"""
from random import random
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, TopAbs_FACE
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
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_MakeOffset
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Fuse
from OCC.Display.SimpleGui import init_display
from OCC.Core.GeomAbs import GeomAbs_Intersection, GeomAbs_Arc, GeomAbs_Tangent 
from OCC.Core.AIS import AIS_ColoredShape
from OCC.Display.OCCViewer import rgb_color

display, start_display, add_menu, add_functionto_menu = init_display()

p0 = gp_Pnt()
vnorm = gp_Dir(0, 1, 0)
aPlane = gp_Pln(p0, vnorm)

bcd = 150
tcd = 50
ht = 50
offset = 0
z_of_trapezoid = 0

x0 = -0.5 * bcd + offset
x1 = 0.5 * bcd + offset
x2 = 0.5 * tcd + offset
x3 = -0.5 * tcd + offset
z0 = z_of_trapezoid # bottom line
z1 = z_of_trapezoid + ht # top line
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()

ais_shp = AIS_ColoredShape(aTrapezoid)
ais_shp.SetCustomColor(aTrapezoid, rgb_color(random(), random(), random()))
display.Context.Display(ais_shp, True)

anFaceExplorer = TopExp_Explorer(aTrapezoid, TopAbs_FACE)

aReMerge = False
coating_thickness = 10 
counter = 0
while anFaceExplorer.More(): # 有更多子形状去挖掘
    aFace = topods.Face(anFaceExplorer.Current()) # 当前被探索到的子形状是哪一个
    offset = BRepOffsetAPI_MakeOffset(aFace, GeomAbs_Intersection)
    offset.Perform(coating_thickness)

    if offset.Shape().IsNull():
        continue
    elif not aReMerge:
        aReMerge = BRepBuilderAPI_MakeFace(topods.Wire(offset.Shape())).Shape()
    else:
        aReMerge = BRepAlgoAPI_Fuse(aReMerge, BRepBuilderAPI_MakeFace(topods.Wire(offset.Shape())).Shape()).Shape()
        
    anFaceExplorer.Next() # 下一个子形状
    
    counter += 1
    print("while counter: ", counter)
    
offset = BRepAlgoAPI_Cut(aReMerge, aTrapezoid).Shape() # 从aReMerge中把里面的梯形aTrapezoid挖去

ais_shp = AIS_ColoredShape(offset)
ais_shp.SetCustomColor(offset, rgb_color(1, 0, 0))
display.Context.Display(ais_shp, True)
display.FitAll()
start_display()

红色即为黄色梯形的包络。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中使用qhull二维数据的包络,可以按照以下步骤进行: 1. 安装qhull库 在Qt中使用qhull需要先安装qhull库,可以通过以下命令在Ubuntu上安装qhull: ``` sudo apt-get install libqhull-dev ``` 2. 准备数据 在Qt中准备二维数据,例如: ``` QVector<QPointF> points; points.append(QPointF(0, 0)); points.append(QPointF(1, 0)); points.append(QPointF(1, 1)); points.append(QPointF(0, 1)); ``` 3. 使用qhull包络 在Qt中使用qhull包络,可以通过以下代码实现: ``` #include <libqhullcpp/Qhull.h> ... Qhull qhull; qhull.setDimension(2); qhull.setPoints("2", points.size(), (const qreal*)points.constData()); qhull.runQhull(""); QByteArray result = qhull.facetList().toFacetList().toByteArray(); ``` 其中,`qhull.setDimension(2)`设置维度为2,`qhull.setPoints()`设置输入点集,`qhull.runQhull()`运行qhull解,`qhull.facetList()`返回面列表,`toFacetList()`转换为FacetList类型,`toByteArray()`转换为字节数组类型。 4. 绘制包络 最后,可以将qhull出的包络绘制出来,例如: ``` QPainter painter(this); painter.setPen(QPen(Qt::red, 2)); QDataStream stream(result); FacetList facets; stream >> facets; foreach (FacetT facet, facets) { QVector<QPointF> polygon; foreach (VertexT vertex, facet.vertices()) { polygon.append(QPointF(vertex.point().x(0), vertex.point().x(1))); } painter.drawPolygon(QPolygonF(polygon)); } ``` 以上代码将qhull出的面列表转换为FacetList类型,然后遍历每个面,将顶点转换为QPointF类型,最后绘制多边形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值