0

I would like to get some VBA code which would tell me the number of sheets in a Catia drawing. Each sheet would have a title block placed on it. A text field on each title block would communicates the number of sheets. So if you had three sheets in the drawing you would have 1 of 3 (in the title block sheet 1) 2 of 3 (in the title block shhet 2) and 3 of 3 (in the title block sheet 3). If the macro could update all title blocks on all sheets automatically.

Any help much appreciated.

David Egan
  • 424
  • 2
  • 8
  • 23

1 Answers1

1

The concept is to loop through all of the DrawingSheet objects in the Sheets collection of the DrawingDocument you should put all title block elements in the "Background View". Next we need to update or create existing title block text elements. These are DrawingText objects. We try to access the DrawingText by name(THIS MUST BE UNIQUE!). If it does not exist, we create it. If it does exist, we update the value.

Here's a start to making your title block:

Option Explicit
Sub UpdateSheetPage()

    Dim DrawingDoc As DrawingDocument
    Dim DSheet As DrawingSheet
    Dim DView As DrawingView
    Dim SheetCount As Integer
    Dim currentSheet As Integer

    'the drawing must be the active docuement window or this will fail. you can do more error checking if needed
    On Error GoTo ExitSub
    Set DrawingDoc = CATIA.ActiveDocument
    SheetCount = DrawingDoc.Sheets.Count



    currentSheet = 1 'initialize sheet number
    'loop through all sheets and update or create a sheet number
    For Each DSheet In DrawingDoc.Sheets
        UpdatePageNumber DSheet, currentSheet, SheetCount
        currentSheet = currentSheet + 1
    Next

ExitSub:
End Sub

Sub UpdatePageNumber(currentDrawingSheet As DrawingSheet, currentSheetNumber As Integer, totalSheets As Integer)
    Dim sheetNumber As String
    Dim xPos, yPos As Long 'mm

    'edit these if needed
    xPos = 100 'edit this - only use for new creation
    yPos = 100 'edit this
    'display format
    sheetNumber = "Page " & currentSheetNumber & "/" & totalSheets

    Dim backgroundView As DrawingView
    Dim dTexts As DrawingTexts
    Dim currentText As DrawingText
    Set backgroundView = currentDrawingSheet.Views.Item("Background View")
    Set dTexts = backgroundView.Texts
    On Error GoTo CreateNew
    Set currentText = dTexts.GetItem("SheetNumber")
    currentText.Text = sheetNumber
    Exit Sub

CreateNew:
    Set currentText = dTexts.Add(sheetNumber, xPos, yPos)
    currentText.Name = "SheetNumber" 'so we can access it later for an update
End Sub
GisMofx
  • 982
  • 9
  • 27
  • Thank you for your help GisMofx, I must try the above out – David Egan Oct 15 '15 at 15:27
  • @DavidEgan create a new drawing document and add a few sheets. Just copy and paste the code into a new module in VBA editor. Run UpdateSheetPage() and you'll see the result on the sheets! – GisMofx Oct 15 '15 at 15:30
  • much appreciate it GisMofx – David Egan Oct 15 '15 at 15:40
  • Tried the above code works perfectly. Some tweaking required. Thanks again GisMofx – David Egan Oct 16 '15 at 13:25
  • @DavidEgan great! Would you mind marking this as answered? Another tip: If you have a fixed sheet size, you can make a "template" catdrawing to use as a base titleblock that you would reference when making new drawings. – GisMofx Oct 17 '15 at 12:40