Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Saving a DXF with a SUFFIX

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
dleesuk
718 Views, 7 Replies

Saving a DXF with a SUFFIX

dleesuk
Advocate
Advocate

Hi all,

 

I have a dilema  😊

 

I have a DWG file with multiple sheets, each with a single part view on it.

 

I am then required to save a copy as a DXF file.  This saves the separate sheets as 'FILENAME_Sheet_Name.dxf' if left untouched.

 

To get over this default naming configuration, I wrote an iLogic file to rename the Sheet Name as the name of the part file.  I then save the DXFs with just the name 'DXF'.  This saves the separate DXF files as 'DXF_Part_File_Name.dxf'

 

However, I now need to have the 'DXF' in the file name as a SUFFIX and not a PREFIX, i.e.

 

'Part_File_Name_DXF.dxf' instead of 'DXF_Part_File_Name.dxf'


Obviously, I can't do this with the defaut naming convention (that I'm aware of).

 

Does anyone have a routine or know how to save the separate DWG sheets as DXFs with the required naming convention; 'Part_File_Name_DXF.dxf'??  My knowledge of iLogic is very limited.

 

 

Thanks and kudos to anyone can help!!

 

Regards

 

Darren


Regards

Darren
0 Likes

Saving a DXF with a SUFFIX

Hi all,

 

I have a dilema  😊

 

I have a DWG file with multiple sheets, each with a single part view on it.

 

I am then required to save a copy as a DXF file.  This saves the separate sheets as 'FILENAME_Sheet_Name.dxf' if left untouched.

 

To get over this default naming configuration, I wrote an iLogic file to rename the Sheet Name as the name of the part file.  I then save the DXFs with just the name 'DXF'.  This saves the separate DXF files as 'DXF_Part_File_Name.dxf'

 

However, I now need to have the 'DXF' in the file name as a SUFFIX and not a PREFIX, i.e.

 

'Part_File_Name_DXF.dxf' instead of 'DXF_Part_File_Name.dxf'


Obviously, I can't do this with the defaut naming convention (that I'm aware of).

 

Does anyone have a routine or know how to save the separate DWG sheets as DXFs with the required naming convention; 'Part_File_Name_DXF.dxf'??  My knowledge of iLogic is very limited.

 

 

Thanks and kudos to anyone can help!!

 

Regards

 

Darren


Regards

Darren
Tags (2)
Labels (2)
7 REPLIES 7
Message 2 of 8
WCrihfield
in reply to: dleesuk

WCrihfield
Mentor
Mentor

Hi @dleesuk.  I can think of two alternative processes right now.  The first one would be on the back end of the existing process, and you could simply use some additional code to rename DXF file itself after you have exported it.  The other process would be to copy each sheet to another temporary drawing document, and make sure there are no other sheets in that temporary drawing, then export that drawing to DXF with the naming the way you want it (the drawing file would be named as the part, and the sheet would be named DXF).  That process obviously would require a lot more complication and processing, but seems doable.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @dleesuk.  I can think of two alternative processes right now.  The first one would be on the back end of the existing process, and you could simply use some additional code to rename DXF file itself after you have exported it.  The other process would be to copy each sheet to another temporary drawing document, and make sure there are no other sheets in that temporary drawing, then export that drawing to DXF with the naming the way you want it (the drawing file would be named as the part, and the sheet would be named DXF).  That process obviously would require a lot more complication and processing, but seems doable.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8
romu51
in reply to: dleesuk

romu51
Advocate
Advocate

Hi, 

here is an extract from a rule i have to save as dxf and pdf. 

it could be adapted to remove the prompted question if your suffix is always the same.

 

 Dim addSuffix As Integer = MsgBox("Do you want to add a suffix to the file name?", vbYesNo + vbQuestion, "Add Suffix")
    If addSuffix = vbYes Then
        suffix = InputBox("Enter Issue number")
    Else
        suffix = "" ' Empty string if suffix is not needed
    End If
    
    For Each doc As Document In ThisApplication.Documents
        If doc.DocumentType = kDrawingDocumentObject And doc.FullFileName = ThisApplication.ActiveDocument.FullFileName Then
            file_path = System.IO.Path.GetDirectoryName(doc.FullFileName)
            file_name = System.IO.Path.GetFileNameWithoutExtension(doc.FullFileName) 'without extension
            file_name_path = file_path & "\" & file_name & If(suffix <> "", "-Iss" & suffix, "")
            If System.IO.File.Exists(file_name_path & ".dxf") Or System.IO.File.Exists(file_name_path & ".pdf") Then
                Dim result2 As Integer = MsgBox("File already exists. Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")
                If result2 = vbYes Then
                    doc.SaveAs(file_name_path & ".dxf", True)
                    doc.SaveAs(file_name_path & ".pdf", True)
                End If
            Else
                doc.SaveAs(file_name_path & ".dxf", True)
                doc.SaveAs(file_name_path & ".pdf", True)
            End If
        End If
0 Likes

Hi, 

here is an extract from a rule i have to save as dxf and pdf. 

it could be adapted to remove the prompted question if your suffix is always the same.

 

 Dim addSuffix As Integer = MsgBox("Do you want to add a suffix to the file name?", vbYesNo + vbQuestion, "Add Suffix")
    If addSuffix = vbYes Then
        suffix = InputBox("Enter Issue number")
    Else
        suffix = "" ' Empty string if suffix is not needed
    End If
    
    For Each doc As Document In ThisApplication.Documents
        If doc.DocumentType = kDrawingDocumentObject And doc.FullFileName = ThisApplication.ActiveDocument.FullFileName Then
            file_path = System.IO.Path.GetDirectoryName(doc.FullFileName)
            file_name = System.IO.Path.GetFileNameWithoutExtension(doc.FullFileName) 'without extension
            file_name_path = file_path & "\" & file_name & If(suffix <> "", "-Iss" & suffix, "")
            If System.IO.File.Exists(file_name_path & ".dxf") Or System.IO.File.Exists(file_name_path & ".pdf") Then
                Dim result2 As Integer = MsgBox("File already exists. Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")
                If result2 = vbYes Then
                    doc.SaveAs(file_name_path & ".dxf", True)
                    doc.SaveAs(file_name_path & ".pdf", True)
                End If
            Else
                doc.SaveAs(file_name_path & ".dxf", True)
                doc.SaveAs(file_name_path & ".pdf", True)
            End If
        End If
Message 4 of 8
dleesuk
in reply to: dleesuk

dleesuk
Advocate
Advocate

@WCrihfield,

Thank you for your reply, but I need this as a workstream for others to use so this method would be inappropriate. 

 

Hi @romu51,

Thank you for your reply.  This seems to be VERY close to what I require, but I do not want the FileName of the dwg file included in the save name of the DXF files.

 

I have a routine below which renames the 'sheets' to that of the part on each sheet plus the item number (custom property).

oDoc = ThisDoc.Document

'Find all selected occurences and add them to an ObjectCollection
Dim oDrawingDims As DrawingDimension

oSheets = oDoc.Sheets

Try
    For Each oSheet In oSheets
		oSheet.activate
		oView = oSheet.DrawingViews.Item(1) 
		
		'Loop through all the dimensions
		For Each oDrawingDims In oDoc.ActiveSheet.DrawingDimensions
			'set to reference dims
			oDrawingDims.Tolerance.SetToReference
		Next
		
	        modelName = oView.ReferencedDocumentDescriptor.ReferencedDocument
			
			oPropPartNo = modelName.PropertySets.Item("Design Tracking Properties")
	        oPropItemNo = modelName.PropertySets.Item("Inventor User Defined Properties")
			
	        ActiveSheet.Sheet.Name = oPropPartNo.Item("Part Number").Value + " (Item " + oPropItemNo.Item("ItemNo").Value + ")"
		
    Next
Catch
	MessageBox.Show("One or more of the sheets has no part view associated with it.", "No Views!!")

End Try
oSheets(1).activate

This, using our naming convention, renames the sheets similar to: 'OPPN-72-275_ai (Item 7)'

dleesuk_0-1690529498042.png

The names are different for each sheet/part for however many sheets/parts are within the DWG file.

 

Upon saving the DXFs out, I'd really like the filename to be: 'OPPN-72-275_ai (Item 7)_DXF.dxf', disregarding the actual filename of the DWG file.

 

I've tried amalgamating the two routines, but continually get error messages. Again, my understanding of iLogic is limited (the above routine was created over MANY weeks of research and trial & error.  😁).

 

Thanks in advance


Regards

Darren
0 Likes

@WCrihfield,

Thank you for your reply, but I need this as a workstream for others to use so this method would be inappropriate. 

 

Hi @romu51,

Thank you for your reply.  This seems to be VERY close to what I require, but I do not want the FileName of the dwg file included in the save name of the DXF files.

 

I have a routine below which renames the 'sheets' to that of the part on each sheet plus the item number (custom property).

oDoc = ThisDoc.Document

'Find all selected occurences and add them to an ObjectCollection
Dim oDrawingDims As DrawingDimension

oSheets = oDoc.Sheets

Try
    For Each oSheet In oSheets
		oSheet.activate
		oView = oSheet.DrawingViews.Item(1) 
		
		'Loop through all the dimensions
		For Each oDrawingDims In oDoc.ActiveSheet.DrawingDimensions
			'set to reference dims
			oDrawingDims.Tolerance.SetToReference
		Next
		
	        modelName = oView.ReferencedDocumentDescriptor.ReferencedDocument
			
			oPropPartNo = modelName.PropertySets.Item("Design Tracking Properties")
	        oPropItemNo = modelName.PropertySets.Item("Inventor User Defined Properties")
			
	        ActiveSheet.Sheet.Name = oPropPartNo.Item("Part Number").Value + " (Item " + oPropItemNo.Item("ItemNo").Value + ")"
		
    Next
Catch
	MessageBox.Show("One or more of the sheets has no part view associated with it.", "No Views!!")

End Try
oSheets(1).activate

This, using our naming convention, renames the sheets similar to: 'OPPN-72-275_ai (Item 7)'

dleesuk_0-1690529498042.png

The names are different for each sheet/part for however many sheets/parts are within the DWG file.

 

Upon saving the DXFs out, I'd really like the filename to be: 'OPPN-72-275_ai (Item 7)_DXF.dxf', disregarding the actual filename of the DWG file.

 

I've tried amalgamating the two routines, but continually get error messages. Again, my understanding of iLogic is limited (the above routine was created over MANY weeks of research and trial & error.  😁).

 

Thanks in advance


Regards

Darren
Message 5 of 8
darren.lees
in reply to: dleesuk

darren.lees
Contributor
Contributor

So, I've changed tack a little in my pursuit of a SUFFIX.

I have found this code (below) which saves out the active sheet of the drawing out as a DXF file with the correct file name.  YAY!!

 

'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
oSheets = oDDoc.Sheets
Dim oSheet As Sheet = oDDoc.ActiveSheet

'For Each oSheet In oSheets
'	oSheet.Activate



'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: DXF" 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(ActiveSheet.Sheet.Name)

MessageBox.Show("Sheet Name - " & oFName, "Sheet Name!!")

oDataMedium.FileName = oPath & "\" & oFName & "_DXF.dxf"
MessageBox.Show("FILENAME - " & oFName, "Filename!!")

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\Darren Lees\Desktop\DXF TEST\DXG-DXF\2010 DXF File.ini"
End If

'Check to see if the DXF 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 DXF file with this name already exists." & vbCrLf & "Do you want to over write it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "DXF ALREADY EXISTS")
	If oAnswer = vbNo Then Return
End If

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

oTempDDoc.Close(True)

 

However, this only acts on the currently active sheet.  This means I need to change to the next sheet and run the code again to get the next sheet, etc.  We occasionally have drawings with 40-50+ sheets and this would get quite tedious and time consuming to continually run the rule for every sheet.

 

Therefore, I enclosde the relevant code inside a for-next loop in an attempt to iterate through the sheets and have it saved with the relevant title.


It didn't end well!!

 

I had numerous "variable 'example' hides a variable in an enclosing block"  when I save/run the code. 

 

I have a little idea about 'variable sccope', but absolutely not enough to get around this.

 

I don't know which way to turn, but being so close I'm hoping someone can help??.....please!!

 

I can supply files if required, but in this case I don't believe it'd help as I'm only focusing on the SHEET NAME for my DXF file title. Therefore, the sheet name can be changed to whatever is required.

0 Likes

So, I've changed tack a little in my pursuit of a SUFFIX.

I have found this code (below) which saves out the active sheet of the drawing out as a DXF file with the correct file name.  YAY!!

 

'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
oSheets = oDDoc.Sheets
Dim oSheet As Sheet = oDDoc.ActiveSheet

'For Each oSheet In oSheets
'	oSheet.Activate



'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: DXF" 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(ActiveSheet.Sheet.Name)

MessageBox.Show("Sheet Name - " & oFName, "Sheet Name!!")

oDataMedium.FileName = oPath & "\" & oFName & "_DXF.dxf"
MessageBox.Show("FILENAME - " & oFName, "Filename!!")

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\Darren Lees\Desktop\DXF TEST\DXG-DXF\2010 DXF File.ini"
End If

'Check to see if the DXF 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 DXF file with this name already exists." & vbCrLf & "Do you want to over write it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "DXF ALREADY EXISTS")
	If oAnswer = vbNo Then Return
End If

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

oTempDDoc.Close(True)

 

However, this only acts on the currently active sheet.  This means I need to change to the next sheet and run the code again to get the next sheet, etc.  We occasionally have drawings with 40-50+ sheets and this would get quite tedious and time consuming to continually run the rule for every sheet.

 

Therefore, I enclosde the relevant code inside a for-next loop in an attempt to iterate through the sheets and have it saved with the relevant title.


It didn't end well!!

 

I had numerous "variable 'example' hides a variable in an enclosing block"  when I save/run the code. 

 

I have a little idea about 'variable sccope', but absolutely not enough to get around this.

 

I don't know which way to turn, but being so close I'm hoping someone can help??.....please!!

 

I can supply files if required, but in this case I don't believe it'd help as I'm only focusing on the SHEET NAME for my DXF file title. Therefore, the sheet name can be changed to whatever is required.

Message 6 of 8
dleesuk
in reply to: dleesuk

dleesuk
Advocate
Advocate

FYI, darren.lees is me, dleesuk.  It's just my work account!!  😀


Regards

Darren
0 Likes

FYI, darren.lees is me, dleesuk.  It's just my work account!!  😀


Regards

Darren
Message 7 of 8
WCrihfield
in reply to: dleesuk

WCrihfield
Mentor
Mentor
Accepted solution

Hi @dleesuk.  I have this a shot for you.  The main thing I did here is separate the export process out to a custom Sub routine, while trying to enhance the main code a bit.  I saw something odd in there where you were trying to get a file name without extension from a sheet name, which seemed a bit odd to me, so I figured a way to work around that sheet/view model name part of the routine.  I get the first view on the active sheet, then get the model document of that view, then get its file name, without path or extension.  Then I combine the path of the main drawing with that model file name, and the "_DXF.dxf" bit at the end, to get the full file name of the DXF file we want to create.  Then I do the new temp drawing, copy sheet, delete other sheets bit.  Then I call the custom Sub routine to run.  Then close the temp drawing, just before it loops to the next sheet.  However, I'm guessing that you may be able to create the temporary drawing document before the loop of the sheets, just one time, then keep reusing it.  Then inside the loop, copy the active sheet to it, and delete all others, just like you are doing now.  Then close it after the loop of sheets is done.  Just a thought.

Sub Main
	'Make sure the active document is a drawing document.
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule " & iLogicVb.RuleName & " only works on Drawing Documents. Exiting.", vbCritical, "WRONG DOCUMENT TYPE")
		Return
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim sPath As String = IO.Path.GetDirectoryName(oDDoc.FullFileName)
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	For Each oSheet As Inventor.Sheet In oSheets
		oSheet.Activate
		Dim oView As DrawingView = Nothing
		Try : oView = oSheet.DrawingViews.Item(1) : Catch : End Try
		If oView Is Nothing Then Continue For
		Dim oModelDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
		Dim sModelFileName As String = System.IO.Path.GetFileNameWithoutExtension(oModelDoc.FullFileName)
		Dim sDXF_FullFuleName As String = sPath & "\" & sModelFileName & "_DXF.dxf"
		Logger.Info("Proposed DXF FullFileName = " & sDXF_FullFuleName)
		'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 Inventor.Sheet In oTempDDoc.Sheets
			If oTSheet IsNot oTempSheet Then oTSheet.Delete
		Next 'oTSheet
		'call the custom Sub to run, to export the DXF
		ExportDrawingToDXF(oTempDDoc, sDXF_FullFuleName)
		oTempDDoc.Close(True)
	Next 'oSheet
	'possible message letting user know the process is done
End Sub

Sub ExportDrawingToDXF(oDrawingDoc, sDXFFullFileName)
	'Get the DXF Translator AddIn
	Dim oDXF_AddIn As TranslatorAddIn = Nothing
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: DXF" Then oDXF_AddIn = oAddIn
	Next
	If oDXF_AddIn Is Nothing Then Exit Sub
	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
	'Check to see if the DXF already exists, if it does, ask if you want to over write existing file or not.
	If System.IO.File.Exists(sDXFFullFileName) = True Then
		Dim oAns As MsgBoxResult = MsgBox("A DXF file with the following name already exists:" & vbCrLf & _
		sDXFFullFileName & vbCrLf & "Do you want to over write it with this new one?", _
		vbYesNo + vbQuestion + vbDefaultButton2, "DXF ALREADY EXISTS")
		If oAns = vbNo Then Return
	End If
	oDataMedium.FileName = sDXFFullFileName '<<< assign the file name here >>>
	If oDXF_AddIn.HasSaveCopyAsOptions(oDrawingDoc, 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\Darren Lees\Desktop\DXF TEST\DXG-DXF\2010 DXF File.ini"
	End If
	'Publish DXF
	Try
		oDXF_AddIn.SaveCopyAs(oDrawingDoc, oContext, oOptions, oDataMedium)
		Logger.Info("Just exported the following DXF file:" & vbCrLf & sDXFFullFileName)
	Catch
		Logger.Error("Error exporting the following DXF file:" & vbCrLf & sDXFFullFileName)
	End Try
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) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @dleesuk.  I have this a shot for you.  The main thing I did here is separate the export process out to a custom Sub routine, while trying to enhance the main code a bit.  I saw something odd in there where you were trying to get a file name without extension from a sheet name, which seemed a bit odd to me, so I figured a way to work around that sheet/view model name part of the routine.  I get the first view on the active sheet, then get the model document of that view, then get its file name, without path or extension.  Then I combine the path of the main drawing with that model file name, and the "_DXF.dxf" bit at the end, to get the full file name of the DXF file we want to create.  Then I do the new temp drawing, copy sheet, delete other sheets bit.  Then I call the custom Sub routine to run.  Then close the temp drawing, just before it loops to the next sheet.  However, I'm guessing that you may be able to create the temporary drawing document before the loop of the sheets, just one time, then keep reusing it.  Then inside the loop, copy the active sheet to it, and delete all others, just like you are doing now.  Then close it after the loop of sheets is done.  Just a thought.

Sub Main
	'Make sure the active document is a drawing document.
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule " & iLogicVb.RuleName & " only works on Drawing Documents. Exiting.", vbCritical, "WRONG DOCUMENT TYPE")
		Return
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim sPath As String = IO.Path.GetDirectoryName(oDDoc.FullFileName)
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	For Each oSheet As Inventor.Sheet In oSheets
		oSheet.Activate
		Dim oView As DrawingView = Nothing
		Try : oView = oSheet.DrawingViews.Item(1) : Catch : End Try
		If oView Is Nothing Then Continue For
		Dim oModelDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
		Dim sModelFileName As String = System.IO.Path.GetFileNameWithoutExtension(oModelDoc.FullFileName)
		Dim sDXF_FullFuleName As String = sPath & "\" & sModelFileName & "_DXF.dxf"
		Logger.Info("Proposed DXF FullFileName = " & sDXF_FullFuleName)
		'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 Inventor.Sheet In oTempDDoc.Sheets
			If oTSheet IsNot oTempSheet Then oTSheet.Delete
		Next 'oTSheet
		'call the custom Sub to run, to export the DXF
		ExportDrawingToDXF(oTempDDoc, sDXF_FullFuleName)
		oTempDDoc.Close(True)
	Next 'oSheet
	'possible message letting user know the process is done
End Sub

Sub ExportDrawingToDXF(oDrawingDoc, sDXFFullFileName)
	'Get the DXF Translator AddIn
	Dim oDXF_AddIn As TranslatorAddIn = Nothing
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: DXF" Then oDXF_AddIn = oAddIn
	Next
	If oDXF_AddIn Is Nothing Then Exit Sub
	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
	'Check to see if the DXF already exists, if it does, ask if you want to over write existing file or not.
	If System.IO.File.Exists(sDXFFullFileName) = True Then
		Dim oAns As MsgBoxResult = MsgBox("A DXF file with the following name already exists:" & vbCrLf & _
		sDXFFullFileName & vbCrLf & "Do you want to over write it with this new one?", _
		vbYesNo + vbQuestion + vbDefaultButton2, "DXF ALREADY EXISTS")
		If oAns = vbNo Then Return
	End If
	oDataMedium.FileName = sDXFFullFileName '<<< assign the file name here >>>
	If oDXF_AddIn.HasSaveCopyAsOptions(oDrawingDoc, 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\Darren Lees\Desktop\DXF TEST\DXG-DXF\2010 DXF File.ini"
	End If
	'Publish DXF
	Try
		oDXF_AddIn.SaveCopyAs(oDrawingDoc, oContext, oOptions, oDataMedium)
		Logger.Info("Just exported the following DXF file:" & vbCrLf & sDXFFullFileName)
	Catch
		Logger.Error("Error exporting the following DXF file:" & vbCrLf & sDXFFullFileName)
	End Try
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) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8
dleesuk
in reply to: dleesuk

dleesuk
Advocate
Advocate

This amended routine works a treat!

I've tweaked it slightly to add a Folder Dialogue box.

 

Huge thanks to @WCrihfield and a shout-out to @romu51.

My quest is at an end!!!  😁


Regards

Darren

This amended routine works a treat!

I've tweaked it slightly to add a Folder Dialogue box.

 

Huge thanks to @WCrihfield and a shout-out to @romu51.

My quest is at an end!!!  😁


Regards

Darren

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report