Constrain two parts in subassembly from an assembly

Constrain two parts in subassembly from an assembly

Anonymous
Not applicable
455 Views
2 Replies
Message 1 of 3

Constrain two parts in subassembly from an assembly

Anonymous
Not applicable

Hello everybody,

 

I am trying to constraint two work plan of two part ( Pièce:1 and Pièce2:1) inside a the subassembly (Test:1):

subassemblyTree.PNG

 

When I open "Test:1" like this:

InsubassemblyTree.PNG

I select in Inventor the two parts and I launch this code and it is working:

 

Public Sub TestSub1() 

Dim oMainAsmDoc As AssemblyDocument
Set oMainAsmDoc = ThisApplication.ActiveDocument

Dim oSelectedObject As ComponentOccurrence
Dim oSelectedObject2 As ComponentOccurrence

Set oSelectedObject = oMainAsmDoc.SelectSet.Item(1)
Set oSelectedObject2 = oMainAsmDoc.SelectSet.Item(2)

Dim oSelectedObjectPlaneYZ As WorkPlane
Dim oSelectedObject2PlaneYZ As WorkPlane

Set oSelectedObjectPlaneYZ = oSelectedObject.Definition.WorkPlanes.Item(1) 'YZ
Set oSelectedObject2PlaneYZ = oSelectedObject2.Definition.WorkPlanes.Item(1) 'YZ

Dim oSelectedObjectPlaneYZProxy As WorkPlaneProxy
Dim oSelectedObject2PlaneYZProxy As WorkPlaneProxy

Call oSelectedObject.CreateGeometryProxy(oSelectedObjectPlaneYZ, oSelectedObjectPlaneYZProxy)
Call oSelectedObject2.CreateGeometryProxy(oSelectedObject2PlaneYZ, oSelectedObject2PlaneYZProxy)

ShowProxyPath oSelectedObjectPlaneYZ
ShowProxyPath oSelectedObjectPlaneYZProxy

Set Contraintes = oMainAsmDoc.ComponentDefinition.constraints

Call Contraintes.AddMateConstraint(oSelectedObject2PlaneYZProxy, oSelectedObjectPlaneYZProxy, 50)
        
End Sub

But now when I am in the context of "Ensemble1:1" like the first picture when I launch the code it doesn't work because I don't have created the geometry proxy to the top assembly.

 

I tried like this :

 

Public Sub TestSub3()

Dim oAssemblyLevel0 As AssemblyDocument
Set oAssemblyLevel0 = ThisApplication.ActiveDocument

Dim oSelectedObjectLevel2 As ComponentOccurrence
Dim oSelectedObject2Level2 As ComponentOccurrence

Set oSelectedObjectLevel2 = oAssemblyLevel0.SelectSet.Item(1)
Set oSelectedObject2Level2 = oAssemblyLevel0.SelectSet.Item(2)


Dim oSelectedObjectLevel2PlaneYZ As WorkPlane
Dim oSelectedObject2Level2PlaneYZ As WorkPlane

Set oSelectedObjectLevel2PlaneYZ = oSelectedObjectLevel2.Definition.WorkPlanes.Item(1) 'YZ
Set oSelectedObject2Level2PlaneYZ = oSelectedObject2Level2.Definition.WorkPlanes.Item(1) 'YZ

Dim PlaneYZForProxyForLevel1 As WorkPlaneProxy
Dim Plane2YZForProxyForLevel1 As WorkPlaneProxy

Call oSelectedObjectLevel2.CreateGeometryProxy(oSelectedObjectLevel2PlaneYZ, PlaneYZForProxyForLevel1)
Call oSelectedObject2Level2.CreateGeometryProxy(oSelectedObject2Level2PlaneYZ, Plane2YZForProxyForLevel1)

Set oAssemblyLevel1 = oAssemblyLevel0.ComponentDefinition.Occurrences.ItemByName("Test:1")

Dim PlaneYZProxyForLevel0 As WorkPlaneProxy
Dim Plane2YZProxyForLevel0 As WorkPlaneProxy

Call oAssemblyLevel1.CreateGeometryProxy(PlaneYZForProxyForLevel1, PlaneYZProxyForLevel0)
Call oAssemblyLevel1.CreateGeometryProxy(Plane2YZForProxyForLevel1, Plane2YZProxyForLevel0)

Set Contraintes = oAssemblyLevel0.ComponentDefinition.constraints

Call Contraintes.AddMateConstraint(PlaneYZProxyForLevel0, Plane2YZProxyForLevel0, 50)
        
End Sub

But I got an error error.png on the line :

 

Call Contraintes.AddMateConstraint(PlaneYZProxyForLevel0, Plane2YZProxyForLevel0, 50)

I also tried to edit "Test:1" in the assembly "Ensemble1.iam" when runing the code, I get the same error:

 

EditError.PNG

 

I tried many other method to get the correct proxy to put this constraint but I didn't find the good way.

 

Can someone help me please ?

 

Thank you for your help in advance !

 

Jérémie.

 

0 Likes
Accepted solutions (1)
456 Views
2 Replies
Replies (2)
Message 2 of 3

JaneFan
Autodesk
Autodesk
Accepted solution

Hey @Anonymous, 

 

We need get Constraints object from the sub assembly level which the constraint is created in.

Please change one line in your code then it would work: 

 Sub TestSub3()

Dim oAssemblyLevel0 As AssemblyDocument
Set oAssemblyLevel0 = ThisApplication.ActiveDocument

Dim oSelectedObjectLevel2 As ComponentOccurrence
Dim oSelectedObject2Level2 As ComponentOccurrence

Set oSelectedObjectLevel2 = oAssemblyLevel0.SelectSet.Item(1)
Set oSelectedObject2Level2 = oAssemblyLevel0.SelectSet.Item(2)


Dim oSelectedObjectLevel2PlaneYZ As WorkPlane
Dim oSelectedObject2Level2PlaneYZ As WorkPlane

Set oSelectedObjectLevel2PlaneYZ = oSelectedObjectLevel2.Definition.WorkPlanes.Item(1) 'YZ
Set oSelectedObject2Level2PlaneYZ = oSelectedObject2Level2.Definition.WorkPlanes.Item(1) 'YZ

Dim PlaneYZForProxyForLevel1 As WorkPlaneProxy
Dim Plane2YZForProxyForLevel1 As WorkPlaneProxy

Call oSelectedObjectLevel2.CreateGeometryProxy(oSelectedObjectLevel2PlaneYZ, PlaneYZForProxyForLevel1)
Call oSelectedObject2Level2.CreateGeometryProxy(oSelectedObject2Level2PlaneYZ, Plane2YZForProxyForLevel1)

Set oAssemblyLevel1 = oAssemblyLevel0.ComponentDefinition.Occurrences.ItemByName("Test:1")

Dim PlaneYZProxyForLevel0 As WorkPlaneProxy
Dim Plane2YZProxyForLevel0 As WorkPlaneProxy

Call oAssemblyLevel1.CreateGeometryProxy(PlaneYZForProxyForLevel1, PlaneYZProxyForLevel0)
Call oAssemblyLevel1.CreateGeometryProxy(Plane2YZForProxyForLevel1, Plane2YZProxyForLevel0)

Set Contraintes = oAssemblyLevel1.Definition.Constraints

Call Contraintes.AddMateConstraint(PlaneYZProxyForLevel0, Plane2YZProxyForLevel0, 50)
       
End Sub




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 3 of 3

Anonymous
Not applicable

Hello Jane Fan,

 

It's work !

 

Thank you for your help !

 

Have a nice day,

 

Jérémie

 

 

0 Likes