Is there a way, using iLogic, to attach a step file to the ipt file that it was generated from, so that when I check the ipt file into the vault, it takes the step file with it? I've got a rule already running that automatically saves the step file when I save and close the ipt file, but I need to check them both into vault and I don't want to have to manually add the step file in every time. Any suggestions?
Solved! Go to Solution.
Solved by AlexFielder. Go to Solution.
Hi Escales, I have the following rule: https://github.com/AlexFielder/iLogic/blob/master/AddRemove/embed%20inventor%20drawings.iLogicVb which will do this for you. In your case since you know the path to the output step file, you can simply call the AddReferences() method with the Document object you're attaching to and the string path to the step file. (I would post the code, but the "new" forums don't display and formatting helpers for me currently - not sure if that's because Google Chrome or an extension blocking things...?) Turns out I can see the formatting options if I edit the post. Strange...
Anywho, here's the rule in case anyone else searches for "Embed" in future:
Option Explicit On Sub Main() 'current document Dim doc as Inventor.Document = ThisDoc.Document 'Verify the current document has been saved. If doc.FullFileName = "" Then MessageBox.Show("This document must be saved first.") Exit Sub End If 'default folder Dim FolderName As String = System.IO.Path.GetDirectoryName(doc.FullFileName) Dim selectedfile As String = String.Empty Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt|Inventor iFeatures (*.ide)|*.ide|XML Parameter files (*.xml)|*.xml|Step files (*.stp;*.step;*.stpz)|*.stp;*.step;*.stpz|Other files (*.*)|*.*" oFileDlg.InitialDirectory = FolderName oFileDlg.CancelError = True oFileDlg.MultiSelectEnabled = True Try oFileDlg.ShowOpen() selectedfile = oFileDlg.FileName Catch Return 'operation was cancelled by the user End Try AddReferences(doc, selectedfile) End Sub Public Sub AddReferences(ByVal odoc as Inventor.Document, ByVal selectedfile As String) Dim oleReference As ReferencedOLEFileDescriptor If selectedfile.Contains("|") Then ' we have multiple files selected. Dim file As String() = selectedfile.Split("|") For Each s as String in file oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1) Next Else oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(selectedFile,OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(selectedFile, InStrRev(selectedFile, "\") + 1) End If End Sub
🙂
Well, since I'm basically VB illiterate, I can't get things to work correctly. Below is the rule I'm using to save an .ipt file to a .stp file. I just need to add something to the rule to attach the new .stp to the .ipt file. Sorry I keep going in circles with this. I guess I need to learn VB in my space time. LOL
SyntaxEditor Code Snippet
' 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") 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 oData.FileName = ThisDoc.Path & "\" & oStepFileName & ".stp" oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData) End If
don't feel bad about not knowing VB; we all had to start somewhere right?
Here's the combined rule you need:
''' <summary> ''' Will only run if the activedocument has been saved. ''' </summary> Sub Main() Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Export and Attach Step file") Try If Not ThisApplication.ActiveDocument.FullFilename = String.Empty Then 'won't run if the active document isn't saved. Dim filenameToAttach As String = ExportToStep() If Not filenameToAttach = String.Empty Then Dim doc As Document = ThisApplication.ActiveDocument AddReferences(doc, filenameToAttach) End If Else MessageBox.Show("File not saved; save the file and try again!") trans.Abort() End If trans.End() Catch trans.Abort() End Try End Sub ''' <summary> ''' Returns an empty string if the stp file didn't save for some reason. ''' </summary> ''' <returns></returns> Function ExportToStep() As String Dim filename As String ' 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") 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 oData.FileName = ThisDoc.Path & "\" & oStepFileName & ".stp" oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData) filename = oData.FileName End If If System.IO.File.Exists(filename) Then Return filename Else Return "" End If End Function ''' <summary> ''' Attaches any file using the full c:\path\to\your\file.extension format. ''' </summary> ''' <param name="odoc"></param> ''' <param name="selectedfile"></param> Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String) Dim oleReference As ReferencedOLEFileDescriptor If selectedfile.Contains("|") Then ' we have multiple files selected. Dim file As String() = selectedfile.Split("|") For Each s As String In file oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1) Next Else oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(selectedfile,OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1) End If End Sub
I've enclosed the whole thing in a transaction, to save lots of undo and the export will only happen if the activedocument is saved; that maybe an unecessary step, but it pays to make sure files exist or you'll get errors.
Let me know if you need me to explain anything else.
Cheers,
Alex.
I got that to work now, thank you. I created another rule to do the same thing for .stl files and it saves the file ok, but will not attach it to the .ipt file. It shouldn't matter what type of file it is, should it?
Here is the rule for an .stl file
SyntaxEditor Code Snippet
''' <summary> ''' Will only run if the activedocument has been saved. ''' </summary> Sub Main() Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Export and Attach Stl file") Try If Not ThisApplication.ActiveDocument.FullFilename = String.Empty Then 'won't run if the active document isn't saved. Dim filenameToAttach As String = ExportToStl() If Not filenameToAttach = String.Empty Then Dim doc As Document = ThisApplication.ActiveDocument AddReferences(doc, filenameToAttach) End If Else MessageBox.Show("File not saved; save the file and try again!") trans.Abort() End If trans.End() Catch trans.Abort() End Try End Sub ''' <summary> ''' Returns an empty string if the stl file didn't save for some reason. ''' </summary> ''' <returns></returns> Function ExportToStl() As String oPath = ThisDoc.Path oStlFileName = iProperties.Value("Project", "Part Number") Dim STLAddIn As TranslatorAddIn STLAddIn = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}") Dim oDocument As Document = ThisDoc.Document Dim oContext As TranslationContext oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism ' Create a NameValueMap object Dim oOptions As NameValueMap oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Create a DataMedium object Dim oDataMedium As DataMedium oDataMedium = ThisApplication.TransientObjects.CreateDataMedium ' Check whether the translator has 'SaveCopyAs' options If STLAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then oOptions.Value("OutputFileType") = 0 ' 0 = Binary , 1 = ASCII oOptions.Value("ExportUnits") = 2 ' INCH = 2 , FOOT = 3 , CENTIMETER = 4 , MILLIMETER = 5 , METER = 6 , MICRON = 7 oOptions.Value("Resolution") = 0 ' HIGH = 0 , MEDIUM = 1 , LOW = 2 , CUSTOM = 3 , BREP = 4 oOptions.Value("ExportColor") = False End If 'Set the destination file name oDataMedium.FileName = oPath & "\" & oStlFileName & ".stl" 'Publish document. Call STLAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) End Function ''' <summary> ''' Attaches any file using the full c:\path\to\your\file.extension format. ''' </summary> ''' <param name="odoc"></param> ''' <param name="selectedfile"></param> Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String) Dim oleReference As ReferencedOLEFileDescriptor If selectedfile.Contains("|") Then ' we have multiple files selected. Dim file As String() = selectedfile.Split("|") For Each s As String In file oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1) Next Else oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(selectedfile, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1) End If End Sub
It looks like you were close to getting it working. The reason your second rule doesn't work is because your ExportStl() method doesn't return anything that the addReferences() method can use to attach.
This Diff shows the differences between our two rules: https://www.diffchecker.com/LpyNWKJw
I would make the necessary changes for you but as you said you were VB illiterate, if I do that you won't learn anything.
I am however happy to assist if you get stuck.
**placeholder reply so I can come back and edit because the formatting tools don't always work!**
You are indeed missing a couple of things:
Without returning a string from the Export function the AddReferences() method will get bypassed entirely.
I would suggest you get a good book on Visual Basic .NET; here are some recommendations I sent to a customer recently:
Here are some links that might prove useful in the meantime:
200 video C# playlist: https://www.youtube.com/playlist?list=PLZqR2Xso7_ak3MiEqXIk5B4khrRaolZoz
102 video C# playlist: https://www.youtube.com/playlist?list=PLAC325451207E3105
The previous version of this came highly recommended: https://www.amazon.co.uk/Microsoft-Visual-Step-Developer-Reference/dp/1509301046/ref=dp_ob_title_bk
Some of these replies are insightful:
https://www.pluralsight.com/guides/microsoft-net/tips-for-writing-better-c-code
^Pluralsight could be a good source for a course or two; they have some pretty clued up instructures posting content there.
Apologies for the wall of text, but this is a difficult topic to nail down.
🙂
I am also planning to write a simple add-in for Visual Studio 2017 itself that allows you to navigate straight to the Autodesk help pages as currently it’s a pain to figure out exactly what each method provided by the Inventor API is capable of accepting.
Thanks for all the help. I finally got it to work with the rule below. I definitely need to take some classes or watch some videos for VB. Thanks again for always replying to me. I really appreciate it.
SyntaxEditor Code Snippet
''' <summary> ''' Will only run if the activedocument has been saved. ''' </summary> Sub Main() Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Export and Attach Stl file") Try If Not ThisApplication.ActiveDocument.FullFilename = String.Empty Then 'won't run if the active document isn't saved. Dim filenameToAttach As String = ExportToStl() If Not filenameToAttach = String.Empty Then Dim doc As Document = ThisApplication.ActiveDocument AddReferences(doc, filenameToAttach) End If Else MessageBox.Show("File not saved; save the file and try again!") trans.Abort() End If trans.End() Catch trans.Abort() End Try End Sub ''' <summary> ''' Returns an empty string if the stl file didn't save for some reason. ''' </summary> ''' <returns></returns> Function ExportToStl() As String Dim filename As String ' Get the STL translator Add-In. Dim STLAddIn As TranslatorAddIn STLAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}") Dim oContext As TranslationContext oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism ' Create a NameValueMap object Dim oOptions As NameValueMap oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Create a DataMedium object Dim oDataMedium As DataMedium oDataMedium = ThisApplication.TransientObjects.CreateDataMedium oStlFileName = iProperties.Value("Project", "Part Number") ' Check whether the translator has 'SaveCopyAs' options If STLAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then oOptions.Value("OutputFileType") = 0 ' 0 = Binary , 1 = ASCII oOptions.Value("ExportUnits") = 2 ' INCH = 2 , FOOT = 3 , CENTIMETER = 4 , MILLIMETER = 5 , METER = 6 , MICRON = 7 oOptions.Value("Resolution") = 0 ' HIGH = 0 , MEDIUM = 1 , LOW = 2 , CUSTOM = 3 , BREP = 4 oOptions.Value("ExportColor") = False End If 'Set the destination file name Dim oData As DataMedium oData = ThisApplication.TransientObjects.CreateDataMedium oData.FileName = ThisDoc.Path & "\" & oStlFileName & ".stl" STLAddIn.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData) filename = oData.FileName If System.IO.File.Exists(filename) Then Return filename Else Return "" End If End Function ''' <summary> ''' Attaches any file using the full c:\path\to\your\file.extension format. ''' </summary> ''' <param name="odoc"></param> ''' <param name="selectedfile"></param> Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String) Dim oleReference As ReferencedOLEFileDescriptor If selectedfile.Contains("|") Then ' we have multiple files selected. Dim file As String() = selectedfile.Split("|") For Each s As String In file oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1) Next Else oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(selectedfile,OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1) End If End Sub
Can't find what you're looking for? Ask the community or share your knowledge.