7 Replies Latest reply on Nov 24, 2017 2:18 AM by Andreas Killer

    API: How to load a reference model into a drawing view

    Andreas Killer

      Hello, I need some help to find the right commands for the following (I hope there are some...)

      Attached is an assembly "demo.sldasm" (all parts included) and a drawing template "Spannskizze.drwdot".

       

      The drawing template has (currently) 3 sheets and each sheet has one predefined view. If I create a drawing with this template from my assembly only the view on the first sheet is filled. See read arrows. This is a SW issue and a SPR is already running.

       

      a) How can I set the reference model in a view same as in the first view?

       

      And my assembly has a visible 3D sktech named "Nullpunkt", but inside the view(s) the sketch is always hidden. See blue arrow.

       

      b) How can I make it visible?

       

      As this is a basic setup for a SolidCAM scenario, I really need it often in the future. Therefore I want to automate this task.

       

      Unbenannt.PNG

      Below is all the code I have so far.

       

      Andreas.

       

      Option Explicit

       

      Sub Main()
        Dim swApp As SldWorks.SldWorks
        Dim swModel As SldWorks.ModelDoc2
        Dim swDraw As SldWorks.DrawingDoc
        Dim swSheet As SldWorks.Sheet
        Dim swView As SldWorks.View
        Dim vSheetName, vModelName
        Dim i As Long

       

        Set swApp = Application.SldWorks
        Set swDraw = swApp.ActiveDoc

       

        vSheetName = swDraw.GetSheetNames
        For i = 0 To UBound(vSheetName)
          swDraw.ActivateSheet vSheetName(i)
          Set swSheet = swDraw.Sheet(vSheetName(i))
          If swSheet.IsLoaded Then
            'Okay
          Else
            Stop
          End If

       

          Set swView = swDraw.GetFirstView
          While Not swView Is Nothing
            Debug.Print "    " & swView.GetName2 & " [" & swView.Type & "]"
            Select Case swView.Type
              Case swDrawingViewTypes_e.swDrawingNamedView
                If IsEmpty(vModelName) Then
                  'Get the model loaded in this view
                  vModelName = swView.GetReferencedModelName
                Else
                  'a) Set the model in this view
                End If

       

                'b) Find the Sketch named "Nullpunkt" inside the view and make it visible
                'Recorded:
                'boolstatus = Part.Extension.SelectByID2("Nullpunkt@Demo-1@Zeichenansicht1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
                'Part.UnblankSketch
            End Select
            Set swView = swView.GetNextView
          Wend
        Next
        'Back to first view
        swDraw.ActivateSheet vSheetName(0)
      End Sub

        • Re: API: How to load a reference model into a drawing view
          Alex Burnett

          It's a bit difficult to figure out exactly what you're trying to do but I'll give it a shot.

           

          It seems you're looking for an empty view (no referenced model) and if it is empty then replace or add a model to reference. If that is the case then this code may help.

           

          2014 SOLIDWORKS API Help - Replace View Model Example (VBA)

            • Re: API: How to load a reference model into a drawing view
              Andreas Killer

              It seems you're looking for an empty view (no referenced model) and if it is empty then replace or add a model to reference.

               

              No.

               

              My drawing template already has predefined views, one view on each sheet. The issue is that only the view on the first (active) sheet is filled.
              I've attached a video, please have a look.

               

              Andreas.

                • Re: API: How to load a reference model into a drawing view
                  Deepak Gupta

                  Andreas, this is unforunately a limitation. So instead of using pre-defined views I would suggest to use macro to place views on each sheet.

                    • Re: API: How to load a reference model into a drawing view
                      Andreas Killer

                      Okay, that might be a possible way.

                       

                      So what's the way to go?

                       

                      IMHO I can remove the views from all sheets, except the first (because the first works and I can get the document name from that view).
                      (Or is it better to remove all views? I don't know.)

                       

                      So how to place a new view in the middle on the other sheets, e.g. 2nd sheet with top view, 3rd sheet with left view?

                       

                      Andreas.

                        • Re: API: How to load a reference model into a drawing view
                          Deepak Gupta

                          Andreas Killer wrote:

                           

                          IMHO I can remove the views from all sheets, except the first (because the first works and I can get the document name from that view).

                          I would go with placing all views in one go saving additional mouse click and time.

                           

                          But I think you can use InsertModelInPredefinedView to populate pre-defined views on each sheet. Let me do a quick test and update.

                            • Re: API: How to load a reference model into a drawing view
                              Andreas Killer

                              But I think you can use InsertModelInPredefinedView to populate pre-defined views on each sheet. Let me do a quick test and update.

                              Great, that is a big step in the right direction! See code below.

                               

                              Now I need the final step:

                              Find the 3D-Sketch named "Nullpunkt" in all views and make it visible

                               

                              Any ideas how to solve that?

                               

                              Andreas.

                               

                               

                              Sub Main()
                                Dim swApp As SldWorks.SldWorks
                                Dim swModel As SldWorks.ModelDoc2
                                Dim swModelDocExt As SldWorks.ModelDocExtension
                                Dim swDraw As SldWorks.DrawingDoc
                                Dim swSheet As SldWorks.Sheet
                                Dim swView As SldWorks.View
                                Dim vSheetName, vModelName
                                Dim i As Long

                               

                                Set swApp = Application.SldWorks
                                Set swDraw = swApp.ActiveDoc
                                Set swModel = swDraw

                               

                                vSheetName = swDraw.GetSheetNames
                                For i = 0 To UBound(vSheetName)
                                  swDraw.ActivateSheet vSheetName(i)
                                  Set swSheet = swDraw.Sheet(vSheetName(i))
                                  If swSheet.IsLoaded Then
                                    'Okay
                                  Else
                                    Stop
                                  End If

                               

                                  Set swView = swDraw.GetFirstView
                                  While Not swView Is Nothing
                                    Debug.Print "    " & swView.GetName2 & " [" & swView.Type & "]"
                                    Select Case swView.Type
                                      Case swDrawingViewTypes_e.swDrawingNamedView
                                        If IsEmpty(vModelName) Then
                                          'Get the model loaded in this view
                                          vModelName = swView.GetReferencedModelName
                                        Else
                                          'Set the model in this view
                                          swDraw.InsertModelInPredefinedView vModelName
                                        End If
                                    End Select
                                    Set swView = swView.GetNextView
                                  Wend
                                Next
                                'Back to first view
                                swDraw.ActivateSheet vSheetName(0)
                                'Force a rebuild!
                                swModel.ForceRebuild3 False

                               

                                'b) Find the 3D-Sketch named "Nullpunkt" in all views and make it visible
                              End Sub

                                • Re: API: How to load a reference model into a drawing view
                                  Andreas Killer

                                  I got it!

                                  ResetSketchVisibility is the key, see final code below.

                                   

                                  I would like to thank everyone who helped me with the solution, which will save me a lot of time in my daily work.

                                   

                                  Andreas.

                                   

                                  Option Explicit

                                   

                                  Sub Main()
                                    Dim swApp As SldWorks.SldWorks
                                    Dim swModel As SldWorks.ModelDoc2
                                    Dim swDraw As SldWorks.DrawingDoc
                                    Dim swSheet As SldWorks.Sheet
                                    Dim swView As SldWorks.View
                                    Dim vSheetName, vModelName
                                    Dim i As Long

                                   

                                    Set swApp = Application.SldWorks
                                    Set swDraw = swApp.ActiveDoc
                                    Set swModel = swDraw

                                   

                                    'Visit each sheet
                                    vSheetName = swDraw.GetSheetNames
                                    For i = 0 To UBound(vSheetName)
                                      'Activate and access
                                      swDraw.ActivateSheet vSheetName(i)
                                      Set swSheet = swDraw.Sheet(vSheetName(i))
                                      If swSheet.IsLoaded Then
                                        'Okay
                                      Else
                                        If MsgBox(vSheetName(i) & " is not loaded.", vbOKCancel + vbDefaultButton2) = vbCancel Then Exit Sub
                                      End If

                                   

                                      'Visit all views
                                      Set swView = swDraw.GetFirstView
                                      While Not swView Is Nothing
                                        'Debug.Print "    " & swView.GetName2 & " [" & swView.Type & "]"
                                        Select Case swView.Type
                                          Case swDrawingViewTypes_e.swDrawingNamedView
                                            If IsEmpty(vModelName) Then
                                              'Get the model loaded in this view
                                              vModelName = swView.GetReferencedModelName
                                            Else
                                              'Set the model in this view
                                              swDraw.InsertModelInPredefinedView vModelName
                                            End If
                                            'Make the sketches visible
                                            swView.ResetSketchVisibility
                                        End Select
                                        Set swView = swView.GetNextView
                                      Wend
                                    Next
                                    'Back to first view
                                    swDraw.ActivateSheet vSheetName(0)
                                    'Force a rebuild!
                                    swModel.ForceRebuild3 False
                                  End Sub