Export EACH .ipt referenced in .idw as a STEP

Export EACH .ipt referenced in .idw as a STEP

iogurt1
Advocate Advocate
2,626 Views
8 Replies
Message 1 of 9

Export EACH .ipt referenced in .idw as a STEP

iogurt1
Advocate
Advocate

Hey everyone.

I'm currently creating some buttons in the .idw environment to save us some time. I've got a button for PDF export, one for DXF export (both into a specific subfolder) and they both work perfectly. Now I also want to create one for exporting .stp files. I've found a few codes online that do work, but they only work for the 1st referenced .iam/.ipt on a drawin. But 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.

 

Can anyone help me with the "for each..." issue I'm having?

Thanks!

 

Edit: I've used this one so far

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/export-stp-from-drawing/m-p/9689317#M11...

0 Likes
Accepted solutions (1)
2,627 Views
8 Replies
Replies (8)
Message 2 of 9

Eide.N
Advocate
Advocate

This is for an assembly, but maybe you can use it (it is ugly code, but it works in 2019...).

 

'Makes STEP files for every ipt and iam in Assembly and Subassemblies
Sub Main()

If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("This must be run on an Assembly file.", " Oops! ", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Exit Sub
End If
'Dim DocPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
oPath = "C:\Work\STP Files\AutoSave"
Catches = 0 'catch the errors exporting files
oCSV = "C:\Work\STP Files\STP Errors.csv" 'Error log file

' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
    	oAsmDoc = ThisApplication.ActiveDocument
' Get all of the referenced documents.
    Dim oRefDocs As DocumentsEnumerator
    	oRefDocs = oAsmDoc.AllReferencedDocuments
		
'Check to make sure user wants to!
	i = MessageBox.Show("Export " & (oRefDocs.Count+1)& " STEP Files to directory " _
				& vbLf & "(Overwrite if same Filename and Rev)" _
				& vbLf _
				& vbLf & "C:\Work\STP Files\AutoSave", _
						" Export STEP Files ", _
		MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If i = 2 Then
	'Do nothing if Cancel
Else  'Carry on!

'Create path if doesn't exist
	If Not System.IO.Directory.Exists(oPath) Then
		System.IO.Directory.CreateDirectory(oPath)
	End If
	
'Check to see if Error Log File is open
' If it exists, delete it, It's either open or doesn't exist.
If System.IO.File.Exists(oCSV) Then
	Try
		System.IO.File.Delete(oCSV)
	Catch
		MessageBox.Show("The log file 'STP Errors.csv' is likely open! " _
				& vbLf & "   Please close the file to continue", "  Oops!  ")
		Exit Sub
	End Try
End If

'Progress Bar!
	Dim oProgressBar As Inventor.ProgressBar
	oProgressBar = ThisApplication.CreateProgressBar(False, oRefDocs.Count, "Exporting STEP Files")
    oProgressBar.UpdateProgress
	oProgressBar.Message="Processing...  " & ThisDoc.FileName
	
'Save Top level First
	AsmPathName = oAsmDoc.FullFileName
	AsmExt = System.IO.Path.GetFileName(AsmPathName)
	AsmRevNum = iProperties.Value(oAsmDoc, "Project", "Revision Number")
	If AsmRevNum = "" Then
		Catches = Catches + 1
		ErrorLog(AsmExt, oCSV)
	Else
		AsmName = System.IO.Path.GetFileNameWithoutExtension(AsmPathName)
			oProgressBar.Message="Processing...  " & AsmName & ".stp"
		oAsmDoc.SaveAs(oPath & "\" & AsmName & " Rev" & AsmRevNum & ".stp", True)
	End If

' Iterate through the list of documents.
    Dim oRefDoc As Document
    For Each oRefDoc In oRefDocs
		
				
		FilePathName = oRefDoc.FullFileName
		FileExt = System.IO.Path.GetFileName(FilePathName)
		RevNum = iProperties.Value(FileExt, "Project", "Revision Number")
		If RevNum = "" Then
			Catches = Catches + 1 'Did not export
			ErrorLog(FileExt, oCSV)
		Else
			Try
				If FilePathName.EndsWith("iam") Then
					fNameExt = System.IO.Path.GetFileName(FilePathName)
					fName = iProperties.Value(fNameExt, "Project", "Part Number")
						oProgressBar.Message = "Processing...  " & fName & ".stp"
						oRefDoc.SaveAs(oPath & "\" & fName & " Rev" & RevNum & ".stp", True)
				End If
			Catch
				'Did not export
				Catches = Catches + 1
				ErrorLog(FilePathName, oCSV)
			End Try
			
			Try
				If FilePathName.EndsWith("ipt") Then
					fNameExt = System.IO.Path.GetFileName(FilePathName)
					fName = iProperties.Value(fNameExt, "Project", "Part Number")
						oProgressBar.Message="Processing...  " & fName & ".stp"
					oRefDoc.SaveAs(oPath & "\" & fName & " Rev" & RevNum & ".stp", True)
				End If
			Catch
				'Did not export
				Catches = Catches + 1
				ErrorLog(FilePathName, oCSV)
			End Try
		
	'Update
		oProgressBar.UpdateProgress
		End If
    Next
	
'Close when done
	oProgressBar.Close
	
	If Catches = 0 Then
		MessageBox.Show(Catches & " errors occurred. All files exported!", " Success ", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
	Else
		MessageBox.Show(Catches & " errors occurred. Error log file will open...", " Errors Found ", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
	End If

'Open Folder after successful exports
	Try
		ThisDoc.Launch(oPath)
	Catch
	End Try
	Try
		ThisDoc.Launch(oCSV)
	Catch
	End Try		
End If

End Sub

Sub ErrorLog(i, oCSV)
	Dim oAppend As System.IO.StreamWriter
	oAppend = IO.File.AppendText(oCSV)
	oAppend.WriteLine(i)
	oAppend.Flush()
	oAppend.Close()
	Return
End Sub
Message 3 of 9

WCrihfield
Mentor
Mentor

I think I have something that may work for you.  I use a separate sub routing for the actual export process, to keep the main code cleaner.  I also use a separate Function routine to return only the Parts being referenced by the drawing in an ObjectCollection to the main sub routine, so we can then loop through them.  It then gets the full file name of the part, but without the file extension.  Then gets the revision data from the part's iProperties (if any), then puts the two together with the ".stp" file extension to specify the intended file name of the STEP file that is to be created.  Then we pass the Part and this new name to the sub routine for exporting the STEP file.  Then just a simple finishing message.

 

See if this does the task as you want it to.  You may want to change or adjust the options I'm using within the STEP translator sub routing first though.

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	oRefParts = GetAllDrawingParts(oDDoc)
	If oRefParts.Count = 0 Then
		MsgBox("No referenced Parts found in drawing.  Exiting rule.", vbInformation, "iLogic")
		Exit Sub
	End If
	For Each oObj In oRefParts
		Dim oRefPart As PartDocument = CType(oObj, PartDocument)
		'specify file name for new STEP file
		'first get path & file name, without file extension
		oName = System.IO.Path.ChangeExtension(oRefPart.FullFileName, "")
		'now get Rev from Part iProperties to use in name
		Dim oRev As String = oRefPart.PropertySets.Item(1).Item("Revision Number").Value
		If String.IsNullOrEmpty(oRev) Then oRev = ""
		'now put them together
		oSTEPFile = oName & oRev & ".stp"
		'now run our sub routine defined below
		ExportToSTEP(oRefPart, oSTEPFile)
	Next
	MsgBox("All Parts have been exported to STEP files.", vbInformation, "iLogic")
End Sub

Function GetAllDrawingParts(oDrawing As DrawingDocument) As ObjectCollection
	Dim oDocColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	If oDrawing.AllReferencedDocuments.Count = 0 Then Return oDocColl
	For Each oRefDoc As Document In oDrawing.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			oDocColl.Add(oRefDoc)
		End If
	Next
	Return oDocColl
End Function

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in not found.  Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If

	'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 &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAnswer = 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 this document as a STEP file FAILED!", vbOKOnly + 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) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 9

iogurt1
Advocate
Advocate

Hi WCrihfield

Thank you for your code, it's great! I've modified it a bit to our needs (like, copying the REV number from the 1st referenced file in the drawing to the iProperties of the .idw, so that I can use this REV number for all my STP files).

 

Everything works well, except these 2 things I can't figure out:
1. in the file name, it always adds an extra "." and I don't know where that's coming from. I'm guessing that somehow it gets left in on the line

		oName = System.IO.Path.ChangeExtension(oRefPart.FullFileName, "")

 

Not sure why though. For example it's exported as "123..stp" or if it has a REV as "123.A.stp"

 

Not sure if there's an interference somewhere further down in the code at the part where it creates a new file name?

	oDataMedium = oTO.CreateDataMedium

	oDataMedium.FileName = oNewFileName

 

2. I can't get it to put the STPs into my folder. I copied what I use in my DXF and PDF rules. There this part of the code works and puts the files into my desired location.

Right now, it puts them in the same folder as my ipt/iam files.

 

'get PDF target folder path
oFolder = Left(oPath, InStrRev(oPath, "\")) & "6   PDF Dateien"

'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If

 

 

Here's my full code I've got right now:

 

Sub Main

'Copy Rev Number from .ipt/.iam to .idw iProperties
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
iProperties.Value("Project", "Revision Number") = iProperties.Value(modelName,"Project", "Revision Number")

	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	oRefParts = GetAllDrawingParts(oDDoc)
	oRevNum = iProperties.Value("Project", "Revision Number")
	
	For Each oObj In oRefParts
		Dim oRefPart As PartDocument = CType(oObj, PartDocument)
		'specify file name for new STEP file
		'first get path & file name, without file extension
		oName = System.IO.Path.ChangeExtension(oRefPart.FullFileName, "")
	'Assembling the file name
	If oRevNum = "-" Then
	oSTEPFile = oName & ".stp"
	Else
	oSTEPFile = oName & oRevNum & ".stp"
	End If

	'now run our sub routine defined below
	ExportToSTEP(oRefPart, oSTEPFile)
	Next

End Sub

Function GetAllDrawingParts(oDrawing As DrawingDocument) As ObjectCollection
	Dim oDocColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	If oDrawing.AllReferencedDocuments.Count = 0 Then Return oDocColl
	For Each oRefDoc As Document In oDrawing.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			oDocColl.Add(oRefDoc)
		End If
	Next
	Return oDocColl
End Function

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)

oPath = ThisDoc.Path

	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in nicht gefunden", vbCritical, "iLogic")
		Exit Sub
	End If

	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	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

		'Publish document
		'get STP target folder path
		oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"

		'Check for the STP folder and create it if it does not exist
		If Not System.IO.Directory.Exists(oFolder) Then
    		System.IO.Directory.CreateDirectory(oFolder)
		End If
		
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Etwas ist schief gelaufen, konnte kein STP erstellen", vbOKOnly + vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

 

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

About the extra dot (.) in the file names...It seems like I have used that nearly identical line of code many times before without problems, but I just did another simple test of a line like that and found that it is leaving that dot at the end of the String now.  Apparently when you leave the replacement string empty, it is leaving the dot, but when you specify something line ".stp" it does not add the extra dot in there.  I may have been directly replacing one extension with another, instead of leaving it blank in my regularly used codes.  It's no big deal, because there are lots of ways of manipulating strings the way we want.  You could use "stp" instead of ".stp" when putting the file names together.  Or you could replace that little block of code with this code to fix that problem.

 

'get path of oRefPart (does not include directory seperator character at end)
oRefPath = System.IO.Path.GetDirectoryName(oRefPart.FullFileName)
'get local directory seperator character
oDirChr = System.IO.Path.DirectorySeparatorChar
'get file name (without path or file extension)
oName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullFileName)

If oRevNum = "-" Then
	oSTEPFile = oRefPath & oDirChr & oName & ".stp"
Else
	oSTEPFile = oRefPath & oDirChr & oName & oRevNum & ".stp"
End If

 

As for the oFolder variable and your STEP files not being saved into that folder...I can see why that is not happening.  You are creating that variable and may be getting the right data, but then you are not putting that path data into the full file name of the STEP file that is to be created.  In this case, we have the whole code set up to specify complete file name of STEP file outside of the Export sub routine, then passing that name to the Export sub routine.  The specified name has already been assigned to the DataMedium.FileName, before you create and set the value of your oFolder variable, then it never gets put into the file path.  To fix this, you could either move the whole naming convention part of the code either entirely outside of the Export sub routine, or move it all into the export sub routine instead.  If you move it all into the sub routine, you will want to get rid of the extra input variable in the definition line of the sub routine, because it will no longer be needed.  Then I guess the new code I just posted above may need to change further to incorporate the different path data, instead of the model's path.

 

I also noticed that your code is using several different document reference phrases (ThisDrawing.ModelDocument ; ThisApplication.ActiveDocument ; ThisDoc.Path), which I usually strongly suggest that folks avoid, because they can sometimes be representing different documents, especially when dealing with drawings and assemblies (Link).  I generally choose the one most appropriate to the situation, then stick with it, or just use it once, then use the variable I stored it to throughout the rest of the code, to avoid potential problems.  But if your code is working OK for you, that's all that really matters for now.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 9

iogurt1
Advocate
Advocate

Thanks again for your reply!

I've now gotten hold of the "." problem and changed my code, so that it only gets the file name without extension. And for checking what the code is doing at certain points, I've created a MsgBox command. Both of these MsgBox return the desired value and the extra "." is gone now. And as you mentioned, in your code, you get the exact location and the name of the file. So the MsgBox would say "C:/Inventor/123.". With my code, it now says it's "123" and then the 2nd box shows it as "123A.stp". Exactly how I want it.

 

I've also shortened the code a bit by not having the subroutine anymore. I copied that up to the sub main. But so far I haven't found a problem with that.

 

BUT: I can't figure out how to now actually save the file into my folder. Up until MsxBox #2, the code works flawlessly (in my opinion, but maybe I'm missing something) and then once it gets to

oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)

it falls apart and gives me the error message I defined just below. So I'm not sure what's going wrong.

 

I feel like I'm right there and am missing a tiny bit of code for it to actually create the files and put them where I want them. So frustrating haha. Maybe you have an idea? Thanks!

 

Full code as I have it right now:

 

Sub Main

'Copy Rev Number from .ipt/.iam to .idw iProperties
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
iProperties.Value("Project", "Revision Number") = iProperties.Value(modelName,"Project", "Revision Number")

	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim oDoc As Document
	oRefParts = GetAllDrawingParts(oDDoc)
	oRevNum = iProperties.Value("Project", "Revision Number")
	oPath = ThisDoc.Path
	
	For Each oObj In oRefParts
		Dim oRefPart As PartDocument = CType(oObj, PartDocument)
		oName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullDocumentName)

MsgBox( oName )

	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in nicht gefunden", vbCritical, "iLogic")
		Exit Sub
	End If

	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	If oSTEP.HasSaveCopyAsOptions(oDataMedium, 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
	 End If
		
	'get STP target folder path
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"

	'Check for the STP folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
   	System.IO.Directory.CreateDirectory(oFolder)
	End If
		
	'Set the STP target file name
	If oRevNum = "-" Then
	oDataMedium.FileName = oName & ".stp"
	Else
	oDataMedium.FileName = oName & oRevNum & ".stp"
	End If
	
MsgBox( oDataMedium.FileName )
	
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Etwas ist schief gelaufen, konnte kein STP erstellen", vbOKOnly + vbExclamation, "Export to STEP Error")
		End Try
	Next

End Sub

Function GetAllDrawingParts(oDrawing As DrawingDocument) As ObjectCollection
	Dim oDocColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	If oDrawing.AllReferencedDocuments.Count = 0 Then Return oDocColl
	For Each oRefDoc As Document In oDrawing.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			oDocColl.Add(oRefDoc)
		End If
	Next
	Return oDocColl
End Function
0 Likes
Message 7 of 9

iogurt1
Advocate
Advocate
Accepted solution

I got it!! The solution was to include the file path in my file name assembly. Need both

	oPath = ThisDoc.Path
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"

and then when I assemble the file name I had to include oFolder

 

	'Assembling the file name
	If oRevNum = "-" Then
	oSTEPFile = oFolder & "\" & oName & ".stp"
	Else
	oSTEPFile = oFolder & "\" & oName & oRevNum & ".stp"
	End If

 

Full code:

 

Sub Main

'Copy Rev Number from .ipt/.iam to .idw iProperties
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
iProperties.Value("Project", "Revision Number") = iProperties.Value(modelName,"Project", "Revision Number")

	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	oRefParts = GetAllDrawingParts(oDDoc)
	oRevNum = iProperties.Value("Project", "Revision Number")
	oPath = ThisDoc.Path
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"
	
	'Check for the STP folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If
	
	For Each oObj In oRefParts
		Dim oRefPart As PartDocument = CType(oObj, PartDocument)
		'specify file name for new STEP file
		'first get path & file name, without file extension
		oName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullDocumentName)
	'Assembling the file name
	If oRevNum = "-" Then
	oSTEPFile = oFolder & "\" & oName & ".stp"
	Else
	oSTEPFile = oFolder & "\" & oName & oRevNum & ".stp"
	End If
	
	'now run our sub routine defined below
	ExportToSTEP(oRefPart, oSTEPFile)
	Next

End Sub

Function GetAllDrawingParts(oDrawing As DrawingDocument) As ObjectCollection
	Dim oDocColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	If oDrawing.AllReferencedDocuments.Count = 0 Then Return oDocColl
	For Each oRefDoc As Document In oDrawing.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			oDocColl.Add(oRefDoc)
		End If
	Next
	Return oDocColl
End Function

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)

oPath = ThisDoc.Path

	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in nicht gefunden", vbCritical, "iLogic")
		Exit Sub
	End If

	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	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

		'Publish document
		'get STP target folder path
		oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"

		'Check for the STP folder and create it if it does not exist
		If Not System.IO.Directory.Exists(oFolder) Then
    		System.IO.Directory.CreateDirectory(oFolder)
		End If
		
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Etwas ist schief gelaufen, konnte kein STP erstellen", vbOKOnly + vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

 

 

Message 8 of 9

iogurt1
Advocate
Advocate

One more adjustment I'd like to make, but I'm unsure how to get there.

 

Sometimes we have drawings with an .iam on it, and a couple of single .ipt as well. I only want to create STEP files of directly referenced .ipt files. Right now, it also creates every single STEP file of every .ipt inside the .iam. And depending on the size of the .iam, it creates a ton of STEP files I don't need. Especially from library files like bolts, nuts, washers, and so on.

 

Is there a way to only create STEP files from every .ipt on the .idw, and ignore the .iam if there is one? Thanks!

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Hi @iogurt1.  I do believe this is possible.  First of all, if you may possibly have more than one 'model' document, then we must do away with using the iLogic only snippet 'ThisDrawing.ModelDocument', because that will always only return the document that is being referenced by the first drawing view that was placed in the drawing document.  That may be the assembly, because we have not yet sorted through the referenced documents.  Also, when there may be multiple parts being shown within the same drawing, then how do you decide which model document to copy the 'Revision Number' iProperty value to the drawing from?  I changed the 'GetAllDrawingParts' function to use 'oDrawing.ReferencedDocuments' instead of 'oDrawing.AllReferencedDocuments', to eliminate potentially getting all the documents within all levels of the assembly.  I also changed it to return a 'List(Of PartDocument)' object, because that is way more convenient, efficient, and to the point.  I also chose to copy the Revision Number value from the first part document, for now, since that detail is still up in the air.  I didn't bother messing with the Sub for export.

This is how I would edit the code, but that's just my opinion/style:

 

 

Sub Main
	oDDoc = ThisDrawing.Document
	oPath = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "7   STEP Dateien"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If
	Dim oRefParts As List(Of PartDocument) = GetAllDrawingParts(oDDoc)
	If oRefParts.Count = 0 Then
		MsgBox("No parts found referenced by this drawing.  Exiting rule.", vbCritical, "")
		Exit Sub
	Else
		'copy 'Revision Number' iProperty value from 1st Part type 'model' document to this drawing
		Dim oRevNum As String = oRefParts.Item(1).PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
		oDDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value = oRevNum
		For Each oPDoc In oRefParts
			oName = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
			Dim oSTEPFile As String = ""
			If oRevNum = "-" Then
				oSTEPFile = oFolder & "\" & oName & ".stp"
			Else
				oSTEPFile = oFolder & "\" & oName & oRevNum & ".stp"
			End If
			ExportToSTEP(oRefPart, oSTEPFile)
		Next
	End If
End Sub

Function GetAllDrawingParts(oDrawing As DrawingDocument) As List(Of PartDocument)
	Dim oPartDocs As New List(Of PartDocument)
	If oDrawing.ReferencedDocuments.Count = 0 Then Return oPartDocs
	oPartDocs.AddRange(oDrawing.ReferencedDocuments.OfType(Of Inventor.Document).Where(Function(x) x.DocumentType = DocumentTypeEnum.kPartDocumentObject).Cast(Of PartDocument))
	Return oPartDocs
End Function

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
End Sub

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes