iLogic to Export Model files from Multiple Drawing Sheets to STP

iLogic to Export Model files from Multiple Drawing Sheets to STP

arkelec
Collaborator Collaborator
448 Views
2 Replies
Message 1 of 3

iLogic to Export Model files from Multiple Drawing Sheets to STP

arkelec
Collaborator
Collaborator

I've found some code to export the model file to STP from a drawing sheet, but it only works with the active sheet and a single view.

How would I go about modifying the active sheet part to run through each sheet & up to 2 views, to run the export code? 

 

Here's the code in question:

Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document
Dim sht As Sheet = drawingDoc.ActiveSheet
Dim view As DrawingView = sht.DrawingViews.Item(1)

 

Original code from here 

 

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @arkelec.  You could try it like this.  You would just need an additional way of keeping track of which model files you have already worked with, so you do not try to work with the same model file again.  Just cut & paste all other code that is currently within the Sub Main.....End Sub area to where I am specifying to put it within the loop.  The ExportToStepFile routine will still be separate, like it currently is, after the Sub Main...End Sub area.

Imports System.Windows.Forms
Public Sub Main
	Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document
	For Each sht As Sheet In drawingDoc.Sheets
		For Each view As DrawingView In sht.DrawingViews
			'need to keep track of whick model files you have already worked with
			'to avoid working with the same model file again
			
			'put rest of code from Sub Main area in here
		Next 'view
	Next 'sht
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 3 of 3

arkelec
Collaborator
Collaborator

Thanks @WCrihfield, that worked a treat.

As you rightly pointed out, a lot of duplicate files will be created using each view, so I swapped that out for a single view.

There were some other modifications I made to accommodate the export code & it's a great result.

 

Here's the full code, for anyone interested:

 

Imports System.Windows.Forms

Public Sub Main

	Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document
	Dim strSaveAsSuff As String = " Rev " & iProperties.Value("Custom", "c_Revision_Nº")

	' Get current location of this file
	Dim ExportPath As String = ThisDoc.Path
	
	' Check that this file has been saved and actually exists on disk
	If String.IsNullOrEmpty(ExportPath) Then
		MsgBox("This file has not yet been saved and doesn't exist on disk!")
		Return
	End If
	
		' Browse for location to save file
		Dim dialog = New FolderBrowserDialog()
		dialog.SelectedPath = ExportPath
		If dialog.ShowDialog() = DialogResult.OK Then
		
			For Each sht As Sheet In drawingDoc.Sheets
			Dim oView As DrawingView = sht.DrawingViews.Item(1)

				' export part if part, export assembly if assembly
				Dim refDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
					If refDoc.DocumentType = kPartDocumentObject Then
					'ExportToStepFile(refDoc, dialog.SelectedPath)
					ExportToStepFile(refDoc, dialog.SelectedPath, strSaveAsSuff)
				Else If refDoc.DocumentType = kAssemblyDocumentObject Then
					'ExportToStepFile(refDoc, dialog.SelectedPath)
					ExportToStepFile(refDoc, dialog.SelectedPath, strSaveAsSuff)	
				End If	
			Next 'sheet
		End If
End Sub	


Public Sub ExportToStepFile(docToExport As Document, selectedFolderPath As String, selectedSuffix as String)

	' Get the STEP translator Add-In.
	Dim stepTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	Dim translatorContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim translatorOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	
	Dim newFilename as string = doctoexport.displayname.replace(".ipt","").replace(".iam","")
	Dim expPATH as string = selectedFolderPath & "\STP"
	
	newfilename = newfilename & selectedsuffix
	if doctoexport.documenttype = kassemblydocumentobject then newfilename = newfilename &".iam" else newfilename = newfilename & ".ipt"

	If stepTranslator.HasSaveCopyAsOptions(docToExport, translatorContext, translatorOptions) Then
		translatorOptions.Value("ApplicationProtocolType") = 3
		translatorContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

		Dim translatorData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
		translatorData.FileName = expPATH & "\" & System.IO.Path.ChangeExtension(newFilename, ".stp")
		stepTranslator.SaveCopyAs(docToExport, translatorContext, translatorOptions, translatorData)
	End If 
End Sub

And thanks to @Zach.Stauffer for the original export code.