assembly file insertion with VBA

assembly file insertion with VBA

D.Wheeler3GADA
Advocate Advocate
568 Views
6 Replies
Message 1 of 7

assembly file insertion with VBA

D.Wheeler3GADA
Advocate
Advocate

Is there a way to code in VBA that will allow you to insert a file with the filepath/name/extension being generated by a group of defined Strings?  I've tried many variations, but I believe I have the syntax incorrect again... 

0 Likes
569 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

Are you looking to add an occurrence to an assembly? If so the API sample is below and source is here.

Public Sub AddOccurrenceWithRepresentations()
    ' Set a reference to the assembly component definintion.
    ' This assumes an assembly document is open.
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Set a reference to the transient geometry object.
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry

    ' Create a matrix. A new matrix is initialized with an identity matrix.
    Dim oMatrix As Matrix
    Set oMatrix = oTG.CreateMatrix

    ' Create a new NameValueMap object
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    ' Set the representations to use when creating the occurrence.
    Call oOptions.Add("LevelOfDetailRepresentation", "MyLODRep")
    Call oOptions.Add("PositionalRepresentation", "MyPositionalRep")
    Call oOptions.Add("DesignViewRepresentation", "MyDesignViewRep")
    Call oOptions.Add("DesignViewAssociative", True)

    ' Add the occurrence.
    Dim oOcc As ComponentOccurrence
    Set oOcc = oAsmCompDef.Occurrences.AddWithOptions("C:\Temp\Reps.iam", oMatrix, oOptions)
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

D.Wheeler3GADA
Advocate
Advocate

Alan,

       The source code is where I started, but I am trying to specify the part being opened based on radio button selections in the form that create custom text strings. When the strings from the selected radio buttons are combined, they form the path and filename of the assembly that will be inserted.  The dream is also to be able to insert multiple instances of the assembly, and then launch an iLogic rule inside of the assembly(s) when exiting out of the form. I am not sure if this is possible, but that is the goal... I am trying to break down the steps and tackle the coding one step at a time.

 

Thank you again for all of your help & advice. It is greatly appreciated!

0 Likes
Message 4 of 7

A.Acheson
Mentor
Mentor

With working with concatenate strings I find it easier to either work with either a message box or the debug.print/logger.info methods. Trying to place a fullfilename directly into an open assembly function just screams errors and time lost. So just bring them all into logical strings and test they look correct before trying to open anything is best. Once you have the string and if you have many you could then add to an arraylist for convenience and then loop through the list to perform the operation. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 7

D.Wheeler3GADA
Advocate
Advocate

@A.Acheson ,

    I was able to pull everything together, and the insertion code works as I hoped, but I am curious as to the possibility of being able to place multiple instances of the inserted part by clicking in the model. The code "as is" allows for multiple insertions at 0,0,0 by clicking the form button multiple times, but they stack on top of one another, and then can be buried inside of features of a large assembly.

 

Perhaps I need to try to figure out how to launch the PlaceComponent command in Inventor from the form with a predefined file to insert... I believe you can code so that if that command is launched from a VB form, when you exit the command the form is still active. At least, I would need it to be. 

 

Thank you for the post!

0 Likes
Message 6 of 7

A.Acheson
Mentor
Mentor

You could pick up the mouse location but that is a whole different skill level and has it's own complications.

I would opt for a either hard coded or even pick a part close to your desired location in the model then use it's location to place the assemblies by code. 

 

Hardcoded:

With the above code for multiple insertions you would first need the code in a for loop with the number of assemblies required and then an index value for the new locations this way the insertion point can be moved over on each pass through the loop. The downside is the user can't pick where to place. 

 

You could also use the place command using the command manger to maybe allow the user pick where to place but it can be more difficult to control these interactions. I see you have a post related to that already so best to discuss that there.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 7 of 7

D.Wheeler3GADA
Advocate
Advocate

@A.Acheson,

  Thank you for the direction. I didn't want to assume you had read the similar post when I was replying. I definitely do not want to get in above what I can effectively manage and troubleshoot/errorproof. I am going to bring my expectations back down to earth, or at least within my limited skillset. Thanks again!

0 Likes