这里我们使用Function,而不再使用Sub是因为需要返回图形的参考,图形的参考是后续使用该图形的接口。
绘制一个图形的基本步骤如下:
定义图形,格式:Dim shape As HybridShape...按指定参数赋值给图形,生成图形,格式:shape = Shapes.AddNew...将图形添加到body中,格式:ABody.AppendHybridShape(shape)返回图形的参考,格式:FunctionName= APart.CreateReferenceFromObject(shape)其中,HybridShapePointCoord表示按坐标定义的点,类似的还有其他定义方式,如下图所示;Shapes、ABody、APart是启动CATIA时定义的相关变量,参考前一篇文章基于VB的Catia的二次开发:(1)启动Catia。
其中,输入参数p1和p2是点的参考,即绘制点时返回的值。
实例:绘制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