17 Replies Latest reply on Jan 13, 2015 9:28 PM by Keith Rice

    Drawing API Change all view configurations

    Michael Caulton

      I am trying to write a VB.NET macro that changes all the views in a drawing sheet to match the selected configuration but can't get the drawing to behave predictably.

       

      The idea is that there is a single "template" drawing file/sheet pre-setup for a part with multiple configurations. The user selects the desired configuration through the macro GUI and then the drawing updates so all views are correct. This removes the need to multiple sheets for each configuration and the drawings will look more uniform.

       

      The drawing is setup so the biggest configuration is selected. This means when the configuration is changed there should be no overlap. Also during testing if one went from a smaller configuration to larger it would unpredictably move some dimensions.

       

      if I run the code below to create a sketch point at each view position vPos(0),vPos(1), I get the "Image 1". The sketch point is the center point of each of the views.

       

              Dim swModel As IModelDoc2 = swApp.ActiveDoc

              Dim swDraw As SldWorks.DrawingDoc = swModel

              Dim swView As SldWorks.View

       

              Dim sConfig As String = "L1200"

       

              Dim vSheetProps As Object = swDraw.GetCurrentSheet.GetProperties

              Dim swDrawScale As Single = 1 / (vSheetProps(2) / vSheetProps(3))

       

              swView = swDraw.GetFirstView

              swView = swView.GetNextView

       

              Dim vPos As Object

       

              Do While Not swView Is Nothing

       

                  ' Insert a point at vPos(0), vPos(1)

                  vPos = swView.Position

                  InsertPoint(swApp.ActiveDoc, vPos(0) * swDrawScale, vPos(1) * swDrawScale)

       

                  swView = swView.GetNextView

       

              Loop

       

          Sub InsertPoint(swModel As SldWorks.ModelDoc2, x As Double, y As Double)

              Dim swSkMgr As SldWorks.SketchManager = swModel.SketchManager

              Dim skPoint As SldWorks.SketchPoint

              skPoint = swSkMgr.CreatePoint(x, y, 0.0#)

          End Sub

       

      All the sketch points previously created are deleted and the revised code below is then run. The result "Image 2".

       

              Do While Not swView Is Nothing

       

                  ' Change to new configuration

                  If InStr(swView.ReferencedConfiguration, "SM-FLAT-PATTERN") > 0 Then

                      swView.ReferencedConfiguration = sConfig + "SM-FLAT-PATTERN"

                  Else : swView.ReferencedConfiguration = sConfig

                  End If

       

                  ' Insert a point at vPos(0), vPos(1)

                  vPos = swView.Position

                  InsertPoint(swApp.ActiveDoc, vPos(0) * swDrawScale, vPos(1) * swDrawScale)

       

                  swView = swView.GetNextView

              Loop

       

      The sketch point used to identify the position of the views is still in the same location and no longer in the center of the view.

       

      How do I get the views to all stay centered about their middle during the configuration name change?

       

      Attachments:

           Example part and drawing with single sheet

        • Re: Drawing API Change all view configurations
          Daniel Andersson

          It seems like SolidWorks sets the relation between the geometry and the view position at creation of the view. I tried to add a new view using configuration L0600. Ran the macro to insert points, it worked out well, then I changed to a larger configuration and the location of the view position is still the same.

           

          In this case I would aim to use the GetOutline Method (IView) method. And then calculate where to position the view.

           

          The pseudo code would be like...

          1) Get position (prev)

          2) Get Outline (prev)

          3) Change configuration

          4) Get Outline (new)

          5) Compare Outline (prev) and (new)

          6) Set the new position

           

          I have not done this myself before, but this is the path I would try out...

            • Re: Drawing API Change all view configurations
              Daniel Andersson

              Got some time to try it out, this one works when I test run it...

              Test it out and let me know what you find out.

               

              Code in VB.

              ---- REMOVED VB MACRO ----

              Macro was not functioning as it should, removed it from post to avoid confusion.

                • Re: Drawing API Change all view configurations
                  Michael Caulton

                  Hi Daniel, Thanks for taking the time to answer my question.

                   

                  If I run your code with no modification on the freshly opened drawing, I get the result below. I assume this is not what you are getting?

                  Drawing after code.JPG

                   

                  I originally tried a very similar process to what you are recommending but ran into an issue. in the loop, straight after the first view is changed, before the loop got to the second view, the third view ( the one tot he right) would move right and I couldn't overcome this un-requested movement. Images below.

                   

                  The flat pattern view also seems to move in the wrong direction, I think its because the view was originally "flipped" but am not sure. Images below.

                   

                   

                  Code below in VB.

                   

                  Sub main()

                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swDraw As SldWorks.DrawingDoc

                      Dim swView As SldWorks.View

                     

                      Dim bRet As Boolean

                     

                      Set swApp = CreateObject("SldWorks.Application")

                      Set swModel = swApp.ActiveDoc

                      Set swDraw = swModel

                      Set swView = swDraw.GetFirstView

                     

                      Set swView = swView.GetNextView

                     

                      Dim sConfig As String

                      sConfig = "L1200"

                     

                      Do While Not swView Is Nothing

                     

                          'If swView.LinkParentConfiguration = False Then

                              ChangeView swView, sConfig

                              swModel.ForceRebuild3 False

                              Stop

                          'End If

                          Set swView = swView.GetNextView

                     

                      Loop

                        

                  End Sub

                   

                  Sub ChangeView(swView As SldWorks.View, sConfigName As String)

                     

                  Dim xWidth As Double, yHeight As Double

                  Dim xCentre As Double, yCentre As Double

                  Dim vOutline As Variant

                  Dim vPos As Variant

                     

                      vOutline = swView.GetOutline

                      vPos = swView.Position

                     

                      Debug.Print "View = " + swView.Name

                      Debug.Print "  Pos = (" & Round(vPos(0), 3) & ", " & Round(vPos(1), 3) & ") m"

                     

                      ' Get width and height of view

                      xWidth = (vOutline(2) - vOutline(0))

                      yHeight = (vOutline(3) - vOutline(1))

                      Debug.Print "Width = " & xWidth & "   Height = " & yHeight

                     

                      ' Get centre co-ordinates of view

                      xCentre = vPos(0) + (xWidth / 2)

                      yCentre = vPos(1) - (yHeight / 2)

                      Debug.Print "xCentre = " & Round(xCentre, 3) & "   yCentre = " & Round(yCentre, 3)

                                

                      ' Change to new configuration

                      If InStr(swView.ReferencedConfiguration, "SM-FLAT-PATTERN") > 0 Then

                          swView.ReferencedConfiguration = sConfigName + "SM-FLAT-PATTERN"

                      Else

                          swView.ReferencedConfiguration = sConfigName

                      End If

                     

                      ' Get new config outline size

                      vOutline = swView.GetOutline

                     

                      ' Get width and height of view

                      xWidth = (vOutline(2) - vOutline(0))

                      yHeight = (vOutline(3) - vOutline(1))

                      Debug.Print "Width = " & xWidth & "   Height = " & yHeight

                     

                      ' Redefine xy start positions

                      vPos(0) = xCentre - (xWidth / 2)

                      vPos(1) = yCentre + (yHeight / 2)

                      Debug.Print "  Pos = (" & Round(vPos(0), 3) & ", " & Round(vPos(1), 3) & ") m"

                      swView.Position = vPos

                     

                  End Sub

                   

                  Before, after one loop and complete showing how right view moved and flatt patter moved incorrectly

                  Before.JPG Single loop.JPGAfter.JPG

                  javascript:;

                    • Re: Drawing API Change all view configurations
                      Daniel Andersson

                      Sorry for my bad code, turned out that I with some magic got it to work when I tried it yesterday. Perhaps I was not focused... since there was some mistakes in it.

                       

                      But I had a new try this evening. Due to the behaviour of the flat pattern view, I changed approach. I tried out using the outline coordinates to determine how to reposition the view.

                      It works fine for all view... except one. The view that still play tricks is the one you have circled red. Found out that the unwanted movement does not happen if the alignment is removed. I will think about this since there got to be some logical explanation to this. I even added a IF statement in the code so that view is not even moved to the same location as it is by the macro.

                       

                      One idea as a workaround, without understanding why, would be to remove and then add the alignment with the macro... or just simply remove the alignment in the template.

                      I do not like the workaround idea, but it is at least a short term workaround if you have limited time right now.

                       

                      Another idea is to reposition back the view. This is possible since you can determine if the view should be moved and if it has been moved. I would have tried this idea but ran out of time this evening.


                      Here is my updated code (VB), give it a try and let me know what you find out.

                              Dim swModel As IModelDoc2 = swApp.ActiveDoc
                              Dim swDraw As DrawingDoc = swModel
                              Dim swView As View
                              Dim sConfig As String = "L1200"

                              Dim vSheetProps As Object = swDraw.GetCurrentSheet.GetProperties
                              Dim swDrawScale As Single = 1 / (vSheetProps(2) / vSheetProps(3))

                              swView = swDraw.GetFirstView
                              swView = swView.GetNextView

                       

                              Dim vPos As Object
                              Dim objPrevOutline As Object
                              Dim objCurrOutline As Object
                              Dim dblViewOffset(3) As Double

                       

                              Do While Not swView Is Nothing
                                  vPos = swView.Position

                                  objPrevOutline = swView.GetOutline

                       

                                  ' Change to new configuration
                                  If InStr(swView.ReferencedConfiguration, "SM-FLAT-PATTERN") > 0 Then
                                      swView.ReferencedConfiguration = sConfig + "SM-FLAT-PATTERN"
                                  Else : swView.ReferencedConfiguration = sConfig
                                  End If

                       

                                  objCurrOutline = swView.GetOutline

                                  dblViewOffset(0) = (objPrevOutline(0) - objCurrOutline(0)) / 2
                                  dblViewOffset(1) = (objPrevOutline(1) - objCurrOutline(1)) / 2
                                  dblViewOffset(2) = (objPrevOutline(2) - objCurrOutline(2)) / 2
                                  dblViewOffset(3) = (objPrevOutline(3) - objCurrOutline(3)) / 2

                       

                                  'Adjust the position of the view
                                  If vPos(0) <> vPos(0) + dblViewOffset(0) + dblViewOffset(2) Or vPos(1) <> vPos(1) + dblViewOffset(1) + dblViewOffset(3) Then
                                      vPos(0) = vPos(0) + dblViewOffset(0) + dblViewOffset(2)
                                      vPos(1) = vPos(1) + dblViewOffset(1) + dblViewOffset(3)
                                      Debug.Print("Post_vPos(0): " & vPos(0))
                                      Debug.Print("Post_vPos(1): " & vPos(1))
                                      swView.Position = vPos
                                  End If

                       

                                  swView = swView.GetNextView

                       

                              Loop

                       

                              swModel.EditRebuild3()


                      Before macro:
                      Capture.PNG

                       

                      After macro (removed view alignment of the profile view):

                      Capture2.PNG

                        • Re: Drawing API Change all view configurations
                          Michael Caulton

                          If I break the alignment as per your recommendation then run the first loop to change the config on the first view, the view boundaries overlap prior to a rebuild as per images below which i why I think the view might be jumping to the side.

                           

                          View 1 config change to 1200 but before a rebuild

                          View Overlap.JPG

                          After a rebuild

                          View Overlap after rebuild.JPG

                           

                          I agree the breaking of the drawing alignment is not a nice way to do it and I am now thinking I may want to datum some of the view from the points in red below because when on goes to the 600mm view the 3 standard views spread out too much with them datumed around the centre. View 1,2,3,4 as always going to be the same as they are automatically created through the drawing template.

                           

                          To achieve this I think I am going to have to run through the views and record their position and outline, then run through the views again to update configurations and compare to the original spec and preposition as needed. I may also need to look at adjusting scale to increase view size to keep better proportions.

                           

                          Datum Views.jpg

                           

                          L600 config showing disproportionately large gap.

                          L600.JPG

                            • Re: Drawing API Change all view configurations
                              Has Bal

                              Hi Michael,

                               

                              I have managed this problem with  page scale arrangement. It is so complicated.  When you change your dimensions to lower case you should change sheet scale and should arrange center coordinates of drawing views.

                               

                              to do this.

                               

                                            swDraw = ((DrawingDoc)(swDoc));

                                          myView = ((SolidWorks.Interop.sldworks.View)(swDraw.DropDrawingViewFromPalette2("Drawing View1" , xLocation, yLocation, 0)));

                                          swDoc.Rebuild((int)swRebuildOptions_e.swCurrentSheetDisp);

                               

                              However it is about c# , you may find the correct code in VBA.

                               

                              Best Regards.

                              • Re: Drawing API Change all view configurations
                                Daniel Andersson

                                Interesting details, it is for sure the reason you have found.

                                 

                                I was able to reposition the view back to where it should be. Just made a simple loop that checks if the view should have been moved or not and if needed it changes back the position of the view. So this might be an option to solve the tricks the view does.

                                 

                                Just some thoughts about changing view scale....

                                I personally would not change the scale of the sheet to have the views to close the gaps on the drawing better. As a mechanical engineer, I usually have the point of view that the sheet scale (or view scale) is set depending on the level of details that needs to be presented. I would go for changing the size of the sheet instead and keep the scale.

                                 

                                I have also seen this scenario in a previous position I had within a company that had automated design to great extent. In that case, they just where happy to leave the drawings with large gaps. It still provides the necessary information even if it might look funny to some people, and the engineering time was cut down alot.

                                 

                                Depending on how your project is planned you might think of implement this as a second step in order to be able to release some of the automation to the engineers. Just wanted to think out loud and share my thoughts about it...

                                 

                                Just let me know if you need any further assistance with the macro.

                                // Daniel

                                  • Re: Drawing API Change all view configurations
                                    Has Bal

                                    You are right at some point. For this example actually we don't need to change sheet scale due to staying layout borders.

                                    I've got a question. If our drawing view bigger than the size of layout. How can we arrange it's scale instead of changing paper size.

                                    I am working on it and trying to figure out its logic. Therefore your conversation has taken my attention.

                                     

                                    best regards.

                                    • Re: Drawing API Change all view configurations
                                      Michael Caulton

                                      Daniel, Can you please post the code you used to track the change in position. I always find it interesting how others achieve the same thing to see if I can learn anything.

                                       

                                      I agree about not changing the view scales. I think it will cause more headaches than its worth, but if I get time, which I probably wont, I might look into it more, but for not the solution we have come up with is great.

                                       

                                      Thanks for all the help.

                                        • Re: Drawing API Change all view configurations
                                          Daniel Andersson

                                          I just played around a bit and did not save it. I will get back to you with an update of how I did that, will try to update tomorrow or by the latest during the weekend.

                                          • Re: Drawing API Change all view configurations
                                            Daniel Andersson

                                            I changed the loops to use the array of views that is returned by the ISheet interface. I was thinking of creating a new class and using a collection for the views and the data that is stored in array myView. But I just went for using an array to store the previous position and have a boolean value if the view should be moved or not. It might be worth thinking of to encapsulate all the functions into a new class, but I was a bit lazy tonight...

                                             

                                             

                                            Partial Class SolidWorksMacro

                                                Public Sub main()
                                                    Dim swModel As IModelDoc2 = swApp.ActiveDoc
                                                    Dim swDraw As DrawingDoc = swModel
                                                    Dim swSheet As Sheet = swDraw.GetCurrentSheet
                                                    Dim swViews As Object = swSheet.GetViews
                                                    Dim swView As View
                                                    Dim sConfig As String = "L1200"

                                                    Dim vSheetProps As Object = swSheet.GetProperties
                                                    Dim swDrawScale As Single = 1 / (vSheetProps(2) / vSheetProps(3))

                                                    Dim myView(0) As viewData
                                                    Dim i As Integer
                                                    Dim vPos As Object
                                                    Dim objPrevOutline As Object
                                                    Dim objCurrOutline As Object
                                                    Dim dblViewOffset(3) As Double

                                             

                                                    i = 0

                                             

                                                    For Each swView In swViews
                                                        If i > 0 Then ReDim Preserve myView((myView.Length) + 1)
                                                        vPos = swView.Position

                                                        myView(i).Name = swView.GetName2
                                                        myView(i).prevPosX = vPos(0)
                                                        myView(i).prevPosY = vPos(1)
                                                        myView(i).Translate = True

                                                        i = i + 1
                                                    Next

                                             

                                                    For Each swView In swViews
                                                        vPos = swView.Position
                                                        objPrevOutline = swView.GetOutline

                                             

                                                        ' Change to new configuration
                                                        If InStr(swView.ReferencedConfiguration, "SM-FLAT-PATTERN") > 0 Then
                                                            swView.ReferencedConfiguration = sConfig + "SM-FLAT-PATTERN"
                                                        Else : swView.ReferencedConfiguration = sConfig
                                                        End If

                                             

                                                        objCurrOutline = swView.GetOutline

                                                        dblViewOffset(0) = (objPrevOutline(0) - objCurrOutline(0)) / 2
                                                        dblViewOffset(1) = (objPrevOutline(1) - objCurrOutline(1)) / 2
                                                        dblViewOffset(2) = (objPrevOutline(2) - objCurrOutline(2)) / 2
                                                        dblViewOffset(3) = (objPrevOutline(3) - objCurrOutline(3)) / 2

                                             

                                                        If dblViewOffset(0) + dblViewOffset(2) + dblViewOffset(1) + dblViewOffset(3) = 0 Then
                                                            For i = 0 To myView.Length - 1
                                                                If myView(i).Name = swView.GetName2 Then
                                                                    myView(i).Translate = False
                                                                    Exit For
                                                                End If
                                                            Next
                                                        End If

                                             

                                                        'Adjust the position of the view
                                                        vPos(0) = vPos(0) + dblViewOffset(0) + dblViewOffset(2)
                                                        vPos(1) = vPos(1) + dblViewOffset(1) + dblViewOffset(3)
                                                        swView.Position = vPos
                                                    Next

                                             

                                                    For Each swView In swViews
                                                        For i = 0 To myView.Length - 1
                                                            If swView.GetName2 = myView(i).Name And myView(i).Translate = False Then
                                                                vPos(0) = myView(i).prevPosX
                                                                vPos(1) = myView(i).prevPosY
                                                                swView.Position = vPos
                                                            End If
                                                        Next
                                                    Next

                                             

                                                    swModel.EditRebuild3()

                                             

                                                End Sub

                                             

                                                ''' <summary>
                                                ''' The SldWorks swApp variable is pre-assigned for you.
                                                ''' </summary>
                                                Public swApp As SldWorks

                                             

                                                Private Structure viewData
                                                    Dim Name As String
                                                    Dim prevPosX As Double
                                                    Dim prevPosY As Double
                                                    Dim Translate As Boolean
                                                End Structure

                                             

                                            End Class

                                             

                                              • Re: Drawing API Change all view configurations
                                                Michael Caulton

                                                I ended up changing it a little because I was also getting all The palette views too when I used "GetViews". I also added the outline into the routine.

                                                 

                                                I have marked one of the previous replies as correct. thanks for all the help.

                                                 

                                                    Structure ViewData

                                                        Dim Name As String

                                                        Dim Pos As Object

                                                        Dim Outline As Object

                                                        Dim Translate As Boolean

                                                    End Structure

                                                 

                                                        Dim aViewData(0) As ViewData

                                                 

                                                        swView = swDraw.GetFirstView ' Get sheet

                                                        swView = swView.GetNextView ' Get first view

                                                        Dim nView As Long = 1

                                                 

                                                        ' Get all view positions and outlines

                                                        Do While Not swView Is Nothing

                                                            ReDim Preserve aViewData(aViewData.Length + 1)

                                                            aViewData(nView).Name = swView.GetName2

                                                            'Debug.Print(aViewData(nView).Name)

                                                            aViewData(nView).Pos = swView.Position

                                                            'Debug.Print(vbTab + aViewData(nView).Pos(0).ToString + " " + aViewData(nView).Pos(1).ToString)

                                                            aViewData(nView).Outline = swView.GetOutline

                                                            'Debug.Print(vbTab + aViewData(nView).Outline(0).ToString + " " + aViewData(nView).Outline(1).ToString + " " + aViewData(nView).Outline(2).ToString + " " + aViewData(nView).Outline(3).ToString)

                                                            aViewData(nView).Translate = True

                                                            swView = swView.GetNextView ' Get first view

                                                            nView = nView + 1

                                                        Loop

                                                • Re: Drawing API Change all view configurations
                                                  Has Bal

                                                  Deniel,

                                                   

                                                  I am trying to find outline and center position of a drawing view. I have managed thanks to your help.

                                                  When i go further for my purpose to reach.I faced one problem.

                                                  My aim is that; first draw a part and draw its drawing and save them.

                                                  After, I will change part's outside dimension.

                                                  Next, open its drawing and find outline of drawing view and its center position to evaluate and change its position.

                                                  You can see in picture.

                                                  pic 1 ; 50x50 mm square block

                                                  pic 2; 50 x50mm square block drawing view and its position and outlines.

                                                   

                                                  pic 3; changing of square to  rectangular block which outside dimension is 200x50 mm.

                                                  pic 4; new block drawing and its position and outline coordinates.

                                                   

                                                  As you can see, when I change dimension of parts its drawing view center position stay same.

                                                  It is problem for me . Because when i try to move drawing view to another coordinate ,in picture 5, its center position still act like old position.So I cannot understand its center position. (I know and I can  calculate  from outline point its center coordinate but it produce heavy calculation for future) 

                                                   

                                                   

                                                  Finally , are there anything to refresh its center position?

                                                  Pic 11.jpg

                                                  pic22.jpg

                                                  pic 33.jpg

                                                  pic44.jpg

                                                  pic 55.jpg

                                                    • Re: Drawing API Change all view configurations
                                                      Michael Caulton

                                                      I used configurations to setup the different sized parts I needed, then setup the drawing to look correct with the largest view. I then used the macro to change configuration to a smaller part and offset it accordingly.

                                                       

                                                      This way you can measure the outline of the view, change view and remeasure the outline and then offset the view the correct amount.

                                                       

                                                      The way you are doing it you will have to constant setup for the original part pos and outline and then offset it against the constant values once you have changed the part dimensions. I do not think this is a good solution because  its is not universal across multiple drawings whereas the method i was using was.

                                                       

                                                      The alternative would be to open the drawing with the original part, get the pos and outline, then change the dimensions, then get the new pos and outline and offset the view accordingly.

                                                        • Re: Drawing API Change all view configurations
                                                          Daniel Andersson

                                                          I agree with Michael.

                                                           

                                                          To that I would like to add that I do not know if you could change the position of the origin position of the view.

                                                          It seems like that the relation between the position property of the view and one of the corners of the outline is set when the view is created.

                                                           

                                                          I do not know if it is possible or not to change that relation. Have to say that I'm a mechanical design engineer that just like coding when I have some time left, so I do not know that many details. Software developers such as Keith Rice and Artem Taturevych knows endlessly more about the API model for SolidWorks.

                                                           

                                                          The solution that we came up with above is a result of using the outlines as Michael describes. Instead of using the position. Then the difference between new and old outline is compared. The comparision is then used to relocate the position of the view.