Export STP from drawing

Export STP from drawing

ChristianAndersenIsmyname
Advocate Advocate
1,963 Views
6 Replies
Message 1 of 7

Export STP from drawing

ChristianAndersenIsmyname
Advocate
Advocate

Hello,

I've looked around and found a code from this forum post that makes a .stp file of part that's used in a drawing.

I've modified bretrick30's code (in comment 9) , as this only works for Sheet Metal parts.

The code below is what I use. This code do generate a .stp file, but the file is blank (2 kB size).

 

'Dim statement: Declares and allocates storage space for variables
Dim oPart As Inventor.PartDocument
'Takes the part shown in the 1st view
'of the current sheet's DrawingViews collection
oDrawingDoc = ThisDrawing.Document
oSheet = ActiveSheet.Sheet
oDrawingView = oSheet.DrawingViews.Item(1)

oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument
' Get the STEP translator Add-In.
	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim oOptions As NameValueMap
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		
	oStepFileName = iProperties.Value("Project", "Part Number") & " " & iProperties.Value("Project", "Description")
		
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
		    ' Set application protocol.
		    ' 2 = AP 203 - Configuration Controlled Design
		    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 3
		    ' Other options...
		    'oOptions.Value("Author") = ""
		    'oOptions.Value("Authorization") = ""
		    'oOptions.Value("Description") = ""
		    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
		strFolder = "C:\TEMP\"
   		oData.FileName = strFolder & "\" & oStepFileName & ".stp"
	oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If

 

Incase it's unclear: I open a .idw to make .stp of the part/assembly used in opened .idw.

Inventor 2021.1

0 Likes
Accepted solutions (1)
1,964 Views
6 Replies
Replies (6)
Message 2 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @ChristianAndersenIsmyname 

This line:

oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)

Is trying to make a step of the drawing. (The active document is the drawing, not the ipt)

You want to make a step of the part thats in the drawing:

'Dim statement: Declares and allocates storage space for variables
Dim oPart As Inventor.PartDocument
'Takes the part shown in the 1st view
'of the current sheet's DrawingViews collection
oDrawingDoc = ThisDrawing.Document
oSheet = ActiveSheet.Sheet
oDrawingView = oSheet.DrawingViews.Item(1)

oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument
' Get the STEP translator Add-In.
	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim oOptions As NameValueMap
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		
	oStepFileName = iProperties.Value("Project", "Part Number") & " " & iProperties.Value("Project", "Description")
		
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
		    ' Set application protocol.
		    ' 2 = AP 203 - Configuration Controlled Design
		    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 3
		    ' Other options...
		    'oOptions.Value("Author") = ""
		    'oOptions.Value("Authorization") = ""
		    'oOptions.Value("Description") = ""
		    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
		strFolder = "C:\TEMP\"
   		oData.FileName = strFolder & "\" & oStepFileName & ".stp"
	oSTEPTranslator.SaveCopyAs(oPart, oContext, oOptions, oData)
End If

 

Message 3 of 7

ChristianAndersenIsmyname
Advocate
Advocate

Thank you!

Should've noticed that myself.

Message 4 of 7

WCrihfield
Mentor
Mentor

Looks like Joel beet me to the punch, but here's what I had too, just for reference.

It shows a couple more available options within the STEP translator.

It also pulls the iProperties from the model too.

Although, I think there might still be a problem within the Path too, because it may be putting two "\" between C:\Temp\ and the rest of the path.

 

 

Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oMDoc As Document = ThisDrawing.ModelDocument
Dim oAddIns As ApplicationAddIns = ThisApplication.ApplicationAddIns
Dim oSTEP As TranslatorAddIn
For Each oAddIn As ApplicationAddIn In oAddIns
	If oAddIn.DisplayName = "Translator: STEP" Then
		oSTEP = 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 oStepFileName As String = iProperties.Value(oMDoc, "Project", "Part Number") & " " & iProperties.Value(oMDoc, "Project", "Description")
Dim strFolder As String = "C:\TEMP\"
oDataMedium.FileName = strFolder & "\" & oStepFileName & ".stp"

If oSTEP.HasSaveCopyAsOptions(oMDoc, 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
	' Other options...
	'oOptions.Value("export_fit_tolerance") = .000393701
	'oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
	'oOptions.Value("Authorization") = ""
	'oOptions.Value("Description") = iProperties.Value(oMDoc, "Project", "Description")
	'oOptions.Value("Organization") = iProperties.Value(oMDoc, "Document Summary Information", "Company")
	oSTEP.SaveCopyAs(oMDoc, oContext, oOptions, oDataMedium)
End If

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

iogurt1
Advocate
Advocate

Amazing code, thanks! I do have a question though:

 

Sometimes we have drawings with more than 1 .ipt referenced (For examble on drawing "123.idw" we have a "123.ipt", a "123-2.ipt" and a "123-3.ipt". And also a "123.iam"). I'd like to create .stp files for each .ipt (but not the .iam). I've found some solutions where it checks the file type of a referenced file. That should work fine. Where I'm struggling is finding a solution with a "for each..." command in my iLogic code, where it saves "123A.stp", "123-1A.stp" and a "123-2A.stp" taking the same file name as the ipt and adding the Rev at the end. Adding the Rev in the file name I've got working as well already.

 

Would this be possible?
Thanks!

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

Just adding the link here to the related post to that last question, because I too just responded to it.

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/export-each-ipt-referenced-in-idw-as-a-... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

donnie.morris
Enthusiast
Enthusiast
Is there anyway to adapt this code from the location being "C:\TEMP\" to something where you can choose the file location like follows:

' 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!" _
& vbLf & "Please save it first",64, "Lord iLogic")
Return
End If

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
' User clicked 'ok' on dialog box - capture the export path
ExportPath = Dialog.SelectedPath & "\"
Else
' User clicked 'cancel' on dialog box - exit
Return
End If

oFileName = iProperties.Value("Custom", "Drawing Number")

' Define the filename of the file to be exported
' In this Case it Is a PDF file extension
ExportFilename = oFileName & ".pdf"
0 Likes