Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Getting user selection within part edit mode in assembly file

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
yingyinw
600 Views, 2 Replies

Getting user selection within part edit mode in assembly file

Hi, 

 

I'm working on Autodesk VBA, and I'm trying to get user input on which vertex to use for a constrain. I want to make a macro that can go into part edit, allow user to select a vertex, and then automatically set up the constraint. Initially I wanted to do this in a single macro, using this short piece of code

 

while oSelectSet.Count <>1

  DoEvents

Wend

 

While this worked when run directly from VBA, if I call it as a macro in Autodesk itself, the DoEvents doesn't seem o work because I wasn't able to make any selection at all. I googled and it seems like this is just how macros are.

 

So I split the code into a 2 subs and a function (as attached), and put them into separates rules that I call using a form in Autodesk. The first sub "Meta1Pin1_1" works, then I would select a vertex in edit mode. But when I go ahead and call the second sub "Meta1Pin1_2", I get the error message that I created which says no vertex is selected. But if I select a vertex and call the second sub again, it will work.

 

So I've been trying different things and I realize that every time when I first call the second sub, my selection is erased teh moment it goes into the sub. But, for some unknown reason, the selection will remain when I call it the second time. I can't figure out what exactly happened between the first and second call of the same sub, that allowed the selection to remain. Any help on solving this issue or new suggestion on better ways of doing this is hugely appreciated. 

 

Regards, 

Ying Ying

 

 

' prepares assembly for user input by grounding bones and going into part edit

Public Sub Meta1Pin1_1()

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

' Check it is an assembly
If oDoc.DocumentType <> kAssemblyDocumentObject Then Exit Sub

' Group Undos to undo whole assembly!
Call ThisApplication.TransactionManager.StartTransaction(oDoc, "Fix pins to metatarsals")

' Ground first two metatarsals
Dim oMeta1 As ComponentOccurrence
Set oMeta1 = oDoc.ComponentDefinition.Occurrences.ItemByName("00000025solid:1")
oMeta1.Grounded = True

Dim oMeta2 As ComponentOccurrence
Set oMeta2 = oDoc.ComponentDefinition.Occurrences.ItemByName("00000001solid:1")
oMeta2.Grounded = True

' Go into Meta1 for selection
Call oMeta1.Edit

' prompt user
MsgBox ("Select vertex to insert pin")

' end group undo
Call ThisApplication.TransactionManager.EndTransaction


End Sub


' ALWAYS CALL THIS AFTER Meta1Pin1_1

Public Sub Meta1Pin1_2()

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

' Check it is an assembly
If oDoc.DocumentType <> kAssemblyDocumentObject Then Exit Sub

' Group Undos to undo whole assembly!
Call ThisApplication.TransactionManager.StartTransaction(oDoc, "Fix pins to metatarsals")

' Ground first two metatarsals
Dim oMeta1 As ComponentOccurrence
Set oMeta1 = oDoc.ComponentDefinition.Occurrences.ItemByName("00000025solid:1")

'Dim oMeta2 As ComponentOccurrence
Set oMeta2 = oDoc.ComponentDefinition.Occurrences.ItemByName("00000001solid:1")

' call the desired pin
Dim oPin1 As ComponentOccurrence
Set oPin1 = oDoc.ComponentDefinition.Occurrences.ItemByName("pin:1")

' call the constraint function
Call constraintBoneWithUserInput(oDoc, oMeta1, oPin1, "UCS14")

' end group undo
Call ThisApplication.TransactionManager.EndTransaction


End Sub

 

Public Function constraintBoneWithUserInput(ByRef oDoc As Document, _
ByRef oMeta As ComponentOccurrence, _
ByRef oPin As ComponentOccurrence, _
ByVal UCSName As String)

' Set reference to constraints
Dim oConstraints As AssemblyConstraints
Set oConstraints = oDoc.ComponentDefinition.constraints

' Set a reference to the select set.
Dim oSelectSet As SelectSet
Set oSelectSet = oDoc.SelectSet

' Validate the correct data is in the select set.
If oSelectSet.Count > 1 Then
MsgBox ("Select only ONE vertex for insertion of pin")
Exit Function
ElseIf oSelectSet.Count < 1 Then
MsgBox ("Please select one vertex before calling macro")
Exit Function
End If

' Get the two entities from the select set.
Dim oBrepEnt1 As Object
Set oBrepEnt1 = oSelectSet.Item(1)

' Get UCS Z axis of pin1
Dim oUCS As UserCoordinateSystem
Call GetUCS(oPin, oUCS, UCSName)
Call oPin.CreateGeometryProxy(oUCS, oUCS)

' Set constraint
oPin.Grounded = False
Call oConstraints.AddMateConstraint(oUCS.ZAxis, oBrepEnt1, 0)

' Go back to assembly file
oMeta.ExitEdit (kExitToPrevious)

End Function

2 REPLIES 2
Message 2 of 3
nagwani
in reply to: yingyinw

Hi Ying Ying, Since you want to select a single vertex in assembly context you can achieve same using following sample code without going into edit mode. dim oBrepEnt1 as object set oBrepEnt1=ThisApplication.CommandManager.Pick(kPartVertexFilter, "Pick a vertext") Regards, -Ishwar N
Message 3 of 3
yingyinw
in reply to: nagwani

Hi Ishwar, 

 

That's really useful! It solved my problem in a very elegant way 😃 Thanks a lot! 

 

Regards, 

Ying Ying

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report