Adding contraints with VBA

Adding contraints with VBA

Anonymous
Not applicable
1,981 Views
3 Replies
Message 1 of 4

Adding contraints with VBA

Anonymous
Not applicable
I have an assembly that contains multiple parts. I have been successful at adding parts with VBA code, but want to place constraints (mate and flush) on the parts I add. I created work planes specifically for this task in each of the parts I want to constrain. If I mate Work PlaneX in Part1 to Work PlaneZ in Part2, I will have exactly what I'm looking for. Below is the VBA code I have been using to try this. The code references specific Part Documents and Work Planes. These are working properly (referencing the correct parts and documents). The constraint call is the only thing not working. Am I on the right track?

TIA,

Chris Gitzlaff

Code:
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oPt1CompDef As PartComponentDefinition
Dim oPt2CompDef As PartComponentDefinition
Dim oMate As MateConstraint

Dim oWPlane1 As WorkPlane
Dim oWPlane2 As WorkPlane

Set oPt1CompDef = ThisApplication.Documents.Item(3).ComponentDefinition
Set oWPlane1 = oPt1CompDef.WorkPlanes.Item(9)

Set oPt2CompDef = ThisApplication.Documents.Item(6).ComponentDefinition
Set oWPlane2 = oPt2CompDef.WorkPlanes.Item(4)

Set oMate = oAsmCompDef.Constraints.AddMateConstraint(oWPlane1, oWPlane2, 0, kInferredLine, kInferredLine)
0 Likes
1,982 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Chris,

 

The last two arguments need to be
face="Times New Roman" size=3>kNoInference in this case since you are
constraining planes. kInferredLine is valid only for cylindrical & conical
surfaces. Please see the help file for valid combinations.

 

Sanjay-

Inventor API

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I
have an assembly that contains multiple parts. I have been successful at
adding parts with VBA code, but want to place constraints (mate and flush) on
the parts I add. I created work planes specifically for this task in each of
the parts I want to constrain. If I mate Work PlaneX in Part1 to Work PlaneZ
in Part2, I will have exactly what I'm looking for. Below is the VBA code I
have been using to try this. The code references specific Part Documents and
Work Planes. These are working properly (referencing the correct parts and
documents). The constraint call is the only thing not working. Am I on the
right track?

TIA,

Chris Gitzlaff

Code:
Dim oAsmCompDef As AssemblyComponentDefinition
Set
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oPt1CompDef As PartComponentDefinition
Dim oPt2CompDef As
PartComponentDefinition
Dim oMate As MateConstraint

Dim oWPlane1 As WorkPlane
Dim oWPlane2 As WorkPlane

Set oPt1CompDef = ThisApplication.Documents.Item(3).ComponentDefinition

Set oWPlane1 = oPt1CompDef.WorkPlanes.Item(9)

Set oPt2CompDef = ThisApplication.Documents.Item(6).ComponentDefinition

Set oWPlane2 = oPt2CompDef.WorkPlanes.Item(4)

Set oMate = oAsmCompDef.Constraints.AddMateConstraint(oWPlane1, oWPlane2,
0, kInferredLine, kInferredLine)

0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks for the response, but it wasn't the solution. I had tried the kNoInference options before (all combinations, really). I have a feeling that I'm going about this the wrong way. What I would really like to see is an example where two parts (any parts) are placed in a model, then constrained using VBA. I suspect my problem is that I'm referencing parts, not occurrences, but I'm not sure.

Thanks for your help,

Chris G.
0 Likes
Message 4 of 4

Anonymous
Not applicable
I figured it out 🙂 I had placed a chunk of code into a temporary module a few days ago, but hadn't looked at it. It was the solution! I think it may have come straight out of the help files - it has excellent comments. The problem - I was referencing the parts, not the occurrences.


Anyway, here is the code:


Dim oAsmCompDef As AssemblyComponentDefinition

Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition


' Get references to the two occurrences to constrain.

' This arbitrarily gets the first and second occurrence.

Dim oOcc1 As ComponentOccurrence

Set oOcc1 = oAsmCompDef.Occurrences.Item(1)


Dim oOcc2 As ComponentOccurrence

Set oOcc2 = oAsmCompDef.Occurrences.Item(2)


' Get the XY plane from each occurrence. This goes to the

' component definition of the part to get this information.

' This is the same as accessing the part document directly.

' The work plane obtained is in the context of the part,

' not the assembly.

Dim oPartPlane1 As WorkPlane

Set oPartPlane1 = oOcc1.Definition.WorkPlanes.Item(3)


Dim oPartPlane2 As WorkPlane

Set oPartPlane2 = oOcc2.Definition.WorkPlanes.Item(3)


' Because we need the work plane in the context of the assembly

' we need to create proxies for the work planes. The proxies

' represent the work planes in the context of the assembly.

Dim oAsmPlane1 As WorkPlaneProxy

Call oOcc1.CreateGeometryProxy(oPartPlane1, oAsmPlane1)


Dim oAsmPlane2 As WorkPlaneProxy

Call oOcc2.CreateGeometryProxy(oPartPlane2, oAsmPlane2)


' Create the constraint using the work plane proxies.

Call oAsmCompDef.Constraints.AddMateConstraint(oAsmPlane1, oAsmPlane2, 0)


--

Chris Gitzlaff