Constraining a component workplane to a sub-assembly workplane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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