vba 自定义 CATIA V5 宏以浏览 Excel 坐标文件和绘图点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23097672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:46:43  来源:igfitidea点击:

Customized CATIA V5 Macro to browse Excel coordinate file & plot points

vbacatia

提问by IMch

Please bear with my limited knowledge in CATIA VBA. I am having some difficulties in customize a CATIA V5 macro to browse for Excel coordinate points and plot it in CATIA, all with a click on the customized CATIA icon.

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

  1. I got an Excel file with many XYZ coordinates, let call it ExcelP1 (The excel file has no scripts/Macro in it), I would like to develop a macro in CATIA to read & plot points from ExcelP1.
  2. Currently i have another "Excel file with macro" to browse the ExcelP1, and plot the points in CATIA. But i need to open and run the "Excel file with macro" first to initiate CATIA. The scripts are as below (i didn't develop this)

    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
    
  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
    

What do I need to add/modify in order for the scripts to run in CATIA?

我需要添加/修改什么才能让脚本在 CATIA 中运行?

回答by GisMofx

The first thing you need to do after you start Catia and get the application is to create a new Part in which you will be adding the points.

启动 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"

The next thing is to create the point from the excel data by using a function like this. I like to create a wrapper, but you can rewrite this anyway you want:

接下来是使用这样的函数从 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

You Would call CreateZYXPoint MyPart, PointGeoSet,x,y,z,cstr(i)in your loop

你会CreateZYXPoint MyPart, PointGeoSet,x,y,z,cstr(i)在你的循环中调用

Finally, at the end of your loop, you will want to update the part so call: MyPart.Update

最后,在循环结束时,您将需要更新该部分,因此调用: MyPart.Update

It is much faster to do a single update at the end of your program than to update after each point is created.

在程序结束时进行一次更新比在创建每个点后进行更新要快得多。

This should get you started. Remember, Catia uses Millimeters as it's base internal units. Therefore, your spreadsheet match units or you must do a unit conversion before calling CreateXYZPoint...or However you want to accomplish that.

这应该让你开始。请记住,Catia 使用毫米作为它的基本内部单位。因此,您的电子表格匹配单位,或者您必须在调用 CreateXYZPoint 之前进行单位转换……或者无论如何您想要实现这一点。

Let me know if this works for you.

让我知道这是否适合您。

Edit:Here's a link to the code put together with your code above. You need to make sure you excel code is working, but where I inserted the Catia code is correct: http://pastebin.com/vxFcPw52

编辑:这是与上面的代码放在一起的代码链接。您需要确保您的 excel 代码正常工作,但我插入 Catia 代码的位置是正确的:http: //pastebin.com/vxFcPw52