Place components using iLogic

Place components using iLogic

andrew_canfield
Collaborator Collaborator
2,709 Views
4 Replies
Message 1 of 5

Place components using iLogic

andrew_canfield
Collaborator
Collaborator

After creating an assembly I'd like two sub-assemblies saving in the same location, one for the new design & one to include any reference parts required.

The two files have been created using 'save as', but placing these back into the assembly is a challenge - pasting VBA code into iLogic is returning a few errors. 

The forum contains a few examples but not in iLogic ( still searching).

is there code to 'place grounded at origin'?

ref.JPG

 

Regards

 

Andrew

0 Likes
Accepted solutions (1)
2,710 Views
4 Replies
Replies (4)
Message 2 of 5

JhoelForshav
Mentor
Mentor

Are you looking for something like this?

Dim asm As AssemblyDocument = ThisDoc.Document
Dim asmCompDef As AssemblyComponentDefinition = asm.ComponentDefinition
Dim oOcc = asmCompDef.Occurrences.Add("Path ex. C:\VaultWorkingFolder\Designs\test.iam", ThisApplication.TransientGeometry.CreateMatrix)
oOcc.Grounded = True

The occurrence gets grounded at origin because CreateMatrix creates an identity matrix 🙂

Message 3 of 5

andrew_canfield
Collaborator
Collaborator

thankyou - it works 🙂

 

but i've managed to break it too:

(added comment in code marked '***** below)

 

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument


xx=ThisDoc.Path
yy=ThisDoc.FileName(False) 'without extension
nn = xx &"\"&  yy
'nn =yy
nnM = nn & "_M.iam"
nnR = nn & "_R.iam"
'MessageBox.Show(nn, "ThisDoc.Path")
MessageBox.Show(nnM, "ThisDoc.Path")
'MessageBox.Show(nnR, "ThisDoc.Path")

ThisDoc.Document.SaveAs(nnM , True)
ThisDoc.Document.SaveAs(nnR , True)


Dim asm As AssemblyDocument = ThisDoc.Document
Dim asmCompDef As AssemblyComponentDefinition = asm.ComponentDefinition
Dim oOcc = asmCompDef.Occurrences.Add(nnM, ThisApplication.TransientGeometry.CreateMatrix)
oOcc.Grounded = True

'***** everything above this line is good & one sub assembly is placed in the assembly
'***** below a 2 suffix was added because errors were cropping up & still do. trying to place the second assy (nnR) Dim asm2 As AssemblyDocument = ThisDoc.Document Dim asmCompDef2 As AssemblyComponentDefinition = asm.ComponentDefinition Dim oOcc2 = asmCompDef.Occurrences.Add(nnR, ThisApplication.TransientGeometry.CreateMatrix) oOcc2.Grounded = True

 Regards

 

Andrew 

0 Likes
Message 4 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Did some cleaning up in the code. Try this. Works fine for me.

 

Dim oDoc As Document
oDoc = ThisDoc.Document
Dim asmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition

xx=ThisDoc.Path
yy=ThisDoc.FileName(False) 'without extension
nn = xx &"\"&  yy
'nn =yy
nnM = nn & "_M.iam"
nnR = nn & "_R.iam"

'''Remove old _M & _R
For Each item As ComponentOccurrence In asmCompDef.Occurrences
	If item.Name.Contains(yy) Then item.Delete
Next
'''----------------------------------------------------------------------------
'''Remove old _M & _R from inventor session, otherwise new ones can't be saved
ThisApplication.Documents.CloseAll(True)
'''----------------------------------------------------------------------------
If System.IO.File.Exists(nnM) Then System.IO.File.Delete(nnM) 'Delete old file
oDoc.SaveAs(nnM, True)
If System.IO.File.Exists(nnR) Then System.IO.File.Delete(nnR) 'Delete old file
oDoc.SaveAs(nnR, True)
Dim oIDMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
Dim oOcc = asmCompDef.Occurrences.Add(nnM, oIDMatrix)
oOcc.Grounded = True
Dim oOcc2 = asmCompDef.Occurrences.Add(nnR, oIDMatrix)
oOcc2.Grounded = True
Message 5 of 5

andrew_canfield
Collaborator
Collaborator

thankyou

0 Likes