Constraint "Z Axis" of 'placed component' with the "Y Axis" of 'active assembly document'

Constraint "Z Axis" of 'placed component' with the "Y Axis" of 'active assembly document'

dsakpal
Contributor Contributor
561 Views
7 Replies
Message 1 of 8

Constraint "Z Axis" of 'placed component' with the "Y Axis" of 'active assembly document'

dsakpal
Contributor
Contributor

Hello Friends, I'm trying to constraint "Z Axis" of 'placed component' with the "Y Axis" of 'active assembly document' but its giving error at the syntax of "Set oMateConstraint3 = oAssemblyConstraints3.AddMateConstraint(oActiveAssemblyYAxis, oSecondPlacedComponentZAxis, 0)".

Error is "Method 'AddMateConstraint' of object 'AssemblyConstraints' failed.

Please let me know, what's wrong I'm doing?

 

 

' Find the second placed component occurrence by its occurrence name
Dim secondPlacedComponentOccurrenceName As String
secondPlacedComponentOccurrenceName = "AB150N LH_CAD Atmn:1" ' Replace with the actual occurrence name of the second placed component

' Get the second placed component occurrence
Dim oSecondPlacedComponentOccurrence As ComponentOccurrence
Set oSecondPlacedComponentOccurrence = oAssemblyDef.Occurrences.ItemByName("AB150N LH_CAD Atmn:1")

' Get the "Y Axis" of the active assembly document
Dim oActiveAssemblyYAxis As WorkAxis
Set oActiveAssemblyYAxis = oAssemblyDef.WorkAxes.Item("Y Axis") ' Replace with the actual Y Axis name

' Get the "Z Axis" of the second placed component
Dim oSecondPlacedComponentZAxis As WorkAxis
Set oSecondPlacedComponentZAxis = oSecondPlacedComponentOccurrence.Definition.WorkAxes.Item("Z Axis") ' Replace with the actual Z Axis name

' Create a work axis proxy for the sub-component
Dim oSecondPlacedComponentWorkAxisProxy As WorkAxisProxy
Call oSecondPlacedComponentOccurrence.CreateGeometryProxy(oSecondPlacedComponentZAxis, oSecondPlacedComponentWorkAxisProxy)

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

Dim oMateConstraint3 As mateConstraint
Set oMateConstraint3 = oAssemblyConstraints3.AddMateConstraint(oActiveAssemblyYAxis, oSecondPlacedComponentZAxis, 0)

 

 

 

@Andrii_Humeniuk 

 

0 Likes
Accepted solutions (2)
562 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @dsakpal.  Maybe give this code a try.  I am simply using the Index number of 2 to get the second placed component, instead of by its name, but you can change that if you want.  Just change 'oOccs.Item(1)' to 'oOccs.ItemByName("component name")'.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oAsmYAxis As WorkAxis = oADef.WorkAxes.Item(2)
	Dim oConsts As AssemblyConstraints = oADef.Constraints
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim oOcc As ComponentOccurrence = oOccs.Item(2)
	Dim oOccZAxis As WorkAxis = oOcc.Definition.WorkAxes.Item(3)
	Dim oOccZAxisProxy As WorkAxisProxy = Nothing
	oOcc.CreateGeometryProxy(oOccZAxis, oOccZAxisProxy)
	Dim oMate As MateConstraint = oConsts.AddMateConstraint2( _
	oAsmYAxis, oOccZAxisProxy, 0, InferredTypeEnum.kInferredLine, _
	InferredTypeEnum.kInferredLine, MateConstraintSolutionTypeEnum.kAlignedSolutionType)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

FINET_Laurent
Advisor
Advisor

Hi @dsakpaln,

 

Here is the code working :

Sub Main()
    Dim oDoc As Inventor.AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oAssemblyDef As AssemblyComponentDefinition
    Set oAssemblyDef = oDoc.ComponentDefinition
    
     ' Find the second placed component occurrence by its occurrence name
    Dim secondPlacedComponentOccurrenceName As String
    secondPlacedComponentOccurrenceName = "AB150N LH_CAD Atmn:1" ' Replace with the actual occurrence name of the second placed component
    
    ' Get the second placed component occurrence
    Dim oSecondPlacedComponentOccurrence As ComponentOccurrence
    Set oSecondPlacedComponentOccurrence = oAssemblyDef.Occurrences.ItemByName(secondPlacedComponentOccurrenceName)
    
    ' Get the "Y Axis" of the active assembly document
    Dim oActiveAssemblyYAxis As WorkAxis
    Set oActiveAssemblyYAxis = oAssemblyDef.WorkAxes.Item(2) ' Replace with the actual Y Axis name
    
    ' Get the "Z Axis" of the second placed component
    Dim oSecondPlacedComponentZAxis As WorkAxis
    Set oSecondPlacedComponentZAxis = oSecondPlacedComponentOccurrence.Definition.WorkAxes.Item(3) ' Replace with the actual Z Axis name
    
    ' Create a work axis proxy for the sub-component
    Dim oSecondPlacedComponentWorkAxisProxy As WorkAxisProxy
    Call oSecondPlacedComponentOccurrence.CreateGeometryProxy(oSecondPlacedComponentZAxis, oSecondPlacedComponentWorkAxisProxy)
    
    ' Add the mate constraint between the two work axes
    Dim oAssemblyConstraints3 As AssemblyConstraints
    Set oAssemblyConstraints3 = oAssemblyDef.Constraints
    
    Dim oMateConstraint3 As MateConstraint
    Set oMateConstraint3 = oAssemblyConstraints3.AddMateConstraint2(oActiveAssemblyYAxis, oSecondPlacedComponentWorkAxisProxy, 0, InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine, MateConstraintSolutionTypeEnum.kNoSolutionType)
End Sub

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 4 of 8

FINET_Laurent
Advisor
Advisor
Accepted solution

@dsakpal, the main mistake was on the constraint line, you didn't use the proxy you created beforehand : 

Set oMateConstraint3 = oAssemblyConstraints3.AddMateConstraint(oActiveAssemblyYAxis, oSecondPlacedComponentZAxis, 0)

 

The correct line is : 

Set oMateConstraint3 = oAssemblyConstraints3.AddMateConstraint(oActiveAssemblyYAxis, oSecondPlacedComponentWorkAxisProxy, 0)

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 5 of 8

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @dsakpal . You need to create a constraint with the proxy geometry of your sub component.
AxisProxy.png

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 6 of 8

dsakpal
Contributor
Contributor
Hello WCrihfield, Thanks for your prompt response, but the solution suggested by Mr. FINET_Laurent & Mr.
Andrii_Humeniuk is perfectly works to resolve my problem.
As suggested by them, I just did minor changes in my code & its works fine!!
Thank you dear!!
0 Likes
Message 7 of 8

dsakpal
Contributor
Contributor
Hello Mr. FINET_Laurent, Thanks for your kind attention & suggestion. It works completely fine!!
Message 8 of 8

dsakpal
Contributor
Contributor
Hello Mr. Andrii_Humeniuk, Thanks once again for your kind suggestion. It works completely fine!!
0 Likes