AnsweredAssumed Answered

Macro to insert parts to an assembly - help with origin matching?

Question asked by John Shaw on Apr 8, 2020
Latest reply on Jan 31, 2021 by Deepak Gupta

Hi all, I am working on a macro to inserting multiple parts into an assembly from a single folder location. It assumes the assembly is already open.


It loops through fine, however only the first part is coincident and fixed to the assembly origin. All other parts are not coincident to the origin, even visually.  How would I correct this? I would prefer to do it after each part is added rather than all at the end. Code below, any help gratefully received!


Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssem As SldWorks.AssemblyDoc
Dim boolstatus As Boolean
Dim sFileName As String
Dim Path As String
Dim nErrors As Long
Dim nWarnings As Long


On Error Resume Next

Set swApp = Application.SldWorks
Set swAssem = swApp.ActiveDoc

Path = "E:\Scripting\"
sFileName = Dir(Path & "*.sldprt")

Do Until sFileName = ""

Set swModel = swApp.OpenDoc6(Path & sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
boolstatus = swAssem.AddComponent(Path & sFileName, 0, 0, 0)
swApp.CloseDoc Path & sFileName

Set swModel = Nothing
Set swAssem = swApp.ActiveDoc
sFileName = Dir


End Sub


Thanks, John

Credit: Deepak Gupta's code on this forum has already helped greatly, thank you sir.