iLogic meant to apply angle constraint to every part only does it on one

iLogic meant to apply angle constraint to every part only does it on one

Anonymous
Not applicable
839 Views
7 Replies
Message 1 of 8

iLogic meant to apply angle constraint to every part only does it on one

Anonymous
Not applicable

Hello,

 

I'm using a fairly simple iLogic code to apply angle constraint to each component in an assembly but it's only applied on the last part in the design tree. Anything I can do to get the constraint to stick to every part/plane?

 

I wish to constraint each part's YZ plane to the assembly's XZ plane so they're all facing the same direction.

 

Sub Main()
    Dim doc As AssemblyDocument
    doc = ThisApplication.ActiveDocument
    
    Dim acd As AssemblyComponentDefinition
    acd = doc.ComponentDefinition
    
    Call ConstraintAngle(acd.Occurrences)
End Sub

Sub ConstraintAngle(occs As ComponentOccurrences)
    Dim occ As ComponentOccurrence
    For Each occ In occs
	Dim Osa Part As String
	Part = occ.Name
 	Try 
	    Constraints.AddAngle("Angle1", Part, "YZ plane", oAsmDoc,"XZ plane",
                angle := 0.0, solutionType := AngleConstraintSolutionTypeEnum.kDirectedSolution, 
                refVecComponent := Nothing, refEntityName := Nothing, biasPoint1 := Nothing, biasPoint2 := Nothing)
	Catch
        End Try
	Next
End Sub
0 Likes
Accepted solutions (1)
840 Views
7 Replies
Replies (7)
Message 2 of 8

JelteDeJong
Mentor
Mentor

i added some comments to your code below. that is what i see without running your code:

Sub ConstraintAngle(occs As ComponentOccurrences)
    Dim occ As ComponentOccurrence
    For Each occ In occs
	Dim Osa As String '<-- string is not used
	Part = occ.Name  
 	Try 
	    Constraints.AddAngle("Angle1", Part, "YZ plane", oAsmDoc,"XZ plane",
'^-- oAsmDoc is not defined angle := 0.0, solutionType := AngleConstraintSolutionTypeEnum.kDirectedSolution, refVecComponent := Nothing, refEntityName := Nothing, biasPoint1 := Nothing, biasPoint2 := Nothing) Catch ex as Exception
'when you use a try catch block show that ther was an exception.
msgbox(ex.message) End Try Next End Sub

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 8

Anonymous
Not applicable

Dang. I must've posted some old code. Thanks for noticing! I'll fix it in the original post.

 

edit. Apparently I can't edit it anymore but here it is. This worked on one single part in an assembly.

 

Sub Main()
    Dim doc As AssemblyDocument
    doc = ThisApplication.ActiveDocument
    
    Dim acd As AssemblyComponentDefinition
    acd = doc.ComponentDefinition
    
    Call ConstraintAngle(acd.Occurrences)
End Sub

Sub ConstraintAngle(occs As ComponentOccurrences)
    Dim occ As ComponentOccurrence
    For Each occ In occs
        Dim Part As String
	Part = occ.Name
 	    Try 
	        Constraints.AddAngle("Angle1", Part, "YZ plane", oAsmDoc,"XZ plane",
                angle := 0.0, solutionType := AngleConstraintSolutionTypeEnum.kDirectedSolution, 
                refVecComponent := Nothing, refEntityName := Nothing, biasPoint1 := Nothing, biasPoint2 := Nothing)
	    Catch
            End Try
    Next
End Sub

 

 

0 Likes
Message 4 of 8

bradeneuropeArthur
Mentor
Mentor

The complete code is not working.

Where did you get this Code from.

 

Could you help us what your needs are, because this piece of code will not work this way.

 

If i can give you a tip.

 

Please try to understand codes you are getting from anywhere.

This will help you and others in future to understand the code easier.

 

Try to take a look at this piece of code and try to understand it!

 

Public Sub InsertConstraint()
    ' Set a reference to the assembly component definintion.
    Dim oAsmCompDef As AssemblyComponentDefinition
    oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
    
    ' Set a reference to the select set.
    Dim oSelectSet As SelectSet
    oSelectSet = ThisApplication.ActiveDocument.SelectSet
    
    ' Validate the correct data is in the select set.
    If oSelectSet.Count  2 Then
        MsgBox ("You must select the two circular edges for the insert.")
        Exit Sub
    End If

    If Not TypeOf oSelectSet.Item(1) Is Edge Or Not TypeOf oSelectSet.Item(2) Is Edge Then
        MsgBox ("You must select the two circular edges for the insert.")
        Exit Sub
    End If

    ' Get the two edges from the select set.
    Dim oEdge1 As Edge
    Dim oEdge2 As Edge
    oEdge1 = oSelectSet.Item(1)
    oEdge2 = oSelectSet.Item(2)
 
    ' Create the insert constraint between the parts.
    Dim oInsert As InsertConstraint
    oInsert = oAsmCompDef.Constraints.AddInsertConstraint(oEdge1, oEdge2, True, 0)
End Sub

 

Regards,

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 8

JelteDeJong
Mentor
Mentor

@bradeneuropeArthuraccording to the online help there is this "new" iLogic function from inventor 23 (2019 if im not mistaking):

Function AddAngle ( 
	constraintName As String,
	component1 As ComponentArgument,
	entityName1 As String,
	component2 As ComponentArgument,
	entityName2 As String,
	Optional angle As Object = Nothing,
	Optional solutionType As AngleConstraintSolutionTypeEnum = AngleConstraintSolutionTypeEnum.kDirectedSolution,
	Optional refVecComponent As ComponentArgument = Nothing,
	Optional refEntityName As String = Nothing,
	Optional biasPoint1 As Object = Nothing,
	Optional biasPoint2 As Object = Nothing
) As IManagedConstraint

i guess that @Anonymous is trying to use this new function.

im working on inventor 2018 so i cant test this code.

As you wrote "it's only applied on the last part" it can work. i can only advice to add some exception handeling. (like showing a messagebox with the exception message. Also try manulay add the exact same constrain and see if that is possible.

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 6 of 8

bradeneuropeArthur
Mentor
Mentor

Ok @JelteDeJong 

 

Appologize, then i didn't see this since I am on 2018 too.

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 7 of 8

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Anonymous 

 

I think the issue is just that the code attempts to add the same constraint name to each component... Angle1....

 

If you increment the name, then it works... note that I stripped out a few things in this example, but the important part is just to have the i =i+1 and,  "Angle:" & i

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub Main()
    Dim doc As AssemblyDocument
    doc = ThisApplication.ActiveDocument
    
    Dim acd As AssemblyComponentDefinition
    acd = doc.ComponentDefinition
    
    Call ConstraintAngle(acd.Occurrences)
End Sub

Sub ConstraintAngle(occs As ComponentOccurrences)
    Dim occ As ComponentOccurrence
    i=0
    For Each occ In occs
	i = i+1 	
	Constraints.AddAngle("Angle:" & i, occ.Name, "YZ plane", oAsmDoc,"XZ plane",
        angle := 0.0, solutionType := AngleConstraintSolutionTypeEnum.kDirectedSolution)
    Next
End Sub

EESignature

Message 8 of 8

Anonymous
Not applicable

Thank you! The constraint name thing completely makes sense now. I didn't take that into consideration.

 

Thanks to everyone for helping out. I'm in the beginning of my VB journey and there were a couple of silly mistakes in the code. It kinda worked up to a point so I didn't notice them all.