Export multiple STP from drawing with naming the STP files as their parts name

Export multiple STP from drawing with naming the STP files as their parts name

baraknidam1
Explorer Explorer
539 Views
5 Replies
Message 1 of 6

Export multiple STP from drawing with naming the STP files as their parts name

baraknidam1
Explorer
Explorer

Hey guys!

 

I have a drawing file with multiple sheets in it and with every sheet there is a drawing of a single part.

I want to generate STP file from every part that is in every sheet.

 

In addition, I want every single STP to have the same name as it's original part.

If it make it easier, the names can be pulled from the sheet name.

 

For example: in the attached picture I have a multiple sheets.

Every sheet have a drawing of a single part.

I want to save a STP file from every part with it's original name.

The name can be pulled from the sheet name.

Sheets.PNG

 

Thank you very much!

0 Likes
Accepted solutions (3)
540 Views
5 Replies
Replies (5)
Message 2 of 6

jatindevaiya08
Contributor
Contributor
Accepted solution

Hello @baraknidam1 ,

Please check below iLogic code for your requirement. It will generate STP file of each sheets reference part with same name of the part file.

oDoc = ThisDoc.Document

If oDoc.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject Then
	For Each oSheet As Sheet In oDoc.Sheets
		rDoc = oSheet.DrawingViews(1).ReferencedDocumentDescriptor.FullDocumentName
		oSTP = rDoc.Replace(".ipt",".stp")
		oRefDoc = ThisApplication.Documents.Open(rDoc, False)
		ThisApplication.SilentOperation = True
			Call oRefDoc.SaveAs(oSTP, True)
			Call oRefDoc.close
	Next
	ThisApplication.SilentOperation = False
	MessageBox.Show("All STP file generated!","Notification")
End If

  
Hope it's inline with your requirement.

Regards,
Jatin Devaiya

If a response answers your question, please use ACCEPT SOLUTION to assist other users later.


Also be generous with Likes! Thank you and enjoy!
Message 3 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @baraknidam1.  Just posting another iLogic rule example for the process you seem to be wanting to do.  This version does not bother iterating through drawing sheets or drawing views, but simply uses the drawing's AllReferencedDocuments collection, and filters for parts only (does not process any assemblies, if it finds any).  When using 'AllReferencedDocuments', it will look at every level of any assemblies that may be referenced, and process the parts within those levels.  If you only want to process top level documents, that have a direct reference to the drawing, you can change that one term on line 7 from 'AllReferencedDocuments' to 'ReferencedDocuments' instead.  This export process accesses the translator AddIn directly, allowing you to specify some settings/options, similar to when you do it manually, and choose the Options button, and fill in those options/settings.  I left several of those options commented out, because I was not sure if or how you would want them set.

Here is the code:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oDDoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	For Each oRefDoc As Document In oRefDocs
		'only process parts, not assemblies
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		'same path and name as original, but with ".stp" file extension
		Dim oStepFileName As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".stp")
		ExportToSTEP(oRefDoc, oStepFileName)
	Next
	MsgBox("Done exporting all referenced parts to STEP files.", vbInformation, "Export To STEP Done")
End Sub

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
	Dim oSTEP As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
	"{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
		oNewFileName & vbCrLf & _
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName
	If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oOptions.Value("ApplicationProtocolType") = 3
		oOptions.Value("IncludeSketches") = True
		oOptions.Value("export_fit_tolerance") = .000393701  'minimum
		'oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = oDoc.PropertySets.Item(3).Item("Description").Value
		'oOptions.Value("Organization") = oDoc.PropertySets.Item(2).Item("Company").Value
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export the following document:" & vbCrLf & _
			oDoc.FullFileName & vbCrLf & _
			"as the following STEP file:" & vbCrLf & _
			oDataMedium.FileName & vbCrLf & _
			"FAILED!", vbExclamation, "Export to STEP Error")
		End Try
	End If
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 4 of 6

baraknidam1
Explorer
Explorer

That works perfect!

 

Thank you for the quick replay!

 

May I ask you for addition in the rule? 

 

I want to save all the STP in one place. Can you change the rule so that every time I use it, a window opens up and let me pick a folder to save all the STP files?

 

Thank you very much!

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @baraknidam1.  Below is a modified version of the first code I posted here.  It incorporates another custom Function, which encloses a FolderBrowserDialog routine.  That function is now called to run within the main section of the code, just before we start looping through referenced documents, and allows you to browse for a folder to put the STEP files into, and returns the String which represents that folder path (without the "\" at the end).  Then within the loop, it is now extracting just the file's name, without path or file extension, then using that to build the new FullFileName of the STEP file we want to create.  Then it supplies that to the same custom Sub routine as before for exporting the STEP file.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oDDoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	Dim oSTEPFolder As String = SelectFolder("Specify Folder For STEP Files.")
	If oSTEPFolder = "" Then Exit Sub 'no path was returned from SelectFolder function
	For Each oRefDoc As Document In oRefDocs
		'only process parts, not assemblies
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		Dim oNewFullFileName As String = oSTEPFolder & "\" & oFileName & ".stp"
		ExportToSTEP(oRefDoc, oNewFullFileName)
	Next
	MsgBox("Done exporting all referenced parts to STEP files.", vbInformation, "Export To STEP Done")
End Sub

Function SelectFolder(Optional oPrompt As String = vbNullString) As String
	If oPrompt = "" Then oPrompt = "Select Folder"
	Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
	oFDialog.Description = oPrompt
	oFDialog.ShowNewFolderButton = True
	oFDialog.RootFolder = System.Environment.SpecialFolder.MyComputer
	Dim oResult As System.Windows.Forms.DialogResult = oFDialog.ShowDialog()
	If oResult = System.Windows.Forms.DialogResult.OK Then
		Return oFDialog.SelectedPath
	Else 'if dialog was canceled or something else
		Return Nothing
	End If
End Function

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
	Dim oSTEP As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
	"{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
		oNewFileName & vbCrLf & _
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName
	If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oOptions.Value("ApplicationProtocolType") = 3
		oOptions.Value("IncludeSketches") = True
		oOptions.Value("export_fit_tolerance") = .000393701  'minimum
		'oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = oDoc.PropertySets.Item(3).Item("Description").Value
		'oOptions.Value("Organization") = oDoc.PropertySets.Item(2).Item("Company").Value
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export the following document:" & vbCrLf & _
			oDoc.FullFileName & vbCrLf & _
			"as the following STEP file:" & vbCrLf & _
			oDataMedium.FileName & vbCrLf & _
			"FAILED!", vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

baraknidam1
Explorer
Explorer

Works perfectly!!!

 

Thank you very much!!!

0 Likes