Export to DWG with filename based on sheetname

Export to DWG with filename based on sheetname

Anonymous
Not applicable
991 Views
2 Replies
Message 1 of 3

Export to DWG with filename based on sheetname

Anonymous
Not applicable

Goal: Export to dwg with a custom filename based on sheetname.

 

Problem: This iLogic code (cobbed together from examples on the web) is creating multiple DWG files and adding the sheet name at the end. I already have the sheetname within the filename, so I don't want Inventor to add it again at the end. I use a similiar code for PDFs and it works great.

 

 

SyntaxEditor Code Snippet

' Get the DWG translator Add-In.
Dim DWGAddIn As TranslatorAddIn
DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Autodesk 2018\Configuration 2018\DWGconfiguration2018.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If


oPath = ThisDoc.Path
oFolder = oPath & "\DWG"

'Check for the DWG folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If

'Define the drawing 
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document

Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer

'step through each drawing sheet
For Each oSheet In oDrawing.Sheets

'find the seperator in the sheet name:number
lPos = InStr(oSheet.Name, ":")
'find the number of characters in the sheet name
sLen = Len(oSheet.Name)
'find the sheet name
sSheetName = Left(oSheet.Name, lPos -1)
'find the sheet number
sSheetNumber = Right(oSheet.Name, sLen -lPos)

Dim oDesc1 As String
Dim oDesc2 As String
Dim oDesc3 As String
Dim oDescription As String
oDesc1 = iProperties.Value("Custom", "DrawingTitle_Line1") 
oDesc2 = iProperties.Value("Custom", "DrawingTitle_Line2") 
oDesc3 = iProperties.Value("Custom", "DrawingTitle_Line3") 

If (oDesc1 = Nothing And oDesc2 = Nothing And oDesc3 = Nothing) Then
oDescription = ""
Else If (oDesc1 <> Nothing And oDesc2 = Nothing And oDesc3 = Nothing) Then
oDescription = " " & oDesc1
Else If (oDesc1 = Nothing And oDesc2 <> Nothing And oDesc3 = Nothing) Then
oDescription = " " & oDesc2
Else If (oDesc1 = Nothing And oDesc2 = Nothing And oDesc3 <> Nothing) Then
oDescription = " " & oDesc3
Else If (oDesc1 <> Nothing And oDesc2 <> Nothing And oDesc3 = Nothing) Then
oDescription = " " & oDesc1 & " " & oDesc2
Else If (oDesc1 = Nothing And oDesc2 <> Nothing And oDesc3 <> Nothing) Then
oDescription = " " & oDesc2 & " " & oDesc3
Else If (oDesc1 <> Nothing And oDesc2 <> Nothing And oDesc3 <> Nothing) Then
oDescription = " " & oDesc1 & " " & oDesc2 & " " & oDesc3
End If

If sSheetName = "-" Then
oFileName = iProperties.Value("Custom", "DrawingNumber") & "-R" & iProperties.Value("Custom", "Revision")
Else
oFileName = iProperties.Value("Custom", "DrawingNumber") & "-" & sSheetName & "-R" & iProperties.Value("Custom", "Revision")
End If

'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & oDescription &  ".dwg"

'Publish document.
Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

Next
'------end of iLogic-------

 

0 Likes
Accepted solutions (1)
992 Views
2 Replies
Replies (2)
Message 2 of 3

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

 

By default, DWGAddin adds sheet name to file name while exporting to DWG. This is to avoid overriding file names for different sheets.

 

In order to avoid sheet name in filename, there are two changes needed.

 

  1. Adding the line "ALL SHEETS=No" in ".ini" file (attached with this post). For more details, refer the forum discussion link(https://forums.autodesk.com/t5/inventor-forum/ilogic-dwg-saving-only-active-sheet/td-p/6293859).
  2. For every drawing sheet, activate the drawing sheets. Refer the modified iLogic code below.

 

' Get the DWG translator Add-In.
Dim DWGAddIn As TranslatorAddIn
DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Autodesk 2018\Configuration 2018\DWGconfiguration2018.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If


oPath = ThisDoc.Path
oFolder = oPath & "\DWG"

'Check for the DWG folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If

'Define the drawing 
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document

Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer

'step through each drawing sheet
For Each oSheet In oDrawing.Sheets

Call oSheet.Activate() 'find the seperator in the sheet name:number lPos = InStr(oSheet.Name, ":") 'find the number of characters in the sheet name sLen = Len(oSheet.Name) 'find the sheet name sSheetName = Left(oSheet.Name, lPos -1) 'find the sheet number sSheetNumber = Right(oSheet.Name, sLen -lPos) Dim oDesc1 As String Dim oDesc2 As String Dim oDesc3 As String Dim oDescription As String oDesc1 = iProperties.Value("Custom", "DrawingTitle_Line1") oDesc2 = iProperties.Value("Custom", "DrawingTitle_Line2") oDesc3 = iProperties.Value("Custom", "DrawingTitle_Line3") If (oDesc1 = Nothing And oDesc2 = Nothing And oDesc3 = Nothing) Then oDescription = "" Else If (oDesc1 <> Nothing And oDesc2 = Nothing And oDesc3 = Nothing) Then oDescription = " " & oDesc1 Else If (oDesc1 = Nothing And oDesc2 <> Nothing And oDesc3 = Nothing) Then oDescription = " " & oDesc2 Else If (oDesc1 = Nothing And oDesc2 = Nothing And oDesc3 <> Nothing) Then oDescription = " " & oDesc3 Else If (oDesc1 <> Nothing And oDesc2 <> Nothing And oDesc3 = Nothing) Then oDescription = " " & oDesc1 & " " & oDesc2 Else If (oDesc1 = Nothing And oDesc2 <> Nothing And oDesc3 <> Nothing) Then oDescription = " " & oDesc2 & " " & oDesc3 Else If (oDesc1 <> Nothing And oDesc2 <> Nothing And oDesc3 <> Nothing) Then oDescription = " " & oDesc1 & " " & oDesc2 & " " & oDesc3 End If If sSheetName = "-" Then oFileName = iProperties.Value("Custom", "DrawingNumber") & "-R" & iProperties.Value("Custom", "Revision") Else oFileName = iProperties.Value("Custom", "DrawingNumber") & "-" & sSheetName & "-R" & iProperties.Value("Custom", "Revision") End If 'Set the PDF target file name oDataMedium.FileName = oFolder & "\" & oFileName & oDescription & ".dwg" 'Publish document. Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) Next '------end of iLogic-------

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 3

Anonymous
Not applicable

Thanks for the quick reply Chandra!

Everything works great.

0 Likes