@JBerns wrote:
This is the code I am using. I wanted to create a part from a template, but instead it places the template in the assembly.
'' Body
oTempCompName = oTemplatePath1 & "Tail_Bearing_Guard-Body.ipt"
Dim Tail_Bearing_Body = Components.Add("Tail_Bearing_Guard-Body", oTempCompName)
Constraints.AddMate("Mate:1", "", "YZ Plane", "Tail_Bearing_Guard-Body", "XZ Plane")
Constraints.AddFlush("Flush:1", "", "XZ Plane", "Tail_Bearing_Guard-Body", "YZ Plane")
Constraints.AddFlush("Flush:2", "", "XY Plane", "Tail_Bearing_Guard-Body", "XY Plane")
Sticking with what you have, you could catch the unsaved assembly, and then force the user to save it. That would allow you to glean some info from the assembly and do an in-step save-as of the template part being placed.
Here's a quick example where I just saved the part to the same folder as the assembly and used the assembly name with a suffix for the part name. If it exists, I just replace the template with it.
If ThisDoc.Document.FileSaveCounter = 0 Then
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
oFileDlg.CancelError = True
Try
oFileDlg.ShowSave()
Catch
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
Exit Sub
End Try
If oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
ThisDoc.Document.SaveAs(MyFile, False)
End If
End If
oThisAssembly_Folder = IO.Path.GetDirectoryName(ThisDoc.Document.FullFileName)
oThisAssembly_Name = IO.Path.GetFileNameWithoutExtension(ThisDoc.Document.FullFileName)
oNewPartName = oThisAssembly_Folder & "\" & oThisAssembly_Name & "-01.ipt"
oTempCompName = oTemplatePath1 & "Tail_Bearing_Guard-Body.ipt"
Dim oComp = Components.Add("Tail_Bearing_Guard-Body", oTempCompName)
Dim oDoc As Document = oComp.Occurrence.Definition.Document
If IO.Path.GetFullPath(oDoc.FullFileName).Contains(oTemplatePath1) Then
Try
oDoc.SaveAs(oNewPartName, False)
Catch
Component.Replace(oComp.Name, oNewPartName, True)
End Try
End If
Constraints.AddMate("", "", "YZ Plane", oComp.Name, "XZ Plane")
Constraints.AddFlush("", "", "XZ Plane", oComp.Name, "YZ Plane")
Constraints.AddFlush("", "", "XY Plane", oComp.Name, "XY Plane")
