How to create a new component in an assembly template?

How to create a new component in an assembly template?

mikejones
Collaborator Collaborator
1,296 Views
8 Replies
Message 1 of 9

How to create a new component in an assembly template?

mikejones
Collaborator
Collaborator

Hi all and thanks in advance for any help given.

 

The problem that I am looking to solve is I'm sure quite simple. When creating a new assembly from a template file named "Hopper Fabrication.iam" I want to trigger the creation and placement of a new part from a template called "Master Hopper.ipt" and put it inside the new assembly.

 

So far I have the following

 

Dim oTemplate As String = "C:\Users\Public\Documents\Templates\Master Hopper.ipt"
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDocumentDefinition As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
oDocumentDefinition.Occurrences.Add(oTemplate,oMatrix)

This is placing the actual template in rather than creating a new part based on the template rather than creating a new in-place compoent based on the named template

Can anyone help me out please.

Mike

 

Autodesk Certified Professional
0 Likes
Accepted solutions (2)
1,297 Views
8 Replies
Replies (8)
Message 2 of 9

sam
Advocate
Advocate

Hi, 

Use code something like this creating a part using the defined template. 

Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, oTemplate)

 

Hope this helps. 

 

Best regards, 

sam

0 Likes
Message 3 of 9

sam
Advocate
Advocate

in your case while using vb.net/ilogic, this could work. 

 

Dim oPartDoc as PartDocument = ThisApplication.Documents.Add(kPartDocumentObject, oTemplate)

 

regards, 

sam

0 Likes
Message 4 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @mikejones 

The place new component command creates the new document invisible in Inventor and then places it into the assembly. It creates a document only in inventor memory, so no filepath exists meaning occurrences.add will not work.

You'll have to add by component definition. like this 🙂

Dim oTemplate As String = "C:\Users\Public\Documents\Templates\Master Hopper.ipt"
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDocumentDefinition As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Dim pDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, oTemplate, False)
oDocumentDefinition.Occurrences.AddByComponentDefinition(pDoc.ComponentDefinition, oMatrix)

 

Message 5 of 9

mikejones
Collaborator
Collaborator

Thanks for the reply @JhoelForshav 

 

I tried the code you suggested but it stops with the following error

 

Error in rule: New Hopper, in document: Hopper Fabrication.iam

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

And this from the More Info tab

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Documents.Add(DocumentTypeEnum DocumentType, String TemplateFileName, Boolean CreateVisible)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Any ideas?

 

Mike

Autodesk Certified Professional
0 Likes
Message 6 of 9

mikejones
Collaborator
Collaborator

Actually @JhoelForshav  you're code works fine, there was a typo in the file name that I put in for the template so thank you very much for your help on this.

 

Mike

Autodesk Certified Professional
0 Likes
Message 7 of 9

mikejones
Collaborator
Collaborator

Hi @JhoelForshav. The code you gave me works great but I am having a problem in getting the new part file to save for the first time. Is there anything that can be added in the code to make sure that both the iam and ipt files will save together ?

 

Mike

Autodesk Certified Professional
0 Likes
Message 8 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @mikejones 

The document will save with the assembly as long as it has a filename. Since you haven't specified a filename for it yet, Inventor wouldn't know where to save it. You can just add a filepath to the document:

Dim oTemplate As String = "C:\Users\Public\Documents\Templates\Master Hopper.ipt"
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDocumentDefinition As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Dim pDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, oTemplate, False)
pDoc.FullFileName = ThisDoc.Path & "\some name.ipt" 'Add a filepath
oDocumentDefinition.Occurrences.AddByComponentDefinition(pDoc.ComponentDefinition, oMatrix)
oDoc.Save

Of course, the assembly has to have a filepath aswell for the save command to work. But I'm assuming in this case it has already been saved at some time 🙂

If you want to use SaveAs on the assembly that would work too anyways, but then you cannot use ThisDoc.Path before as in the example above, becase if the assembly hasn't been saved ThisDoc.Path is nothing.

Message 9 of 9

tecnico
Contributor
Contributor

I would have a similar question, I have to make sure that when I create a new assembly it also creates a part called Skel, preferably which will then be saved in the same destination as the assembly.

0 Likes