【VBA的官方解释】
VBA(Visual Basic for Applications)是VisualBasic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。主要能用来扩展Windows的应用程序功能,特别是MicrosoftOffice软件。它也可说是一种应用程式视觉化的 Basic 脚本。
CATIA提供了多种二次开发的接口,其中包括自动化对象编程(Automation)和开放的基于构件的应用编程接口(CAA)。本文主要介绍Visual Basic进行自动化对象编程。
相对Visual Studio等集成编译环境,宏编辑器无需引用API,直接可以调用Automation对象,进行CATIA二次开发。本文以VB.Net语言编写案例,介绍CATIA基本对象及几何元素的构造和运算方法。
下图为宏编辑器图标,快捷键为 Alt+F11
Dim oActiveEditor As Editor
Set oActiveEditor = CATIA.ActiveEditor
Dim oPart As Part
Set oPart = CATIA.ActiveEditor.ActiveObject
Set PlaneXY = oPart.OriginElements.PlaneXY
Set PlaneXZ = oPart.OriginElements.PlaneZX
Set PlaneYZ = oPart.OriginElements.PlaneYZ
Dim params As Parameters
Set params = oPart.Parameters
Dim parameter1 As parameter
Set parameter1 = params.CreateDimension("长度", "Length", 1000)
Set parameter2 = params.CreateReal("实数", 100)
Set parameter3 = parameters1.Item("长度")
Dim mainBody As Body
Set mainBody= oPart.MainBody
Dim Bodies As Bodies
Set Bodies = part1.Bodies
Dim Body As Body
Set Body = Bodies.Add()
Body.Name = "TestBody"
Dim Body As Body
Set Body = Bodies.Item("TestBody")
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = oPart.HybridBodies
Set ohybridBody= hybridBodies1.add()
ohybridBody.Name="TestHybridBody"
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = oPart.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("process")
Dim orderHybridBodies As OrderedGeometricalSets
Set orderHybridBodies = part1.OrderedGeometricalSets
Dim orderHybridBody As OrderedGeometricalSet
Set orderHybridBody = orderHybridBodies.Add()
orderHybridBody.Name = "TestOrderHybridBody"
Dim orderHybridBody As OrderedGeometricalSet
Set orderHybridBody = orderHybridBodies.Item("process")
先获取几何构造方法库HybridShapeFactory
Dim oHybridShapeFactory As HybridShapeFactory
Set oHybridShapeFactory = oPart.HybridShapeFactory
Dim iPoint1 As HybridShapePointCoord
Set iPoint1= oHybridShapeFactory.AddNewPointCoord(0, 10, 20)
Dim iPoint3 As HybridShapePointBetween
Set iPoint3 = oHybridShapeFactory.AddNewPointBetween(iPoint1, iPoint2, 0.5, 0)
AddNewPointBetween(参考点1,参考点2,比率,方向)
Dim iPoint4 As HybridShapePointOnCurve
Set iPoint4 = oHybridShapeFactory.AddNewPointOnCurveFromDistance(refLine, 20, False)
AddNewPointOnCurveFromDistance(参考线,长度,布尔)
Dim iPoint5 As HybridShapePointOnCurve
Set iPoint5 = oHybridShapeFactory.AddNewPointOnCurveFromPercent(refLine, 0.5, False)
AddNewPointOnCurveFromPercent(参考线,比率,布尔)
Dim refLine As HybridShapeLinePtPt
Set refLine = oHybridShapeFactory.AddNewLinePtPt(iPoint1, iPoint2)
AddNewLinePtPt(参考点1,参考点2)
Dim dirLine As HybridShapeLinePtDir
Set dirLine = oHybridShapeFactory.AddNewLinePtDir(iPoint5, iDirection, 0, 100, False)
AddNewLinePtDir(参考点,方向,起始长度,结束长度,布尔)
Dim angleLine As HybridShapeLineAngle
Set angleLine=oHybridShapeFactory. AddNewLineAngle(refLine,oPart.
OriginElements.PlaneYZ, iPoint1, False, 0, 200, 30, False)
AddNewLineAngle(参考线,支持面,起点,布尔,起始长度,结束长度,布尔)
Dim normalLine As HybridShapeLineNormal
Set normalLine = oHybridShapeFactory.AddNewLineNormal(refSurface, iPoint3, 0, 200, False)
AddNewLineNormal(参考曲面,参考点,起始长度,结束长度,布尔)
Dim circle0 As HybridShapeCircle3Points
Set circle0 = oHybridShapeFactory.AddNewCircle3Points(iPoint1, iPoint2, iPoint3)
circle0.SetLimitation 1
Dim extrudeSurface As HybridShapeExtrude
Set extrudeSurface = oHybridShapeFactory.AddNewExtrude(CurveJoin, 1000, 1000, oHybridShapeFactory.AddNewDirectionByCoord(0, 0, 1))
Dim fillSurface As HybridShapeFill
Set fillSurface = oHybridShapeFactory.AddNewFill()
Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromObject(circle0)
fillSurface.AddBound reference1
Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromObject(circle0)
Dim pad1 As Pad
Set pad1 = shapeFactory1.AddNewPadFromRef(reference1, 500)
Dim projectPoint As HybridShapeProject
Set projectPoint = oHybridShapeFactory.AddNewProject(iPoint4, extrudeSurface)
Dim intersection As HybridShapeIntersection
Set intersection = oHybridShapeFactory.AddNewIntersection
(fillSurface, extrudeSurface)
Dim Z As HybridShapeDirection
Set Z = oHybridShapeFactory.AddNewDirectionByCoord(0, 0, 1)
Dim translate1 As HybridShapeTranslate
Set translate1 = oHybridShapeFactory.AddNewTranslate(iPoint4, Z, 100)
Dim splitSurface As HybridShapeSplit
Set splitSurface = oHybridShapeFactory.AddNewHybridSplit(extrudeSurface, intersection, 0)
splitSurface.AddElementToKeep projectPoint2
Dim extrapolElement As HybridShapeExtrapol
Set extrapolElement = oHybridShapeFactory.AddNewExtrapolLength(iPoint1, intersection, 100)
第一步,引用COM组件
Imports INFITF
Imports HybridShapeTypeLib
Imports MECMOD
public static class CatiaMethods
{
public static Editor GetEditor()
{
Application CATIA;
try
{
CATIA= (INFITF.Application)Marshal.GetActiveObject("CATIA.Application");
}
catch
{
Type oType = System.Type.GetTypeFromProgID("CATIA.Application");
CATIA = (INFITF.Application)Activator.CreateInstance(oType);
CATIA.Visible = true;
}
Editor ed = CATIA.ActiveEditor;
return ed;
}
}
接下来的套路就和宏编辑器里一样了,但是在Visual Studio中,编写代码要比宏编辑器要方便很多,主要是高级,有逼格。。。