This topic contains 3 replies, has 2 voices, and was last updated by  Matt 2 years, 4 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1719

    fredtheblue
    Participant

    Hi there!

    I often have to open many individual catdrawings along with maybe a catproduct and catparts.
    I have written a macro that when run should close each catdrawing but leave the catparts/catproducts loaded.

    It works to a degree, however often gives a run time error when returning activation to a loaded catpart.
    ie run time error 2147467259 (800004005)
    method ‘setImpl’ of object ‘documents’ failed

    Are there any obvious errors in the program.

    forgive me if it’s not a perfectly neatly written macro as I’m relatively new to programming!

    Option Explicit

    Sub CATMain()

    Dim iDoc As Integer
    Dim strType As String
    For iDoc = 1 To CATIA.Documents.Count
    ‘On Error Resume Next
    CATIA.Documents.Item(iDoc).Activate
    strType = TypeName(CATIA.ActiveDocument)

    Select Case strType
    Case “ProductDocument”
    ‘ just to do something
    CATIA.StartCommand “Center Graph”

    Case “PartDocument”
    ‘ just to do something
    CATIA.StartCommand “Center Graph”
    Case “DrawingDocument”
    CATIA.Documents.Item(iDoc).Activate
    CATIA.Documents.Item(iDoc).Close

    End Select

    Next
    MsgBox (“macro ended.”)

    End Sub

    cheers

    Dave

    • This topic was modified 2 years, 6 months ago by  fredtheblue.
    #2050

    Matt
    Participant

    Do you know what line it is failing on? First, I would remove CATIA.StartCommand “Center Graph” because you really don’t need to do this.

    Concerning your runtime error, You are looping through the number of open documents and closing them as you go. So let’s say there are 10 documents open, you close one, now there are only 9 documents open…but your macro told CATIA to loop through 10 documents…but there isn’t a 10th document. I think this is your problem. Also, sometimes it takes a while to close documents so the macro may be trying to close documents while CATIA is trying to close the first document, the code below won’t solve this but we can probably come up with something if it doesn’t work.

    If you are using VBA I think you can:
    Loop through all the documents and add them to a collection if they are a drawing, then loop through the collection of drawings and close them based on the name.

    If you are not using VBA, there is no collection class in CATScript/catvbs so you would need to use an array.

    I think the name of the window is the same as the name of the document…so it would look something like this in VBA:

    Dim cDrawings As New Collection
    Dim oDrawing as DrawingDocument

    For iDoc = 1 To CATIA.Documents.Count
    CATIA.Documents.Item(iDoc).Activate
    strType = TypeName(CATIA.ActiveDocument)

    Select Case strType

    Case “DrawingDocument”
    cDrawings.Add CATIA.Documents.Item(iDoc)

    End Select
    Next

    For iDoc = 1 to cDrawings.Count
    Set oDrawing = CATIA.Documents.Item(cDrawings.Item(i).Name)
    oDrawing.Close
    next

    #2064

    fredtheblue
    Participant

    Thanks Matt much appreciated.

    Macro was amended as below and sometimes worked but more often than not the drawings looped more than once and then ended with a run-time error 13: type mismatch on the line “Set oDrawing = CATIA.Documents.Item(cDrawings.Item(i).Name)”

    I have changed the odrawing type to an object and this seems to have worked. I think this is a bit of a fiddle but it works!

    Option Explicit
    Sub CATMain()

    Dim iDoc As Integer
    Dim i As Integer
    Dim N As Integer
    Dim strType As String
    Dim cDrawings As New Collection
    Dim oDrawing As Object
    For iDoc = 1 To CATIA.Documents.Count
    CATIA.Documents.Item(iDoc).Activate

    strType = TypeName(CATIA.ActiveDocument)

    Select Case strType

    Case “DrawingDocument”

    cDrawings.Add CATIA.Documents.Item(iDoc)

    Case “ProductDocument”

    ‘ just to do something

    CATIA.StartCommand “Center Graph”

    Case “PartDocument”

    ‘ just to do something

    CATIA.StartCommand “Center Graph”

    End Select
    Next

    For i = 1 To cDrawings.Count
    Set oDrawing = CATIA.Documents.Item(cDrawings.Item(i).Name)
    oDrawing.Close

    Next

    MsgBox (“macro ended.”)

    End Sub

    cheers

    Dave

    #2178

    Matt
    Participant

    That is great Dave, glad I could help.

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘CATIA Macro Programming’ is closed to new topics and replies.