Save .pdf and .dxf file with special condition.

Save .pdf and .dxf file with special condition.

deak_peter
Contributor Contributor
973 Views
7 Replies
Message 1 of 8

Save .pdf and .dxf file with special condition.

deak_peter
Contributor
Contributor

Hello everybody,

 

In a previous post I was able to help create a series of code that uses an excel spreadsheet to open a given .idw file and automatically save it to .pdf and all pages to dxf format. Thanks @A.Acheson. 🙂

 

The first tests have been successful and a small development opportunity has already arisen that I would like to ask for help with.


For our drawings, we need to make a cleaned up version to simplify the production, name these as the original part and add "-MOD" in quotes after it.

For example original partname: "ASD-54-4210 and cleaned up partname: ASD-54-4210-MOD")

My question would be, is there a way to save the .dxf in the opened .idw file, pay attention to what model is called for it on page 2 and 3. If the partnummer or something like that parameter has the above mentioned naming in its name, should the program save only those pages to .dxf format? But if the .idw file is only 1 page long, should it save it without any problems according to the original program?

I attach attached the vba code for excel and inventor in .txt format, hope these will help.

 

Thanks in advance.

 

0 Likes
Accepted solutions (1)
974 Views
7 Replies
Replies (7)
Message 2 of 8

deak_peter
Contributor
Contributor

Hallo,

 

I wrote code, but it didn't work and i don't know, how break my code.

Can you help me?

 

Public Sub PublishDXF()
    Dim doc As DrawingDocument
    Set doc = ThisApplication.ActiveDocument
    
    ' Check if the active document is a DrawingDocument
    If Not TypeOf doc Is DrawingDocument Then
        MsgBox "The active document is not a .idw file!", vbExclamation
        Exit Sub
    End If
    
    ' Check if the document has sheets
    If Not doc.Sheets.Count > 0 Then
        MsgBox "The document does not contain any sheets!", vbInformation
        Exit Sub
    End If
    
    Dim pageCount As Integer
    pageCount = 0
    
    ' Count the sheets and check for the specified part name
    For Each drawingSheet In doc.Sheets
        ' Check if the sheet has a model associated with it
        If drawingSheet.DrawingViews.Count > 0 Then
            Dim drawingView As DrawingView
            Set drawingView = drawingSheet.DrawingViews.Item(1)
            
            Dim modelDoc As Document
            Set modelDoc = drawingView.ReferencedDocumentDescriptor.ReferencedDocument
            
            If TypeOf modelDoc Is PartDocument Then
                Dim partDoc As PartDocument
                Set partDoc = modelDoc
                
                ' Check if the part name matches the criteria
                Dim partName As String
                partName = partDoc.DisplayName
                
                If partName Like "*-MOD" Or partName Like "*-MOD2" Then
                    ' Export only this sheet to DXF format
                    Dim DXFAddIn As TranslatorAddIn
                    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
                    
                    Dim oContext As TranslationContext
                    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
                    oContext.Type = kFileBrowseIOMechanism
                    
                    Dim oOptions As NameValueMap
                    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
                    
                    Dim oDataMedium As DataMedium
                    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

                    ' Check whether the translator has 'SaveCopyAs' options
                    If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
                    Dim strIniFile As String
                    strIniFile = "C:\Vault\exportdxf.ini"

                       ' Create the name-value that specifies the ini file to use.
                    oOptions.Value("Export_Acad_IniFile") = strIniFile
                    End If
                    
                    Dim fileName As String
                    fileName = "C:\Users\user\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & "_Sheet" & pageCount & ".dxf"
                    oDataMedium.FileName = fileName
                    
                    DXFAddIn.SaveCopyAs modelDoc, oContext, oOptions, oDataMedium
                End If
            End If
        End If
        
        ' Increment the page count
        pageCount = pageCount + 1
    Next drawingSheet
    
    ' If no sheets matched the criteria, export the entire document
    If pageCount = 1 Then
        Dim DXFAddIn As TranslatorAddIn
        Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
        
        Dim oContext As TranslationContext
        Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
        oContext.Type = kFileBrowseIOMechanism
        
        Dim oOptions As NameValueMap
        Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
        
        Dim oDataMedium As DataMedium
        Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

                    ' Check whether the translator has 'SaveCopyAs' options
                    If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
                    Dim strIniFile As String
                    strIniFile = "C:\Vault\exportdxf.ini"

                       ' Create the name-value that specifies the ini file to use.
                    oOptions.Value("Export_Acad_IniFile") = strIniFile
                    End If
        
        Dim fileName As String
        fileName = "C:\Users\user\Desktop\PDF-DXF program\"& Left(doc.DisplayName, Len(doc.DisplayName) - 4) & ".dxf"
        oDataMedium.FileName = fileName
        
        DXFAddIn.SaveCopyAs doc, oContext, oOptions, oDataMedium
    End If
End Sub
0 Likes
Message 3 of 8

A.Acheson
Mentor
Mentor

Hi @deak_peter 

 

Can you indicate where the failure is occurring? I am not at a computer to test so cannot offer any recommendations. A screenshot of the error line from the vba editor always helps. 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 4 of 8

A.Acheson
Mentor
Mentor

Hi @deak_peter

Some things I found

Missing Declaration

Dim drawingSheet As Sheet

Incorrect document reference

AAcheson_0-1687486396361.png

and again here

 DXFAddIn.SaveCopyAs modelDoc, oContext, oOptions, oDataMedium

  

Working Code: I removed the secondary dxf export block as I was not sure what the aim was. If you need to reuse blocks of code put them inside a sub routine and pass in the arguments required. 

Option Explicit
Public Sub PublishDXF()
    Dim doc As DrawingDocument
    Set doc = ThisApplication.ActiveDocument
    
    ' Check if the active document is a DrawingDocument
    If Not TypeOf doc Is DrawingDocument Then
        MsgBox "The active document is not a .idw file!", vbExclamation
        Exit Sub
    End If
    
    ' Check if the document has sheets
    If Not doc.Sheets.Count > 0 Then
        MsgBox "The document does not contain any sheets!", vbInformation
        Exit Sub
    End If
    
    Dim pageCount As Integer
    pageCount = 0
    
    Dim drawingSheet As Sheet
    ' Count the sheets and check for the specified part name
    For Each drawingSheet In doc.Sheets
   
        ' Check if the sheet has a model associated with it
        If drawingSheet.DrawingViews.Count > 0 Then
            
            Dim drawingView As drawingView
            Set drawingView = drawingSheet.DrawingViews.Item(1)
            
            Dim modelDoc As Document
            Set modelDoc = drawingView.ReferencedDocumentDescriptor.ReferencedDocument
            
            If TypeOf modelDoc Is PartDocument Then
                
                Dim partDoc As PartDocument
                Set partDoc = modelDoc
                
                ' Check if the part name matches the criteria
                Dim partName As String
                partName = partDoc.DisplayName
                
                'Debug.Print (partName)
                If partName Like "*-MOD" Or partName Like "*-MOD2" Then
                    ' Export only this sheet to DXF format
                    Dim DXFAddIn As TranslatorAddIn
                    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")

                    Dim oContext As TranslationContext
                    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
                    oContext.Type = kFileBrowseIOMechanism

                    Dim oOptions As NameValueMap
                    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

                    Dim oDataMedium As DataMedium
                    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

                    ' Check whether the translator has 'SaveCopyAs' options
                    If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
                        Dim strIniFile As String
                        strIniFile = "C:\Vault\exportdxf.ini"
    
                        ' Create the name-value that specifies the ini file to use.
                        oOptions.Value("Export_Acad_IniFile") = strIniFile
                    End If
    
                    Dim fileName As String
                    fileName = "C:\Users\user\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & "_Sheet" & pageCount & ".dxf"
                    Debug.Print fileName
                    oDataMedium.fileName = fileName
                        
                     DXFAddIn.SaveCopyAs doc, oContext, oOptions, oDataMedium
                End If
            End If
        End If
        ' Increment the page count
        pageCount = pageCount + 1
    Next drawingSheet
    
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 8

deak_peter
Contributor
Contributor

Hello @A.Acheson,

 

This code is not completely correct because there are 2 pages in the tested .idw drawing. The first page does not have the condition that the part name is "-MOD" or "-MOD2". On page 2, this condition is met, but the program generates both pages in .dxf format, even though the condition says that I only need page 2.

 

The second dxf generator was included because if the .idw file at the beginning of the program has the number of drawing pages: 1, it will not output the file to .dxf at all.

 

I wrote a new code which separates whether it should generate only a specific sheet or the whole document but throws the following error message:

 

 

Public Sub PublishDXF()
    Dim doc As DrawingDocument
    Set doc = ThisApplication.ActiveDocument
    
    ' Check if the active document is a DrawingDocument
    If Not TypeOf doc Is DrawingDocument Then
        MsgBox "The active document is not a .idw file!", vbExclamation
        Exit Sub
    End If
    
    ' Check if the document has sheets
    If Not doc.Sheets.Count > 0 Then
        MsgBox "The document does not contain any sheets!", vbInformation
        Exit Sub
    End If
    
    Dim pageCount As Integer
    pageCount = 0
    
    ' Count the sheets and check for the specified part name
    For Each drawingSheet In doc.Sheets
        ' Check if the sheet has a model associated with it
        If drawingSheet.DrawingViews.Count > 0 Then
            Dim drawingView As drawingView
            Set drawingView = drawingSheet.DrawingViews.Item(1)
            
            Dim modelDoc As Document
            Set modelDoc = drawingView.ReferencedDocumentDescriptor.ReferencedDocument
            
            If TypeOf modelDoc Is PartDocument Then
                Dim partDoc As PartDocument
                Set partDoc = modelDoc
                
                ' Check if the part name matches the criteria
                Dim partName As String
                partName = Left(partDoc.DisplayName, Len(partDoc.DisplayName) - 4)
                
                If partName Like "*-MOD" Or partName Like "*-MOD2" Then
                    ' Export only this sheet to DXF format
                    ExportSheetToDXF doc, drawingSheet, pageCount
                End If
            End If
        End If
        
        ' Increment the page count
        pageCount = pageCount + 1
    Next drawingSheet
    
    ' If no sheets matched the criteria or there is only one sheet, export the entire document
    If pageCount = 0 Or doc.Sheets.Count = 1 Then
        ExportDocumentToDXF doc
    End If
End Sub

Private Sub ExportSheetToDXF(ByVal doc As DrawingDocument, ByVal sheet As sheet, ByVal pageCount As Integer)
    ' Export the specified sheet to DXF format
    Dim DXFAddIn As TranslatorAddIn
    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
    
    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism
    
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

    ' Check whether the translator has 'SaveCopyAs' options
    If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
        Dim strIniFile As String
        strIniFile = "C:\Vault\exportdxf.ini"

        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If
    
    Dim fileName As String
    fileName = "C:\Users\deak.peter\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & "-MOD" & ".dxf"
    oDataMedium.fileName = fileName
    
    DXFAddIn.SaveCopyAs sheet, oContext, oOptions, oDataMedium
End Sub

Private Sub ExportDocumentToDXF(ByVal doc As DrawingDocument)
    ' Export the entire document to DXF format
    Dim DXFAddIn As TranslatorAddIn
    Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
    
    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism
    
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

    ' Check whether the translator has 'SaveCopyAs' options
    If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
        Dim strIniFile As String
        strIniFile = "C:\Vault\exportdxf.ini"

        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If
    
    Dim fileName As String
    fileName = "C:\Users\user\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & ".dxf"
    oDataMedium.fileName = fileName
    
    DXFAddIn.SaveCopyAs doc, oContext, oOptions, oDataMedium
End Sub

 

hiba1.PNG

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

Hi @deak_peter 

 The document object is required for export, you cannot supply a sheet object in place of drawing document object. See SaveCopyAsOptions API Help

 

Each sheet can be saved as a dxf file. If you want all sheets in one dxf file you must save all drawing views to one sheet then export sheet to dxf. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

deak_peter
Contributor
Contributor

So I can't do something like export only pages 2 and 3 of a 4 page drawing to .dxf? That would be the ultimate goal.

To export only pages 2 and 3 in a multi-page .idw file if you only need pages 2 and 3, not 1-2-3-4 pages.

0 Likes
Message 8 of 8

A.Acheson
Mentor
Mentor
Accepted solution

Hi @deak_peter 

Yes you can filter each sheet to be exported once you call the export sub routine on the given sheet and ensure the filename is unique per sheet. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes