Save a specific sheet as dxf

Save a specific sheet as dxf

AMN3161
Advocate Advocate
3,337 Views
9 Replies
Message 1 of 10

Save a specific sheet as dxf

AMN3161
Advocate
Advocate

hello,

 

Is there a easy way to have ilogic only export a sheet of a multisheet idw into a dxf and to ignore others?

 

I tried this but it ignores the fact its excluded from count and print

ThisDoc.Document.Sheets.Item("Sheet:2").ExcludeFromPrinting = True
ThisDoc.Document.Sheets.Item("Sheet:2").ExcludeFromCount = True

file_path = ThisDoc.Path
file_name = ThisDoc.FileName(False) 'without extension
file_name_path = file_path & "\" & file_name

ThisDoc.Document.SaveAs(file_name_path & ".[dxf", True)

0 Likes
Accepted solutions (1)
3,338 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

You should be able to use the sheet's DataIO property to save it off.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

WCrihfield
Mentor
Mentor

My mistake. It appears you can only use that to save a drawing sheet to a DWF.

Here's my VBA code used to investigate:

Sub GetOutputFormats()
    Dim oDDoc As DrawingDocument
    Set oDDoc = ThisApplication.ActiveDocument
    Dim oSheet As Inventor.Sheet
    Set oSheet = oDDoc.ActiveSheet
    Dim oFormats() As String
    Dim oStorageTypes() As StorageTypeEnum
    Dim oDataIO As DataIO
    Set oDataIO = oSheet.DataIO
    Call oDataIO.GetOutputFormats(oFormats, oStorageTypes)
    For Each oFormat In oFormats
        MsgBox (oFormat)
    Next
    For Each oStorageType In oStorageTypes
        MsgBox (oStorageType)
    Next
End Sub

And here are the links to more info on the subject:

Sheet.DataIO Property 

DataIO 

StorageTypeEnum Enumerator 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

AMN3161
Advocate
Advocate

I am a little confused, i am very novice to ilogic. Does that sub routine not work with dxfs?

0 Likes
Message 5 of 10

WCrihfield
Mentor
Mentor

The code I posted was using VBA code, which is used within the VBA Editor, not within iLogic Rule Editor.

For some reason, it wasn't working when I tried to do it using iLogic.

So, basically there are many Inventor objects that have a ReadOnly DataIO property.  That property can be used to read and/or write data from/to a file or iStream.  But it only supports certain file formats for certain objects.

Before investigating further, I assumed you could use the sheet's DataIO property to save the active sheet out to its own DXF file, but I was wrong.  The sheet's DataIO property only allows you to save the sheet to a DWF file format, and not to DXF.  The code I posted was the code I used to investigate which file formats the sheet's DataIO property would save out to.  If the MsgBox() would have showed DXF, it would have been possible, but it only showed DWF.

 

So, what you're most likely going to have to do to save just one sheet off as its own DXF file, is to access the DXF Translator Add-in by code, and use it to save that sheet out to a file.  I have exported multi-sheet IDW drawings out to multi-page PDF's and out so that each sheet is its own PDF file.  I'm guessing exporting a single page of an idw drwing to a DXF file can be done the same way.  Unfortunately, this is a much longer and more complex code, which I don't have ready to post just yet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 10

AMN3161
Advocate
Advocate

I found a much easier/dumber way to do it for my purposes

 

instead of two sheets, ill have one sheet with 2 views on it. 

ActiveSheet.View("DXF_View_Sheet1").View.Suppressed = True

'pdfname = ThisDoc.FileName(False)
'filePath = ThisDoc.Path
'Dim doc = ThisDoc.Document 
'doc.SaveAs(filePath + "\" + pdfname + ".pdf", True)

file_path = ThisDoc.Path
file_name = ThisDoc.FileName(False) 'without extension
file_name_path = file_path & "\" & file_name

ThisDoc.Document.SaveAs(file_name_path & ".dxf", True)

ActiveSheet.View("DXF_View_Sheet1").View.Suppressed = False
0 Likes
Message 7 of 10

NachoShaw
Advisor
Advisor
Accepted solution

I was thinking the same

 

copy the sheet you want as dxf to a new temp doc

dxf the temp doc sheet from there

close the temp doc sheet without saving

 

i put this quick & dirty sample together to give you an idea. This example will -

  • copy the active sheet to a temp hidden document
  • delete any sheets in hidden doc that are not named as the active sheet (unless you sheet is called 'Sheet')
  • dxf the remaining sheet (unzipped)

 

you will need to do the following in order for it to work

establish a full path name for the file output in 'SingleDXF'. Mine was on the desktop

VBA
'create a name for the dxf to include the full path
Dim CreateSaveName As String
CreateSaveName = "C:\Users\Nigel Shaw\Desktop\TestDXF"

 

set your DXF ini file path in 'PublishDXF'. mine was in the 2021 DWG-DXF folder (also included as a zip)

VBA
' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
   Dim strIniFile As String
   strIniFile = "C:\Users\Public\Documents\Autodesk\Inventor 2021\Design Data\DWG-DXF\exportdxfNoZip.ini"

   ' Create the name-value that specifies the ini file to use.
   oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

 

Here is the full code in VBA.

Public Sub SingleDXF()

    Dim CopyFromDoc As DrawingDocument
    Dim CopyToDoc As DrawingDocument
    Dim oSheet1 As Sheet
    Dim oSheet2 As Sheet
    
    Dim CopiedSheetName() As String
      
    'the sheet to copy
    Set CopyFromDoc = ThisApplication.ActiveDocument
    
    'add a new drawing silent to copy the sheet to
    'change false to true if you want to see the temp doc during the process
    Set CopyToDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, , False)
    
    'focus on the sheet to copy
    Set oSheet1 = CopyFromDoc.ActiveSheet
    
    'get the name and remove everything from :
    CopiedSheetName() = Split(oSheet1.Name, ":")
     
    'copy the sheet to the new silent drawing
    oSheet1.CopyTo CopyToDoc
    
    'focus on the new drawing that you have just copied to
    Set oSheet2 = CopyToDoc.Sheets.Item(CopyToDoc.Sheets.Count)
    oSheet2.Activate
     
    Dim oSheets As Sheets
    Set oSheets = CopyToDoc.Sheets

    'delete any other sheets so that you only have 1 sheet to dxf
    Dim oSheet As Sheet
    For Each oSheet In oSheets
    Dim GetCurrSheetName() As String
        GetCurrSheetName() = Split(oSheet.Name, ":")
        If Not GetCurrSheetName(0) = CopiedSheetName(0) Then
            oSheet.Delete
        End If
    Next

    'create a name for the dxf to include the full path
    Dim CreateSaveName As String
    CreateSaveName = "C:\Users\<UserName>\Desktop\TestDXF"
    
    Call PublishDXF(CopyToDoc, CreateSaveName)
    
    'close the temp drawing
    CopyToDoc.Close True
    
    MsgBox ("DXF exported!")

End Sub

Public Sub PublishDXF(oDocument As Document, SaveName As String)
    ' Get the DXF translator Add-In.
    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

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

    ' Create a DataMedium object
    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:\Users\Public\Documents\Autodesk\Inventor 2021\Design Data\DWG-DXF\exportdxfNoZip.ini"

        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If

    'Set the destination file name
    oDataMedium.FileName = SaveName & ".dxf"

    'Publish document.
    Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

 

Hope that helps

 

 

Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 8 of 10

AMN3161
Advocate
Advocate

appreciate the help but i your code is well outside of my knowledge right now. I will use my hide view, export sheet, re-hide because I can manipulate that without breaking it. But ill accept yours as a solution for anyone else

 

thank you!

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor

OK. Here's the iLogic version.  This works pretty much the same way as @NachoShaw 's code does, but is in iLogic (vb.net) instead of VBA.  The only known option we can deal with for the DXF Translator AddIn is the option to specify a specific 'ini' file to use when exporting the DXF.  This is where your previous export settings were likely saved to.  If you haven't altered this file's name or location, it should work.  Although, you may still need to customize the path to match your computer system and version of Inventor.  The default installed path for my computer is shown within the code.  I'm using Inventor 2021 though, so you may need to change that.

 

The code captures the currently active drawing document and active sheet, then creates a new drawing document (silently, in the background), and copies your active sheet to that new drawing file, then deletes all other sheets from that new drawing file.

Then it proceeds to get the DXF Translator AddIn, and create the necessary variables to use its SaveCopyAs methods, then sets the one option mentioned before.

Then it checks to see if there is already an existing DXF file by that name.  If there is, it pauses to let you know and ask if you want to overwrite that existing file or not.

If it didn't find an existing file, or if you said yes to the question, it then exports the new copied drawing file as a DXF with the same path and name as the original drawing file, but with the DXF file extension.

Then it closes the new temporary drawing file, without saving it (all in the background).

Here's the code.

'Make sure the active document is a drawing document.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("This Rule " & iLogicVb.RuleName & " only works on Drawing Documents. Exiting.",vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
	Return
End If

'Capture the active drawing document, and its active sheet. (make sure the active sheet is the one you want to copy.)
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Sheet = oDDoc.ActiveSheet

'Create the temporary drawing file, copy the sheet, then delete all sheets in new drawing.
Dim oTempDDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,,False)
Dim oTempSheet As Sheet = oSheet.CopyTo(oTempDDoc)
For Each oTSheet As Sheet In oTempDDoc.Sheets
	If oTSheet IsNot oTempSheet Then
		oTSheet.Delete
	End If
Next

'Get the DXF Translator AddIn
Dim oDXF_AddIn As TranslatorAddIn
For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
	If oAddIn.DisplayName = "Translator: PDF" Then
		oDXF_AddIn = oAddIn
	End If
Next

Dim oTO As TransientObjects = ThisApplication.TransientObjects
Dim oContext As TranslationContext = oTO.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = oTO.CreateNameValueMap
Dim oDataMedium As DataMedium = oTO.CreateDataMedium

Dim oPath As String = IO.Path.GetDirectoryName(oDDoc.FullFileName)
Dim oFName As String = IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
oDataMedium.FileName = oPath & "\" & oFName & ".dxf"

If oDXF_AddIn.HasSaveCopyAsOptions(oTempDDoc, oContext, oOptions) Then
	'If your export DXF settings are saved somewhere else or named differently, you will have to change this next line.
	oOptions.Value("Export_Acad_IniFile") = "C:\Users\Public\Documents\Autodesk\Inventor 2021\Design Data\DWG-DXF\exportdxf.ini"
End If

'Check to see if the PDF already exists, if it does, ask if you want to over write existing file or not.
If System.IO.File.Exists(oDataMedium.FileName) = True Then
	oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
	"Do you want to over write it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
	If oAnswer = vbNo Then 	Return
End If

'Publish DXF
oDXF_AddIn.SaveCopyAs(oTempDDoc, oContext, oOptions, oDataMedium)

oTempDDoc.Close(True)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 10

Kreshnik.HASANI
Contributor
Contributor

You can also delete all the sheets but keep the needed one. Then after export don't forget to skip save. Here the code in C# :

 // Keep only selected Sheet
                Inventor.DrawingDocument docInventor = (Inventor.DrawingDocument)InventorArchivePlugin.InventorApplication.ActiveDocument;

                foreach(Inventor.Sheet sheet in docInventor.Sheets)
                {
                    if (docInventor.ActiveSheet.Name != sheet.Name)
                    {
                        sheet.Delete();
                    }
                }

                // Export in DXF
                InventorArchivePlugin.InventorApplication.ActiveDocument.SaveAs(strDXFFile, true);
0 Likes