Automated Mates

Automated Mates

pranil_sevak
Explorer Explorer
226 Views
7 Replies
Message 1 of 8

Automated Mates

pranil_sevak
Explorer
Explorer

Hello everyone,

I'm working on placing .ipt and .iam files into an assembly and want to create an iLogic rule that mates the objects to the origin — but at the position and orientation where they're placed.

That means I don’t want the components to move to the origin or be grounded; instead, they should be locked in place exactly where and how they were inserted. This will apply to multiple parts placed into the assembly simultaneously.

Below is the code I received from Grok, but I'm running into errors. Unfortunately, I'm not very experienced with coding, so I’d really appreciate any help in figuring this out.

Thanks so much in advance!!!

Dim oAsm As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oAsm.ComponentDefinition

Dim asmXY As WorkPlane = oDef.WorkPlanes.Item("XY Plane")
Dim asmYZ As WorkPlane = oDef.WorkPlanes.Item("YZ Plane")
Dim asmXZ As WorkPlane = oDef.WorkPlanes.Item("XZ Plane")

For Each oOcc As ComponentOccurrence In oDef.Occurrences
    If oOcc.Grounded Then Continue For

    ' Optional: Temporarily skip known problematic components
    ' If oOcc.Name = "P25237-CMS-QTS-QTS-RIC2:1" Or oOcc.Name = "25237-WALL-1" Then Continue For

    Dim compDef As Object = oOcc.Definition
    Dim xyPlane As WorkPlane = Nothing
    Dim yzPlane As WorkPlane = Nothing
    Dim xzPlane As WorkPlane = Nothing

    ' Check component type and retrieve work planes
    If TypeOf compDef Is PartComponentDefinition Then
        Dim partDef As PartComponentDefinition = compDef
        xyPlane = TryCast(partDef.WorkPlanes.Item("XY Plane"), WorkPlane)
        yzPlane = TryCast(partDef.WorkPlanes.Item("YZ Plane"), WorkPlane)
        xzPlane = TryCast(partDef.WorkPlanes.Item("XZ Plane"), WorkPlane)
    ElseIf TypeOf compDef Is AssemblyComponentDefinition Then
        Dim asmDef As AssemblyComponentDefinition = compDef
        xyPlane = TryCast(asmDef.WorkPlanes.Item("XY Plane"), WorkPlane)
        yzPlane = TryCast(asmDef.WorkPlanes.Item("YZ Plane"), WorkPlane)
        xzPlane = TryCast(asmDef.WorkPlanes.Item("XZ Plane"), WorkPlane)
    Else
        MessageBox.Show("Unsupported component type: " & oOcc.Name)
        Continue For
    End If

    ' Skip if any plane is missing
    If xyPlane Is Nothing Or yzPlane Is Nothing Or xzPlane Is Nothing Then
        MessageBox.Show("Missing work plane for: " & oOcc.Name)
        Continue For
    End If

    ' Check component state
    If oOcc.Suppressed Then
        MessageBox.Show("Component suppressed: " & oOcc.Name)
        Continue For
    End If
    If oOcc.IsPatternElement Then
        MessageBox.Show("Component in pattern: " & oOcc.Name)
        Continue For
    End If

    ' Apply flush constraints
    Try
        oDef.Constraints.AddFlushConstraint(asmXY, xyPlane, 0)
        oDef.Constraints.AddFlushConstraint(asmYZ, yzPlane, 0)
        oDef.Constraints.AddFlushConstraint(asmXZ, xzPlane, 0)
        oOcc.Grounded = False
    Catch ex As Exception
        Dim compType As String = If(TypeOf compDef Is PartComponentDefinition, "Part", "Assembly")
        MessageBox.Show("Constraint error with: " & oOcc.Name & vbCrLf & _
                        "Type: " & compType & vbCrLf & _
                        "Details: " & ex.Message & vbCrLf & _
                        "Source: " & ex.Source)
        Continue For
    End Try
Next

 

 

0 Likes
227 Views
7 Replies
Replies (7)
Message 2 of 8

J_Pfeifer_
Advocate
Advocate

For this solution I would personally use iMates. Especially if you're attaching to static objects in the assembly such as the origins. For example, we have standard lugs that get applied to our assemblies. These have all been built into UCS objects on the main part file. The UCS's have iMate sets that match all our possible lug files. Allowing for the placement of lugs without directly applying each constraint manually or requiring any Illogic to apply constraints properly. 

 

Something to consider when placing constraints manually. The order in which, and direction of which has an impact on the success of that constraint. Meaning, you'd really have to pay attention to the order and constraints as applied anyway while programming. They could fail and error due to a flush vs mate or incorrect tangent mate. Or you have to account for a failure with an extra bit of code trying the other variation possible at that time. 

 

Once applied you could use component occurrences to pattern out that item

Message 3 of 8

WCrihfield
Mentor
Mentor

Hi @pranil_sevak.  I don't really have much time to spend on this right now, but I saw two main things on quick inspection of your rule that I wanted to mention.  First is, you will need to move the 'check' for the Suppressed status from down in the middle of the iteration section of the code, to just inside the iteration block of code.  This is because, if a component is Suppressed, then accessing its 'Definition' property will throw an error.  So, you will want your iteration to skip over any components that are suppressed, before it gets to the point where it is attempting to access its 'Definition' property.  The other main thing I saw is that, there should be some code in the middle of that iteration block of code where it is 'measuring' between the origin planes of the component, and the origin planes of the assembly, and recording those offset distances, and angles.  Then, near the end of the iteration block of code, where it is applying the constraints between the component's origin work planes and the assembly's origin work planes, you would need to be using those variables for those measured offsets &/or angles, instead of zero values.  Using zero values will move the component's origin work plane to be 'co-planar' with the assembly's origin planes, but you don't want the component to move.  But doing those measurements will likely not be easy, because the component could be in a rotated orientation compared to the assembly's origin planes, making it difficult to know which component origin plane should be compared (offset distance or angle) with which assembly origin plane.  There have been a few other discussions on this forum over the years with similar goals, which you might find helpful, and include several code examples along the way.  I wish I had those Links to post, but I did not keep them at the time.  Below is a Link to one I found in a quick search though, which you will likely find interesting and helpful.

https://forums.autodesk.com/t5/inventor-programming-ilogic/auto-constrain-in-current-position-with-o... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

hollypapp65
Advocate
Advocate

@pranil_sevak wrote:

 

I'm working on placing .ipt and .iam files into an assembly and want to create an iLogic rule that mates the objects to the origin — but at the position and orientation where they're placed.


Sounds like you just need to "Ground" them.

0 Likes
Message 5 of 8

pranil_sevak
Explorer
Explorer

Thank You I will look into this

0 Likes
Message 6 of 8

pranil_sevak
Explorer
Explorer

Thank You for this, I think I will be able to solve my conundrum with this. I will update this thread if I can work something out

 

0 Likes
Message 7 of 8

pranil_sevak
Explorer
Explorer

It is the simplest solution, sure, but I have reasons for not grounding them, which I already mentioned in the post.

0 Likes
Message 8 of 8

SevInventor
Advocate
Advocate

Maybe you need to check if AddMateConstraint or AddFlushConstraint like this:


Dim oParalell As Boolean= curAsmOrPlane.Plane.IsParallelTo(curCompOriPlane.Plane, 0.00001)
Dim oSameDirection As Boolean= curAsmOrPlane.Plane.Normal.IsEqualTo(curCompOriPlane.Plane.Normal)

	Dim oNV As NameValueMap


	If oParalell=True Then
'

				If oSameDirection=True Then
				oConstraint=oAsmComp.Constraints.AddFlushConstraint(curAsmOrPlane, curCompOriPlane, AbstandvonEbene(Zähler))
				End If
				If oSameDirection=False Then
				oConstraint=oAsmComp.Constraints.AddMateConstraint(curAsmOrPlane, curCompOriPlane, AbstandvonEbene(Zähler))
				End If

	  End If

	Next
	

Next

 

0 Likes