4 Replies Latest reply on Dec 25, 2016 5:56 AM by Yong Ning

    How to create line in view

    Yong Ning

      Follow code,The location of the Drawing line is wrong

       

      CreateLine.jpg

       

       

      Coordinate , How to create point in view.

       

       

      Point.jpg

       

       

       

      Function CreateCenterLine(SwModel As ModelDoc2, SwDispDim As DisplayDimension)

         Dim SwAnn As Annotation, Ss

             Set SwAnn = SwDispDim.GetAnnotation

             Ss = SwAnn.GetAttachedEntities

         Dim SwPt As SketchPoint, SwSketchSeg As SketchSegment

         Dim SwSketchLine As SketchLine

         Dim X(1), Y(1)

             For ii = 0 To 1

                Set SwPt = Ss(ii)

                With SwPt

                   X(ii) = .X

                   Y(ii) = .Y

                End With

             Next ii

             Set SwSketchLine = SwModel.CreateLine2(X(0), Y(0), 0, X(1), Y(1), 0)

             Set SwSketchSeg = SwSketchLine

             SwSketchSeg.ConstructionGeometry = True

             SwSketchSeg.Layer = "中心线"

             Stop

       

       

       

       

      End Function

       

       

       

       

      Private Sub ll()

         Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

             Set SwApp = Application.SldWorks

             Set SwModel = SwApp.ActiveDoc

         Dim SwDraw As DrawingDoc

             Set SwDraw = SwModel

         Dim SwView As View

             Set SwView = SwDraw.GetFirstView '

             Set SwView = SwView.GetNextView

             SwDraw.ActivateView SwView.Name

         Dim SwSelMgr As SelectionMgr

             Set SwSelMgr = SwModel.SelectionManager

         Dim SwDispDim As DisplayDimension, SwDim As Dimension, SwAnn As Annotation

             Set SwDispDim = SwSelMgr.GetSelectedObject5(1)

             CreateCenterLine SwDraw, SwDispDim

             Stop

      End Sub

        • Re: How to create line in view
          Raghvendra Bhargava

          HI Yong,

          Drawing View GetOutline  . It may help you.

            • Re: How to create line in view
              Yong Ning

              Coordinate conversion error

              GetOutline → bounding box for a view → error. | SOLIDWORKS Forums  https://forum.solidworks.com/message/703783

               

              Drawing View GetOutline | SOLIDWORKS Forums  https://forum.solidworks.com/message/680142

              OutLine1.jpg

               

              Private Sub del20161223()

                 Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                     Set SwApp = Application.SldWorks

                     Set SwModel = SwApp.ActiveDoc

                 Dim swSelMgr As SelectionMgr, Ss

                     Set swSelMgr = SwModel.SelectionManager

                 Dim SwDraw As DrawingDoc, SwView As View

                     Set SwDraw = SwModel

                     Set SwView = swSelMgr.GetSelectedObject5(1)

                     Debug.Print SwView.Name

                     Ss = SwView.GetOutline

                     SwDraw.ActivateView SwView.Name

                      'tmp = SwDraw.SketchRectangle(-Ss(2), -Ss(3), 0, Ss(2), Ss(3), 0, 1)

                      tmp = SwDraw.SketchRectangle(Ss(0), Ss(1), 0, Ss(2), Ss(3), 0, 1)

                     Stop  

              End Sub

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

              Scale is 1:1,  Drawing rectangle ,  Coordinate is error.

               

              OutLine3.jpg

               

               

               

              '''

              Private Sub dd20161223()

                 Dim Var, vPos, tmp, oScale, Vpts

                 ''

                 Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                     Set SwApp = Application.SldWorks

                     Set SwModel = SwApp.ActiveDoc

                 Dim swSelMgr As SelectionMgr, Ss

                     Set swSelMgr = SwModel.SelectionManager

                 Dim SwDraw As DrawingDoc, SwView As View

                     Set SwDraw = SwModel

                     ''''

                     Set SwView = swSelMgr.GetSelectedObject5(1)

                     SwDraw.ActivateView SwView.Name

                     SwView.ScaleDecimal = 1

                     Var = SwView.GetOutline

                     Stop

                     tmp = SwDraw.SketchRectangle(Var(0), Var(1), 0, Var(2), Var(3), 0, 1)

              End Sub

                • Re: How to create line in view
                  Steven Barry

                  Yong, try this:

                  Dim swSketchMgr as SketchManager

                  Set swSketchMgr as SwDraw.SketchManager

                  tmp = swSketchMgr.CreateCornerRectangle(Var(0), Var(3), 0, Var(2), Var(1), 0)

                   

                  According to the API reference, the corner rectangle sketch goes from TOP LEFT to BOTTOM RIGHT

                  2015 SOLIDWORKS API Help - CreateCornerRectangle Method (ISketchManager)

                  The "GetOutline" method returns coordinates from BOTTOM LEFT to TOP RIGHT

                  2015 SOLIDWORKS API Help - GetOutline Method (IView)

                   

                  Upper left X coordinate = X min

                  Upper left Y coordinate = Y MAX

                  Lower right X coordinate = X max

                  Lower right Y coordinate = Y MIN

                   

                  EDIT:  CreateCornerRectangle is apparently a method belonging to the ISketchManager interface.  Added a few lines to show that.

                   

                  EDIT 2:  You may also need to incorporate the view position:

                  2015 SOLIDWORKS API Help - Position Property (IView)

                  So that it would become:

                  Dim pos as Variant

                  Dim swSketchMgr as SketchManager

                  Set swSketchMgr as SwDraw.SketchManager

                  pos = SwView.Position

                  tmp = swSketchMgr.CreateCornerRectangle(Var(0)+pos(0), Var(3)+pos(1), 0, Var(2)+pos(0), Var(1)+pos(1), 0)

                    • Re: How to create line in view
                      Yong Ning

                      .CreateCornerRectangle(Var(0) + Pos(0), Var(3) + Pos(1), 0, Var(2) + Pos(0), Var(1) + Pos(1), 0) @→Error??

                       

                      OutLine4.jpg

                       

                       

                       

                      Private Sub del25()

                         Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                             Set SwApp = Application.SldWorks

                             Set SwModel = SwApp.ActiveDoc

                         Dim SwSelMgr As SelectionMgr

                             Set SwSelMgr = SwModel.SelectionManager

                         Dim SwDraw As DrawingDoc

                             Set SwDraw = SwModel

                         Dim SwView As View, oScale

                             Set SwView = SwDraw.GetFirstView

                             Set SwView = SwView.GetNextView

                             oScale = 1 / SwView.ScaleDecimal

                         Dim Pos As Variant, Var

                             Pos = SwView.Position

                             Var = SwView.GetOutline

                             For ii = 0 To 3

                                 Var(ii) = Var(ii) * oScale

                             Next ii

                         Dim SwSketchMgr As SketchManager

                             Set SwSketchMgr = SwModel.SketchManager

                             Pos = SwView.Position

                             tmp = SwSketchMgr.CreateCornerRectangle(Var(0) + Pos(0), Var(3) + Pos(1), 0, Var(2) + Pos(0), Var(1) + Pos(1), 0)

                      End Sub