2 Replies Latest reply on May 25, 2017 3:45 AM by Di Anne

    ReplaceViewModel(IDrawingDoc) not working

    Edgar Quintana

      The example in SW API help is not doing what is supposed to do. I 'm assuming that the API call is not functioning correctly. Can anybody run this macro and get the correct output?

       

      This example shows how to replace a model in drawing views.

       

       

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

      ' Preconditions:

      ' 1. Open: install_dir\samples\tutorial\api\assem20.slddrw.

      ' 2. Ensure that the specified replacement model exists.

      '

      ' Postconditions: The specified component in Drawing View1 is replaced

      ' with the specified model.

      '

      ' NOTE: Because the model is used elsewhere, do not save changes

      ' when closing it.

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

      Dim swApp As SldWorks.SldWorks

      Dim Part As SldWorks.ModelDoc2

      Dim draw As SldWorks.DrawingDoc

      Dim selMgr As SldWorks.SelectionMgr

      Dim vViews(0) As Variant

      Dim vInstances(0) As Variant

      Dim boolstatus As Boolean

       

       

      Option Explicit

       

       

      Sub main()

       

       

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

          Set draw = Part

         

       

       

          boolstatus = Part.ActivateView("Drawing View1")

         

       

       

          ' Select the view in which to replace the model

          boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

          Set selMgr = Part.SelectionManager

          Set vViews(0) = selMgr.GetSelectedObject6(1, -1)

         

       

       

          ' Select the instance of the model to replace

          boolstatus = Part.Extension.SelectByID2("Assem20-3@Drawing View1/block20-1@assem20", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

          Set vInstances(0) = selMgr.GetSelectedObject6(1, -1)

         

       

       

          boolstatus = draw.ReplaceViewModel("C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\api\bagel.sldprt", vViews, vInstances)

       

       

      End Sub

        • Re: ReplaceViewModel(IDrawingDoc) not working
          Edgar Quintana

          I contacted apisupport@solidworks.com and they found an error on this example. They sent me a correction, i haven't tested yet, but i assume it should work now.

           

           

          /////////////////////////////////////

           

          Hi Edgar,

           

          Sorry about the issue with that help example. I opened SPR 818790 to modify the example by our documentation team. We will try to get that into the next service pack of SolidWorks 2014.

           

          Here is the code as it should be:

           

          Option Explicit

           

          Dim swApp As SldWorks.SldWorks

          Dim Part As SldWorks.ModelDoc2

          Dim draw As SldWorks.DrawingDoc

          Dim selMgr As SldWorks.SelectionMgr

          Dim vViews As Variant

          Dim viewArray(0) As Object

          Dim vInstances As Variant

          Dim InstanceArray(0) As Object

          Dim boolstatus As Boolean

          Dim drComp As SldWorks.DrawingComponent

           

           

           

          Sub main()

           

           

              Set swApp = Application.SldWorks

              Set Part = swApp.ActiveDoc

              Set draw = Part

             

              boolstatus = Part.ActivateView("Drawing View1")

             

              ' Select the view in which to replace the model

              boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

              Set selMgr = Part.SelectionManager

              Set viewArray(0) = selMgr.GetSelectedObject6(1, -1)

              vViews = viewArray

             

              ' Select the instance of the model to replace

              boolstatus = Part.Extension.SelectByID2("Assem20-3@Drawing View1/block20-1@assem20", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

              Set InstanceArray(0) = selMgr.GetSelectedObject6(1, -1)

              Set drComp = InstanceArray(0)

              Set InstanceArray(0) = drComp.Component

              vInstances = InstanceArray

           

           

              boolstatus = draw.ReplaceViewModel("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\samples\tutorial\api\bagel.sldprt", (vViews), (vInstances))

           

           

          End Sub

           

          I hope this helps.

           

          Regards,

           

          Mick Kozikowski

          Sr. API Support Engineer

          Dassault Systèmes SolidWorks Corp.