Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Owner2229
in reply to: Anonymous

You can use this to add ALL parts from one directory selected by prefix to your currently opened assembly:

 

 

Imports System.IO
Imports System.IO.File
Sub Main
    Dim oDoc As Document = ThisApplication.ActiveDocument
    If Not oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
        MsgBox("This rule is for assembly only.")
        Exit Sub
    End If
'You can change this line to match your location if you have all files in one folder 'Otherwise it will be used as initial folder for folder search
oPath = "C:\FolderContainingAllParts"
'Or you can use this to select the folder, delete it if not needed
Dim dialog1 = New System.Windows.Forms.FolderBrowserDialog()
dialog1.SelectedPath = oPath
dialog1.ShowNewFolderButton = True
If System.Windows.Forms.DialogResult.OK = dialog1.ShowDialog() Then
oPath = dialog1.SelectedPath
Else
MsgBox("No folder selected. Copiyng canceled.")
Exit Sub
End If
If Not System.IO.Directory.Exists(oPath) Then MsgBox("Folder doesn't exist") End If SearchedPart = InputBox("Please insert prefix of searched parts", "Prefix", DXFName) If SearchedPart = vbNullString Then Exit Sub End If Dim oFile As String
'It will only look for parts, as there is "*.ipt" at the end For Each oFile In System.IO.Directory.EnumerateFiles(oPath, SearchedPart & "*.ipt") AddOccurrence(oFile) Next End Sub Public Sub AddOccurrence(oPartToAdd As String) Dim oDoc As Document = ThisApplication.ActiveDocument Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oMatrix As Matrix = oTG.CreateMatrix Call oMatrix.SetToRotation(3.14159265358979 / 4, _ oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0)) Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1)) Dim oOcc As ComponentOccurrence oOcc = oAsmCompDef.Occurrences.Add(oPartToAdd, oMatrix) End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods