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!

Set text color Drafting Macro

Status
Not open for further replies.

JrJBS

New member
Dec 12, 2017
5
Hello all,

I'm trying to set the red color on a text I have added with the following code taken from VBA help:

Property Color( ) As long

Returns or sets the color of the drawing text.
Example:
This example sets the Color type of the MyText drawing text to red
redCol =-16776961 'Encoded RGBA color within long integer (R=255 G=0 B=0 A=255)
MyText.Color = redCol

Code I'm trying:

MyText.SetFontSize 0, 0, 20
redCol = -16776961
MyText.Color = redCol 'ERROR

SetFontSize works perfectly but the line for the colour doesn't...

Another thing I've tried is this code, based on a search I have done in the forum, but doesn't work neither:

MyText.visProperties1.SetRealColor 255, 0, 0, 1

Any ideas?

(I'm coding on VBA)



 
Replies continue below

Recommended for you

Hi

Code:
' ==============================================================
' Purpose: This macro will change the color of all text in drawing to red
' Usage:   1 - Works in an active catdrawing
'          2 - Run macro 
' Author: ferdo (Disclaimer: You use this code at your own risk) 
' ===============================================================

Language="VBSCRIPT"

Sub CATMain()

' Set the CATIA popup file alerts to False
' It prevents to stop the macro at each alert during its execution

CATIA.DisplayFileAlerts = False
Set oDrwDocument = CATIA.ActiveDocument

' Retrieve the drawing document's view collection
Set oDrwView = oDrwDocument.Sheets.Item(1).views
Dim drawingDocument1 As Document
Set drawingDocument1 = CATIA.ActiveDocument
Dim selection1 As Selection
Set selection1 = drawingDocument1.Selection

selection1.Search "CATDrwSearch.DrwText.TextString=*,all"

'For each view search text
For i = 2 To oDrwView.Count

'Scan all the Texts of the Views
For numtxt = 1 To oDrwView.Item(i).Texts.Count
Set CurrentView = oDrwView.Item(i)

'Change the color of the text in RED
Set Drwselect = CATIA.ActiveDocument.Selection
Drwselect.Add oDrwView.Item(i).Texts.Item(numtxt)
Drwselect.VisProperties.SetRealColor 255, 0, 0, 0

Next
Next

End Sub

Regards
Fernando

- Romania
- EU
 
Thank you for answering but I have the same problem...

What I'm trying to do is put in red only the word I've just written.


Sub InsertText()

Set MyDrawingDoc = CATIA.ActiveDocument
Set MyDrawingSheets = MyDrawingDoc.Sheets
Set MyDrawingSheet = MyDrawingSheets.ActiveSheet

Dim MyDrawingViews As DrawingViews
Set MyDrawingViews = MyDrawingSheet.Views

Dim InsertText As String

InsertText = InputBox("Insert Text")

'Set myText.... As DrawingText - adding texts
Set myText = MyDrawingViews.ActiveView.Texts.Add(InsertText, 33, 75)

myText.SetFontSize 0, 0, 20
myText.VisProperties.SetRealColor 255, 0, 0, 0 'ERROR

End Sub
 
Or, if you don't want to use search

Code:
Sub CATMain()

Set MyDrawingDoc = CATIA.ActiveDocument
Set MyDrawingSheets = MyDrawingDoc.Sheets
Set MyDrawingSheet = MyDrawingSheets.ActiveSheet

Dim MyDrawingViews As DrawingViews
Set MyDrawingViews = MyDrawingSheet.Views

Dim InsertText As String

InsertText = InputBox("Insert Text")

'Set myText.... As DrawingText - adding texts
Set myText = MyDrawingViews.ActiveView.Texts.Add(InsertText, 33, 75)
myText.SetFontSize 0, 0, 20

           '~ 'Change the color of the text in RED
                Set Drwselect = CATIA.ActiveDocument.Selection
                           DrwSelect.Add myText
                  DrwSelect.VisProperties.SetRealColor 255, 0, 0, 0

End Sub

Regards
Fernando

- Romania
- EU
 
Great, much better!

I didn't find the way of putting the variable in the following code...

selection1.Search "CATDrwSearch.DrwText.TextString= ____ "

Actually, I have to put a string followed by the variable, anyway... now works perfectly.

Thank you very much Berto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor