Find the required occurrence name and rotate that part at 180 deg around Z axis

Find the required occurrence name and rotate that part at 180 deg around Z axis

2020mme013
Enthusiast Enthusiast
684 Views
5 Replies
Message 1 of 6

Find the required occurrence name and rotate that part at 180 deg around Z axis

2020mme013
Enthusiast
Enthusiast

Hi, 

I am trying to get the name of occurrence first from the open document assembly say name is "abc:S2". I want this part to be rotated at 180 degrees as per assembly's  xyz axis. Is it possible in vb.Net, please help.

Thanks in advance

Function(OccName as string)

\set the occName = "abc:S2"

\create a loop and get the occName

\Here will be the code for rotate the occName

End

0 Likes
Accepted solutions (1)
685 Views
5 Replies
Replies (5)
Message 2 of 6

2020mme013
Enthusiast
Enthusiast

This code is getting error, I am not able to search the desired name of the occurrence. Please any one help in this.

Sub Main()
RotateCoil("StdParts\Bv06Coil_N:S1")

End Sub


Sub RotateCoil(pointValue As String)

	'Dim Doc As XmlDocument
	Dim oAsmDoc As AssemblyDocument
	oAsmDoc = ThisApplication.ActiveDocument
	
	'Search for occurence name
	Dim oOccurence As ComponentOccurrence= FindOccByName(oAsmDoc.ComponentDefinition.Occurrences, pointValue)
'	For Each oOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
'		If oOcc.Name=("StdParts\Bv06Coil_N:S2" )Then
'			oOccurence = oOcc
'			Exit For
'		End If
'	Next
	
	'Check if Occurrence was not found
	If oOccurence Is Nothing Then
		MessageBox.Show("Occurrence with the given name was not found")
		Exit Sub
	End If
	
	'Count the number of sections
	Dim sectionCount As Integer = CountSections(oOccurence)

	'Define the rotation matrix
	Dim oMatrix As Matrix
	oMatrix = ThisApplication.TransientGeometry.CreateMatrix
	oMatrix.SetToRotation(Math.PI, ThisApplication.TransientGeometry.CreateVector(0,0,1),ThisApplication.TransientGeometry.CreatePoint(0,0,0))
	'Perform the rotation based on the section
	If sectionCount > 3 Then
		'Rotate 2 scetion Coil
		Dim oPart2Occ As ComponentOccurrence = FindOccByName(oOccurence.Definition.Occurrences, "StdParts\Bv06Coil_N:S2")
'		For Each comp As ComponentOccurrence In oOccurence.Defination.Occurrences
'			If comp.Name ="StdParts\Bv06Coil_N:S2" Then
'			oPart2Occ = comp
'			Exit For
'	        End If
'	   Next
		If Not oPart2Occ Is Nothing Then
			oPart2Occ.Transformation = oMatrix
		Else
			MessageBox.Show("BV06Coil:S2 not found within the occurrence")
		End If
	Else
		'Rotate the 1 section coil
		Dim oPart1Occ As ComponentOccurrence = FindOccByName(oOccurence.SubOccurrences, "StdParts\Bv06Coil_N:S1")
'		For Each comp As ComponentOccurrence In oOccurence.Defination.Occurrences
'			If comp.Name ="StdParts\Bv06Coil_N:S2" Then
'			oPart1Occ = comp
'			Exit For
'	        End If
'	   Next
		If Not oPart1Occ Is Nothing Then
			oPart1Occ.Transformation = oMatrix
		Else
			MessageBox.Show("BV06Coil:S1 not found within the occurrence")
		End If
	End If	
End Sub

Function FindOccByName(occurrences As ComponentOccurrences, name As String) As ComponentOccurrence
For Each occ As ComponentOccurrence In occurrences
	If occ.Name = name Then
		Return occ
	End If	
	'Recursively search innested Occurrences
	Dim foundOcc As ComponentOccurrence = FindOccByName(occ.SubOccurrences, name)
	If Not foundOcc Is Nothing Then
		Return foundOcc
	End If
	Next
	Return Nothing


End Function
Function CountSections(occurrence As ComponentOccurrence) As Integer
	Dim count As Integer = 0
	For Each comp As ComponentOccurrence In occurrence.SubOccurrences
		If comp.Name.Contains("Bodies") Then
			count += 1
		End If
	Next
	Return count
End Function	
	
0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @2020mme013.  You may be able to rotate a top level assembly component that is not already constrained in any way, but you will most likely not be able to rotate a lower level component, and especially not based on the main assembly's origin work axis.  This is because the two are defined within different 'context' (3D model space).  In order to rotate a part that is within a sub assembly, you would have to be in edit mode of that sub assembly first, then use the origin work axis of that sub assembly as the rotation axis, instead of the axis of the main assembly.  Below is a short example of getting a top level component by its name, then rotating it 180 degrees around the Z-Axis of the main assembly.

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oOcc As ComponentOccurrence = oADef.Occurrences.ItemByName("Part3:2")
Dim oXAxis As WorkAxis = oADef.WorkAxes.Item("Z Axis")
Dim oLine As Line = oXAxis.Line
Dim oOrigMatrix As Matrix = oOcc.Transformation
Dim oNewMatrix As Matrix = oTG.CreateMatrix
oNewMatrix.SetToRotation((Math.PI), oLine.Direction.AsVector, oLine.RootPoint)
oOrigMatrix.PreMultiplyBy(oNewMatrix)
oOcc.Transformation = oOrigMatrix

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

2020mme013
Enthusiast
Enthusiast

Hi @WCrihfield, This is perfectly working. Thank you so much for. but how to set the vector that part is translating at (0,0,0) position of the assembly. Can we do something like it should not move from its place and should be check the condition as counting no. of sections if more than 3 sections then second coil should rotate else first coil should rotate, have added the screenshot.

 

Thank you so much

 

0 Likes
Message 5 of 6

2020mme013
Enthusiast
Enthusiast

Hi @WCrihfield

 

Can I use the matrix instead of the point in my code. the matrix which you have created, can I pass that one in the AddToAssy.... line. so that I could rotate the part before placing it at particular co-ordinate system. could you please add both the code. here I am pasting it.

Thanks

   Dim AddToAssy As ManagedComponentOccurrence
	Dim IsGrounded As Boolean = True
	Dim oPNT As DocumentUnitsPoint
	Dim oPath As String
	Dim oPath1 As String
	Dim PartName As String	
	Dim PortPartName As String	
	oPNT=ThisAssembly.Geometry.Point(SharedVariable("	OffsetX1"),SharedVariable("OffsetY1"), 	SharedVariable("OffsetZ1"))
	PortPartName = "StdParts\Bv06Coil_N"
	oPath1 = ThisDoc.Path & "\Parts\"
	   AddToAssy = Components.Add(PortPartName & ":S" & NoOfSection, oPath1 & PortPartName & ".ipt", oPNT, IsGrounded)
0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Hi @2020mme013.  I honestly do not use the iLogic only IManagedComponents.Add method that much myself.  However, that method uses different types of input geometry than what the normal Inventor API methods would use.  It is asking for something called a PointOrMatrix (an Interface), which sounds like a type that can contain either a DocumentUnitsPoint object, or a DocumentUnitsMatrix object.  My code example above included the use of two normal Matrix objects (defined within Inventor's API, not just within the iLogic add-in).  I am not familiar with exactly where you want to insert the new component, or what orientation it needs to be in, but yes, it does look like you could create a DocumentUnitsMatrix object ahead of time (on an earlier row), then specify the variable for that object in that location of the Add method.  When we look at the help page for the IGeometry object (from ThisAssembly.Geometry), it looks like there are 6 different ways to initially create a new DocumentUnitsMatrix, including one where no input is required.  So, that should make it relatively easy to specify not only where you want to insert it, but in what orientation you want it to be.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes