Exporting multi page DWG using Sheet Name for exported files

Exporting multi page DWG using Sheet Name for exported files

sam_dobell
Participant Participant
1,267 Views
7 Replies
Message 1 of 8

Exporting multi page DWG using Sheet Name for exported files

sam_dobell
Participant
Participant

My team current creates part drawings with a part drawing & cutfile on different sheets in the same drawing file.

The current drawing template currently renames the sheets based on the first part placed on the drawing, and adds the "_CutFile" suffix if the view is a flat pattern.

 

I have been using the code below to export all open drawings to PDF & DWG in their own folder which works great, but each multi page DWG gets exported with the naming convension "DrawingName_SheetName"

 

Example 

A103-6-1001-TestPart.idw exports to

A103-6-1001-TestPart_A103-6-1001-TestPart.dwg

A103-6-1001-TestPart_A103-6-1001-TestPart_Cutfile.dwg

 

I would like to remove eveything before the underscore, and just have each file export with the sheet name only.

I have an idea about how to achieve this with the Sheet.Name property but unsure how to add it into the export rule below

 

Any help would be appreciated

 

  Sub main()

        For Each document As Document In ThisApplication.Documents.VisibleDocuments
            If (Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject) Then
                Logger.Info("Skipped file: " + Document.FullFileName)
                Continue For

            End If

            Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(Document.FullFileName)
            Dim fileInfo As New System.IO.FileInfo(Document.FullFileName)
            Dim subFolder = fileInfo.Directory.CreateSubdirectory(fileName)

            Dim pdfFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".pdf")
            CreatePdf(pdfFullFileName)

            Dim dwgFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".dwg")
            CreateDwg(dwgFullFileName)

            Logger.Info("Created pdf and dwg from file: " + Document.FullFileName)
        Next
    End Sub

    Private Sub CreatePdf(fullFileName As String)
        ' Get the PDF add-in
        Dim pdfAddIn As ApplicationAddIn
        pdfAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

        ' Create the translation context
        Dim context As TranslationContext
        context = ThisApplication.TransientObjects.CreateTranslationContext
        context.Type = IOMechanismEnum.kFileBrowseIOMechanism

        ' Create the name-value map for options
        Dim options As NameValueMap
        options = ThisApplication.TransientObjects.CreateNameValueMap

        ' Set PDF export options
        options.Value("Remove_Line_Weights") = 1
        options.Value("All_Color_AS_Black") = 0
        options.Value("Vector_Resolution") = 400
        options.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

        ' Create the data medium for PDF
        Dim dataMediumPDF As DataMedium
        dataMediumPDF = ThisApplication.TransientObjects.CreateDataMedium
        dataMediumPDF.FileName = fullFileName

        ' Export the document to PDF
        If pdfAddIn.HasSaveCopyAsOptions(ThisDoc.Document, context, options) Then
            pdfAddIn.SaveCopyAs(ThisDoc.Document, context, options, dataMediumPDF)
        End If
    End Sub

    Private Sub CreateDwg(fullFileName As String)

        ' Get the DWG add-in
        Dim dwgAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

        ' Create the translation context
        Dim context As TranslationContext
        context = ThisApplication.TransientObjects.CreateTranslationContext
        context.Type = IOMechanismEnum.kFileBrowseIOMechanism

        ' Reset the options for DWG export
        Dim options = ThisApplication.TransientObjects.CreateNameValueMap

        ' Set DWG export options using an INI file
        Dim workspacePath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
        Dim strIniFile As String = System.IO.Path.Combine(workspacePath, "zInventorStuff\iLogic\DWGExport.ini")
        ' Create the name-value that specifies the ini file to use.
        options.Value("Export_Acad_IniFile") = strIniFile

        ' Create the data medium for DWG
        Dim dataMediumDWG As DataMedium
        dataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium
        dataMediumDWG.FileName = fullFileName

        ' Export the document to DWG
        If dwgAddIn.HasSaveCopyAsOptions(ThisDoc.Document, context, options) Then
            dwgAddIn.SaveCopyAs(ThisDoc.Document, context, options, dataMediumDWG)
        End If
    End Sub

 

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

WCrihfield
Mentor
Mentor

Hi @sam_dobell.  Although I do not need to export multi-sheet drawings to DXF or DWG formats where I work, I have been involved in very similar forum discussions about trying to customize the names of the individual resulting files for each sheet before, and I remember it being pretty complicated.  It seems like the simplest way to handle this situation is to predict the resulting 'FullFileNames' of the DXF/DWG files it will produce.  Then after the export steps are all finished, change the names of those existing files the way you want.  This is because we supply the whole DrawingDocument to the export method, not just one sheet at a time.  So, although we can influence the drawing name portion of it, the sheet name portion of the file names seem to be automatically generated by the translator add-in's export method.

 

Seems like I have seen folks rename their sheets just before the export process, then change them back after the export process, but that is a lot of extra steps and processing, and often not resulting in satisfactory results. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

jnowel
Collaborator
Collaborator

my workaround here is to send each sheet for export but it requires a different ini file where:

 

jnowel_0-1728481176862.png

 

 

All Sheets=No

 


Edited Main Subroutine

 

 

 Sub Main()

        For Each document As Document In ThisApplication.Documents.VisibleDocuments
            If (Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject) Then
                Logger.Info("Skipped file: " + Document.FullFileName)
                Continue For
            End If
			
			'with the DWG Export Config ini file, you need to activate the DrawingDocument for export
			Document.Activate

            Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(Document.FullFileName)
            Dim fileInfo As New System.IO.FileInfo(Document.FullFileName)
            Dim subFolder = fileInfo.Directory.CreateSubdirectory(fileName)

            Dim pdfFullFileName = System.IO.Path.Combine(subFolder.FullName, fileName + ".pdf")
            CreatePdf(pdfFullFileName)
			
			'And run the DWG export rule per sheet
			For Each oSheet As Sheet In Document.Sheets
				oSheet.Activate
	            Dim dwgFullFileName As String = Trim(Split(oSheet.Name, ":")(0)) &  ".dwg"
	            CreateDwg(dwgFullFileName)
			Next oSheet
			
            Logger.Info("Created pdf and dwg from file: " + Document.FullFileName)
        Next
    End Sub

 


in  video attached, i have customized the dwgFullFileName to other details like revision and sheet number.
(not shown in above code, but can easily be configured as such)

edit: not sure why the uploaded video was so low-resolution.


(view in My Videos)

Message 4 of 8

WCrihfield
Mentor
Mentor

That's certainly one way of doing it.  It's been a while since I last looked into this process, since I do not use it myself.  Seems to me like another strategy was...while iterating through each sheet, set the Sheet.ExcludeFromCount and the Sheet.ExcludeFromPrinting properties to False (include) for the one sheet you want to export, while setting all other sheets to True (exclude), then do the export within the loop, then on to the next sheet, and so on.  Just another way of exporting a single sheet at a time.  I do not recall if that let you have total control of each file name, but it seems like it should.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

jnowel
Collaborator
Collaborator

Changing Sheet.ExcludeFromCount midrule would mess up the drawing if "Total Number of Sheets" is included in the Drawing titleblock.

Message 6 of 8

WCrihfield
Mentor
Mentor

Good point.  That was one of the drawbacks, and complications of doing things that way in previous discussions, and one of the reasons I first suggested just changing the file names of the resulting files by code, after they get generated.  Just seems simpler to me, but I do tend to think about processes like these a bit differently than most, also.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

sam_dobell
Participant
Participant
Accepted solution

I was able to get this to work by adding a rename fix after the drawing is exported. Not a perfect solution but it seems to work for my usecase

 

Below is the updated CreateDWG subroutine

 

    Private Sub CreateDwg(document As Document, fullFileName As String)

    Dim oDoc As DrawingDocument = document
    Dim oPath As String = System.IO.Path.GetDirectoryName(fullFileName)
    Dim baseName As String = System.IO.Path.GetFileNameWithoutExtension(document.FullFileName)

    ' Get the DWG add-in
    Dim dwgAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

    ' Create the translation context
    Dim context As TranslationContext
    context = ThisApplication.TransientObjects.CreateTranslationContext
    context.Type = IOMechanismEnum.kFileBrowseIOMechanism

    ' Options
    Dim options = ThisApplication.TransientObjects.CreateNameValueMap

    ' INI file
    Dim workspacePath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim strIniFile As String = System.IO.Path.Combine(workspacePath, "zInventorStuff\iLogic\DWGExport.ini")
    options.Value("Export_Acad_IniFile") = strIniFile

    ' Data medium
    Dim dataMediumDWG As DataMedium
    dataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium
    dataMediumDWG.FileName = fullFileName

    ' --- EXPORT ---
    If dwgAddIn.HasSaveCopyAsOptions(document, context, options) Then
        dwgAddIn.SaveCopyAs(document, context, options, dataMediumDWG)
    End If

    ' --- RENAME FIX ---
    Dim oFiles() As String = System.IO.Directory.GetFiles(oPath, baseName & "_*.dwg")

    For Each file As String In oFiles

        Dim fileNameOnly As String = System.IO.Path.GetFileNameWithoutExtension(File)
        Dim newName As String = fileNameOnly

        ' Count occurrences of base name
        Dim occurrences As Integer = (fileNameOnly.Length - fileNameOnly.Replace(baseName, "").Length) / baseName.Length

        ' Only fix duplicated names
        If occurrences >= 2 Then
            newName = fileNameOnly.Substring(baseName.Length + 1)
        End If

        Dim newFullPath As String = System.IO.Path.Combine(oPath, newName & ".dwg")

        If System.IO.File.Exists(newFullPath) Then
            System.IO.File.Delete(newFullPath)
        End If

        System.IO.File.Move(File, newFullPath)

    Next

End Sub

 

 

 

Message 8 of 8

WCrihfield
Mentor
Mentor

hi @sam_dobell.  Glad to see that you now have a solution that works for you.  And thanks for posting it back into the forum, and marking it as a solution, so others can find and benefit from it.  Looks like you ended up taking my original advise about renaming the resulting files after the export process.

I would like to suggest one final tip or piece of advice though.  I think you should change the spelling of the one variable in your solution code from "file" to something like "sFile" or "oFile", because without renaming it, it can get mistakenly identified as representing something else, such as the Inventor.File object, or the System.IO.File Class.  As you can already see, you originally spelled it with a lowercase 'f', but the following instances of that variable automatically switched to an uppercase 'F', because of that Type 'recognition'.  Changing how it is spelled will help eliminate that instability.  In general, we should never spell our variables the same as a Type name or Keyword.  That's one of the reasons why adding a lowercase letter to the front, or leaving out vowels, and similar tactics are so popular.  Simply using lowercase letter at the start of the variable name, when it is still spelled the same as a Type or Keyword, is not a good practice, even if it may work sometimes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)