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: 

Calling a specific part in an assembly through a macro

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
590 Views, 5 Replies

Calling a specific part in an assembly through a macro

I have been trying to create a macro to control an assembly. The problem is that I need to access a certain parameter in a specific part in the assembly. I can get the exact parameter by using the "name" of the given parameter, ex .Item("d7") to call the parameter d7. The problem for me has been what name do I use to call out a given part, I have been using .Item(Index number), ex .Item(12), but this number can change as parts are added, deleted, or suppressed. The question is what "name" should I be using and how do I use it. I have tried "InternalName", "PrimaryDeselGUID",  "DisplayName", "FullDocumentName", "FullFileName" but there is no "Name" to input like the parameter. Thanks for any help.

5 REPLIES 5
Message 2 of 6
Vladimir.Ananyev
in reply to: Anonymous

You can always  get reference to desired parameter by its name using Item property of the Parameters collection.

 

Parameters.Item( Index As Variant ) As Parameter 

Index

Input Variant value that specifies the object to return. This can be either a numeric value indicating the index of the item in the collection or it can be a string indicating the parameter name

 

You should use the name you see in the Inventor parameters editor in UI,  e.g. "d7". 
It is good idea to give parameters more meaningful names.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 3 of 6
Anonymous
in reply to: Vladimir.Ananyev

Vladimir, Thanks for your input. I guess I was a bit long-winded as usual, because I was trying to explain that I have figured out how to call a parameter, and gave an example of how I did it. (I normally do give them meaningful names like "Cyl_Exten_0_to_24.25" to say what it does and within what limits, the "d7" just happened to be the dimension that I was changing, at that point my only concern was trying to figure out how to use VBA.) The problem is how do I call a specific part that is within an assembly. For example I have a part that I call "4XXXXXXG-(Template for AC Func)" that as the name implies it controls the function of the assembly, which is usually indexed as Item (42), (but at first it was Item(2), see code below), in the collection of parts in the top assembly, but if parts are added or suppressed the index number will change. The problem is that I have tried "InternalName", "PrimaryDeselGUID", "DisplayName", "FullDocumentName", "FullFileName" but there is no "Name" to input like with the parameters, and I was unable to get the VBA to recognize any of the above names. Display name (the part number above) seems to be the most likely answer but I must be missing some piece of info. Here is part of the code I am using, I need the oDoc.Item(42) to be oDoc.Item("4XXXXXXG-(Template for AC Func)"), but I have tried this many different ways with no success. When the index number of the item (Item(42)) changes parameter "d7" of the new Item(42) will change which causes problems. Private Sub cmdRaise_Click() Dim oDoc As Documents Set oDoc = ThisApplication.Documents Set oADoc = ThisApplication.ActiveDocument 'Set Temp = oDoc.Item(2) Set Temp = oDoc.Item(42) Dim oCompDef As ComponentDefinition Set oCompDef = Temp.ComponentDefinition Dim oPar As Parameters Set oPar = oCompDef.Parameters Do Until oPar.Item("d7").Value >= 2.5 * 2.54 oPar.Item("d7").Value = oPar.Item("d7").Value + 0.1 * 2.54 oADoc.Update Loop oADoc.Update End Sub Sorry I was so long-winded again, and please forgive my lack of knowledge for writing code, I just started, Rob
Message 4 of 6
Vladimir.Ananyev
in reply to: Anonymous

When you work in an assembly context access to parts parameters become slightly more complicated. 

Assembly document has a collection of references to all its components.  They can be parts or subassemblies.

Component is represented by ComponentOccurrence object in the Inventor API.  If component is a part then property ComponentOccurrence.Definition returns PartComponentDefinition object that allows you to get references to all part parameters.

Private Sub Test_Occurrences()

  Dim oAssyDoc As AssemblyDocument
  Set oAssyDoc = ThisApplication.ActiveDocument
  Dim oAssyDef As AssemblyComponentDefinition
  Set oAssyDef = oAssyDoc.ComponentDefinition

  'process all subassemblies
  Dim oOcc As ComponentOccurrence

  'access to component by its name (as it is shown in browser)
  Set oOcc = oAssyDef.Occurrences.ItemByName("LD-049:1")
  
  If oOcc.Definition.Type = kPartComponentDefinitionObject Then
    
    'reference to Component Definition
    Dim oDef As PartComponentDefinition
    Set oDef = oOcc.Definition
    
    'reference to part document
    Dim oDoc As PartDocument
    Set oDoc = oDef.Document
    
    'reference to parameters
    Dim oPars As Inventor.Parameters
    Set oPars = oDef.Parameters
    
    Dim oPar As Inventor.Parameter
    Set oPar = oPars.Item("d7")
    Debug.Print oPar.Expression
    
    oPar.Expression = "50 mm"
    
    'update & save changes
    oDoc.Update
    oDoc.Save
    
  End If
  
End Sub

 See also overview "Building an Assembly" in the Inventor API Help.

 

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 5 of 6
Mark_Wigan
in reply to: Anonymous

Edit: I see you have some code now to work with (added while i was typing my reply) so therefore some of my reply is now irrelevant. I will however leave it in case any of It is of use...

 

 

Hi VS,

 

i am not at my computer at the moment but I will give you some code tomorrow that you can edit to get what you want.

 

if I understand you right then your immediate challenge is that you just need to find the property set / collection set, that the referenced file descriptor resides in, to then allocate the referenced file to your variable.

 

One of the biggest breakthroughs i had when staritng out was to understand that one easy way of finding a specific property set is to use the debug functions in the code window, to step one line at a time while you observe your Locals window. In there you can burrow down into the object variable, in this case your assembly document is the uppermost  object, then just expand likey candidates within that object to see various types of collection sets or property sets to see what the appropriate category names are for you to transverse or choose specific items from within.

 

another handy tip, you may now this but if you find a function, property set or method that you wish to understand further is to type it into your code, then highlight the word then hit F1 to bring up the API supporting documentation. It's will then provide examples of the concept, that you can then work out to your own requirements.

 

i hope this helps.

best regards,
- Mark

(Kudo or Tag if helpful - in case it also helps others)

PDSU 2020 Windows 10, 64bit.

Message 6 of 6
Anonymous
in reply to: Mark_Wigan

Thanks for the input guys, I will read thru your suggestions carefully. I think that the code below is what I needed, I have tried it and it is working somewhat, I just need to clean out my code. I have been using Inventor for over five years now and I always felt that I had bearly scratched the surface, now that I am trying to learn the VBA I see that I was right. 'access to component by its name (as it is shown in browser) Set oOcc = oAssyDef.Occurrences.ItemByName("LD-049:1") Thanks again and if there are any more suggestions please let me know. Rob

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report