Hi,
I have found a macro on here that works to export al our part drawings within an assembly, I would like to make it export the drawing behind the subassembly of the assembly aswell. Does anyone know how i could change my macro to do so?
Secondary i would also like to add the description of a drawing to the export file name if possible.
Any help would be much appreciated.
'Set Folder options as you see fit here
oFolder = 'Enter Folder String here.
'PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oPDFContext = oTG.CreateTranslationContext
oPDFContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oPDFOptions = oTG.CreateNameValueMap
oPDFDataMedium = oTG.CreateDataMedium
oPDFOptions.Value("All_Color_AS_Black") = 0
oPDFOptions.Value("Remove_Line_Weights") = 1
oPDFOptions.Value("Vector_Resolution") = 4800
oPDFOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'Generate List of all Documents Referenced for this Assembly.
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
'Generate Document Reference.
Dim oDoc As Document
'Peruse each document in generated Documents list.
For Each oDoc In oRefDocs
'Generate text string of .idw file to look for in the same folder as component file.
idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "idw"
'Check if generated drawing file name exists.
If System.IO.File.Exists(idwPathName) Then
'Open drawing file.
oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
'Remove idw extension for PDF save.
oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 3)
'Generate text string of idw name and folder location.
oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"
'Save PDF Copy.
Call PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)
'Close drawing file.
oDrawDoc.Close(True)
End If
Next
'Top Level Document reference.
oDoc = ThisApplication.ActiveDocument
'Generate text string of .idw file to look for in the same folder as Assembly file.
idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "idw"
'Check if generated drawing file name exists.
If System.IO.File.Exists(idwPathName) Then
'Open drawing file.
oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
'Remove idw extension for PDF save.
oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 3)
''Generate text string of idw name and folder location.
oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"
'Save PDF Copy.
Call PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)
'Close drawing file.
oDrawDoc.Close(True)
End If
Solved! Go to Solution.
Solved by jesserop. Go to Solution.
Hi @jesserop. After reviewing the code you posted, it looks like it was missing a couple of variable declarations and their values. As well as needing a couple more variable declarations to help maintain the 'intellisense' type support. Since the code doesn't filter the referenced documents by document type, I don't know why it wouldn't be creating the PDF's for any sub-assemblies within the main assembly, unless they just did not already have a drawing to export the PDF from. I edited your code a bit and condensed it for you. I put most of the code needed for exporting a drawing to PDF into a custom Sub routine, so that it can be called to run on the main assembly, then for each of its referenced documents. I haven't re-tested the code after making these changes, so review it and test with caution.
Make sure those sub-assemblies have a drawing document, and that they are named the same as the sub-assembly file, but with different file extension, otherwise this code won't export a PDF for them.
I included a line of code to get the value of the Drawing's Description iProperty from the Drawing file, but I didn't write any additional lines of code to incorporate it into the name of the PDF that is to be created, because I did not know in which way you wanted it added, or where.
Sub Main
'get the main assembly document
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
'run the Sub below on the main assembly
ExportDrawingToPDF(oADoc)
'Peruse each document in generated Documents list.
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
'run the Sub below on each referenced document
ExportDrawingToPDF(oRefDoc)
Next
End Sub
Sub ExportDrawingToPDF(oDoc As Document)
'Set Folder options as you see fit here
oFolder = "" 'Enter Folder String here.
'PDF Options
oAddIns = ThisApplication.ApplicationAddIns
Dim PDFAddIn As TranslatorAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oTO = ThisApplication.TransientObjects
oPDFContext = oTO.CreateTranslationContext
oPDFContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oPDFOptions = oTO.CreateNameValueMap
oPDFDataMedium = oTO.CreateDataMedium
oPDFOptions.Value("All_Color_AS_Black") = 0
oPDFOptions.Value("Remove_Line_Weights") = 1
oPDFOptions.Value("Vector_Resolution") = 4800
oPDFOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'Generate text string of .idw file to look for in the same folder as component file.
idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "idw"
'Check if generated drawing file name exists.
If System.IO.File.Exists(idwPathName) Then
'Open drawing file.
Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
'get the value of the description iProperty, within the drawing
Dim oDescription As String = oDrawDoc.PropertySets.Item("Design Tracking Properties").Item("Description").Value
'Remove idw extension for PDF save.
oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 3)
'Generate text string of idw name and folder location.
oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"
'Save PDF Copy.
PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)
'Close drawing file.
oDrawDoc.Close(True)
End If
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
Hi @WCrihfield ,
Thanks for the help!
Your code is giving me an error after it opens the first drawing:
Do you have a solution for this?
It's hard to tell anything from the error message screen capture you posted, which translates to "Unspecified Error" in English. It's usually good practice to include a screen shot of the information in both tabs of the error message, when possible. Especially when the main error says something useless like "Unspecified Error". Often times the information in the other tab is much more informational.
I'm curious...did you fill in a value for the oFolder variable? If not, it would have likely opened the drawing OK, but then tried to save the resulting PDF file without any specified path, because of the following lines:
oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"
'Save PDF Copy.
PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)
Wesley Crihfield
(Not an Autodesk Employee)
You're completely right, I totally didn't see that i did not have a folder path.
Massive thanks or helping me out!
Can't find what you're looking for? Ask the community or share your knowledge.