Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding contraints with VBA

3 REPLIES 3
Reply
Message 1 of 4
cgitzlaff
1463 Views, 3 Replies

Adding contraints with VBA

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)
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: cgitzlaff

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)

Message 3 of 4
cgitzlaff
in reply to: cgitzlaff

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.
Message 4 of 4
cgitzlaff
in reply to: cgitzlaff

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report