0

I currently have a set of VB CATIA Macro codes that, when a user runs the code, it opens up a form that prompts the user to input the text that he/she wants to change.(See image for the form that will pop up) The code will then run through the whole drawings to search for and replace the text inputted by the user accordingly.

Currently, I am modifying the code so that the changes is to be applied to all the opened CAT Drawings.

What I want the code to do: User opens 5 CATIA drawings for example --> When user runs the code --> the Form opens up to prompt user to input Text to search for and Text to be replaced for --> Once user fills in for the two inputs and hit the 'Apply to all opened sheets' command button --> the code runs and changes all the 5 opened CATDrawings accordingly.

I'm new to the VB scripting language and have little to no experience with VB scripting language. Any help will be greatly appreciated.

MainForm

Below are the working codes that will only apply the changes to 1 CATIA drawing opened.

'For the Module,

Sub CATMain()

MainForm.Show

End Sub



'For the Form, 

Dim b As Variant

Dim a As String

Dim c As String

Dim selection1 As Selection

Dim drawingDocument1 As DrawingDocument

Private Sub CommandButton1_Click()

    a = TextBox1.Value
    b = TextBox2.Value
    c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"
        Set drawingDocument1 = CATIA.ActiveDocument
        Set DrawSheets1 = drawingDocument1.Sheets
        Set DrawSheet1 = DrawSheets1.ActiveSheet
        Set selection1 = drawingDocument1.Selection


    selection1.Search c
    TextBox3 = selection1.Count

End Sub

Private Sub CommandButton2_Click()

    a = TextBox1.Value
    b = TextBox2.Value
    c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"
        Set drawingDocument1 = CATIA.ActiveDocument
        Set selection1 = drawingDocument1.Selection

    selection1.Search c
    Text2 = selection1.Count
    i = 1

    For i = 1 To Text2
        If Text2 > 0 Then
        selection1.Item(i).Value.Text = b
        End If
    Next
End Sub

Private Sub CommandButton3_Click()

On Error Resume Next
Err.Clear

Set myCATIA = GetObject(, "CATIA.Application")

Dim myDocs As Documents
Set myDocs = myCATIA.Documents

Dim numofopeneddoc, n, j As Integer
numofopeneddoc = myDocs.Count

    For n = 1 To numofopeneddoc

       a = TextBox1.Value
       b = TextBox2.Value
       c = "CATDrwSearch.DrwText.TextString_CAP=*" & a & "*,all"

        Set drawingDocument1 = CATIA.ActiveDocument
        Set selection1 = drawingDocument1.Selection
        selection1.Search c     'Starts the search
        Text2 = selection1.Count
         i = 1

        For i = 1 To Text2
        If Text2 > 0 Then
            selection1.Item(i).Value.Text = b  'actual text replacement
        End If
        Next

    Next n

End Sub

Alternatively, I also tried this method to no avail as well. Apparently, I can get around the .Selection portion enter image description here

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
CATIA
  • 9
  • 1
  • 3
  • CATIA.Documents will give you alle opened documents. These can be different types of Documents: DrawingDocument, PartDocument, ProductDocument etc. Loop through all documents from CATIA.Documents, check if they are a DrawingDocument and then apply all your functions on them in the same way that you do it for your single DrawingDocument, – gdir Jun 18 '19 at 07:20
  • I've modified what I wanted slightly by adding a new third command button which allows the user to apply changes to all the opened sheets. (See the post and image attached) I've followed your instructions with regards to the CATIA.Documents but was unable to get it to loop through. The code is able to run without any error msg but the end result still did not looped through as what I wanted. Is it possible if you can help me with this by showing me the way ? Your help i greatly appreciated! Thankyou – CATIA Jun 18 '19 at 14:35
  • Please don't post images of code. Post the code as actual code in the question. – Geert Bellekens Jun 19 '19 at 07:40

0 Answers0