Managing offsets in iProperties with VBA

Managing offsets in iProperties with VBA

Anonymous
Not applicable
636 Views
5 Replies
Message 1 of 6

Managing offsets in iProperties with VBA

Anonymous
Not applicable

Hi everybody!

 

I have an assembly with several components in it and I want to change their offset using VBA.

I’m here: Assembly -> Component -> iProperties -> Occurrence -> X/Y/Z Offset.

 

This is how far I’ve got:

 

[code]

Sub Test_Occurrence()

 

Dim od As Document
Set od = ThisApplication.ActiveDocument

 

Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = od.ComponentDefinition

 

Dim oOccurrence As ComponentOccurrence

 

For Each oOccurrence In oAsmCompDef.Occurrences

   Dim oName As String
    oName = oOccurrence.Name
    oOccurrence.Grounded = True 'not necessary, just to show that it works
    ‘manage offsets ???
 Next

 

End Sub

[/code]

 

So, I can access the occurrence settings of each component. But I just don’t know the correct command for changing the offsets.

Can anyone help me, please?

 

Regards
Heiko1985

0 Likes
Accepted solutions (2)
637 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Anyone? :*-(

0 Likes
Message 3 of 6

GeorgK
Advisor
Advisor

http://ww3.cad.de/foren/ubb/Forum258/HTML/000967.shtml

 

or from the help:

 

Public Sub MateConstraintWithLimits()
    ' Set a reference to the assembly component definintion.
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Set a reference to the select set.
    Dim oSelectSet As SelectSet
    Set oSelectSet = ThisApplication.ActiveDocument.SelectSet

    ' Validate the correct data is in the select set.
    If oSelectSet.Count <> 2 Then
        MsgBox "You must select the two entities valid for mate."
        Exit Sub
    End If

    ' Get the two entities from the select set.
    Dim oBrepEnt1 As Object
    Dim oBrepEnt2 As Object
    Set oBrepEnt1 = oSelectSet.Item(1)
    Set oBrepEnt2 = oSelectSet.Item(2)

    ' Create the mate constraint between the parts, with an offset value of 0.
    Dim oMate As MateConstraint
    Set oMate = oAsmCompDef.Constraints.AddMateConstraint(oBrepEnt1, oBrepEnt2, 0)

    ' Set a maximum value of 2 inches
    oMate.ConstraintLimits.MaximumEnabled = True
    oMate.ConstraintLimits.Maximum.Expression = "2 in"

    ' Set a minimum value of -2 inches
    oMate.ConstraintLimits.MinimumEnabled = True
    oMate.ConstraintLimits.Minimum.Expression = "-2 in"
End Sub
0 Likes
Message 4 of 6

Anonymous
Not applicable

Thank you for your help. I can use this in a different case.

However, it doesn't solve my problem because I don't want to create constraints.

 

All my components are grounded already and I want to manage their offsets/origins in the iProperties.

 

0 Likes
Message 5 of 6

GeorgK
Advisor
Advisor
Accepted solution
0 Likes
Message 6 of 6

Anonymous
Not applicable
Accepted solution

@Anonymous wrote:

Hello Heiko, 

http://inventorhub.autodesk.com/discussions/threads/78/post/3930721 

Hope that helps 

Georg


Yes, Georg, that's what I was looking for! Well, not 100 percent, but it helped me finding what I wanted.

 

My code now places all the components next to each other at specific distances:

 

Public Sub Test_Occurrence()

' Set a reference to the assembly component definition.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Get an occurrence from the select set.
Dim oOccurrence As ComponentOccurrence

i = 1
For Each oOccurrence In oAsmCompDef.Occurrences

    'Set oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)
    'Set oOccurrence = ThisApplication.ActiveDocument.
    
    oName = oOccurrence.Name
    
    ' Get the current transformation matrix from the occurrence.
    Dim oTransform As Matrix
    Set oTransform = oOccurrence.Transformation
    
    ' 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(i * 5, 0, 0)
    Call oOccurrence.SetTransformWithoutConstraints(oTransform)
    
    i = i + 1
  
Next
  
End Sub

 

 

Might not be proper programming, but it does what I want it to.

 

 

Thanks again! Smiley Happy

 

0 Likes