0

请忍受我对 CATIA VBA 的有限知识。我在自定义 CATIA V5 宏以浏览 Excel 坐标点并在 CATIA 中绘制它时遇到了一些困难,只需单击自定义的 CATIA 图标即可。

  1. 我有一个带有许多 XYZ 坐标的 Excel 文件,我们称之为 ExcelP1(Excel 文件中没有脚本/宏),我想在 CATIA 中开发一个宏来从 ExcelP1 读取和绘制点。
  2. 目前我有另一个“带有宏的 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 中运行,我需要添加/修改什么?

4

1 回答 1

1

启动 Catia 并获取应用程序后,您需要做的第一件事是创建一个新零件,您将在其中添加点。

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

于 2014-04-21T16:30:27.530 回答