0

I am trying to simplify the title block input in Catia V5.21 by reading data from an .xls table and using it to populate the title block (part number, material code, description, revision, date, author etc). I want to do this in a title block that I will design (not the styles already implemented in Catia).

I would love to do it by myself but I have no idea where to start. Does anyone have any pointers or are there any tutorials to get me started?

user2882635
  • 133
  • 2
  • 19

3 Answers3

1

Try first to record a macro when you are creating your new title block, this will give you an idea how lines and text are created. After that you can start to connect Excel cells values with text values in CATIA.

OK, agree, drafting is not the most user friendly when coding :-) . Still, if I remember correctly (because now I don't have CATIA) some things are recorded...

        ' ======================================================
        ' Purpose: Macro will activate the backgroud view in an active CATIA drawing (A4 sheet) and will draw a title block
        ' Usage:   1 - A CATDrawing must be active
        '          2 - Run macro 
        ' Author: ferdo (Disclaimer: You use this code at your own risk) 
        ' ======================================================
        Language="VBSCRIPT"

        ' made as example by ferdo for auxcad.com

        Sub CATMain()

        Dim CATIA As Object
        Set CATIA = GetObject(, "CATIA.Application")

        Dim MyDrawingDoc As DrawingDocument
        Set MyDrawingDoc = CATIA.ActiveDocument

        Dim MyDrawingSheets As DrawingSheets
        Set MyDrawingSheets = MyDrawingDoc.Sheets

        Dim MyDrawingSheet As DrawingSheet
        Set MyDrawingSheet = MyDrawingSheets.ActiveSheet

        Dim MyDrawingViews As DrawingViews
        Set MyDrawingViews = MyDrawingSheet.Views

        Dim drwviews As DrawingViews  'make background view active
        Set drwviews = MyDrawingSheet.Views
        drwviews.Item("Background View").Activate

        'Set myText.... As DrawingText - adding texts
        Set myText = MyDrawingViews.ActiveView.Texts.Add ("Dibujado", 22, 38) 'coordinates x=22, y=38 of left bottom corner of the text location
        Set myText1 = MyDrawingViews.ActiveView.Texts.Add ("Corregido", 22, 31)
        Set myText2 = MyDrawingViews.ActiveView.Texts.Add ("Fecha", 57, 46)
        Set myText3 = MyDrawingViews.ActiveView.Texts.Add ("DD-mm-08", 57, 38)
        Set myText4 = MyDrawingViews.ActiveView.Texts.Add ("DD-mm-08", 57, 31)
        Set myText5 = MyDrawingViews.ActiveView.Texts.Add ("Nombre", 87, 46)
        Set myText6 = MyDrawingViews.ActiveView.Texts.Add ("Jefatura", 87, 38)
        Set myText7 = MyDrawingViews.ActiveView.Texts.Add ("Delineante", 87, 31)
        Set myText8 = MyDrawingViews.ActiveView.Texts.Add ("Empresa S.A.", 159, 40)
        Set myText9 = MyDrawingViews.ActiveView.Texts.Add ("C/laredo 8, 2B", 159, 32)

        Set myText13 = MyDrawingViews.ActiveView.Texts.Add ("Escalas:", 22, 23)
        Set myText14 = MyDrawingViews.ActiveView.Texts.Add ("1/X", 22, 17)
        Set myText15 = MyDrawingViews.ActiveView.Texts.Add ("1/X", 22, 11)
        Set myText16 = MyDrawingViews.ActiveView.Texts.Add ("Firma", 128, 38)

        Dim iFortSize1 As Double 'font text size 
        iFontSize1 = 3.500
        myText1.SetFontSize 0, 0, 3.500  'iFontSize

        'next lines with a different size for fonts - 2.5
        Set myText10 = MyDrawingViews.ActiveView.Texts.Add ("Sustituye a: xxx-08", 155, 22)
        Set myText11 = MyDrawingViews.ActiveView.Texts.Add ("Sustituido por: xxx-08", 155, 12)

        Dim iFortSize10 As Double
        iFontSize10 = 2.500
        myText10.SetFontSize 0, 0, 2.500  'iFontSize

        Dim iFortSize11 As Double
        iFontSize11 = 2.500
        myText11.SetFontSize 0, 0, 2.500  'iFontSize

        'next lines with a different size for fonts - 5
        Set myText12 = MyDrawingViews.ActiveView.Texts.Add ("plano No xxx-08", 70, 18)

        Dim iFortSize12 As Double
        iFontSize12 = 5.00
        myText12.SetFontSize 0, 0, 5.00  'iFontSize

        'Declarations

        Dim DrwDocument   As DrawingDocument
        Dim DrwSheets     As DrawingSheets
        Dim DrwSheet      As DrawingSheet
        Dim DrwView       As DrawingView
        Dim DrwTexts      As DrawingTexts
        Dim Text          As DrawingText
        Dim Fact          As Factory2D
        Dim Point         As Point2D
        Dim Line          As Line2D
        Dim Cicle         As Circle2D
        Dim Selection     As Selection
        Dim GeomElems     As GeometricElements


          Set DrwDocument = CATIA.ActiveDocument
          Set DrwSheets   = DrwDocument.Sheets
          Set Selection   = DrwDocument.Selection
          Set DrwSheet    = DrwSheets.ActiveSheet
          Set DrwView     = DrwSheet.Views.ActiveView
          Set DrwTexts    = DrwView.Texts
          Set Fact        = DrwView.Factory2D
          Set GeomElems   = DrwView.GeometricElements


        'draw frame bottom line
            Set Line1 = Fact.CreateLine(20, 5, 205, 5) 'these are the coordinates of the starting point x=20, y=5 of the line and end point of the line x=205, y=5
            Line1.Name = "Line1"
            CATIA.ActiveDocument.Selection.VisProperties.SetRealWidth 3,1
            CATIA.ActiveDocument.Selection.Clear

        'draw frame upper line
            Set Line2 = Fact.CreateLine(20, 292, 205, 292)
            Line2.Name = "Line2"
            CATIA.ActiveDocument.Selection.VisProperties.SetRealWidth 3,1
            CATIA.ActiveDocument.Selection.Clear

        'draw a thin line 
            Set Line3 = Fact.CreateLine(20, 40, 120, 40)
            Line3.Name = "Line3"
            CATIA.ActiveDocument.Selection.Add Line3
            Set visProperties1 = CATIA.ActiveDocument.Selection.VisProperties
            visProperties1.SetRealLineType 1,0.2
            Set visProperties1 = CATIA.ActiveDocument.Selection.VisProperties 
            visProperties1.SetRealWidth 1,0.2


            CATIA.ActiveDocument.Selection.Clear
        ' You can continue to draw the rest of the lines and try other settings...


        End Sub
ferdo
  • 178
  • 4
  • Remember that recording a macro while in drafting workbench yields an empty `Sub` – GisMofx Jun 15 '16 at 18:48
  • Exactly! that's why I'm getting nowhere as I have no reference points to start writing the script – user2882635 Jun 16 '16 at 07:24
  • Thanks ferdo, the code works great and I'll be able to modify it. One more question: do you have and pointers how to read the text in the Properties of The Part? – user2882635 Jun 17 '16 at 14:17
  • You mean reading user added properties and catpart properties? Yes, I know, but would be better for you to check v5automation.chm file. If you still have problems, come back here with the code.You can check also Google. – ferdo Jun 17 '16 at 16:10
  • Please see my answer – user2882635 Jun 20 '16 at 13:24
0

Ferdo, I modified your code so it now reads data from an .xlsx file and uses it to fill in the text boxes on the drawing. Now I'm having a few problems: 1. I had to deactivate the code for drawing lines as I got an error for duplicate declaration in current scope for CATIA object. After I removed the code, everything worked fine. Do you perhaps know what would be the cause? 2. I cannot change the font using normal VBA methods. When I add the line that's commented in the code bellow i get an error: Method 'Open? of Object 'WorkBooks' Failed. 3. I'm having issues with opening the xlsx file even when I close Catia. I thought it was because the macro opens the file but doesn't close it and I tried adding the close method at the end but I also keep getting errors.

Code:

Sub CATMain()
    'Define the variables
    Dim GetData As Range    'range for finding cells in workbook
    Dim PartNum As String   'variable for search key
    Dim MyPath As String    'variable for workbook file path
    Dim MyWB As String      'variable for workbook file name

    Dim Datum As Date

    Dim FontSize1 As Double 'font text size
    Dim FontSize2 As Double
    Dim FontSize3 As Double

    Dim FontName1 As Double

    'The text for which to search
    PartNum = InputBox(prompt:="Enter Filter Part Number", Title:="Filter Part Number")

    'The path to the workbook
    MyPath = "C:\New folder\"

    'The name of the workbook in which to search.
    MyWB = "Podatki.xlsx"

    'Turn off screen updating, and then open the target workbook.
    Application.ScreenUpdating = False
    Workbooks.Open Filename:=MyPath & MyWB

    'Search for specified text
    Set GetData = ActiveSheet.Cells.Find(PartNum)


    Dim CATIA As Object
    Set CATIA = GetObject(, "CATIA.Application")

    Dim MyDrawingDoc As DrawingDocument
    Set MyDrawingDoc = CATIA.ActiveDocument

    Dim MyDrawingSheets As DrawingSheets
    Set MyDrawingSheets = MyDrawingDoc.Sheets

    Dim MyDrawingSheet As DrawingSheet
    Set MyDrawingSheet = MyDrawingSheets.ActiveSheet

    Dim MyDrawingViews As DrawingViews
    Set MyDrawingViews = MyDrawingSheet.Views

    Dim drwviews As DrawingViews  'make background view active
    Set drwviews = MyDrawingSheet.Views
    drwviews.Item("Background View").Activate



    'Set myText.... As DrawingText - adding texts
    Set myText1 = MyDrawingViews.ActiveView.Texts.Add(GetData.Value, 376, 19)
    Set myText2 = MyDrawingViews.ActiveView.Texts.Add(GetData.Offset(0, -1), 374, 24)
    Set myText3 = MyDrawingViews.ActiveView.Texts.Add(GetData.Offset(0, 1), 376, 14)
    Set myText4 = MyDrawingViews.ActiveView.Texts.Add(Date, 357, 34)
    Set myText5 = MyDrawingViews.ActiveView.Texts.Add(Date, 357, 39)
    Set myText6 = MyDrawingViews.ActiveView.Texts.Add(Date, 357, 44)
    Set myText7 = MyDrawingViews.ActiveView.Texts.Add("Surname Name", 374, 44)


    FontSize1 = 2.5
    FontSize2 = 2
    FONTNAME = "Arial (TrueType)"  ''if I remember correctly, here is only Arial without TrueType
    myText1.SetFontSize 0, 0, FontSize1
    myText2.SetFontSize 0, 0, FontSize1
    myText3.SetFontSize 0, 0, FontSize1
    myText4.SetFontSize 0, 0, FontSize2
    myText5.SetFontSize 0, 0, FontSize2
    myText6.SetFontSize 0, 0, FontSize2
    myText7.SetFontSize 0, 0, FontSize2

    'myText1.SetFontName 0, 0, FontName1


    'Workbooks(MyPath & MyWB).Close SaveChanges:=False
    'Workbooks.Close Filename:=MyPath & MyWB

End Sub
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
user2882635
  • 133
  • 2
  • 19
0

You can't declare same thing two times, you'll get an error. On the other hand, where did you declared Excel? Something like bellow ? Don't forget also to close Excel and check your code, I've done a small edit regarding the font type

' Open an Excel File from CATIA 

Dim OutPath 
Dim OutIndex 
Dim wbk As  Excel.Workbook 
Dim xlApp As Excel.Application   
OutPath = "C:\temp\" 
OutIndex = "YourFile.xls"
ferdo
  • 178
  • 4