4 Replies Latest reply on Sep 6, 2016 7:52 PM by Yong Ning

    How to Replace model reference in view on drawing

    Yong Ning

      changeModelName.jpg

      Follow code , don't change  View reference Model name.

      Private Sub ll()

         Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2, oModel As ModelDoc2

             Set SwApp = Application.SldWorks

             Set SwModel = SwApp.ActiveDoc

         Dim SwSelMgr As SelectionMgr

             Set SwSelMgr = SwModel.SelectionManager

         Dim SwView As View

             Set SwView = SwSelMgr.GetSelectedObject5(1)

             Debug.Print SwView.Name,

             Set oModel = SwView.ReferencedDocument

             Debug.Print oModel.GetPathName

             tmp = SwView.LoadModel

             Stop

         

       

      End Sub

      *************************************************************************

      2014 SOLIDWORKS Help - Replacing Model in a Drawing View  http://help.solidworks.com/2014/English/SolidWorks/sldworks/t_replace_model_in_view.htm

      Manual method

       

      OpenModel.jpg

       

      To edit part location:

       

       

      Click File, Open, References, or click File, Save As, References.

      In the New pathname list, select a path or filename that you want to edit , or click Select All.

      You can edit the path or filename using one of the following methods:

      Select a list entry by double-clicking. Browse to a new folder or type a new name in the Open dialog box, then click Open.

      Select the check box for a list entry and click Browse to change to a new drive or directory path. The new drive or path appears in the New folder box.

       

       

      Click Replace and enter a word to find and a word to replace with. (For example, change "\samples" to "\archive" to move one or all part files to the archive directory.) Click the Match case check box if the case is significant in the search.

          • Re: How to Replace model reference in view on drawing
            Yong Ning

            Thank you tip

             

            '-----------------------------------------

            '

            ' Preconditions:

            '           (1) c:\samples\whistles.sldasm, whistle.sldprt, and whistleLonger.sldprt

            '               exist.

            '           (2) c:\samples\whistles.sldasm is not open.

            '

            ' Postconditions: c:\samples\whistleLonger.sldprt replaced c:\samples\whistle.sldprt

            '               reference within c:\samples\whistles.sldasm.

            '

            '-----------------------------------------

            Option Explicit

            Const sReferencingDoc       As String = "c:\samples\whistles.sldasm"

            Const sOldDoc               As String = "c:\samples\whistle.sldprt"

            Const sNewDoc               As String = "c:\samples\whistleLonger.sldprt"

            Sub main()

                Dim swApp               As SldWorks.SldWorks

                Dim bRet                As Boolean

             

                Set swApp = CreateObject("SldWorks.Application")

             

                bRet = swApp.ReplaceReferencedDocument(sReferencingDoc, sOldDoc, sNewDoc)

                Debug.Assert bRet

            End Sub

             

            **********************************************

             

            Dim sReferencingDoc, sOldDoc, sNewDoc
            Sub main()
                Dim swApp               As SldWorks.SldWorks
                Dim bRet                As Boolean
                Dim SwPart As SldWorks.ModelDoc2
                Dim Str, Path
                Set swApp = Application.SldWorks
                Set SwPart = swApp.ActiveDoc
                Str = SwPart.GetPathName
                Path = Left(Str, InStrRev(Str, "\"))
                'Debug.Print Path
                Set swApp = CreateObject("SldWorks.Application")
                sReferencingDoc = Path & "a.slddrw"
                sOldDoc = Path & "a.sldprt"
                sNewDoc = Path & "b.sldprt"
                bRet = swApp.ReplaceReferencedDocument(sReferencingDoc, sOldDoc, sNewDoc)
                'Debug.Assert bRet
            End Sub

             

            *****************************************************

            Private Sub del20()

               Dim Xls As Excel.Application, Rng As Range

                    Set Xls = GetObject(, "Excel.Application")

                    Set Rng = Xls.Selection

               Dim sReferencingDoc, sOldDoc, sNewDoc

                    sOldDoc = Rng(1, 3)

                    sNewDoc = Rng(1, 4)

               Dim SwApp As SldWorks.SldWorks

                    Set SwApp = Application.SldWorks

               Dim DrawRng As Range, FileName

                    Set DrawRng = Xls.Range(Rng(1, 1).Formula)

                    Debug.Print DrawRng.Address

                    For ii = 1 To DrawRng.Rows.Count

                         FileName = Rng(1, 2) & DrawRng(ii, 1)

                         tmp = SwApp.ReplaceReferencedDocument(FileName, sOldDoc, sNewDoc)

                         Debug.Print DrawRng(ii, 1), tmp

                    Next ii

            End Sub

            • Re: How to Replace model reference in view on drawing
              Anton Miller

              Is there a way to do like a get current reference file name, then use that instead of specifying a file name each time?