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 macro for searching specified text

Status
Not open for further replies.

durga_abishek_

Mechanical
Jul 28, 2023
3
Greetings!!

I need help in refining of my Catia Macro where it searches the given input text and highlight them as red
1.in my case suppose i have a text in drawing as Engineeringstandards ,my input text is standards
when i run the macro it highlights the entire Engineeringstandards words not the specific words given in the input
please help me on this

2.this works only for the first sheet not the activated sheet
please help me to resolve the above issues

here is the code

Sub CATMain()

' Set the CATIA popup file alerts to False
' It prevents the macro from stopping at each alert during its execution
CATIA.DisplayFileAlerts = False

Set oDrwDocument = CATIA.ActiveDocument
Dim InsertText As String
Dim TextFound As Boolean

InsertText = InputBox("Insert Text")

' Check if the user clicked the "Cancel" button or provided an empty input
If InsertText = "" Then
MsgBox "No input provided. Macro will now exit.", vbExclamation
Exit Sub
End If

TextFound = False

' Retrieve the drawing document's view collection
Set oDrwView = oDrwDocument.Sheets.Item(1).Views

' Loop through each view and check for the input text
For i = 1 To oDrwView.Count

' Loop through all the texts of the view
For numtxt = 1 To oDrwView.Item(i).Texts.Count
Set CurrentText = oDrwView.Item(i).Texts.Item(numtxt)

' Check if the text matches the input text
If InStr(1, CurrentText.Text, InsertText, vbTextCompare) > 0 Then
' Change the color of the matched text to RED
Set DrwSelect = CATIA.ActiveDocument.Selection
DrwSelect.Clear
DrwSelect.Add CurrentText
DrwSelect.VisProperties.SetRealColor 255, 0, 0, 0

' Set TextFound to true since we found at least one matching text
TextFound = True
End If
Next 'numtxt

Next 'i

CATIA.ActiveWindow.ActiveViewer.Reframe

' Check if any text was found and show a message accordingly
If TextFound Then
MsgBox "Text containing '" & InsertText & "' has been highlighted in RED.", vbInformation
Else
MsgBox "No text containing '" & InsertText & "' found.", vbExclamation
End If

End Sub


regards
abishek
 
Replies continue below

Recommended for you

if you want to change only the matching text, you should work on subtext and use
Code:
CurrentText.SetParameterOnSubString(...)

if you want to work on ActiveSheet you should change
Code:
Set oDrwView = oDrwDocument.Sheets.[COLOR=#EF2929]Item(1)[/color].Views

Eric N.
indocti discant et ament meminisse periti
 
UPDATED THE CODE AS BELOW ITS WORKS FOR THE ACTIVE SHEET THANKS FOR THAT BUT I COULDNT ABLE TO RESOLVE THE MATCHING TEXT
PLEASE HELP ME ON THIS

Sub CATMain()

' Set the CATIA popup file alerts to False
' It prevents the macro from stopping at each alert during its execution
CATIA.DisplayFileAlerts = False

Set oDrwDocument = CATIA.ActiveDocument
Dim InsertText As String
Dim TextFound As Boolean

InsertText = InputBox("Insert Text")

' Check if the user clicked the "Cancel" button or provided an empty input
If InsertText = "" Then
MsgBox "No input provided. Macro will now exit.", vbExclamation
Exit Sub
End If

TextFound = False

' Retrieve the drawing document's view collection
Set oDrwView = oDrwDocument.Sheets.ActiveSheet.Views

' Loop through each view and check for the input text
For i = 1 To oDrwView.Count

' Loop through all the texts of the view
For numtxt = 1 To oDrwView.Item(i).Texts.Count
Set CurrentText = oDrwView.Item(i).Texts.Item(numtxt)

' Check if the text matches the input text
If InStr(1, CurrentText.Text, InsertText, vbTextCompare) > 0 Then
' Change the color of the matched text to RED
Set DrwSelect = CATIA.ActiveDocument.Selection
DrwSelect.Clear
DrwSelect.Add CurrentText
DrwSelect.VisProperties.SetRealColor 255, 0, 0, 0

' Set TextFound to true since we found at least one matching text
TextFound = True
End If
Next 'numtxt

Next 'i

CATIA.ActiveWindow.ActiveViewer.Reframe

' Check if any text was found and show a message accordingly
If TextFound Then
MsgBox "Text containing '" & InsertText & "' has been highlighted in RED.", vbInformation
Else
MsgBox "No text containing '" & InsertText & "' found.", vbExclamation
End If

End Sub
 
that is because you use selection.visproperty and not text.SetParameterOnSubString

Check the doc about SetParameterOnSubString

Eric N.
indocti discant et ament meminisse periti
 
TRIED BUT I COULDNT FIND THE EXACT MATCHING RESULT

Sub CATMain()

' Set the CATIA popup file alerts to False
' It prevents the macro from stopping at each alert during its execution
CATIA.DisplayFileAlerts = False

Set oDrwDocument = CATIA.ActiveDocument
Dim InsertText As String
Dim TextFound As Boolean

InsertText = InputBox("Insert Text")

' Check if the user clicked the "Cancel" button or provided an empty input
If InsertText = "" Then
MsgBox "No input provided. Macro will now exit.", vbExclamation
Exit Sub
End If

TextFound = False

' Retrieve the drawing document's view collection
Set oDrwView = oDrwDocument.Sheets.ActiveSheet.Views

' Loop through each view and check for the input text
For i = 1 To oDrwView.Count

' Loop through all the texts of the view
For numtxt = 1 To oDrwView.Item(i).Texts.Count
Set CurrentText = oDrwView.Item(i).Texts.Item(numtxt)

' Check if the text contains the input text
Dim startPos As Integer
startPos = InStr(1, CurrentText.Text, InsertText, vbTextCompare)
Do While startPos > 0
Dim endPos As Integer
endPos = startPos + Len(InsertText) - 1

' Change the color of the matched text to RED
CurrentText.SetParameterOnSubString catColor, startPos - 1, endPos, -16776961 ' Color: Red

' Set TextFound to true since we found at least one matching text
TextFound = True

' Search for the next occurrence of the input text
startPos = InStr(startPos + 1, CurrentText.Text, InsertText, vbTextCompare)
Loop
Next 'numtxt

Next 'i

CATIA.ActiveWindow.ActiveViewer.Reframe

' Check if any text was found and show a message accordingly
If TextFound Then
MsgBox "Text containing '" & InsertText & "' has been highlighted in RED.", vbInformation
Else
MsgBox "No text containing '" & InsertText & "' found.", vbExclamation
End If

End Sub
 
I did a minor update but your code works on my side
Code:
endPos = startPos + Len(InsertText)
2023-08-01_7-16-39_nj6wo1.png


it could be also better to change the name of your oDrwView parameter to oDrwViews...just saying

Can you explain more about
you said:
I COULDNT FIND THE EXACT MATCHING RESULT

Eric N.
indocti discant et ament meminisse periti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor