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 Drafting Text Properties Macro 1

Status
Not open for further replies.

Yodi.dl

New member
Oct 4, 2023
12
Hi everyone. I was just wondering if someone can help me with a macro that would greatly help me with my projects. In CATIA V5 drawing, I insert text to list my instructioms. These instructions have item number on it so that the reader can refer to the Bill of materials when reading it. For example, "Remove screw item 123 and screw 456." In that text, i manually selext 123 and 456 one by one then change the frame from none to circle in the text properties. Is there a way that a macro is created to automatically detect the numbers and automatically add circle frames on them?
 
Replies continue below

Recommended for you

Hi thank you for the response. Actually I've been looking around in the forums but i couldn't see something similar to what i want. Unfortunately I'm not really familiar with macros and only been using chat gpt to try and have the code for what i need. But unfortunately im stuck and always get an error with the codes. I believe the main problem is detecting specific words in a sentence in catia drafting then adding frames on those specific words.

Hoping someone can provide me with the codes. I would really appreciate the help and it will greatly help me with my work.
 
maybe you should get familiar with Regular Expression ( also check
This could help find 3 digit number in a string.

Share your code and the errors and we will help...

Eric N.
indocti discant et ament meminisse periti
 
There is a code that opens a msgbox and asks for input text.
It will then search drawing for that text and change it to red.
see thread Link

I had to change a line of this code to be "endPos = Len(InsertText)" for it to work correctly for me.

Look at each line of that code and try to understand what it does.
Once you get that code to work, look to modify code for border of that same text.
look in the CAA help file (should be installed on your computer)
important for SetParameterOnSubString as this modifies a portion of the complete text.
231005_Image009_qeszbn.jpg



231005_Image010_oauv4b.jpg


the value for the circle border will probably be 3 ( the fourth option with No border being zero)
Once you get border to work.
231005_Image011_vtupq1.jpg

Now you can remove or comment out ' the change to red portion of code.

this macro searched complete drawing for text input.
so, you may want it to search only selected text.
you can search codes on here and find something that will work for that.

This still requires you to input what text to search for.
you could change this to look for each number from 1-1000, or whatever your range would be, instead of the "insert text"
this could probably be done with a "i to xx" loop, with a variable value in a loop that increases by 1 until it reached x

or if you typically only have a few numbers within your text, just type those in the input box.
still much quicker than the manual method.

and if you cant get the selected text to work, maybe copy the text to an empty drawing, run the macro and copy the text back.

Not an expert here, but I am pretty confident it can be done!
Most of my scripting is similar to above.
I find some similar ones and piece them together. then google search for many hours.
the initial hardest thing is to understand what each line is doing and why its needed.
Take each macro development in steps, get something to work (save it) then add/modify it.
 
Help me with this one please. It also add ellipse to the word on its right. For example the sentence is "remove screw 101-1 from the panel" it add ellipse on "101-1 from the pan". It looks like this "remove screw (101-1 from the pan)el.

User
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 TextFound As Boolean

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 pattern
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)
Dim pattern As String

' Check if the text contains the desired pattern
For j = 1 To 1999
For k = 1 To 50 ' You can adjust the upper limit if necessary
pattern = CStr(j) & "-" & CStr(k)
Dim startPos As Integer
startPos = InStr(1, CurrentText.Text, pattern, vbTextCompare)

' If the pattern is found, set the border type to Ellipse
If startPos > 0 Then
Dim endPos As Integer
endPos = startPos + Len(pattern) - 1
CurrentText.SetParameterOnSubString catBorder, startPos, endPos, 11 ' Border Type: Ellipse
TextFound = True
End If
Next k
Next j
Next numtxt
Next i

CATIA.ActiveWindow.ActiveViewer.Reframe

' Check if any text was found and show a message accordingly
If TextFound Then
MsgBox "Matching text patterns have been changed to Ellipse border.", vbInformation
Else
MsgBox "No matching text patterns found.", vbExclamation
End If
End Sub
 
endpos for SetParameterOnSubString should be something like the length of substring on which you want to apply the change...not the position where it ends

Eric N.
indocti discant et ament meminisse periti
 
I'm stuck on this one:

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 TextFound As Boolean

TextFound = False

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

' Create a regular expression object to match the pattern
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.pattern = "\b\d{1,4}-\d{1,2}\b" ' Match patterns like 123-45

' Loop through each view and check for the input text pattern
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 desired pattern
If regex.Test(CurrentText.Text) Then
' Set the border type to Ellipse for the pattern
CurrentText.SetParameter catBorder, 11 ' Border Type: Ellipse
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 "Matching text patterns have been changed to Ellipse border.", vbInformation
Else
MsgBox "No matching text patterns found.", vbExclamation
End If
End Sub

I have an error in
CurrentText.SetParameter catBorder, 11
 
CurrentText.FrameType = catEllipse

Eric N.
indocti discant et ament meminisse periti
 
It made the whole text frame to change into ellipse. The outcome i was hoping to have is the macro will detect all the number with patterns like 123-45 then change the properties of those detected number to have an ellipse border/frame.
 
Yodi.dl,
I think you need to use the SetParameterOnSubString, like you had on your first version.
and need to then identify start and end positions of the frame.

Below is my simplified version that checks selected text (not the whole drawing).
In my case I need an oblong frame surrounding something like 1|SC or 4|CC.
For me I always have two characters after the "|" and one character before.
Therefore the pattern search for me isn't really needed, Unless I had a bunch of other "|" symbols in my selected text (I don't).
I just tried the regex, but couldn't figure it out (got into a infinite loop).

=========================

Sub CATMain()
On Error Resume Next

Set MyDoc = CATIA.ActiveDocument
Set Mysel = MyDoc.Selection
SearchText = "|"

For i = 1 To Mysel.Count
Set CurrentText = Mysel.Item(i).Value

' Check if the text contains the Search text
startPos = InStr(1, CurrentText.Text, SearchText, vbTextCompare)
Do While startPos > 0
endPos = Len(SearchText)
CurrentText.SetParameterOnSubString catBorder, startPos - 1, endPos + 3, catOblong
startPos = InStr(startPos + 1, CurrentText.Text, SearchText, vbTextCompare)
Loop
Next

End Sub

============
 
in order to frame only the regex you need to
[ol 1]
[li]check if it exists (done with regex.Test(CurrentText.Text))

[/li]

[li]then check where it is to define the starting position of the subtring[/li]
please check this post to see how that is done

[li]finally get the length of the regex to define the length of the substring you will modify[/li]

[/ol]



Eric N.
indocti discant et ament meminisse periti
 
Thank you everyone for the help. But i cant get it to work. It still detect the other words aside from my pattern. Is it possible that it is because my pattern varies in length?

Example I have an instruction like:
1. Remove the sealant 101-1 from the head of screws 1-1. Clean using alcohol 201-1.

I want to add ellipse on the 101-1, 1-1, and 201-1.

The expected output is:
1. Remove the sealant (101-1) from the head of screws (1-1). Clean using alcohol (201-1).

Hope someone can me with this one. Many thanks!
 
so what is your code so far?

The expected output is:
1. Remove the sealant (101-1) from the head of screws (1-1). Clean using alcohol (201-1).

What is the actual output ?

Hope you checked my previous post as it should help find the solution...

Eric N.
indocti discant et ament meminisse periti
 
The only problem i have now is it also detects the pattern even it is not standalone. Example it add ellipse on "1001" in "123226-1001". My pattern so far is x-y where x is 1-4 digits, y is 1-2 digits, and numbers from 1000-1999.

Here's my current 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 TextFound As Boolean
TextFound = False

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

' Regular expression pattern to match "x-y" format where x can be 1 to 4 digits and y can be 1 to 2 digits,
' or numbers in the range 1000-1999
Dim pattern As String
pattern = "\b(?:\d{1,3}-\d{1,2}|1\d{3})\b"

' Loop through each view and check for the input text pattern
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 desired pattern using regular expression
With CreateObject("VBScript.RegExp")
.Global = True
.pattern = pattern
If .Test(CurrentText.text) Then
' Find all matches within the text
Set matches = .Execute(CurrentText.text)

' Apply ellipse border to each match
For Each match In matches
startPos = match.FirstIndex + 1
patternLength = Len(match.Value)
CurrentText.SetParameterOnSubString catBorder, startPos, patternLength, 11 ' Border Type: Ellipse
TextFound = True
Next match
End If
End With
Next numtxt
Next i

CATIA.ActiveWindow.ActiveViewer.Reframe

' Check if any text was found and show a message accordingly
If TextFound Then
MsgBox "Matching text patterns have been changed to Ellipse border.", vbInformation
Else
MsgBox "No matching text patterns found.", vbExclamation
End If
End Sub
 
once you have a match, you can check previous char and skip if it is a -
or play with regex until you find the proper syntax
maybe something like this will do \b(?:\d{1,3}-\d{1,2}|((?<!-)1\d{3}))\b

you can test your regex here

Eric N.
indocti discant et ament meminisse periti
 
I followed your advice to check for previous char and skip it if it's a "-" since i can't get the correct regex that works like that. Thank you! :)

Considering that ballons in figures are considered as text, and my balloons have those patterns like (1-1), is there a way to exlude the text in the balloons so that it won't have the ellipse?
 
better to post a picture with what you want (or dont) .

The regex in my previous post should work.

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

Part and Inventory Search

Sponsor