How to place a ipart from an exist ipt file by iLogic code?

2208571753
Enthusiast

How to place a ipart from an exist ipt file by iLogic code?

2208571753
Enthusiast
Enthusiast

I want to place a exist ipt file into the active assemble document.

How can i do this by iLogic code?

2208571753_0-1656992505993.png

2208571753_1-1656992571909.png

 

0 Likes
Reply
Accepted solutions (1)
833 Views
11 Replies
Replies (11)

A.Acheson
Mentor
Mentor

Here is the API help sample for regular part.

The sample is written in VBA. To convert to ilogic/VB.NET environment

  1. Remove the word set.
  2. Change the sub routine name to Sub Main. 

To add an ipart use this modification of the sample. Where iRow is the member row number. 

oOcc = oOccs.AddiPartMember("C:\temp\iPartFactory.ipt ", oPos, iRow)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

A.Acheson
Mentor
Mentor

Alternatively you can use pure ilogic snippets to add iparts. Simply place a part manually then use capture snippets and  component.Add to capture the parts location and occurrence name and any constraints. You can then use the ipart change row

to work with other members. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

2208571753
Enthusiast
Enthusiast

2208571753_0-1657006974924.png

If oCompDef.IsiPartFactory = False Then
MsgBox("Chosen document is not a factory.")
Exit Sub
End If

 

the ipart must be a factory? What's the meaning?

0 Likes

A.Acheson
Mentor
Mentor

The ipartfactory is the factory file containing the excel table that the members are derived from. If your just adding an ipart you likely don't need to work  directly with the factory document. Just supplying the filename and member number would be enough. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

2208571753
Enthusiast
Enthusiast

20220705154707.png

Method1:Dim asmDef As AssemblyComponentDefinition= asmDoc.ComponentDefinition

asmDef.Occurrences.Add(memberFilename, transMatrix)

Method2:

dim oPartFile as string="C:\temp\123.ipt"

Dim oPartDoc As PartDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, oPartFile, True)

 

These two method all opened the ipart file in a new window. This is not what i want.

 

I want to place the opened ipart file in the assembly window, just like the part circled by red line, like the screeshot below.

2208571753_0-1657074292140.png

 

0 Likes

A.Acheson
Mentor
Mentor
Accepted solution

Here is the minimum required  for adding occurrence to the active assembly. 

  ' Set a reference to the assembly component definintion.
    ' This assumes an assembly document is open.
    Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition

    ' Set a reference to the transient geometry object.
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

    ' Create a matrix.  A new matrix is initialized with an identity matrix.
    Dim oMatrix As Matrix = oTG.CreateMatrix

    ' Add the occurrence.
    'Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.Add("C:\1234-M-001.ipt", oMatrix)'Add standard part
	Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.AddiPartMember("C:\ipart factory.ipt", oMatrix,1) 'Add ipart member

AAcheson_0-1657085681200.png

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

2208571753
Enthusiast
Enthusiast
Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.Add("C:\1234-M-001.ipt", oMatrix)'Add standard part

I try to run your code, but it still can't place the ipart into the opened assembly. It only opened the ipart file in a new window.

Just like the screeshot below. Maybe the problem is the Inventor version. I use the Inventor professional 2018, maybe this version can't do this action?

2208571753_0-1657165930041.png

 

0 Likes

A.Acheson
Mentor
Mentor

Have you tried placing the ipart factory and specifying the member?

Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.AddiPartMember("C:\ipart factory.ipt", oMatrix,1) 'Add ipart member from factory

The 2018 help file suggest there is no change.

 

Cam you also try a regular part note the differences in syntax. Can you post back the code your using it is displaying strange behavior. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

2208571753
Enthusiast
Enthusiast
Public Sub AddOccurrence()
    ' Set a reference to the assembly component definintion.
    ' This assumes an assembly document is open.
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Set a reference to the transient geometry object.
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry

    ' Create a matrix.  A new matrix is initialized with an identity matrix.
    Dim oMatrix As Matrix
    Set oMatrix = oTG.CreateMatrix

    ' Set the rotation of the matrix for a 45 degree rotation about the Z axis.
    Call oMatrix.SetToRotation(3.14159265358979 / 4, _
                            oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))

    ' Set the translation portion of the matrix so the part will be positioned
    ' at (3,2,1).
    Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

    ' Add the occurrence.
    Dim oOcc As ComponentOccurrence
    Set oOcc = oAsmCompDef.Occurrences.Add("C:\Temp\Part1.ipt", oMatrix)
End Sub
0 Likes

A.Acheson
Mentor
Mentor

The above code is VBA version have you tried to run that also? Can you record a video of the code being run? 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

2208571753
Enthusiast
Enthusiast
Thanks for your help,I made a code error.When i corrected it, it works!
I have another question,can you take a look?
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/how-to-add-a-button-in-bill-of-mater...
0 Likes