0

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.

  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
    

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

Chrismas007
  • 6,085
  • 4
  • 24
  • 47
IMch
  • 1
  • 1
  • 1
  • 1
  • Have you tried creating a Macro from within Catia and importing or reading the excel file that way? It's a very straight forward operation. Also, reading a CSV file is even easier to read into a Catia VBA macro. Otherwise you need to add the catia vba libraries to your excel vba project. My suggestion is start with Catia and pull the data in, unless you have a specific need to stay in excel. – GisMofx Apr 16 '14 at 16:44
  • Hi GisMofx, i am trying to create a Macro within CATIA (IN process) to browse an excel file and plot the points. I have no problem running the scripts posted earlier in Excel and plot the point in CATIA (OUT process). My intention is to create a customized icon in excel which will browse and read point form excel. Sorry for my very limited knowledge in CATIA vba, hope to get some guidance in here. Cheers – IMch Apr 18 '14 at 01:50
  • Ok, your comment is confusing...Catia Macro _In Process_ ..you need to get the excel object: `Set myEXCEL = GetObject(, "EXCEL.Application")` or `Set myEXCEL = New Excel.Application '= CreateObject("Excel.Application")` Do you just want to know the correct methods to create points in a Catia Script? If yes, I can help you with a code snippet to create points. – GisMofx Apr 19 '14 at 18:32
  • I am sorry for the confuse comment. Yes please show me that.Thanks – IMch Apr 21 '14 at 13:53
  • Thanks. Will do. I will help provide you with the code for the sub `CreatePoint` based on your code above. – GisMofx Apr 21 '14 at 15:59

1 Answers1

1

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.

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:

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

Finally, at the end of your loop, you will want to update the part so call: 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.

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

GisMofx
  • 982
  • 9
  • 27
  • Thanks GisMofx for providing this snippet...it's would be great if you can show on the code of import file and create point. Pardon me for my very poor VBA Knowledge...am working hard trying to catch up with it. Million Thanks – IMch Apr 23 '14 at 02:59
  • @IMch I've updated and added a link in my response with the code combined. It should be clear that I added the new Sub to the bottom of the code and inserted the call after you read the point data from excel. As long as your excel code is working, the Catia calls should work just fine. – GisMofx Apr 23 '14 at 13:55
  • @IMch Did this resolve your issue? If so, can you please mark this as the accepted answer? Otherwise, let me know if you have any other questions about this. – GisMofx Apr 26 '14 at 22:27
  • Hi GisMofx, thank you for the code but the code didn't work in the CATIA VBA (IN-Process). – IMch Apr 28 '14 at 02:30
  • Hi GisMofx, Private Sub UserForm_Initialize() – IMch May 07 '14 at 06:19
  • @IMch That comment is not helpful. Please post your whole code or the error that you are receiving. On another note, This code should love in another sub, **not** the user form initialize Sub. Help me help you. – GisMofx May 08 '14 at 10:50