6 Replies Latest reply on Dec 15, 2015 8:58 AM by John Alexander

    Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.

    John Alexander

      Please excuse the long explanation.

       

      I'm trying to write a macro that will eventually be able to circle user-specified sub-assemblies on a drawing view. I've finished what I thought was the hard part of this process and am now trying to get mathpoints (in the reference frame for a Sketch on an active drawing view) from all vertices on that drawing view. These mathpoints will then be processed to find the convex hull of the 2D "point cloud" and the points on the hull will be scaled about their centroid and used to create a closed spline.

       

      2012 SOLIDWORKS API Help - GetPoint Method (IVertex)

      To collect the mathPoints, I'm using swVertex.getPoint() for every visible vertex on an active drawing view. My problem is that I can't figure out what coordinate system these vertices are reporting their points with respect to. I feel like I'm missing something fundamental about how to transform from a referenced model to a drawing to a view in that drawing. It would be helpful if: the methods that return mathtransforms explicitly described what they were transforming to and from and if methods that report dimensions documented what reference frame they are reported with respect to.

       

          Set swViewComponent = swView.RootDrawingComponent.Component
          
          If swView.GetVisibleEntityCount(swViewComponent, swViewEntityType_e.swViewEntityType_Vertex) > 0 Then
              math_point_iterator = 0
              For Each vVisibleEntity In swView.GetVisibleEntities(swViewComponent, swViewEntityType_e.swViewEntityType_Vertex)
                  Set swEntity = vVisibleEntity
                  Set swVertex = swEntity
                  Set swMathPoint = swMathUtility.CreatePoint(swVertex.GetPoint)
                  ReDim Preserve swMathPoints(math_point_iterator)
                  Set swMathPoints(math_point_iterator) = swMathPoint
                  math_point_iterator = math_point_iterator + 1
              Next vVisibleEntity
          End If
      

       

      I've taken these points and used swView.ModelToViewTransform and attempted to pass them to the sketch manager to draw on the sketch associated with the active view.

       

      I've tried drawing them on the sketch associated with the current sheet.

       

      I've tried every combination of swView.ModelToViewTransform and swSketch.ModelToSketchTransform (inverted both, inverted their product, inverted one or the other, did all of those things after changing the order of muiltiplication, omitted one, omitted the other). None of these approaches have gotten me sketch points that align with the vertices visible in the drawing view.

       

      When I use the mis-aligned sketchpoints with my convexhull spline operation and manually drag the feature over, it looks great. I just can't get it to align with the view automatically.

       

      If the sketchpoints align with the vertices, the scale is off. If the scale is correct, they don't align. If the scale and alignment are close, the orientation is wrong. I was going to write a detailed analysis of everything that I have tried but the list is so long at this point, I don't know that it will add clarity. Is there something fundamentally wrong with my approach?

       

      I have transformed into and out of a model in a drawing view successfully in the past. The application involved drawing grid lines for a view in a drawing that aligned with a custom coordinate system int he model. When I try to apply what I learned with that project, it doesn't work.

        • Re: Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.
          Digvijay Taunk

          Hi John,

          The fundamental behind transformation what I learnt, I am writing below. May be you have tried that combination also but it did worked for me always.

           

          Once you have the swMathPoint,

          swMathPoint = swMathUtility.CreatePoint(swVertex.GetPoint)

           

          you need to get ModelToViewTransform for your view

          ModelToViewTransform = View.ModelToViewTransform

           

          then get the View sketch

          Dim Sketch As SldWorks.Sketch = View.GetSketch()

           

          then get ModelToSketchTransform

          Dim ModelToSketchTransform As SldWorks.MathTransform = Sketch.ModelToSketchTransform

           

          then do the multiply transform to get cordinates for sketch in view.

          swMathPoint = swMathPoint .MultiplyTransform(ModelToViewTransform)

          swMathPoint = swMathPoint .MultiplyTransform(ModelToSketchTransform)

           

          skViewMAthPt = swMathPoint .ArrayData

           

          skViewMAthPt(0), skViewMAthPt(1) will have X, Y cordinates.

           

          To plot sketch you need to activate view first & create sketch point.

          Status = DrawDoc.ActivateView("View")

          skPt = MOdelDoc.SketchManager.CreatePoint(X,Y,0)

           

          I hope this works for you as well.

            • Re: Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.
              John Alexander

              Thanks for your thorough response,

               

              I've tried implementing your process (below). This code follows immediately after the excerpt in my original post.

               

                  Set swModelToViewXForm = swView.ModelToViewTransform
                  Set swSketch = swView.GetSketch
                  Set swModelToSketchXForm = swSketch.ModelToSketchTransform
                  
                  For i = 0 To UBound(swMathPoints)
                      Set swMathPoint = swMathPoints(i)
                      Set swMathPoint = swMathPoint.MultiplyTransform(swModelToViewXForm)
                      Set swMathPoint = swMathPoint.MultiplyTransform(swModelToSketchXForm)
                      Set swMathPoints(i) = swMathPoint
                  Next i
              
                  drawPoints swModel, swMathPoints
              

               

              drawPoints takes an array of mathpoints and draws them on the active sketch:

               

              Public Sub drawPoints(swModel As SldWorks.ModelDoc2, swMathPoints() As SldWorks.MathPoint)
                  Dim swSketchPoint      As SldWorks.SketchPoint
                  Dim i      As Integer
              
                  swModel.SketchManager.InferenceMode = False
                  swModel.SketchManager.AddToDB = True
                  For i = 0 To UBound(swMathPoints)
                      Set swSketchPoint = swModel.SketchManager.CreatePoint(swMathPoints(i).ArrayData(0), swMathPoints(i).ArrayData(1), 0#)
                  Next i
                  swModel.SketchManager.InferenceMode = True
                  swModel.SketchManager.AddToDB = False
              End Sub
              

               

               

              This is how the points are being drawn. The pattern is scaled correctly and aligned vertically but it is translated. The distance that it translates depends on the vertical distance from the origin (bottom left of the sheet).

               

              edit: swView came from swModel.activeDrawingview originally so it should be active. Just in case, I added the following line before drawPoints() and the results were the same.

               

              bRet = swDraw.ActivateView(swView.Name)

                • Re: Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.
                  John Alexander

                  Some more info:

                   

                  swModelToViewXForm

                  0  0  1  0

                  0  1  0  0

                  -1  0  0  0

                  4.32018298131121E-02  0.117548431410249 -2.38125000000011E-02  1

                   

                  swModelToSketchXForm

                  1  0  0  0

                  0  1  0  0

                  0  0  1  0

                  -8.13018298131121E-02 -8.31484314102494E-02  1.08196018860265E-15  1

                   

                  swModelToViewXForm.Multiply(swModelToSketchXForm)

                  0  0  1  0

                  0  1  0  0

                  -1  0  0  0

                  -0.0381  0.0344 -0.0238125  1

                  -------------------------------------------------------------------------------------------------

                  swModelToViewXForm

                  0  0  1  0

                  0  1  0  0

                  -1  0  0  0

                  3.71587669559692E-02  8.77329998167799E-03 -2.38125000000011E-02  1

                   

                  swModelToSketchXForm

                  1  0  0  0

                  0  1  0  0

                  0  0  1  0

                  -7.52587669559693E-02  0.025626700018322  1.08196018860265E-15  1

                   

                  swModelToViewXForm.Multiply(swModelToSketchXForm)

                  0  0  1  0

                  0  1  0  0

                  -1  0  0  0

                  -0.0381  0.0344 -0.0238125  1

                  --------------------------------------------------------------------------------------------------------

                  If I select a view that is (3rd angle) projected to the right, the transforms are:

                   

                  swModelToViewXForm

                  -1  0 -3.49148133884313E-15  0

                  0  1  0  0

                  3.49148133884313E-15  0 -1  0

                  0.125031940138438  0.123233960759389 -3.81000000000016E-02  1

                   

                  swModelToSketchXForm

                  1  0  0  0

                  0  1  0  0

                  0  0  1  0

                  -0.101219440138437 -8.88339607593895E-02  1.63344921469493E-15  1

                   

                  swModelToViewXForm.Multiply(swModelToSketchXForm)

                  -1  0 -3.49148133884313E-15  0

                  0  1  0  0

                  3.49148133884313E-15  0 -1  0

                  2.38125000000001E-02  0.0344 -3.80999999999999E-02  1

                   

                  It seems to translate the points according to how far it is from the origin along both the vertical and horizontal directions.

                    • Re: Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.
                      John Alexander

                      Finally got this working.

                       

                       

                      Using the standard transforms from model to sketch as Digvijay Taunk and Rajat Jain suggested:

                       

                              Set swMathPoint = swMathPoint.MultiplyTransform(swModelToViewXForm)

                              Set swMathPoint = swMathPoint.MultiplyTransform(swModelToSketchXForm)

                       

                      yielded the results in my previous post.

                       

                      This is apparently because the vertices from GetVisibleEntities aren't reported with respect to the model's coordinate system; They are reported with respect to the drawing sheet's coordinate system. In order to draw these on a sketch attached to a drawing view, it is necessary to translate into the view's coordinate system before performing the standard approach above.

                       

                          Set swModelToViewXForm = swView.ModelToViewTransform
                          Set swModelToSketchXForm = swSketch.ModelToSketchTransform
                          Set swDrawingToViewXForm = drawingToViewTransform(swView)
                      
                          For i = 0 To UBound(swMathPoints)
                              Set swMathPoint = swMathPoints(i)
                              vDoubles = swMathPoint.ArrayData
                              Set swMathPoint = swMathPoint.MultiplyTransform(swDrawingToViewXForm.Inverse)
                              Set swMathPoint = swMathPoint.MultiplyTransform(swModelToViewXForm)
                              Set swMathPoint = swMathPoint.MultiplyTransform(swModelToSketchXForm)
                              Set swMathPoints(i) = swMathPoint
                              vDoubles = swMathPoint.ArrayData
                          Next i
                      
                      

                       

                      where drawingToViewTransform looks like:

                       

                      Public Function drawingToViewTransform(swView As SldWorks.View) As SldWorks.MathTransform
                          Dim swMathUtil  As SldWorks.MathUtility
                          Dim transformData(15) As Double
                         
                          Set swMathUtil = swApp.GetMathUtility
                         
                          transformData(0) = 1#
                          transformData(1) = 0#
                          transformData(2) = 0#
                          transformData(3) = 0#
                          transformData(4) = 1#
                          transformData(5) = 0#
                          transformData(6) = 0#
                          transformData(7) = 0#
                          transformData(8) = 1#
                          transformData(9) = swView.Position(0)
                          transformData(10) = swView.Position(1)
                          transformData(11) = 0#
                          transformData(12) = 1#
                          transformData(13) = 0#
                          transformData(14) = 0#
                          transformData(15) = 0#
                         
                          Set drawingToViewTransform = swMathUtil.CreateTransform(transformData)
                      End Function
                      

                       

                       

                      Thanks again for the help,

                       

                      John Alexander

                • Re: Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.
                  Rajat Jain

                  John,

                   

                  Just follow these steps and you shall get the correct results: -

                  1. Get Point from vertex (as you are currently doing).
                  2. Use ModelToViewTransform to transform this point to the view space. (Multiple vertex point with ModelToViewTransform)
                  3. Get the Sketch of the view (using view::GetSketch()).
                  4. Get ModelToSketchTransform for this Sketch.
                  5. Use ModelToSketchTransform to transform point (generated in step 2) to the sketch space. (Multiply point generated in step 2 with ModelToSketchTransform).
                  6. Activate the sheet.
                  7. Activate the view.
                  8. create your spline.

                   

                  Hope it helps!!!

                  Rajat Jain