# PythonOCC基础使用：建模——二维草图（线/任意/圆弧/圆/椭圆/三角形/矩形/任意多边形/双曲线/抛物线）

### 圆形

``````from OCC.Core.GC import GC_MakeCircle
from OCC.Core.gp import gp_Pnt
from OCC.Core.gp import gp_Ax2,gp_Dir
from OCC.Display.OCCViewer import rgb_color
#圆形
Circle=GC_MakeCircle(gp_Ax2 (gp_Pnt(0,0,5),gp_Dir (0,0,-1)),8).Value()

if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display.DisplayShape(Circle,update=True,color=rgb_color(0,0,1))
start_display()
``````
• 线段

``````from OCC.Core.gp import gp_Pnt
from OCC.Core.GC import GC_MakeSegment
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Display.OCCViewer import rgb_color
# 函数作用：通过两点生成线段
# 输入：两点
# 输出：线段
aSegment = GC_MakeSegment(gp_Pnt(1,1,1), gp_Pnt(1,10,1))
anEdge = BRepBuilderAPI_MakeEdge(aSegment.Value())
aWire = BRepBuilderAPI_MakeWire(anEdge.Edge())
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display.DisplayShape(aWire.Shape(), update=True,color=rgb_color(1,0,0))
start_display()
``````

``````from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeEdge
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCylinder
from OCC.Core.GCE2d import  GCE2d_MakeSegment
from OCC.Core.GeomAPI import geomapi_To3d
from OCC.Core.gp import gp_Pnt2d, gp_Pln, gp_Pnt, gp_Dir
from OCC.Display.SimpleGui import init_display

sideLength=20
l1 = GCE2d_MakeSegment(gp_Pnt2d(-sideLength / 2, sideLength / 2), gp_Pnt2d(sideLength / 2, sideLength / 2))
l2 = GCE2d_MakeSegment(gp_Pnt2d(sideLength / 2, sideLength / 2), gp_Pnt2d(sideLength / 2, -sideLength / 2))
l3 = GCE2d_MakeSegment(gp_Pnt2d(sideLength / 2, -sideLength / 2), gp_Pnt2d(-sideLength / 2, -sideLength / 2))
l4 = GCE2d_MakeSegment(gp_Pnt2d(-sideLength / 2, -sideLength / 2), gp_Pnt2d(-sideLength / 2, sideLength / 2))

e1=BRepBuilderAPI_MakeEdge(geomapi_To3d(l1.Value(),gp_Pln(gp_Pnt(0,0,20),gp_Dir(0,0,1)))).Edge()
e2=BRepBuilderAPI_MakeEdge(geomapi_To3d(l2.Value(),gp_Pln(gp_Pnt(0,0,20),gp_Dir(0,0,1)))).Edge()
e3=BRepBuilderAPI_MakeEdge(geomapi_To3d(l3.Value(),gp_Pln(gp_Pnt(0,0,20),gp_Dir(0,0,1)))).Edge()
e4=BRepBuilderAPI_MakeEdge(geomapi_To3d(l4.Value(),gp_Pln(gp_Pnt(0,0,20),gp_Dir(0,0,1)))).Edge()

square= BRepBuilderAPI_MakeWire(e1,e2,e3,e4)

display.DisplayShape(square.Wire(), update=True)

my_box = BRepPrimAPI_MakeCylinder(10., 20.).Shape()
display.DisplayShape(my_box,update=True)

start_display()
``````
• 三角形
``````from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_DX, gp_Circ, gp_Elips
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire

# 函数作用：通过三点生成任意三角形
# 输入：三点
# 输出：三角形
aSegment1 = GC_MakeSegment(gp_Pnt(1,1,1), gp_Pnt(2,1,5))
anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aWire1 = BRepBuilderAPI_MakeWire(anEdge1.Edge())
aSegment2 = GC_MakeSegment(gp_Pnt(2,1,5), gp_Pnt(9,1,5))
anEdge2 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
aWire2 = BRepBuilderAPI_MakeWire(anEdge2.Edge())
aSegment3 = GC_MakeSegment(gp_Pnt(9,1,5), gp_Pnt(1,1,1))
anEdge3 = BRepBuilderAPI_MakeEdge(aSegment3.Value())
aWire3 = BRepBuilderAPI_MakeWire(anEdge3.Edge())
aTriangle = BRepBuilderAPI_MakeWire(aWire1 .Edge(),aWire2 .Edge(),aWire3 .Edge())
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display.DisplayShape(aTriangle.Shape(), update=True)
start_display()
``````
• 圆弧
``````from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_Circ, gp_Elips
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
# 函数作用：通过圆心和半径和角度生成圆弧
# 输入：圆心坐标，半径值，角度值
# 输出：暂定只输出圆弧
Location = gp_Pnt(0,0,0)
Axis = gp_Dir (0,0,-1)
CircleAxis= gp_Ax2(Location, Axis)
Circle = gp_Circ(CircleAxis,5)
ArcofCircle0 = GC_MakeArcOfCircle(Circle, 0/180*3.14, 180/180*3.14, True)
ArcofCircle1 = BRepBuilderAPI_MakeEdge(ArcofCircle0.Value())
ArcofCircle = BRepBuilderAPI_MakeWire(ArcofCircle1.Edge())
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display.DisplayShape(ArcofCircle.Edge(),update=True)
start_display()
``````
• 椭圆
``````from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_Circ, gp_Elips
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
# 函数作用：通过半长轴和半短轴及原点绘制椭圆
# 输入：原点坐标，半长轴值，半短轴值
# 输出：暂定只输出椭圆
Location = gp_Pnt(0,0,0)
Axis = gp_Dir(0,0,1)
ElipsAxis= gp_Ax2(Location, Axis)
ElipsAxis0 = gp_Elips(ElipsAxis, 9, 3)
ElipsAxis1 = BRepBuilderAPI_MakeEdge(ElipsAxis0)
ElipsAxis = BRepBuilderAPI_MakeWire(ElipsAxis1.Edge())

if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display.DisplayShape(ElipsAxis.Edge(),update=True)
start_display()
``````
• 绘制双曲线hyperbola
``````
from OCC.Core.GCE2d import GCE2d_MakeHyperbola
from OCC.Core.gp import gp_Pnt, gp_Ax2d, gp_Pnt2d, gp_Dir2d
from OCC.Display.SimpleGui import init_display

display.DisplayShape(hyperbola.Value(),update=True)

start_display()

``````
• 抛物线 parabola
``````from OCC.Core.GCE2d import GCE2d_MakeParabola
from OCC.Core.gp import gp_Pnt, gp_Ax2d, gp_Pnt2d, gp_Dir2d
from OCC.Display.SimpleGui import init_display

focal=5
parabola=GCE2d_MakeParabola(gp_Ax2d(gp_Pnt2d(0 , 0),gp_Dir2d(0,1)),focal,False)

display.DisplayShape(parabola.Value(),update=True)

start_display()
``````
• 矩形（任意多边形就是改变Pi数量顺次连接）
``````from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_DX, gp_Circ, gp_Elips
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
# 函数作用：在yz平面上通过一顶点及长宽绘制矩形
# 输入：一顶点坐标，长值，宽值
# 输出：暂定只输出矩形
coor0=[0,3,3]#矩阵某一行
Length=3
Width=1
yPlus = [0, Length, 0]
zPlus = [0, 0, Width]
coor1 = [coor0[i] + yPlus[i] for i in range(3)]
coor2 = [coor0[i] + zPlus[i]+ yPlus[i] for i in range(3)]
coor3 = [coor0[i] + zPlus[i] for i in range(3)]
P0 = gp_Pnt(coor0[0],coor0[1],coor0[2])
P1 = gp_Pnt(coor1[0],coor1[1],coor1[2])
P2 = gp_Pnt(coor2[0],coor2[1],coor2[2])
P3 = gp_Pnt(coor3[0],coor3[1],coor3[2])
aSegment1 = GC_MakeSegment(P0, P1)
anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aWire1 = BRepBuilderAPI_MakeWire(anEdge1.Edge())
aSegment2 = GC_MakeSegment(P1, P2)
anEdge2 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
aWire2 = BRepBuilderAPI_MakeWire(anEdge2.Edge())
aSegment3 = GC_MakeSegment(P2, P3)
anEdge3 = BRepBuilderAPI_MakeEdge(aSegment3.Value())
aWire3 = BRepBuilderAPI_MakeWire(anEdge3.Edge())
aSegment4 = GC_MakeSegment(P3, P0)
anEdge4 = BRepBuilderAPI_MakeEdge(aSegment4.Value())
aWire4 = BRepBuilderAPI_MakeWire(anEdge4.Edge())
aRectangle = BRepBuilderAPI_MakeWire(aWire1 .Edge(),aWire2 .Edge(),aWire3 .Edge(),aWire4 .Edge())
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display