2 Replies Latest reply on Apr 26, 2013 11:57 AM by Simon Turner

    Copying File And Then Replacing Component With Document Manager Not Working API

    Aj Jones

      I have a SolidWorks assembly file that I am copying in VB.Net into the same folder as a new name.  So I now have two assemblies that are identical, but different names.  The code I'm using to do this is:





      This works fine, and copies exactly how I want it to.  Right after this code is ran, I use the replace component code from the following link:



      I replaced the license key, and the directory for that of the C:\NewCopied.sldasm as well as the components to replace, and it will not work.  If I open up C:\NewCopied.sldasm, the components I told it to replace are not replaced.  It even says "True" on the output in Visual Studio that the swDmComponent.Replace went through just fine.


      If I use the same code, and only change the assembly to a different one, it works fine.  Is there something about copying the file that makes it to where the document manager doesn't interact with it correctly?



        • Re: Copying File And Then Replacing Component With Document Manager Not Working API
          Artem Taturevych

          Looks very strange. Can you try two things:


          1) Try to copy this assembly manually and run your code

          2) Try to use DM API instead File.Copy to copy the document: ISwDMApplication::CopyDocument



          Artem Taturevych, Application Engineer at Intercad (Australia)


          translationXpert – add-in to translate SolidWorks models

          LinkedIn SolidWorks API Education Group

          • Re: Copying File And Then Replacing Component With Document Manager Not Working API
            Simon Turner

            I remember running into this in the past.

            I think you have to call SWDMDocument:GetAllExternalReferences4 - not sure why - just what the API documentation says to do.

            Then call SWDMDocument:ReplaceReference for each of the parts to replace.


            Here is a snippet from a working VBA macro:


                    Dim varRef As Variant, brokenRefVar As Variant, IsVirtual As Variant, myTimeStamp As Variant

                    Dim myDoc As SwDMDocument13


                    Set myDoc = myApp.GetDocument(aFile, swDmDocumentAssembly, False, errors)

            If Not myDoc Is Nothing Then

                    varRef = myDoc.GetAllExternalReferences4(src, brokenRefVar, IsVirtual, myTimeStamp)

                    If Not IsEmpty(varRef) Then

                        For i = 0 To UBound(myComps) 'myComps is an array of filenames to be replaced. myReps is an array of the same size containing the filenames to replace with

                            myDoc.ReplaceReference myComps(i), myReps(i)


                    End If



            End If