Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Insert component at specific co-ordinates with iLogic

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
waynehelley
2536 Views, 8 Replies

Insert component at specific co-ordinates with iLogic

Hi all,

 

I'm not sure if this has been asked before (I may have asked before!) but is it possible to use iLogic to insert a component at specific co-ordinates...

 

I have been using iLogic to insert using iMates but they can be very tedious to update if things need to change location.

 

Ideally I would just like to insert a component at specific co-ordinates and ground it.  Assume I have already orientated the component correctly.

 

Failing this...

 

I could create a bunch of 'Points' in the assembly environment, give them appropriate names, would it then be possible to constrain components to these points??

 

Please help!

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
8 REPLIES 8
Message 2 of 9

Hi Wayne,

I'm not sure of understanding your requirements but the code included in the attached file may help you with your final solution...

 

I also publish the code here:

 

Sub Main
	AddWP(100, 10, 20, "Test")
	AddComponent(100, 100, 0, ThisDoc.Path & "\Parte2.ipt") 'change the partname as you like
	MsgBox("Now I move the Component")
	MoveComponent(200, 300, 500, ComponentByName("Sfera"))
End Sub

Private Sub AddWP(x As Double, y As Double, z As Double, oName As String) ' this routine work on the activedocument
    ' Create a TransientGeometry
	oTG = ThisApplication.TransientGeometry
    ' Create 3 workpoints to define the origin, x-direction and y-direction points
    oWorkPoint = ThisApplication.ActiveDocument.ComponentDefinition.WorkPoints.AddFixed(oTG.CreatePoint(x,y,z))
	oWorkPoint.Name = oName
End Sub

Private Sub AddComponent(X As Double, Y As Double, Z As Double, oPath As String)
	If System.IO.File.Exists(oPath) Then 
		' Create a TransientGeometry
		oTG = ThisApplication.TransientGeometry
		oMatrix = oTG.CreateMatrix
		oMatrix.SetTranslation(oTG.CreateVector(X, Y, Z)) 
		' Add the occurrence
		oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Add(oPath, oMatrix)
		oOccurrence.Grounded = False ' True to ground the component
		oOccurrence.Name = "Sfera" ' if you like to rename the occurence you can add the new name as parameter of this Sub
	End If
End Sub

Private Sub MoveComponent(X As Double, Y As Double, Z As Double, oOccurrence As ComponentOccurrence)
	If oOccurrence IsNot Nothing Then
		' Get the current transformation matrix from the occurrence.
		Dim oTransform As Matrix
		oTransform = oOccurrence.Transformation
		
		' Move the occurrence honoring any existing constraints.
		'oTransform.SetTranslation ThisApplication.TransientGeometry.CreateVector(oNOri.Point.X, oNOri.Point.Y, oNOri.Point.Z)
		'oOccurrence.Transformation = oTransform
		
		' Move the occurrence ignoring any constraints.
		' Anything that causes the assembly to recompute will cause the
		' occurrence to reposition itself to honor the constraints.
		oTransform.SetTranslation(ThisApplication.TransientGeometry.CreateVector(X, Y, Z))
		Call oOccurrence.SetTransformWithoutConstraints(oTransform)
	End If
End Sub

Private Function ComponentByName(oCname As String) As ComponentOccurrence
	ComponentByName = Nothing
	For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
		If oOcc.Name = oCname Then
			ComponentByName = oOcc
			Exit Function
		End If
	Next
End Function

 Bregs

Rossano Praderi

 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 3 of 9

Thanks very much... that is exactly what I needed!

 

My only issue now is that i have a few components that were modelled backwards so it would be great to be able to rotate when using the 'AddComponent' sub

 

Private Sub AddComponent(X As Double, Y As Double, Z As Double, oPath As String)
	If System.IO.File.Exists(oPath) Then 
		' Create a TransientGeometry
		oTG = ThisApplication.TransientGeometry
		oMatrix = oTG.CreateMatrix
		oMatrix.SetTranslation(oTG.CreateVector(X, Y, Z)) 
		' Add the occurrence
		oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Add(oPath, oMatrix)

'Insert code here which would rotate the component by 180 degrees around its own Y-axis

oOccurrence.Grounded = True ' oOccurrence.Name = "Sfera" ' if you like to rename the occurence you can add the new name as parameter of this Sub End If End Sub
Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 4 of 9

Hi Wayne,

I've made few modifications on my posted example, if want to rotate the component of 180° on insertion, this can be a solution.

 

Public Sub AddComponent(X As Double, Y As Double, Z As Double, oPath As String)
	If System.IO.File.Exists(oPath) Then 
		Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
		Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
		Dim oMatrix As Matrix = oTG.CreateMatrix
	
		Call oMatrix.SetToRotation(3.14159265358979, oTG.CreateVector(0, 1, 0), oTG.CreatePoint(X / 10, Y / 10, Z / 10)) ' Rotate of 180° on axis Y
		Call oMatrix.SetTranslation(oTG.CreateVector(X / 10, Y / 10, Z / 10))
		
		Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.Add(oPath, oMatrix)
	End If
End Sub

 

For your information, you will find more details about occurrences positioning.

http://forums.autodesk.com/t5/inventor-customization/settorotation-question/td-p/2488976

http://forums.autodesk.com/t5/inventor-general-discussion/place-component-automatically-to-the-xz-pl...

 

This is a link to a very useful guide

http://modthemachine.typepad.com/files/mathgeometry.pdf

 

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 5 of 9
Frodl
in reply to: rossano_praderi

Hi,

this is a great code.

Is it also possible to rotate one and the same component first around the y-axis (as in your code), second around the x-axis and last around the z-axis?

I tried but did not get it.

Is it possible do rotate around all the axis at once or must this happend step by step?

Thank you for your help.

Message 6 of 9
rossano_praderi
in reply to: Frodl

Hi,

yes you can do many changes at once by changing the matrix object, as explained in the follow link (already shared in my last post).

 

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 7 of 9
Frodl
in reply to: rossano_praderi

Hi,

thank you for the answer. The document in the link I've read before. But I've some problems to get the necessary code.

Meanwhile I've found the solution:

 

		Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
		Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
		

		' Erzeugen einer Matrix zur Rotation der Tübbingringe - Drehung um die z-Achse um den 180° wegen umgekehrter Orientierung Tübbingmodell
		Dim oTrans_Matrix As Matrix = oTG.CreateMatrix
		Call oTrans_Matrix.SetToRotation(PI, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(X, Y, Z)) ' Rotation um Y-Achse
		
		' Erzeugen einer Matrix zur Rotation um die y-Achse um den Winkel roty
		Dim oMatrix_y As Matrix = oTG.CreateMatrix
		Call oMatrix_y.SetToRotation(PI/180*roty, oTG.CreateVector(0, 1, 0), oTG.CreatePoint(X, Y, Z)) ' Rotation um Y-Achse
		Call oTrans_Matrix.TransformBy(oMatrix_y)
		
		' Erzeugen einer Matrix zur Rotation um die z-Achse um den Winkel rotz und hinzufügen zur vorherigen Matrix
		Dim oMatrix_z As Matrix = oTG.CreateMatrix
		Call oMatrix_z.SetToRotation(PI/180*rotz, oTG.CreateVector(0 , 0 , 1), oTG.CreatePoint(X , Y , Z )) ' Rotation um Z-Achse
		Call oTrans_Matrix.TransformBy(oMatrix_z)
		
		' Erzeugen einer Matrix zur Rotation um die x-Achse um den Winkel rotz und hinzufügen zur vorherigen Matrix
		Dim oMatrix_x As Matrix = oTG.CreateMatrix
		Call oMatrix_x.SetToRotation(PI/180*rotx, oTG.CreateVector(1 , 0 , 0), oTG.CreatePoint(X , Y , Z )) ' Rotation um X-Achse
		Call oTrans_Matrix.TransformBy(oMatrix_x)
		
		
		Call oTrans_Matrix.SetTranslation(oTG.CreateVector(X, Y, Z))
		
		Dim oOcc As ComponentOccurrence = oAsmCompDef.Occurrences.Add(oPath, oTrans_Matrix)
		oOcc.Grounded = True ' "True" zum Fixieren des Bautels, "False" für keine Fixierung 
		oOcc.Name = "Ring_"&Ringnr ' if you like to rename the occurence you can add the new name as parameter of this Sub
Message 8 of 9

I used the code from https://modthemachine.typepad.com/files/mathgeometry.pdf but I get the error

'Error on Line 18 : 'Let' and 'Set' assignment statements are no longer supported.'

  Expert Elite
  Inventor Certified Professional
Message 9 of 9

Hi James,
the referenced document (https://modthemachine.typepad.com/files/mathgeometry.pdf) contain samples macros written in VBA wich use the "Set" statement (for reference https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/set-statement) , this statement is not used in the Ilogic code ti assign object variables/properties.

Then if this is the case, remove the "Set" statement from your code.
Otherwise give us more details about your code.

Bregs
Rossano Praderi


--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report