iLogic copy part in assembly with constraints

iLogic copy part in assembly with constraints

They_Call_Me_Jake
Advocate Advocate
1,235 Views
3 Replies
Message 1 of 4

iLogic copy part in assembly with constraints

They_Call_Me_Jake
Advocate
Advocate

I am trying to copy a part that is in an assembly and completely constrained and select which constraints I want to leave untouched and which ones I want to redefine. For example say I have a part that is using  it's origin planes to constrain to to another part that has planes I've made to constrain to. I want to copy this part but in each of the copies I want to leave the constraints that constrain it in say the X plane and Y plane and I want to choose a new reference to constrain it in the Z axis. Is there a way to retrieve the constraints of the part I want to copy and choose which constraints I want to re-reference them to while leaving the constraint reference of the part I am copying untouched. I have looked at this post https://forums.autodesk.com/t5/inventor-forum/copy-assembly-components-with-constraints/m-p/7401863#... and seen that a Justin K wrote a macro that copies a part along with it's contraints but places it in the same location as the original. This is half the battle I am trying to win as now I want to not only change the location to another plane but also do it multiple times each time changing its location. Here is the VB macro from Justin K .

Private oNewlyInsertedColl As Collection
Private oOriginalItemColl As Collection

Public Sub DupeSelectionWithConstraints()
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then MsgBox ("Rule not valid for non-assembly files!"): Exit Sub
    
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oSS As SelectSet
    Set oSS = oDoc.SelectSet
    
    If oSS.Count < 1 Then MsgBox ("Rule Requires a Select Set!"): Exit Sub
    
    Call DuplicateSS(oDoc, oSS)
End Sub


Private Sub DuplicateSS(oParentDoc As Document, oSS As SelectSet)
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
    
    Set oPasteMatrix = oTG.CreateMatrix()
    
    Set oOriginalItemColl = New Collection
    Set oNewlyInsertedColl = New Collection
    
    For Each oItem In oSS
        Set oPasteMatrix = oItem.Transformation
        Set oNewOcc = oParentDoc.ComponentDefinition.Occurrences.Add(oItem.Definition.Document.FullDocumentName, oPasteMatrix)
        oOriginalItemColl.Add oItem
        oNewlyInsertedColl.Add oNewOcc
    Next
    
    Dim oTestoOcc1 As Object
    Dim oTestoOcc2 As Object
    
    Dim oOcc1 As ComponentOccurrence
    Dim oOcc2 As ComponentOccurrence
    
    Dim oEntityOne As Object
    Dim oEntityTwo As Object
    
    For Each oConstraint In oParentDoc.ComponentDefinition.Constraints
    'Grab entities for new constraint to create
            Set oTestoOcc1 = GrabObjectFromColl(oOriginalItemColl, oConstraint.OccurrenceOne)
            Set oTestoOcc2 = GrabObjectFromColl(oOriginalItemColl, oConstraint.OccurrenceTwo)
            
            If oTestoOcc1 Is Nothing And oTestoOcc2 Is Nothing Then GoTo NextConstraint
            
            If oTestoOcc1 Is Nothing Then
                Set oEntityOne = oConstraint.EntityOne
            Else
                For Each oPossibleOcc In oNewlyInsertedColl
                    If oPossibleOcc.Definition Is oTestoOcc1.Definition Then
                        Set oOcc1 = oPossibleOcc
                    End If
                Next
                Call oOcc1.CreateGeometryProxy(GetProxy(oConstraint.EntityOne, oOcc1), oEntityOne)
            End If
            
            If oTestoOcc2 Is Nothing Then
                Set oEntityTwo = oConstraint.EntityTwo
            Else
                For Each oPossibleOcc In oNewlyInsertedColl
                    If oPossibleOcc.Definition Is oTestoOcc2.Definition Then
                        Set oOcc2 = oPossibleOcc
                    End If
                Next
                Call oOcc2.CreateGeometryProxy(GetProxy(oConstraint.EntityTwo, oOcc2), oEntityTwo)
            End If
        'End Grab entities
        
        
        'Check type of constraint
        Select Case oConstraint.Type
            Case 100665088 'kAngleConstraintObject
                'oParentDoc.Constraints.AddAngleConstraint(EntityOne As Object,
                '                                          EntityTwo As Object,
                '                                          Angle As Variant,
                '                                          [SolutionType] As AngleConstraintSolutionTypeEnum,
                '                                          [ReferenceVectorEntity] As Variant,
                '                                          [BiasPointOne] As Variant,
                '                                          [BiasPointTwo] As Variant )
                '                                      As AngleConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddAngleConstraint(oEntityOne, oEntityTwo, oConstraint.Angle, oConstraint.SolutionType, oConstraint.ReferenceVectorEntity)
                
            Case 100707840 'kAssemblySymmetryConstraintObject
                'oParentDoc.AddSymmetryConstraint( EntityOne As Object,
                '                                  EntityTwo As Object,
                '                                  SymmetryPlane As Object,
                '                                  [EntityOneInferredType] As InferredTypeEnum,
                '                                  [EntityTwoInferredType] As InferredTypeEnum,
                '                                  [NormalsOpposed] As Boolean )
                '                                 As AssemblySymmetryConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddSymmetryConstraint(oEntityOne, oEntityTwo, oConstraint.SymmetryPlane, oConstraint.EntityOneInferredType, oConstraint.EntityTwoInferredType, oConstraint.NormalsOpposed)
                
            Case 100666368 'kFlushConstraintObject
                'oParentDoc.AddFlushConstraint( EntityOne As Object,
                '                               EntityTwo As Object,
                '                               Offset As Variant,
                '                               [BiasPointOne] As Variant,
                '                               [BiasPointTwo] As Variant )
                '                            As FlushConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddFlushConstraint(oEntityOne, oEntityTwo, oConstraint.Offset.Expression)
                
                
            Case 100665344 'kInsertConstraintObject
                'oParentDoc.AddInsertConstraint( EntityOne As Object,
                '                                EntityTwo As Object,
                '                                AxesOpposed As Boolean,
                '                                Distance As Variant,
                '                                [BiasPointOne] As Variant,
                '                                [BiasPointTwo] As Variant )
                '                             As InsertConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddInsertConstraint(oEntityOne, oEntityTwo, oConstraint.AxesOpposed, oConstraint.Distance.Expression)
                
            Case 100665856 'kMateConstraintObject
                'oParentDoc.AddMateConstraint( EntityOne As Object,
                '                               EntityTwo As Object,
                '                               Offset As Variant,
                '                               [EntityOneInferredType] As InferredTypeEnum,
                '                               [EntityTwoInferredType] As InferredTypeEnum,
                '                               [BiasPointOne] As Variant,
                '                               [BiasPointTwo] As Variant )
                '                             As MateConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddMateConstraint(oEntityOne, oEntityTwo, oConstraint.Offset.Expression, oConstraint.EntityOneInferredType, oConstraint.EntityTwoInferredType)
                
            Case 100665600 'kTangentConstraintObject
                '.AddTangentConstraint( EntityOne As Object,
                '                       EntityTwo As Object,
                '                       InsideTangency As Boolean,
                '                       Offset As Variant,
                '                       [BiasPointOne] As Variant,
                '                       [BiasPointTwo] As Variant )
                '                     As TangentConstraint
                Call oParentDoc.ComponentDefinition.Constraints.AddTangentConstraint(oEntityOne, oEntityTwo, oConstraint.InsideTangency, oConstraint.Offset.Expression)
            
        End Select
NextConstraint:
    Next 'constraint
    
    
End Sub

Private Function GrabObjectFromColl(ByVal oColl As Collection, ByVal oObj As Object) As Object
    For Each oItem In oColl
        If oItem Is oObj Then
            Set GrabObjectFromColl = oObj
            Exit Function
        End If
    Next
    Set GrabObjectFromColl = Nothing
End Function

Private Function GetProxy(ByRef Prxy As Object, ByRef ContOcc As ComponentOccurrence) As Object
    Dim TempPrxy As Object
    Dim Occ As Object
    If Prxy.ContainingOccurrence.Type = kComponentOccurrenceObject Then
        Set Occ = ContOcc
    Else
        On Error Resume Next
            Set Occ = ContOcc.Definition.Occurrences.ItemByName(Prxy.ContainingOccurrence.Name)
            
        If Err.Number <> 0 Then
            On Error GoTo 0
            Set TempPrxy = Prxy.ContainingOccurrence
            Call ContOcc.CreateGeometryProxy(GetProxy(TempPrxy, ContOcc), Occ)
        End If
    End If
    Call Occ.CreateGeometryProxy(Prxy.NativeObject, GetProxy)
End Function

If what I am wanting to do is possible could someone help me figure this out. I know that there has been numerous people asking for a feature like "copy with mates" that Solidworks has and it hasn't been available in Inventor yet but maybe with 2019, which is what I am using, it might be possible.   

0 Likes
1,236 Views
3 Replies
Replies (3)
Message 2 of 4

chandra.shekar.g
Autodesk Support
Autodesk Support

@They_Call_Me_Jake,

 

VBA macro from Justin K will change part with same location. Can you please demonstrate your requirement with screen shots of samples?

 

Please make sure that files are non confidential.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 4

They_Call_Me_Jake
Advocate
Advocate

@chandra.shekar.g Thank you for your time in looking at this for me. It would be hard to demonstrate with screenshots of what I'm trying to to so instead I have attached a couple of example assemblies and parts with annotations to try and show what I'm wanting to do. I have been looking more into iLogic and there is the new Relationships (Add) that was added in 2019 that if you have 2 parts with an existing constraint it gives you the ability to update the entities that make the constraint. I was wanting to try and convert Justin K's macro over to iLogic and then have the ability to copy a part with constraints and allow the user to select a new entity on the base part that the newly copied part would be constrained to. This would allow you to copy a part with constraints say like a fastener that has an insert constraint and just select an entity on another hole you want to add that fastener to and it would copy it and then change the location based on the new entity selection and then repeat that process for all the holes you want that fastener in. I know that there are smart fasteners and iMates that can be used but this code would be used for much more than fasteners and the quantity of parts we have that we want to have this ability with is to large to go back and apply iMates to. Thanks again for your help.    

0 Likes
Message 4 of 4

chandra.shekar.g
Autodesk Support
Autodesk Support

@They_Call_Me_Jake,

 

As per understanding from examples, there are 3 inputs required to copy part in assembly at different location.

 

  1. Selection of entity in base part (it should be similar kind of entity).
  2. Constraint that need to be copied at different location.
  3. Finally, copying part selection.

With these 3 inputs, hoping that part can be copied in assembly at different location.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes