请加上我对CATIA VBA的有限知识。 我在定制CATIA V5宏以浏览Excel坐标点并在CATIA中绘制它时遇到一些困难,只需点击自定义的CATIA图标即可。
目前我还有另一个带有宏"的Excel文件。浏览 ExcelP1,并在CATIA中绘制点。但我需要打开并运行 "带有宏的Excel文件"首先启动CATIA。脚本是 如下(我没有发展这个)
Public Filename As String
Private Sub Browse_Click()
'Open File
Mainform.Hide
Filename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
If Filename <> "False" Then
Application.Visible = False
filenamebox.Value = Filename
Else
Application.Visible = False
Filename = filenamebox.Value
End If
Mainform.Show
End Sub
Private Sub ClearButton_Click()
Mainform.Hide
ActiveWorkbook.Close (False)
Application.Visible = False
End Sub
Private Sub OKButton_Click()
'Set Up Message Labels
Title = "Information Message"
'Check for Entered Values
If filenamebox.Value <> "" Then
Workbooks.Open Filename:=Filename
Application.Visible = False
'Start CATIA and add an Open body to the document
Start_CATIA
Mainform.Hide
'Read Point Data from file and create point in CATIA
i = 2
Do Until Worksheets("Sheet1").Range("a" & i).Value = ""
x = Worksheets("Sheet1").Range("a" & i).Value
y = Worksheets("Sheet1").Range("b" & i).Value
z = Worksheets("Sheet1").Range("c" & i).Value
Create_Point
i = i + 1
Loop
i = i - 2
MsgBox i & " Points Created in New Part", , Title
Else
MsgBox "Enter a Filename", , Title
End If
ActiveWorkbook.Close (False)
Mainform.Show
End Sub
Private Sub UserForm_Initialize()
If Worksheets("Filepath_Location").Range("a1").Value <> "" Then
Filename = Worksheets("Filepath_Location").Range("a1").Value
filenamebox.Value = Filename
End If
End Sub
为了让脚本在CATIA中运行,我需要添加/修改什么?
答案 0 :(得分:1)
启动Catia并获得应用程序后,您需要做的第一件事是创建一个新的Part,您将在其中添加点。
Dim MyPartDocument As PartDocument
Dim MyPart As Part
Dim PointGeoSet As HybridBody
Set MyPartDocument = CATIA.Documents.Add("Part")
Set MyPart = MyPartDocument.Part
Set PointGeoSet = MyPart.HybridBodies.Add()
PointGeoSet.Name = "MyPoints"
接下来是使用这样的函数从excel数据创建点。我喜欢创建一个包装器,但你可以随意改写它:
Sub CreateXYZPoint(TargetPart As Part, TargetGeometricalSet As HybridBody, _
Xmm As Double, Ymm As Double, Zmm As Double, _
PointCount As String)
Dim HSFactory As HybridShapeFactory
Dim NewPoint As Point
'get the factory
Set HSFactory = TargetPart.HybridShapeFactory
'create the point with the factory
Set NewPoint = HSFactory.AddNewPointCoord(Xmm, Ymm, Zmm)
'Append the point to the geometrical set
TargetGeometricalSet.AppendHybridShape NewPoint
'rename the point
NewPoint.Name = "Point." & PointCount
End Sub
你会打电话
循环中的CreateZYXPoint MyPart, PointGeoSet,x,y,z,cstr(i)
最后,在循环结束时,您需要更新部件以便调用:
MyPart.Update
在程序结束时进行单次更新比在创建每个点后更新要快得多。
这应该让你开始。请记住,Catia使用毫米作为基本内部单位。因此,您的电子表格匹配单位或您必须在调用CreateXYZPoint之前进行单位转换...或者您想要完成此操作。
请告诉我这是否适合您。
修改:以下是与上述代码放在一起的代码链接。您需要确保excel代码正常工作,但我插入Catia代码的位置是正确的: http://pastebin.com/vxFcPw52