If you use “Virtual Parts” in your assemblies, you probably find yourself creating the same parts over and over. This quick blog post shares some iLogic code to create “Virtual Parts” from an Input List Box.
The contents of this Input List Box are created from an Array which you can edit to suit your needs:
'Create Array & Input list box Dim ClintBrown3D As New ArrayList ClintBrown3D.Add("Locktite") ClintBrown3D.Add("Tape") ClintBrown3D.Add("Carton") ClintBrown3D.Add("Grease") ClintBrown3D.Add("Oil")
The Animated GIF below shows how the code works:
Note that the code is only designed to run in an assembly, it will return an error stating as much if it is run in a part or drawing file. This version of code will not create a 2nd instance of a “Virtual Part”.
Here’s the iLogic code:
'iLogic code by Clint Brown @ClintBrown3D, adapted from Inventor API sample code found here: 'http://adndevblog.typepad.com/manufacturing/2013/04/create-virtual-component-by-code.html 'this version originally posted at https://clintbrown.co.uk/virtualpart 'Create Array & Input list box Dim ClintBrown3D As New ArrayList ClintBrown3D.Add("Locktite") ClintBrown3D.Add("Tape") ClintBrown3D.Add("Carton") ClintBrown3D.Add("Grease") ClintBrown3D.Add("Oil") On Error GoTo ClintBrown3D: 'Ignore multiple instances of the Virtual Component 'Check that this is an Assembly oDoc = ThisDoc.ModelDocument If oDoc.DocumentType = kPartDocumentObject Then MsgBox("This only works in Assemblies") Return End If Dim oAssDef As AssemblyComponentDefinition oAssDef = oDoc.ComponentDefinition Dim oMatrix As Matrix oMatrix = ThisApplication.TransientGeometry.CreateMatrix NameOfVirtualPart = InputListBox("Virtual Part Creator", ClintBrown3D, d0, Title := "@ClintBrown3D ", ListName := "Pick a Virtual Part to Add") ' Create Virtual part Dim oNewOcc As ComponentOccurrence oNewOcc = oAssDef.Occurrences.AddVirtual(NameOfVirtualPart, oMatrix) Dim oCVirtualCompDef As VirtualComponentDefinition oCVirtualCompDef = oNewOcc.Definition Exit Sub ClintBrown3D: Return
I hope this is useful, please enjoy, share, edit, remix and use this code, but please point back to this page if you do.
You must be logged in to post a comment.