Reusing iMates

Reusing iMates

Anonymous
Not applicable
553 Views
2 Replies
Message 1 of 3

Reusing iMates

Anonymous
Not applicable

I need to reuse some iMates and would like some advice about how to code this.

 

Background Information

 connected.PNG

I will be constraining parts/assemblies to an extruded aluminum beam.

 

beam.PNG

This beam has 4 slots and thus four composite iMates.

 

 clamp.PNG

This is a simplified shaft clamp and it has 2 similar composite iMates that mate to any slot on the beam. There are 2 to allow me to flip the clamp over. (We have lots of different clamps so that is necessary). There are also other iMates on it that are important

 

Part 1

First, I ALT+drag-and-drop the composite iMates to the slot I want:

dragging.png

 connected.PNG

Next, I want the code to reapply the iMates within the composite iMate and then delete the composite iMate so it looks like this:

 code after.PNG

 

It should be noted that I mated the iMates of the under the Clamp's composite iMate with the Beam's original geometry that the iMates referenced, not the iMates themselves as you can't do that.

 

The reason for this is so that I can reuse the Composite if I want to change the location on the beam.

 

Part 2

I want to be able to change the location of the clamp a different slot by ALT+dragging the composite iMate. Thus, I need the code to delete the previous iMates and then repeat part 1.

 

Problem

When you ALT+drag a composite iMate over another, before releasing the mouse, Inventor fires a new relationship event (in code) to visualize what will happen. Thus, if you ALT+drag a composite over another composite, drag it away, and then bring it back, it fires a new relationship event multiple times in the AssemblyEvents_OnNewRelationship. This is a problem because I was thinking of using that event to add iMates and then delete the Composite. Or can I still do that anyway?

 

In other words, I am wondering if anyone has advice or some pseudocode about the best way to implement this idea. I was thinking about handling the assembly Events to do it but not sure if this is best. If all else fails, I can have multiple commands that I manually fire but this is less than optimal. Also, do you think it would be possible to have the code change the Position representation of the clamps depending on which iMate I grab?

 

Thank you

Let me know if this is confusing and you need me to clear anything up. Attached should be the parts but here is an extra link for more convenience.

https://drive.google.com/file/d/1LmP9PrehjmeN_VYk9bez1FcVMk-M_8Vb/view?usp=sharing

0 Likes
554 Views
2 Replies
Replies (2)
Message 2 of 3

HermJan.Otterman
Advisor
Advisor

so in short? you have 4 places that you want to be able to place some component (A)?

all 4 places must have the same iMate composite and also the component (A).

the create a new empty component (E) that has only the imate composite.

in your assembly place on all four places the (dummy) empty component (E)

than with code you can use replace to change Component (A) for Component (E) 1, 2, 3 or 4..

I hope you can follow...

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 3 of 3

Anonymous
Not applicable

I got the general concept working and I still need to do some modifying but here is the code so far. Fair warning, I got my degree in Mechanical Engineering, not Computer science. The code is very sloppy and there is no documentation.... yet. To get it to work you need to change the iComposite name to "Regular" or "Flipped." I still need the code to change the Position Rep depending on which iComposite you grab and I need to be able to make it work on mutiple Composites.

 

Here is the module.

Public oCustomCommand As class1

Sub starting()
    Set oCustomCommand = New class1
End Sub

And the class

 

Private WithEvents oAssemblyEvents As AssemblyEvents

Private WithEvents oDocEvents As DocumentEvents

Private WithEvents oTrans As TransactionEvents

Private A1 As iMateResult

Private oCommit As Boolean

Private oAbort As Boolean

Private DeleteConstraints As Boolean

Private la As Transaction

Private Sub Class_Initialize()

    Set oAssemblyEvents = ThisApplication.AssemblyEvents

    Set oDocEvents = ThisDocument.DocumentEvents

    oCommit = False

    oAbort = False

    DeleteConstraints = False

End Sub

Private Sub Class_Terminate()

    Set oAssemblyEvents = Nothing

    Set oAssemblyEvents = Nothing

    Set oBrows = Nothing

    Set oUser = Nothing

End Sub

Private Sub oAssemblyEvents_OnNewRelationship(ByVal DocumentObject As AssemblyDocument, _
    ByVal Relationship As Object, ByVal BeforeOrAfter As EventTimingEnum, _
    ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum)

    

    If BeforeOrAfter = kAfter Then

        'On Error GoTo 1:

        

        ' Check if it is a composite

        If Relationship.Type = kiMateResultObject Then

            If Relationship.IsComposite = True Then

                Dim oAttribSet As AttributeSet
                Dim oCon As AssemblyConstraint

                

                Dim oRelationship As iMateResult
                Set oRelationship = Relationship

                Debug.Print "test"
            

                ' If it is a composite then start up the transaction manager to
                '       monitor when it is done

                If oTrans Is Nothing Then
                    Set oTrans = ThisApplication.TransactionManager.TransactionEvents
                End If

                

                ' The iMate has been chosen

                If oCommit = True Then
                    oCommit = False
                    Set oTrans = Nothing

                    Dim oP As MateConstraint
                    Set oP = oRelationship.Constraints(1)
                    
                    For Each iMateDef In oP.AffectedOccurrenceOne.iMateDefinitions
                        
                        oCompositeName = iMateDef.Name
                        If (iMateDef.Name = "Regular") Or (iMateDef.Name = "Flipped") Then
                            Set oEntityCompare1 = iMateDef.Item(1).Entity
                            Set oEntityCompare2 = iMateDef.Item(2).Entity
                            If oEntityCompare1 Is oP.EntityOne Then
                                If oEntityCompare2 Is oRelationship.Constraints(2).EntityOne Then
                                    Set iMate1 = iMateDef.Item(1)
                                    Set oEntity1 = oRelationship.Constraints(1).EntityTwo
                                    Set iMate2 = iMateDef.Item(2)
                                    Set oEntity2 = oRelationship.Constraints(2).EntityTwo
                                End If
                            End If
                        End If
                        

                    Next
    


                    Set oOcc = oRelationship.Constraints(1).AffectedOccurrenceOne

                    Dim oConstraints As AssemblyConstraintsEnumerator

                    

                    Call ThisApplication.TransactionManager.UndoTransaction

                    

                    Dim oDef As AssemblyComponentDefinition

                    Set oDef = ThisDocument.ComponentDefinition

                    

                    

                    Dim oTxn As Transaction

                    Set oTxn = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Custom")

    

                    Set oConstraints = oOcc.Constraints

    

                    If DeleteConstraints = True Then

                        DeleteConstraints = False

                        For Each oCon In oConstraints

                            If Not oCon.iMateResult Is Nothing Then

                                For Each oAttribSet In oCon.iMateResult.AttributeSets

                                    If oAttribSet.Name = "Attachment" Then

                                        Call oCon.Delete

                                    End If

                                Next

                            End If

                        Next

                        

                    End If

    

                    Dim Zup As iMateResult

                    Dim Yup As iMateResult

                    Set Zup = oDef.iMateResults.AddByiMateAndEntity(iMate1, oEntity1)

                    Set Yup = oDef.iMateResults.AddByiMateAndEntity(iMate2, oEntity2)

                    

                    ' Mark the new mates

                    Dim oAttribSets As AttributeSets

                    Call Zup.AttributeSets.Add("Attachment")

                    Call Yup.AttributeSets.Add("Attachment")

                    

                    

                    oTxn.End

                    

                ' It's trying to be applied so look to see if constraints are

                '   already there to be suppressed

                ElseIf oCommit = False Then

                

                    Dim oRunning As Boolean

                    oRunning = False

                

                    For Each oCon In oRelationship.Constraints(1).AffectedOccurrenceOne.Constraints

                        If Not oCon.iMateResult Is Nothing Then

                            For Each oAttribSet In oCon.iMateResult.AttributeSets

                                If oAttribSet.Name = "Attachment" Then

                                    If oRunning = False Then

                                        oRunning = True

                                        Set oTrans = Nothing

                                        Dim oSuppressTxn As Transaction

                                        Set oSuppressTxn = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Supress custom constraints")

                                        Set la = oSuppressTxn

                                        DeleteConstraints = True

                                    End If

                                    Call oCon.Delete

                                End If

                            Next

                        End If

                    Next

                    

                    If oRunning = True Then

                        oRunning = False

                        oSuppressTxn.End

                        Set oTrans = ThisApplication.TransactionManager.TransactionEvents

                    End If

                    

                End If

            End If

        End If

1:

            On Error GoTo 0

    End If

    

End Sub


Private Sub oTrans_OnAbort(ByVal TransactionObject As Transaction, ByVal Context As NameValueMap, ByVal BeforeOrAfter As EventTimingEnum)

    'If the operation is aborted, stop monitoring the undo so you don't

    '   accidentally catch something else

    Set oTrans = Nothing

End Sub

Private Sub oTrans_OnCommit(ByVal TransactionObject As Transaction, ByVal Context As NameValueMap, ByVal BeforeOrAfter As EventTimingEnum, HandlingCode As HandlingCodeEnum)

Debug.Print "OnCommit"

oCommit = True

End Sub





0 Likes