Constraining a component workplane to a sub-assembly workplane

Constraining a component workplane to a sub-assembly workplane

keithjk
Advocate Advocate
466 Views
2 Replies
Message 1 of 3

Constraining a component workplane to a sub-assembly workplane

keithjk
Advocate
Advocate

I posted a similiar post to work plane visibility but the scope of the project has changed.  The goal is to insert a component into an assembly and then contstrain it to a sub-assembly within the assembly.  The code below works fine up to the point of adding the mating contstraint.  I have placed a remark in the code where the error occurs.

 

I have also added a screen shot of the browser in my assembly drawing. I hope that gives some insight to the structure of it.


Thank you....

 

Private Sub InsertPart(ofile As String, selectedWP As String)

'declare some variables

Dim doc As Inventor.Document = Nothing

Dim wpFound As Boolean = False

Dim oAsmCompDef1 As AssemblyComponentDefinition

doc = oInventorApp.ActiveDocument

oAsmCompDef1 = doc.ComponentDefinition

' Set a reference to the transient geometry object.

Dim oTG As TransientGeometry

oTG = oInventorApp.TransientGeometry

' Create a matrix.

Dim oMatrix As Matrix

oMatrix = oTG.CreateMatrix

' Set the translation portion of the matrix so the part will be

'positioned at the co-ordinates

oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0))

'place an instance of the component

Dim oOccurrence As ComponentOccurrence

oOccurrence = oAsmCompDef1.Occurrences.Add(ofile, oMatrix)

'make sure the user work plane setting is on

oAssemblyDocument.ObjectVisibility.UserWorkPlanes = True

'Get the plane from the selected part and make sure the work plane is turned off

Dim oPartPlane1 As WorkPlane

oPartPlane1 = oOccurrence.Definition.WorkPlanes.Item("XZ Plane")

oPartPlane1.Visible = False

'find the assembly work plane (this is where the component will be placed).

Dim oAssemblyPlane1 As WorkPlane = Nothing

For Each oOcc As ComponentOccurrence In oAsmCompDef1.Occurrences

If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

oAsmCompDef1 = oOcc.Definition

Dim wpOccurrences As WorkPlanes = Nothing

wpOccurrences = oAsmCompDef1.WorkPlanes

Dim wp As WorkPlane = Nothing

For Each wp In wpOccurrences

If wp.Name = selectedWP Then

wpFound = True

oAssemblyPlane1 = oAsmCompDef1.WorkPlanes.Item(wp.Name)

'now constrain the component to the assembly work plane

Dim oAsmPlane4 As WorkPlaneProxy = Nothing

oOccurrence.CreateGeometryProxy(oPartPlane1, oAsmPlane4)

Dim ConstraintMate As MateConstraint

'///// THIS IS WHERE THE ERROR OCCURS //////

ConstraintMate = oAsmCompDef1.Constraints.AddMateConstraint(oAssemblyPlane1, oAsmPlane4, 0)

'now exit the loop

Exit For

End If

Next

End If

If wpFound = True Then

'exit main loop if work plane was found

Exit For

End If

Next

'update the view

oInventorApp.ActiveView.Update()

End Sub

0 Likes
467 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

I have not tried it, but what I noticed is that your oAssemblyPlane1 object is a WorkPlane and should be a WorkPlaneProxy

 

And also, you should not try to assign you mate contraint to a variable, it is not used afterward. Try to replace

 

ConstraintMate = oAsmCompDef1.Constraints.AddMateConstraint(oAssemblyPlane1, oAsmPlane4, 0)

 

with 

 

Call  oAsmCompDef1.Constraints.AddMateConstraint(oAssemblyPlane1, oAsmPlane4, 0)

 

This is what I can notice from your code, but, like I said, I did not tried it.

 

Hope this can help

Message 3 of 3

Balaji_Ram
Alumni
Alumni

Yes, I tried Keith's code snippet by changing the code to use proxies for both the part plane and the sub-assembly plane as Pierre rightly pointed out.

The mate constraint does get created.

Since both the workplanes are not in the assembly context, the proxies will be required.

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes