Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

CATIA FIND AND REPLACE 1

Status
Not open for further replies.

Yodi.dl

New member
Oct 4, 2023
12
Hi everyone hopefully soneone can correct my code to find number of count of specified text in catia drafting and then replace exact matches only with what i want. Current condition is when i try to replace 1-1 with lets say 2-1, it also replaces the 201-1 with 202-1 since it also has a 1-1 in it.

Dim a As String
Dim b As String
Dim c As String
Dim drawingDocument1 As DrawingDocument
Dim selection1 As Selection

Private Sub CommandButton1_Click()
' Get value from TextBox1
a = TextBox1.Value

' Set up CATIA objects
Set drawingDocument1 = CATIA.ActiveDocument
Set selection1 = drawingDocument1.Selection

' Perform search operation
Dim totalCount As Integer
totalCount = 0

' Loop through all drawing texts and count occurrences of the word
For i = 1 To selection1.Count
Dim textItem As DrawingText
Set textItem = selection1.Item(i).Value

' Check if the text contains the search word using a regular expression
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.pattern = "\b" & a & "\b"
Dim matches As Object
Set matches = regex.Execute(textItem.Text)
totalCount = totalCount + matches.Count
Next i

' Update TextBox3 with the count of matching words
TextBox3.Text = CStr(totalCount)
End Sub

Private Sub CommandButton2_Click()
' Get values from TextBoxes
Dim a As String
Dim b As String
a = TextBox1.Value
b = TextBox2.Value

' Set up CATIA objects
Dim drawingDocument1 As Document
Set drawingDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = drawingDocument1.Selection

' Set up regular expression for word boundary matching
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.pattern = "\b" & a & "\b"

' Loop through the found items and perform replacements
For i = selection1.Count To 1 Step -1
Dim textItem As DrawingText
Set textItem = selection1.Item(i).Value

' Check if the search term is a standalone word using the regular expression
If regex.Test(textItem.Text) Then
' Perform replacement
textItem.Text = Replace(textItem.Text, a, b, , , vbTextCompare)
End If
Next i
End Sub
 
Replies continue below

Recommended for you

I simply want the replacement to happen when the found text is an exact match. Many thanks everyone.
 
I would expect: textItem.Text = Replace(textItem.Text, a, b, , , vbTextCompare)

is the problem

Replace isn't given any index to the exact match on the word boundary.

There is a regex.Replace; perhaps it is available to you. This will replace per the regex match.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor