Can anyone please send me the vba code to select a part file from a file dialog & then automatically place & constrain to the origin. I currently have a macro that uses commondialog but this doesnt seem to be supported with Inventor 2014.
Solved! Go to Solution.
Solved by xiaodong_liang. Go to Solution.
Hi,
I'd suggest you post your code for the peers in forum to diagnose why it failed in 2014.
I still wrote a small code which creates a file dialog and asks user to select a file, next places it to the active assembly. it works well in Inventor 2014.
Sub selectPartAndPlace() 'assume an assembly is opened Dim oAssDoc As AssemblyDocument Set oAssDoc = ThisApplication.ActiveDocument ' Create a new FileDialog object. Dim oFileDlg As FileDialog Call ThisApplication.CreateFileDialog(oFileDlg) ' Define the filter to select part and assembly files or any file. oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" ' Define the part and assembly files filter to be the default filter. oFileDlg.FilterIndex = 1 ' Set the title for the dialog. oFileDlg.DialogTitle = "Open File Test" ' Set the initial directory that will be displayed in the dialog. oFileDlg.InitialDirectory = "C:\Temp" ' Set the flag so an error will be raised if the user clicks the Cancel button. oFileDlg.CancelError = True ' Show the open dialog. The same procedure is also used for the Save dialog. ' The commented code can be used for the Save dialog. On Error Resume Next oFileDlg.ShowOpen ' oFileDlg.ShowSave ' If an error was raised, the user clicked cancel, otherwise display the filename. If Err Then MsgBox "User cancelled out of dialog" ElseIf oFileDlg.FileName <> "" Then 'MsgBox "File " & oFileDlg.FileName & " was selected." Dim oAssDef As AssemblyComponentDefinition Set oAssDef = oAssDoc.ComponentDefinition Dim oM As Matrix Set oM = ThisApplication.TransientGeometry.CreateMatrix() Dim oNewOcc As ComponentOccurrence Set oNewOcc = oAssDef.Occurrences.Add(oFileDlg.FileName, oM) 'if you need to ground it 'oNewOcc.Grounded = True End If End Sub
How can i use this code, but instead of grounding create 3 other constraint?
Assembly Y-axis Mate Sub-assembly Y-axis
Assembly XZ-plane Flush Sub-assembly XZ-axis
Assembly XY-plane 2-angle Sub-assembly XY-axis
This is a constraint that i use very often when placing new parts on a tank so it would be easy if it could be done with placing 🙂
I am using this code (similar to the one above, but without dialogbox and grounding is default off)
I'm not taking credit for this, I found the code here on the forum, but I slightly modifyed it to suit my needs. 🙂
I created a custom button to run this script...nice and effective 🙂
'get file to place Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" oFileDlg.DialogTitle = "Select a File" oFileDlg.InitialDirectory = ThisDoc.Path oFileDlg.CancelError = True On Error Resume Next oFileDlg.ShowOpen() If Err.Number <> 0 Then MessageBox.Show("File not chosen.", "Dialog Cancellation") Return ElseIf oFileDlg.FileName <> "" Then oFile = oFileDlg.FileName End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Set a reference to the transient geometry object. Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry ' Create a matrix. Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix 'place an instance of the component Dim oOccurrence As ComponentOccurrence oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) ' Set the translation portion of the matrix so the part will be 'positioned at the co-ordinates oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0)) 'Iterate through all of the occurrences and ground them. For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then oOccurrence.Grounded = False Else End If Next 'Get the plane from the selected part. ' The work plane obtained is in the context of the part, not the assembly. Dim oPartPlane1 As WorkPlane Dim oPartPlane2 As WorkPlane Dim oPartPlane3 As WorkPlane oPartPlane1 = oOccurrence.Definition.WorkPlanes.Item(1) 'Component YZ Plane oPartPlane2 = oOccurrence.Definition.WorkPlanes.Item(2) 'Component XZ Plane oPartPlane3 = oOccurrence.Definition.WorkPlanes.Item(3) 'Component XY Plane 'Get the plane from the assembly Dim oAsmPlane1 As WorkPlane Dim oAsmPlane2 As WorkPlane Dim oAsmPlane3 As WorkPlane oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(1) 'Assembly YZ Plane oAsmPlane2 = oAsmCompDef.WorkPlanes.Item(2) 'Assembly XZ Plane oAsmPlane3 = oAsmCompDef.WorkPlanes.Item(3) 'Assembly XY Plane ' Create proxies for the work planes of the part. ' The proxies represent the part work planes in the context of the assembly. Dim oAsmPlane4 As WorkPlaneProxy Dim oAsmPlane5 As WorkPlaneProxy Dim oAsmPlane6 As WorkPlaneProxy oOccurrence.CreateGeometryProxy(oPartPlane1, oAsmPlane4) oOccurrence.CreateGeometryProxy(oPartPlane2, oAsmPlane5) oOccurrence.CreateGeometryProxy(oPartPlane3, oAsmPlane6) ' Create the constraint using the part work plane proxies. oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane1, oAsmPlane4, 0) oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane2, oAsmPlane5, 0) oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane3, oAsmPlane6, 0)
I use flushing to 1 or 2 origin planes myself, so I modifyed the script to work in "different levels of flushing" I even created new buttons for them 🙂
check out the attached zip file and use the scripts you need 🙂
Hello itjelta,
is it possible to get the icons for your scripts?
Thanks
Georg
Hello i tried this rule again as is so just pasted it and i get 2x error on this code what am i doing wrong?
Error1 (on the last 3 lines)
Argument not specified for parameter 'EntityTwo' of 'Public function AddFlushConstraint...
Error2 (on the last 3 lines)
Comma,'(', or a valid expression continuation expected.
'get file to place Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" oFileDlg.DialogTitle = "Select a File" oFileDlg.InitialDirectory = ThisDoc.Path oFileDlg.CancelError = True On Error Resume Next oFileDlg.ShowOpen() If Err.Number <> 0 Then MessageBox.Show("File not chosen.", "Dialog Cancellation") Return ElseIf oFileDlg.FileName <> "" Then oFile = oFileDlg.FileName End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Set a reference to the transient geometry object. Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry ' Create a matrix. Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix 'place an instance of the component Dim oOccurrence As ComponentOccurrence oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) ' Set the translation portion of the matrix so the part will be 'positioned at the co-ordinates oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0)) 'Iterate through all of the occurrences and ground them. For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then oOccurrence.Grounded = False Else End If Next 'Get the plane from the selected part. ' The work plane obtained is in the context of the part, not the assembly. Dim oPartPlane1 As WorkPlane Dim oPartPlane2 As WorkPlane Dim oPartPlane3 As WorkPlane oPartPlane1 = oOccurrence.Definition.WorkPlanes.Item(1) 'Component YZ Plane oPartPlane2 = oOccurrence.Definition.WorkPlanes.Item(2) 'Component XZ Plane oPartPlane3 = oOccurrence.Definition.WorkPlanes.Item(3) 'Component XY Plane 'Get the plane from the assembly Dim oAsmPlane1 As WorkPlane Dim oAsmPlane2 As WorkPlane Dim oAsmPlane3 As WorkPlane oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(1) 'Assembly YZ Plane oAsmPlane2 = oAsmCompDef.WorkPlanes.Item(2) 'Assembly XZ Plane oAsmPlane3 = oAsmCompDef.WorkPlanes.Item(3) 'Assembly XY Plane ' Create proxies for the work planes of the part. ' The proxies represent the part work planes in the context of the assembly. Dim oAsmPlane4 As WorkPlaneProxy Dim oAsmPlane5 As WorkPlaneProxy Dim oAsmPlane6 As WorkPlaneProxy oOccurrence.CreateGeometryProxy(oPartPlane1, oAsmPlane4) oOccurrence.CreateGeometryProxy(oPartPlane2, oAsmPlane5) oOccurrence.CreateGeometryProxy(oPartPlane3, oAsmPlane6) '' Create the constraint using the part work plane proxies. oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane1, oAsmPlane4, 0) oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane2, oAsmPlane5, 0) oConstraint = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane3, oAsmPlane6, 0)
Hi,
i'm trying to edit the code provided here to make a MATE constraint for the Yaxis of assembly & Yaxis of part.
I tried this but my result is negative 🙂
'get file to place Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" oFileDlg.DialogTitle = "Select a File" oFileDlg.InitialDirectory = ThisDoc.Path oFileDlg.CancelError = True On Error Resume Next oFileDlg.ShowOpen() If Err.Number <> 0 Then MessageBox.Show("File not chosen.", "Dialog Cancellation") Return ElseIf oFileDlg.FileName <> "" Then oFile = oFileDlg.FileName End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Set a reference to the transient geometry object. Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry ' Create a matrix. Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix 'place an instance of the component Dim oOccurrence As ComponentOccurrence Dim Total As Integer Total = InputBox("Type How Many", "Insert", "1") If Err.Number <> 0 Then 'if there is an error it returns out Return ElseIf Err.Number = 0 Then 'if there is no error, it keeps working, else returns out For X = 1 To Total oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) ' Set the translation portion of the matrix so the part will be 'positioned at the co-ordinates oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0)) 'Get the plane from the selected part. ' The work plane obtained is in the context of the part, not the assembly. Dim oPartAxis1 As WorkAxis oPartAxis1 = oOccurrence.Definition.WorkAxis.Item(2) 'Component Y Axis Dim oAsmAxis1 As WorkAxis oAsmAxis1 = oAsmCompDef.WorkAxis.Item(2) 'Assembly Y Axis Dim oAsmAxis4 As WorkAxisProxy oOccurrence.CreateGeometryProxy(oPartAxis1, oAsmAxis4) oConstraint = oAsmCompDef.Constraints.AddMateConstraint(oAsmAxis1, oAsmAxis4, 0)
Any ideas?
I adjusted my code a little but still no succes.
Just aiming for the axis constraint here. and made some change in the defenition names.
'get file to place Dim oFileDlg As inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" oFileDlg.DialogTitle = "Select a File" oFileDlg.InitialDirectory = ThisDoc.Path oFileDlg.CancelError = True On Error Resume Next oFileDlg.ShowOpen() If Err.Number <> 0 Then MessageBox.Show("File not chosen.", "Dialog Cancellation") Return ElseIf oFileDlg.FileName <> "" Then oFile = oFileDlg.FileName End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Set a reference to the transient geometry object. Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry ' Create a matrix. Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix 'place an instance of the component Dim oOccurrence As ComponentOccurrence Dim Total As Integer Total = InputBox("Type How Many", "Insert", "1") If Err.Number <> 0 Then 'if there is an error it returns out Return ElseIf Err.Number = 0 Then 'if there is no error, it keeps working, else returns out For X = 1 To Total oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) ' Set the translation portion of the matrix so the part will be 'positioned at the co-ordinates oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0)) 'Get the plane from the selected part. ' The work plane obtained is in the context of the part, not the assembly. Dim oPartAxis1 As WorkAxis oPartAxis1 = oOccurrence.Definition.WorkAxes.Item(2) 'Component Y Axis 'Get the plane from the assembly Dim oAsmAxis1 As WorkAxis oAsmAxis1 = oAsmCompDef.WorkAxes.Item(2) 'Assembly Y Axis ' Create proxies for the work planes of the part. ' The proxies represent the part work planes in the context of the assembly. Dim oAsmAxis4 As WorkAxisProxy oOccurrence.CreateGeometryProxy(oPartAxis1, oAsmAxis4) ' Create the constraint using the part axis proxies. oConstraint = oAsmCompDef.Constraints.AddMateConstraint(oAsmAxis4, oPartAxis1, 0) Next End If
Hi Georg,
Our engineer checked the test code. She found I made a mistake. I still used part axis as the parameter of AddMateConstraint 😞
after modification, the code works well. I tested a lot and modified a lot… strange I made such stupid mistake. Could you check if it can work at your side?
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click Dim oInventorApplication As Inventor.Application Try oInventorApplication = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") Catch End Try Dim ThisDoc As Document = oInventorApplication.ActiveDocument 'get file to place Dim oFileDlg As Inventor.FileDialog = Nothing oInventorApplication.CreateFileDialog(oFileDlg) oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" oFileDlg.DialogTitle = "Select a File" oFileDlg.InitialDirectory = System.IO.Path.GetFullPath(ThisDoc.FullFileName) oFileDlg.CancelError = True oFileDlg.ShowOpen() Dim oFile As String If Err.Number <> 0 Then MessageBox.Show("File not chosen.", "Dialog Cancellation") Return ElseIf oFileDlg.FileName <> "" Then oFile = oFileDlg.FileName End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oInventorApplication.ActiveDocument.ComponentDefinition ' Set a reference to the transient geometry object. Dim oTG As TransientGeometry oTG = oInventorApplication.TransientGeometry ' Create a matrix. Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix 'place an instance of the component Dim oOccurrence As ComponentOccurrence Dim Total As Integer Total = InputBox("Type How Many", "Insert", "1") If Err.Number <> 0 Then 'if there is an error it returns out Return ElseIf Err.Number = 0 Then 'if there is no error, it keeps working, else returns out For X = 1 To Total oOccurrence = oAsmCompDef.Occurrences.Add(oFile, oMatrix) ' Set the translation portion of the matrix so the part will be 'positioned at the co-ordinates oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0)) 'Get the plane from the selected part. ' The work plane obtained is in the context of the part, not the assembly. Dim oPartAxis1 As WorkAxis oPartAxis1 = oOccurrence.Definition.WorkAxes.Item(2) 'Component Y Axis 'Get the plane from the assembly Dim oAsmAxis1 As WorkAxis oAsmAxis1 = oAsmCompDef.WorkAxes.Item(2) 'Assembly Y Axis ' Create proxies for the work planes of the part. ' The proxies represent the part work planes in the context of the assembly. Dim oObj As Object = Nothing oOccurrence.CreateGeometryProxy(oPartAxis1, oObj) Dim oAsmAxis4 As WorkAxisProxy oAsmAxis4 = oObj 'oNewAxis.Grounded = True Dim oConstraint As AssemblyConstraint ' Create the constraint using the part axis proxies. 'oConstraint = oAsmCompDef.Constraints.AddMateConstraint(oNewAxis, oPartAxis1, 0) oConstraint = oAsmCompDef.Constraints.AddMateConstraint(oAsmAxis1, oObj, 0) 'oNewAxis.Grounded = False Next End If End Sub