Create new part in Assembly with a specifice Template using iLogic

Create new part in Assembly with a specifice Template using iLogic

S_Verbeek
Advocate Advocate
3,139 Views
12 Replies
Message 1 of 13

Create new part in Assembly with a specifice Template using iLogic

S_Verbeek
Advocate
Advocate

 

Hi,

i am looking for a way to create a new part in my assembly with a specifice template.

This code create a new part but only with the Standard.ipt.

 

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, "MyPart.ipt")

ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyCreateComponentCmd").Execute()

 

Does anyone have experience with this or knows of an alternative solution?

 

Thanks!

 

Stefan

__________________________________________________________________________________

Stefan Verbeek
Mitarbeiter Support bei

Bild



0 Likes
Accepted solutions (2)
3,140 Views
12 Replies
Replies (12)
Message 2 of 13

Rob67ert
Collaborator
Collaborator
Accepted solution

Hi,

 

I have used the information from this: http://adndevblog.typepad.com/manufacturing/2012/07/custom-create-in-place-component.html

 

Robert

If you find this reply helpful ? It would be nice if you use the Accept as Solution or Kudos button below.
0 Likes
Message 3 of 13

Anonymous
Not applicable

Hi Stefan,

 

in an iLogic rule you can use this:

 

dim oPartDoc As inventor.PartDocument
oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, "T:\Inventor 2017\Templates\Part.ipt", True)

'Activate the new  PartFile

oPartDoc.Activate()

 

Where "T:\Inventor 2017\Templates\Part.ipt" is the place where your templates are. Change this in the code

 

regards,

 

Geert

Message 4 of 13

JBerns
Advisor
Advisor

Be advised, the above code creates a new part document.

The OP wanted to create a new part within an existing assembly.

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 13

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@JBerns, see this example in case it helps

 

Dim oPartDoc As Inventor.PartDocument
oPartDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, "C:\Temp\Templates\Best Template Ever.ipt", False)
oPartDoc.SaveAs("C:\Temp\Project99\Best Part Ever.ipt",False)

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix

' Add the part to the assembly
Dim oOcc As ComponentOccurrence
oOcc = oAsmCompDef.Occurrences.Add(oPartDoc.FullFileName, oMatrix)

EESignature

Message 6 of 13

JBerns
Advisor
Advisor

@Curtis_Waguespack 

 

Thanks, Curtis! Great code example!

 

Currently, my workflow is as follows:

  • Create new assembly from a template.
  • A rule automatically runs that places template parts into the new assembly.
  • A rule opens an iLogic form.
  • User inputs configuration options.
  • In iLogic form, user clicks "Create" button.
  • SaveAs operations are performed to save the assembly and the parts to the job number specified in form.

This works well provided the user does not close the form and then clicks the Save button.
The workflow is not ideal because the scenario above would alter the part templates.

 

My goal:

  • Create new assembly from template
  • Create new parts within assembly based on part templates
  • Place part occurrences
  • Open form
  • Configure
  • User clicks "Create" button.
  • "SaveAs" operations performed on assembly and parts based on iLogic form job number value.

If user closes form and clicks Save, normal prompting for filenames would occur.
This would protect templates from inadvertent saves.

 

This workflow requires less file/folder protection methods.

 

I like the ability for the user to see the components on screen as they make configuration changes.
I just didn't want to establish filenames before the user input the job number.

 

Is this a practical approach?

 

Regards,
Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 7 of 13

Curtis_Waguespack
Consultant
Consultant

@JBerns, that sounds feasible, and I like the idea.

 

The downside might be that for large/complex assemblies, the code to manage file names, component names, and assembly constraints might get get pretty involved... but I don't see it as that big of an obstacle.

Just in case you overlooked it, Autodesk recently improved the iLogic Design Copy tool to include a string replace( vs the prefix/suffix replace only), so that it is more real world for this type of task.

 

Basically the workflow would be like this:

Create the assembly template with the components assembled. Where the naming is something like this:

XXXX-000.iam

            |_   XXXX-001.ipt

            |_   XXXX-002.ipt

            |_   XXXX-003.ipt

Then use iLogic Design Copy to copy that design, and use the string replace to rename the new file from XXXX to the new job number.

6789-000.iam

            |_  6789-001.ipt

            |_  6789-002.ipt

            |_  6789-003.ipt

 

Then the user configures the 6789 files as needed, with no worries about stepping on the templates.

 

See workflow 2 as described here:

https://help.autodesk.com/view/INVNTOR/2025/ENU/?guid=GUID-0BE1A21F-E2B4-4442-A4B9-F48C26210795



EESignature

Message 8 of 13

WCrihfield
Mentor
Mentor

Hi guys.  The large scale design copy type scenarios are not something that I need to use for work related purposes, so I am not super familiar with every small detail of it.  However, when you are talking about possibly saving over your templates, or your templates somehow becoming modified by your processes, something comes to mind that you may be able to utilize.  Instead of opening your template files, then using SaveAs on them to create new files, why not use your template files like real template files, and use Documents.Add (same as New, manually) then specify the template file as the template.  This can be done even if those files are not in the 'normal/default' templates folder of your project?  That way there is no risk of your original template files ever getting modified.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 13

JBerns
Advisor
Advisor

@WCrihfield 

 

Wes,

I tried to accomplish what you describe, but had difficulties getting a new part from template inside the new assembly. I think the required part filename was where I struggled. How to name the file temporarily when the user has not input a job number yet?

It is encouraging to learn that it is possible.

I am going to revisit this task. Please don't post code just yet, I hope to learn (fail-succeed) before getting free code.

I will post back my success or request for more assistance.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 10 of 13

JBerns
Advisor
Advisor

@WCrihfield 

Wes,

How is 'Documents.Add' different from 'Components.Add'? Is it API code versus iLogic shortcuts?

 

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")

 

I got the code working and advised my team NOT to use Save. Only use the 'Create' button on the form which uses SaveAs. I planned to improve the workflow later. The opportunity is here.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 11 of 13

WCrihfield
Mentor
Mentor

I assumed that your overall code process would be creating new files based on existing template files, but that you do not want the existing template files to be modified in any way by the process.  If that was the case, then I assumed that you would be creating the new files 'before' placing them into the assembly, as assembly components.  If so, using Documents.Add and specifying one of the original template files as the template to base this new document on, then saving that new document to a file (even with a meaningful, but temporary file name, if necessary) would be preferable to using Documents.Open to open the original template part, then using SaveAs to create the new file.  If you are placing the original template parts directly into the assembly first, then somehow creating copies of all of those files later, depending on what all you may be doing to/with those assembly components (or referenced documents/files) before making those new copies (using SaveAs), that just sounds a little risky.  But I am not super familiar with the details of what all is going on within your project code, so I may just be throwing a wrench in there, not sure.  I'm headed out for the day though, so good luck.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 13

Curtis_Waguespack
Consultant
Consultant

@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")

 

EESignature

0 Likes
Message 13 of 13

Curtis_Waguespack
Consultant
Consultant

The other way that came to mind is to use a temporary/staging folder to save the files to. And then have another routine that to save the files to their final folder, once the assembly contains the job information.

 

Initial rule that runs on the "New Document" event

cTempPath = "C:\Temp\iLogic Staging\"
oNewPartName = cTempPath & "XXXX-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")

 

secondary rule that runs after the assembly has been saved in place, etc

oProjectNumber = iProperties.Value("Project", "Project") 'get project number from the assembly

For Each oDoc As Document In ThisDoc.Document.AllReferencedDocuments
	oName = oDoc.FullFileName
	oName = Replace(oName, "XXXX", oProjectNumber)
	oDoc.SaveAs(oName, False)
Next

 

EESignature

0 Likes