8 Replies Latest reply on Mar 22, 2016 3:19 PM by Kevin Chisholm

    Selecting view on the right

    Kevin Chisholm

      Hi everyone,


      I'm currently writing a macro to verify a drawing before saving as a .DWG file.

      I want to verify that the laser view uses swView.UseSheetScale

      but I don't really want the user to select the view each time.


      My question is : Is there a way to select a view that is always to the right of a line?


      Here's an example of the view I want to select:

      I was going to select by type but sometimes the view on the left is the same type...


      Thanks for any help!

      Kevin Chisholm

        • Re: Selecting view on the right
          John Alexander

          For problems like this, it is wise to consider the nature of the task. Are there already drawings that need this macro to be run on them or is this intended for future drawings?


          You could write something that is very general:

          1. traverse all sketch entities on the sheet
          2. test if each is a line that bisects the drawing
          3. get its x coordinates
          4. traverse all views and select any that are located to the right of that coordinate

          Potential problems with this approach involve determining which line should be used as the "boundary". The drafter might have inserted that construction line on any number of views/sketches, they might not have used a consistent line font, they might not have bisected the drawing completely. Checking whether or not a line bisects the drawing could be a matter of comparing the start and end points' y coordinates and testing whether or not they are above and below a threshold value (as a percentage of the overall height of the sheet). This way, you could easily tune the behavior by modifying those thresholds.


          Alternatively, if the view that you want to select has a particular name and you create a drwdot file (drawing start model) with that view on it with the appropriate name assigned, it makes the macro easier to implement but less flexible. The macro would only have to traverse the views on a sheet and check whether or not they have a particular name.

            • Re: Selecting view on the right
              John Alexander

              Easier suggestion: check the x coordinate of all views, select the one that is right-most. It looks like the laser view is always to the right of the others, yeah?


              Just a rough outline:

              Dim vView as variant

              Dim swSheet as sldworks.sheet

              Dim swView as sldworks.view

              Dim ViewToSelect as Sldworks.View

              Dim xCoordinate as Double

              Dim xMax as Double

              'set sheet to current active sheet somehow

              xMax = 0 'a more sophisticated thing to do would be to initialize this with the xCoordinate of the first view on the sheet (but 0 should suffice)

              for each vView in swSheet.getViews

                   set swView = vView

                   xCoordinate = getXCoordinate(swView) 'getXCoordinate is an exercise left to the reader

                   if xMax < xCoordinate Then

                        xMax = xCoordinate

                        ViewToSelect = swView

                   end if

              next vView

              • Re: Selecting view on the right
                Kevin Chisholm

                Hi John,


                That constructed line is in the template of our drawing and isn't set by a drafter each time.

                It's set in the background... which I just realized could be another problem.

                The macro will run on new drawings only since I want to verify different options

                before saving as DWG... Old drawing should be already fixed if they had a problem.


                I'll play around with the steps you mentioned to see if I can get anything started.

                Just not sure about selecting views that are located right of a coordinate since I've

                never played around with the SelectionManager.


                I'll post back "when" I run into problems!!


                Thanks for the help!

                Kevin Chisholm

              • Re: Selecting view on the right
                John Alexander

                Cool, if you're struggling with getting the reference to the sheet or views or with getting the x coordinate from those objects, I could post something more concrete later today.

                  • Re: Selecting view on the right
                    Kevin Chisholm

                    Thanks again John!

                    Here is what I came up with quickly...

                    Note: I changed ViewToSelect to String instead of Sldworks.View... Since the line "ViewToSelect = swView" didn't work.


                    As long as I get the name of the view I can later select that view!


                    Option Explicit
                    Sub main()
                        Dim vView As Variant
                        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 ViewToSelect As String
                        Dim xCoordinate() As Double
                        Dim xMax As Double
                        Set swApp = CreateObject("SldWorks.Application")
                        Set swModel = swApp.ActiveDoc
                        Set swDraw = swModel
                        Set swSheet = swDraw.GetCurrentSheet
                        xMax = 0
                            For Each vView In swSheet.GetViews
                            Set swView = vView
                            xCoordinate = swView.Position
                                If xMax < xCoordinate(0) Then
                                xMax = xCoordinate(0)
                                ViewToSelect = swView.Name
                            End If
                            Next vView
                        Debug.Print "View = " + ViewToSelect
                        Debug.Print "  X = (" & xMax * 1000# & ") mm"
                    End Sub


                    Thanks again for the help!

                    Kevin Chisholm