Constraint work plane of "active assembly document" with the work plane of "placed component"

Constraint work plane of "active assembly document" with the work plane of "placed component"

dsakpal
Contributor Contributor
444 Views
4 Replies
Message 1 of 5

Constraint work plane of "active assembly document" with the work plane of "placed component"

dsakpal
Contributor
Contributor

Hello Friends,

 

I would like to constraint work plane of "active assembly document" with the work plane of "placed component".

Here is the code which I wrote, but I'm getting error at syntax of "If oRefDoc.FullFileName = placedComponentFilename Then". The error is "Object variable or With block variable not set (Error 91)".

I cant recognize the problem. Please help me to resolve this issue.

 

Sub AddMateConstraintBetweenWorkPlanes()
Dim oApp As Inventor.Application
Set oApp = GetObject(, "Inventor.Application")

' Get the active assembly document
Dim oAssemblyDoc As AssemblyDocument
Set oAssemblyDoc = oApp.ActiveDocument

' Check if the active document is an assembly
If Not TypeOf oAssemblyDoc Is AssemblyDocument Then
MsgBox "Active document is not an assembly."
Exit Sub
End If

' Get the active assembly component definition
Dim oAssemblyDef As AssemblyComponentDefinition
Set oAssemblyDef = oAssemblyDoc.ComponentDefinition

' Get the work plane of the active assembly document
Dim oActiveAssemblyWorkPlane As WorkPlane
Set oActiveAssemblyWorkPlane = oAssemblyDef.WorkPlanes.Item("YZ Plane") ' Replace with the actual work plane name

' Find the placed component occurrence by its filename
Dim placedComponentFilename As String
placedComponentFilename = "4924306035.iam" ' Replace with the actual filename of the placed component

' Get the placed component occurrence
Dim oPlacedComponentOccurrence As ComponentOccurrence
Set oPlacedComponentOccurrence = oAssemblyDef.Occurrences.ItemByName("4924306035.iam") ' Replace with the actual occurrence name

' Get the work plane of the placed component
Dim oPlacedComponentWorkPlane As WorkPlane
Set oPlacedComponentWorkPlane = oPlacedComponentOccurrence.Definition.WorkPlanes.Item("YZ Plane") ' Replace with the actual work plane name

' Add the mate constraint between the two work planes
Dim oAssemblyConstraints As AssemblyConstraints
Set oAssemblyConstraints = oAssemblyDef.constraints

Dim oMateConstraint As mateConstraint
Set oMateConstraint = oAssemblyConstraints.AddMateConstraint(oActiveAssemblyWorkPlane, oPlacedComponentWorkPlane, 0)

' Clean up
Set oMateConstraint = Nothing
Set oAssemblyConstraints = Nothing
Set oPlacedComponentWorkPlane = Nothing
Set oPlacedComponentOccurrence = Nothing
Set oActiveAssemblyWorkPlane = Nothing
Set oAssemblyDef = Nothing
Set oAssemblyDoc = Nothing
Set oApp = Nothing
End Sub

0 Likes
Accepted solutions (1)
445 Views
4 Replies
Replies (4)
Message 2 of 5

Andrii_Humeniuk
Advisor
Advisor

Hi @dsakpal . You need to create proxy geometry for sub-components (line 35-36).

Sub AddMateConstraintBetweenWorkPlanes()
Dim oApp As Inventor.Application
Set oApp = GetObject(, "Inventor.Application")

' Get the active assembly document
Dim oAssemblyDoc As AssemblyDocument
Set oAssemblyDoc = oApp.ActiveDocument

' Check if the active document is an assembly
If Not TypeOf oAssemblyDoc Is AssemblyDocument Then
MsgBox "Active document is not an assembly."
Exit Sub
End If

' Get the active assembly component definition
Dim oAssemblyDef As AssemblyComponentDefinition
Set oAssemblyDef = oAssemblyDoc.ComponentDefinition

' Get the work plane of the active assembly document
Dim oActiveAssemblyWorkPlane As WorkPlane
Set oActiveAssemblyWorkPlane = oAssemblyDef.WorkPlanes.Item("YZ Plane") ' Replace with the actual work plane name

' Find the placed component occurrence by its filename
Dim placedComponentFilename As String
placedComponentFilename = "4924306035.iam" ' Replace with the actual filename of the placed component

' Get the placed component occurrence
Dim oPlacedComponentOccurrence As ComponentOccurrence
Set oPlacedComponentOccurrence = oAssemblyDef.Occurrences.ItemByName("4924306035.iam") ' Replace with the actual occurrence name

' Get the work plane of the placed component
Dim oPlacedComponentWorkPlane As WorkPlane
Set oPlacedComponentWorkPlane = oPlacedComponentOccurrence.Definition.WorkPlanes.Item("YZ Plane") ' Replace with the actual work plane name

Dim oPlacedComponentWorkPlaneProxy As WorkPlaneProxy 
Call oPlacedComponentOccurrence.CreateGeometryProxy(oPlacedComponentWorkPlane, oPlacedComponentWorkPlaneProxy)

' Add the mate constraint between the two work planes
Dim oAssemblyConstraints As AssemblyConstraints
Set oAssemblyConstraints = oAssemblyDef.Constraints

Dim oMateConstraint As MateConstraint
Set oMateConstraint = oAssemblyConstraints.AddMateConstraint(oActiveAssemblyWorkPlane, oPlacedComponentWorkPlaneProxy, 0)

' Clean up
Set oMateConstraint = Nothing
Set oAssemblyConstraints = Nothing
Set oPlacedComponentWorkPlane = Nothing
Set oPlacedComponentWorkPlaneProxy = Nothing
Set oPlacedComponentOccurrence = Nothing
Set oActiveAssemblyWorkPlane = Nothing
Set oAssemblyDef = Nothing
Set oAssemblyDoc = Nothing
Set oApp = Nothing
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 5

dsakpal
Contributor
Contributor

Hello Andrii, thanks for the quick response!!

 

as suggested, I have created proxy geometry for the sub-component but its giving error again at the syntax of "Set oPlacedComponentOccurrence = oAssemblyDef.Occurrences.ItemByName("4924306035.iam")"

 

This "4924306035.iam" component is the sub-component. To get the file name of this sub-component, I have assigned separate variable also but still its not working. 

What's wrong I'm doing here?

 

 

 

0 Likes
Message 4 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

"4924306035.iam" is DisplayName file, you need to write occurrence name "4924306035:1" as in the model browser.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 5

dsakpal
Contributor
Contributor

You are great Andrii...!! It works completely fine... Awesome!

Small mistakes, takes long time to recognize!!