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