4 Replies Latest reply on Dec 20, 2016 12:32 PM by William Cruz

    SketchPoint Coordinates to Model Coodinates

    William Cruz

      Can anyone tell me what I'm doing wrong? It returns only the X coordinate, Y and Z return zero "0".

       

             internal void TransformPoints(SketchPoint sp)
              {
                  try
                  {
                      MathUtility swMathUtil = (MathUtility)_swApp.GetMathUtility();
      
                      double[] cPT = { sp.X, sp.Y, sp.Z };
                      double[] vPoint = null;
                      MathPoint swMathPt2 = default(MathPoint);
      
                      swMathPt2 = (MathPoint)swMathUtil.CreatePoint(cPT);
                      swMathPt2 = (MathPoint)swMathPt2.MultiplyTransform(sp.GetSketch().ModelToSketchTransform.Inverse());
      
                      vPoint = (double[])swMathPt2.ArrayData;
      
                  }
                  catch (Exception ex)
                  {
      
                  }
              }
      
      
        • Re: SketchPoint Coordinates to Model Coodinates
          John Alexander

          Is SketchPoint coming from a drawing? I had a similar problem a year ago which was resolved in this post.

           

          Troubleshooting mathTransforms, ivertex getpoints, and Views/Sketches.

           

          If that doesn't answer your question, it would be helpful for troubleshooting to know what sp.x, sp.y, sp.z evaluate to before the transform and what the transform matrix looks like.

          • Re: SketchPoint Coordinates to Model Coodinates
            William Cruz

            For those who might-of had the same question, the code below works perfectly.

             

                  
                   internal double[] TransformPt(MathUtility swMu, Component2 swComp, double[] dbSketchPt)
                    {
                        try
                        {
                            double[] xPt = dbSketchPt;
                            MathTransform swMt = swComp.Transform2;
                            MathPoint swMp = (MathPoint)swMu.CreatePoint(dbSketchPt);
                            MathPoint swMp2 = (MathPoint)swMp.MultiplyTransform(swMt);
                            xPt = ((double[])swMp2.ArrayData);
                            return xPt;
                        }
                        catch (Exception ex)
                        {
                            LogIt.LogError(ex);
            
                            return null;
                        }
                    }
            
            • Re: SketchPoint Coordinates to Model Coodinates
              William Cruz

              An even simpler solution that does not involve any type of manual transform is by having SW do it for you using the SW Measure. The obvious bool checks are missing in the code below so if you are going to re-use, make sure you add them.

               

                     internal double[] TransformPt(SketchPoint sp)
                      {
                          try
                          {
                              bool retVal = false;
                              Measure swMeasure = default(Measure);
              
                              /*clear any previous selections*/
                              _swModDoc.ClearSelection2(true);
              
                              /*select the sketchpoint*/
                              retVal = sp.Select4(false, null);
              
                              /*get access to sw measure tool*/
                              swMeasure = _swModelDocExt.CreateMeasure();
              
                              /*run the mearsure on the selected sketchpoint*/
                              retVal = swMeasure.Calculate(null);
              
                              /*assign coordinates to an array of doubles*/
                              double[] cPT = { swMeasure.X, swMeasure.Y, swMeasure.Z };
              
                              /*clear selections*/
                              _swModDoc.ClearSelection2(true);
              
                              /*return points*/
                              return cPT;
                          }
                          catch (Exception ex)
                          {
                              LogIt.LogError(ex);
              
                              return null;
                          }
              }