Set Orientation of placed component in inventor assembly

Set Orientation of placed component in inventor assembly

nkambliV2APR
Community Visitor Community Visitor
177 Views
0 Replies
Message 1 of 1

Set Orientation of placed component in inventor assembly

nkambliV2APR
Community Visitor
Community Visitor

I need orientation VBA programming for placed component in available inventor assembly.

 

I have to place component such as gearbox, mounting bracket and handwheel in valve assembly which is already open in inventor. I need a help in programming for placing components with desired orientation with x,y,z coordinates,

Currently components are placed randomly wit no desired orientation.

 

Final conclusion I required is, placing all the components is valve assembly with desired orientation which I needed.

 

I have also given code below.

 

Public Sub AddOccurrenceWithSpecificCoordinates()
' Set a reference to the assembly component definition.
' 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

Dim excelapp As Excel.Application
Set excelapp = CreateObject("excel.application")

Dim Workbook As Workbook
Set Workbook = excelapp.Workbooks.Open("C:\Vault-Delovna\Designs\Nacrti\Pipa\TUF-Seat GAD Configurator.xlsm") ' Give location of excel file

excelapp.Visible = True

Dim worksheet As worksheet ' Set to decoder tab
Set worksheet = Workbook.Sheets.Item("Decoder")

' Get the cell value for Mounting Kit
Dim cellValue1 As Variant
cellValue1 = worksheet.Range("E5").Value

' Get the desired coordinates for Mounting Kit
Dim x1 As Double
Dim y1 As Double
Dim z1 As Double
x1 = -20 ' Example: Set the desired X coordinate for Mounting Kit
y1 = 25 ' Example: Set the desired Y coordinate for Mounting Kit
z1 = 30 ' Example: Set the desired Z coordinate for Mounting Kit

' Create a matrix for Mounting Kit occurrence
Dim oMatrixMountingKit As Matrix
Set oMatrixMountingKit = oTG.CreateMatrix
oMatrixMountingKit.SetTranslation oTG.CreateVector(x1, y1, z1)

' Add Mounting Kit component occurrence
Dim oOccMountingKit As ComponentOccurrence
Set oOccMountingKit = oAsmCompDef.Occurrences.Add("C:\Vault-Delovna\Designs\Nacrti\Pipa\Mounting kits\" & cellValue1 & ".iam", oMatrixMountingKit)

' Continue with the same approach for other components...
' Get the cell value for Gear box
Dim cellValue2 As Variant
cellValue2 = worksheet.Range("E6").Value

' Get the desired coordinates for Gear box
Dim x2 As Double
Dim y2 As Double
Dim z2 As Double
x2 = 0 ' Example: Set the desired X coordinate for Gear box
y2 = 40 ' Example: Set the desired Y coordinate for Gear box
z2 = -30 ' Example: Set the desired Z coordinate for Gear box

' Create a matrix for Gear box occurrence
Dim oMatrixGearBox As Matrix
Set oMatrixGearBox = oTG.CreateMatrix
oMatrixGearBox.SetTranslation oTG.CreateVector(x2, y2, z2)

' Add Gear box component occurrence
Dim oOccGearBox As ComponentOccurrence
Set oOccGearBox = oAsmCompDef.Occurrences.Add("C:\Vault-Delovna\Designs\Nacrti\Pipa\Rotork\" & cellValue2 & ".ipt", oMatrixGearBox)

' Get the desired rotation angles for Mounting Kit
Dim rotationX1 As Double
Dim rotationY1 As Double
Dim rotationZ1 As Double
rotationX1 = 0 ' Example: Set the desired rotation angle around X-axis for Mounting Kit (in degrees)
rotationY1 = 45 ' Example: Set the desired rotation angle around Y-axis for Mounting Kit (in degrees)
rotationZ1 = 0 ' Example: Set the desired rotation angle around Z-axis for Mounting Kit (in degrees)

' Create a rotation matrix for Mounting Kit occurrence
Dim oMatrixRotation1 As Matrix
Set oMatrixRotation1 = oTG.CreateMatrix
oMatrixRotation1.SetToRotation oTG.CreatePoint, oTG.CreateVector(1, 0, 0), rotationX1 * (3.14159 / 180) ' Convert degrees to radians for X-axis rotation
oMatrixRotation1.SetToRotation oMatrixRotation1.ColumnVector(1), oTG.CreateVector(0, 1, 0), rotationY1 * (3.14159 / 180) ' Convert degrees to radians for Y-axis rotation
oMatrixRotation1.SetToRotation oMatrixRotation1.ColumnVector(1), oMatrixRotation1.ColumnVector(2), rotationZ1 * (3.14159 / 180) ' Convert degrees to radians for Z-axis rotation

' Combine the translation and rotation matrices for Mounting Kit occurrence
Dim oMatrixMountingKit As Matrix
Set oMatrixMountingKit = oTG.CreateMatrix
oMatrixMountingKit.SetTranslation oTG.CreateVector(x1, y1, z1)
oMatrixMountingKit.TransformBy oMatrixRotation1

' Add Mounting Kit component occurrence with the combined transformation
Dim oOccMountingKit As ComponentOccurrence
Set oOccMountingKit = oAsmCompDef.Occurrences.Add("C:\Vault-Delovna\Designs\Nacrti\Pipa\Rotork\" & cellValue2 & ".ipt", oMatrixGearBox)

' Get the cell value for Handwheel
Dim cellValue3 As Variant
cellValue3 = worksheet.Range("E7").Value

' Get the desired coordinates for Handwheel
Dim x3 As Double
Dim y3 As Double
Dim z3 As Double
x3 = 20 ' Example: Set the desired X coordinate for Gear box
y3 = 30 ' Example: Set the desired Y coordinate for Gear box
z3 = 30 ' Example: Set the desired Z coordinate for Gear box

' Create a matrix for Handwheel occurrence
Dim oMatrixHandwheel As Matrix
Set oMatrixHandwheel = oTG.CreateMatrix
oMatrixHandwheel.SetTranslation oTG.CreateVector(x3, y3, z3)

' Add Handwheel component occurrence
Dim oOccHandwheel As ComponentOccurrence
Set oOccHandwheel = oAsmCompDef.Occurrences.Add("C:\Vault-Delovna\Designs\Nacrti\Pipa\Handwheels\" & cellValue3 & ".ipt", oMatrixHandwheel)

'--------------------------------------------------------------------------------------------------------------------------------------------

End Sub

0 Likes
178 Views
0 Replies
Replies (0)