Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can I attach a step file to an ipt file, so it'll check into Vault with ipt?

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
EScales
1231 Views, 11 Replies

Can I attach a step file to an ipt file, so it'll check into Vault with ipt?

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?

11 REPLIES 11
Message 2 of 12
AlexFielder
in reply to: EScales

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

🙂

Message 3 of 12
EScales
in reply to: AlexFielder

Thanks for this. It works pretty well, but I'm having a little trouble. I currently have two other rules that run and save my .ipt file to a .stp file and a .stl file. I'm actually pulling the "Part Number" iProperty, not the file name, from the .ipt file when they save. So, after I've save these files, I want to attach both of them to the original .ipt file, so they will check in to the vault along with the .ipt file. When I run your rule, I get an "Open" dialog box in Inventor. Is this because the .stp and .stl file names are not the same as the .ipt file name? I don't really want the "Open" dialog box to pop up. I just want the new .stp and .stl files to be automatically attached to the .ipt file.
Message 4 of 12
AlexFielder
in reply to: EScales

You don't need to run my rule specifically - I pasted it in my previous reply to show how it works when run manually; just copy the AddReferences() method to your rule, call it twice (once for each file you want to attach) and you're good to go!
Message 5 of 12
EScales
in reply to: AlexFielder

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
Message 6 of 12
AlexFielder
in reply to: EScales

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.

Message 7 of 12
EScales
in reply to: AlexFielder

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

 

Message 8 of 12
AlexFielder
in reply to: EScales

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.

Message 9 of 12
EScales
in reply to: AlexFielder

So, you're saying there's something missing from my ExportToStl() method, not the addReferences() method, correct? I'll keep messing with it. I'm not used to the VB terminology. I can usually fake my way through the basic iLogic snippets, but when you add VB into the mix, it's all Greek to me.
Message 10 of 12
AlexFielder
in reply to: EScales

**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:These are the missing bits from your ExportToStl() MethodThese are the missing bits from your ExportToStl() MethodThis is checking the result of ExportToWhatever()This is checking the result of ExportToWhatever()

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.reddit.com/r/learnprogramming/comments/6h7wja/any_selftaught_programmers_care_to_share_t...

 

https://www.reddit.com/r/learnprogramming/comments/4q6tae/i_highly_recommend_harvards_free_online_20...

 

https://www.reddit.com/r/learnprogramming/comments/5ed4xg/ive_taught_30000_students_how_to_code_now_...

 

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.

 

Message 11 of 12
EScales
in reply to: AlexFielder

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
Message 12 of 12
AlexFielder
in reply to: EScales

You are quite welcome, please don't forget to mark one of the replies ^ as the solution so others can see it was solved.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report