Select assembly constraints to rename them with VBA

Select assembly constraints to rename them with VBA

Anonymous
Not applicable
1,934 Views
9 Replies
Message 1 of 10

Select assembly constraints to rename them with VBA

Anonymous
Not applicable

Hi everybody,

 

I am trying to write a macro in VBA that should do the following:

 

1) The user selects a specific assembly constraint in the browser using the mouse

 

2) the user runs the macro and the macro renames the selected constraint using the displaynames of occurrenceone and occurencetwo.

 

The only Problem here is that I cannot get the API to recognize the selected constraint like using ComponentOccurrence for the Parts.

 

Is there any method or idea I could use to implement it? Something like ConstraintOccurrence or an alternative way to do that?

 

Thank you very much!

0 Likes
Accepted solutions (1)
1,935 Views
9 Replies
Replies (9)
Message 2 of 10

JaneFan
Autodesk
Autodesk

Hello @Anonymous, 

 

We can get assembly constraints via API in this way: 

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oCons As AssemblyConstraint
Set oCons = oDoc.ComponentDefinition.Constraints.Item(1)

 

'Or go over each constraint

'For Each oCons In oDoc.ComponentDefinition.Constraints




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 3 of 10

smilinger
Advisor
Advisor
I don't think you need to do this, you can set the option in Application Settings, Assembly to show the information you need automatically.
0 Likes
Message 4 of 10

Anonymous
Not applicable

Hello @JaneFan,

 

thank you, but in your example it is not possible to use the mouse to select the constraint. What I mean is using the mouse to select the constraint like you can do with ComponentOccurrence to select components.

0 Likes
Message 5 of 10

Anonymous
Not applicable

@smilinger I cannot find the Options you metioned . Could you be please more specific?

0 Likes
Message 6 of 10

smilinger
Advisor
Advisor
0 Likes
Message 7 of 10

Anonymous
Not applicable

Thank you,

 

it Looks better. But it is still not what I want to have. As you can see, we rename out browser components with large names. This produces that the names for the constraint become too large. I just Need the Information contained in titel (in red on the Image). And just for certain constraints. This is why I want that the user first selects the desired contraint to be renamed with the mouse and then runs the macro. But it Looks like at the Moment it is not possible to do it from the Inventor API.

 

2018-02-06 13_05_16-Browser-Leiste.jpg

0 Likes
Message 8 of 10

smilinger
Advisor
Advisor
Accepted solution

Try this in iLogic:

 

Dim doc As AssemblyDocument = ThisDoc.Document
Dim comp As AssemblyComponentDefinition = doc.ComponentDefinition
For Each constrt As AssemblyConstraint In doc.SelectSet
	Dim constrtType As String
	Select Case constrt.Type
		Case ObjectTypeEnum.kFlushConstraintObject
			constrtType = "Flush"
		Case ObjectTypeEnum.kMateConstraintObject
			constrtType = "Mate"
		Case ObjectTypeEnum.kAngleConstraintObject
			constrtType = "Angle"
		Case ObjectTypeEnum.kInsertConstraintObject
			constrtType = "Insert"
		Case ObjectTypeEnum.kTangentConstraintObject
			constrtType = "Tangent"
		Case ObjectTypeEnum.kSymmetryConstraintObject
			constrtType = "Symmetry"
		Case Else
			constrtType = "Constraint"
	End Select
	
	constrt.Name = constrtType & " (" & iProperties.Value(constrt.OccurrenceOne.Name, "Summary", "Title") _
	& ", " & iProperties.Value(constrt.OccurrenceOne.Name, "Summary", "Title") & ")"
Next

 

0 Likes
Message 9 of 10

Anonymous
Not applicable

@smilinger

 

That works :D!

 

Thank you!!!

0 Likes
Message 10 of 10

smilinger
Advisor
Advisor

The last line of code should be:

 

constrt.Name = constrtType & " (" & iProperties.Value(constrt.OccurrenceOne.Name, "Summary", "Title") _
	& ", " & iProperties.Value(constrt.OccurrenceTwo.Name, "Summary", "Title") & ")"
0 Likes