基于VB的Catia的二次开发:(2)基本绘图

Catia绘制基本图形

  这里我们使用Function,而不再使用Sub是因为需要返回图形的参考,图形的参考是后续使用该图形的接口。

  绘制一个图形的基本步骤如下:

  1. 定义图形,格式:Dim shape As HybridShape...
  2. 按指定参数赋值给图形,生成图形,格式:shape = Shapes.AddNew...
  3. 将图形添加到body中,格式:ABody.AppendHybridShape(shape)
  4. 返回图形的参考,格式:FunctionName= APart.CreateReferenceFromObject(shape)

绘制

' 函数:绘制点
' 函数:绘制点
Public Function DrawPoint(x, y, z)
    ' 绘制点
    Dim shape As HybridShapePointCoord          ' 按坐标定义的点
    shape = Shapes.AddNewPointCoord(x, y, z)    ' 赋值点的坐标
    ABody.AppendHybridShape(shape)              ' 将点添加到body中
    ' 刷新
    APart.InWorkObject = shape
    APart.Update()
    ' 返回点的参考
    DrawPoint = APart.CreateReferenceFromObject(shape)
End Function

其中,HybridShapePointCoord表示按坐标定义的点,类似的还有其他定义方式,如下图所示;ShapesABodyAPart是启动CATIA时定义的相关变量,参考前一篇文章基于VB的Catia的二次开发:(1)启动Catia
点的定义

绘制直线

' 函数:绘制直线
Public Function DrawLine(p1, p2)
    ' 绘制直线
    Dim shape As HybridShapeLinePtPt            ' 按两点定义的直线
    shape = Shapes.AddNewLinePtPt(p1, p2)       ' 使用两个点生成直线
    ABody.AppendHybridShape(shape)              ' 将点添加到body中
    ' 刷新
    APart.InWorkObject = shape
    APart.Update()
    ' 返回直线的参考
    DrawLine = APart.CreateReferenceFromObject(shape)
End Function

其中,输入参数p1p2是点的参考,即绘制点时返回的值。

设置参考面

' 函数:设置参考面
Public Function SetPlaneXY()
    Dim element As OriginElements
    element = APart.OriginElements
    Dim shape As HybridShapePlaneExplicit
    shape = element.PlaneXY
    SetPlaneXY = APart.CreateReferenceFromObject(shape)
End Function

绘制

' 函数:绘制圆
Public Function DrawCircle(c, plane, r)
    ' 绘制直线
    Dim shape As HybridShapeCircleCtrRad
    shape = Shapes.AddNewCircleCtrRad(c, plane, False, r)
    shape.SetLimitation(1)
    ABody.AppendHybridShape(shape)
    ' 刷新
    APart.InWorkObject = shape
    APart.Update()
    ' 返回直线的参考
    DrawCircle = APart.CreateReferenceFromObject(shape)
End Function

Catia绘图实例

实例:绘制XY平面上圆心为(0, 0, 0),半径为50的圆,并绘制一条半径。

' 主程序:
' 绘制XY平面上圆心为(0, 0, 0),半径为50的圆,并绘制一条半径
Sub Main()

    Dim strStart As String = "开始CATIA建模..."
    Console.WriteLine(strStart)
    StartupCatia()

    Dim strDrawing As String = "开始绘图..."
    Console.WriteLine(strDrawing)

    ' 绘制圆心:坐标为(0, 0, 0)的点
    Dim point As Reference
    point = DrawPoint(0, 0, 0)
    ' 设置参考面
    Dim refXY As Reference
    refXY = SetPlaneXY()
    ' 绘制半径
    Dim point2 As Reference
    point2 = DrawPoint(50, 0, 0)
    Dim line As Reference
    line = DrawLine(point, point2)
    ' 绘制圆
    Dim circle As Reference
    circle = DrawCircle(point, refXY, 50)

    APart.InWorkObject = Shapes
    APart.Update()
    Dim strDoc As String = "绘图完成"
    Console.WriteLine(strDoc)
    Console.ReadKey()

End Sub

绘图实例

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值