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: 

How to get part position

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
AnJanson
3119 Views, 4 Replies

How to get part position

Hi,

I have a quiet simple question. How can I get the xyz position of a part/ComponentOccurrence in an assembly.

I did some tests and found the Transformation.Translation vector.

But the values for x, y and z are only one tenth of the real value (e.g. x is 2222, but the vector.x property gives me 222.2).

My assembly untis are in mm.

Regards Andreas

4 REPLIES 4
Message 2 of 5
bshbsh
in reply to: AnJanson

inventor always uses centimeters internally. you need to convert it to the units you need (eg. the model units). it's simple in the case of cm-mm, but there are built-in unit conversion functions as well.

Message 3 of 5
MechMachineMan
in reply to: bshbsh

Sample:

Sub Main()

On Error Goto Handler:
	Dim oDoc As AssemblyDocument
	oDoc = ThisApplication.ActiveDocument
	
	If oDoc Is Nothing Then
        Exit Sub
    End If
    
    'Ensure you are at the right point to select objects.
    Select Case True
        Case Not oDoc.ActivatedObject Is Nothing
            If oDoc.ActivatedObject.Type = ObjectTypeEnum.kDocumentObject Then
                oDoc = oDoc.ActivatedObject
            Else
                MsgBox ("Invalid active object")
                Exit Sub
            End If
        Case oDoc.ActivatedObject Is Nothing
            'Continue without revising anything
        Case Else
            MsgBox ("Invalid Activated Object!")
    End Select
On Error Goto 0

    Dim oSS As SelectSet
    oSS = oDoc.SelectSet
      If oSS.Count >= 1

		Call ProcessBulkLocations(oSS)
      End If
	
	
	Exit Sub
Handler:
	MsgBox("Doc Not an Assembly!")
End Sub

Sub ProcessBulkLocations(oSS As SelectSet)
    For Each oItem In oSS
        If oItem.Type = ObjectTypeEnum.kComponentOccurrenceObject Then

           	Call GetOccOrigin(oItem)
        End If
    Next
End Sub

Sub GetOccOrigin(oOcc As ComponentOccurrence)

	Dim oUOM As UnitsOfMeasure
	oUOM = ThisApplication.ActiveDocument.UnitsOfMeasure

	' Get the current transformation matrix from the occurrence.
   	Dim oTransform As Matrix
   	oTransform = oOcc.Transformation

	Dim oOriginLocation As Vector
	oOriginLocation = oTransform.Translation

	ptX = oUOM.ConvertUnits(oOriginLocation.X, "cm", "in")
	ptY = oUOM.ConvertUnits(oOriginLocation.Y, "cm", "in")
	ptZ = oUOM.ConvertUnits(oOriginLocation.Z, "cm", "in")

	MsgBox(ptX & vbLf & ptY & vbLf & ptZ)
   	' Move the occurrence honoring any existing constraints.
   	''oTransform.SetTranslation ThisApplication.TransientGeometry.CreateVector(2, 2, 3)
   	''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(3, 4, 5)
   	''Call oOccurrence.SetTransformWithoutConstraints(oTransform)

End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 4 of 5
AnJanson
in reply to: MechMachineMan

Thank you Justin for the snippet and thank you János for the information, that Inventor always uses cm internally. 

Regards Andreas

Message 5 of 5

Hi,

 

I think I have a similar request. I have this assembly with a form where the user can choose between having 1, 2 or 3 of the same part (but 3 different occurrences)

 

Those parts are constrained in 2 axis, but can move freely in the third axis. I'd like to be able to capture, or get, the current position of those parts somehow and put that one coordinate in a parameter that can be exported into a XML file.

 

So I made this dummy assembly that recreates exactly the problem that I have, just not the real thing. There's a form in there that should open with the document. In that form you have the option to load 1, 2 or 3 parts, and the ability to save the parameters to a XML .

 

But right now, I really don't know where to start to capture that position. And I'm a beginner in VB and iLogic so that code above really makes very little sense to me. My plan B would be to constrain those parts to some plane and have a slider change the offset, but at first, I'd like to see if there's a more elegant way of doing this. Also, I like that very intuitive way of just grabbing the part and positioning it manually (without having to rely on a slider), I'd like to retain that.

 

I guess I just need someone to either, explain that snippet above, or send me in some direction to help me start.

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report