7 Replies Latest reply on Jul 8, 2018 12:43 PM by Fifi Riri

    Closestdistance value close to zero

    Iyed Briki

      Hello,

      Sometimes when using the function ModelDoc2::ClosestDistance to evaluate the distance between two surfaces that overlap, the returned value is very small but not 0. ( for example : 4,16333634234434E-17 ). Any ideas why? Can these values be considered as 0? or am I doing something rong?

      Thank you very much

        • Re: Closestdistance value close to zero
          Fifi Riri

          Yes, it's by all mean 0

          it comes from the approximations of the floating-point arithmetic

          I consider anything below 1E-8 to be 0

            • Re: Closestdistance value close to zero
              Iyed Briki

              Thank you very much,

              Can I program the api so that these values are automatically set to zero or schould I go through all values with an IF decision? 

                • Re: Closestdistance value close to zero
                  Fifi Riri

                  It depends of what you use the value for

                  For example instead of

                  if value=0 then

                  Write

                  if Abs(value) < 0.00000001 then

                    • Re: Closestdistance value close to zero
                      Iyed Briki

                      the programm that I am writing goes through all faces on an assembly and returns which components are in contact.

                      -for plane surfaces: it calculates the closest distance between the surfaces and compares it to zero.

                      -for cylindrical and conical surfaces: it compares the axes (vectors) and the radiuses.

                      In both cases it returns sometimes small values. with an IF-condition it will be a lot of work

                        • Re: Closestdistance value close to zero
                          Fifi Riri

                          I think you can incorporate it quite easily.

                          Could you paste the relevant section of your code?

                            • Re: Closestdistance value close to zero
                              Iyed Briki

                              May be it will be difficult to understand (sorry!). It's not the final version. ndist2 and  the values of swPt, swPtz, swPti, swPtii are soometimes very small but not zero.                

                                                                    'get cylinder parameters 

                               

                                                                    swParam1 = surfc.CylinderParams

                                                                    swParam2 = surfci.CylinderParams

                               

                                                                   'calculate closest distance

                               

                                                                   ndist2 = swModel.ClosestDistance(swsurface, swsurfacei, vPt3, vPt4)

                               

                                                                   'get vector axis_1 of the cylinder1 with respect to the coordinates system of the assembly

                               

                                                                   Set swMathUtil = swApp.GetMathUtility

                                                                   Set swXform = oneComponent.Transform2

                                                                   nPt(0) = swParam1(3)

                                                                   nPt(1) = swParam1(4)

                                                                   nPt(2) = swParam1(5)

                                                                   vPut = nPt

                                                                   Set swPt = swMathUtil.CreatePoint(vPut)

                                                                   Set swPt = swPt.MultiplyTransform(swXform)

                                                                   Set swMathUtilz = swApp.GetMathUtility

                                                                   Set swXformz = oneComponent.Transform2

                                                                   nPtz(0) = 0#

                                                                   nPtz(1) = 0#

                                                                   nPtz(2) = 0#

                                                                   vPutz = nPtz

                                                                   Set swPtz = swMathUtilz.CreatePoint(vPutz)

                                                                   Set swPtz = swPtz.MultiplyTransform(swXformz)

                               

                                                                   'get vector axis_2 of the cylinder2 with respect to the coordinates system of the assembly

                               

                                                                   Set swMathUtili = swApp.GetMathUtility

                                                                   Set swXformi = oneComponenti.Transform2

                                                                   nPti(0) = swParam2(3)

                                                                   nPti(1) = swParam2(4)

                                                                   nPti(2) = swParam2(5)

                                                                   vPuti = nPti

                                                                   Set swPti = swMathUtili.CreatePoint(vPuti)

                                                                   Set swPti = swPti.MultiplyTransform(swXformi)

                                                                   Set swMathUtilii = swApp.GetMathUtility

                                                                   Set swXformii = oneComponenti.Transform2

                                                                   nPtii(0) = 0#

                                                                   nPtii(1) = 0#

                                                                   nPtii(2) = 0#

                                                                   vPutii = nPtii

                                                                   Set swPtii = swMathUtilii.CreatePoint(vPutii)

                                                                   Set swPtii = swPtii.MultiplyTransform(swXformii)

                               

                                                                  'calculate the crossproduct of axis_1 and axis_2

                               

                                                                   kreuzprod(0) = (swPt.ArrayData(1) - swPtz.ArrayData(1)) * (swPti.ArrayData(2) - swPtii.ArrayData(2)) -                                         (swPt.ArrayData(2) - swPtz.ArrayData(2)) * (swPti.ArrayData(1) - swPtii.ArrayData(1))

                                                                   kreuzprod(1) = (swPt.ArrayData(2) - swPtz.ArrayData(2)) * (swPti.ArrayData(0) - swPtii.ArrayData(0)) -                                         (swPt.ArrayData(0) - swPtz.ArrayData(0)) * (swPti.ArrayData(2) - swPtii.ArrayData(2))

                                                                   kreuzprod(2) = (swPt.ArrayData(0) - swPtz.ArrayData(0)) * (swPti.ArrayData(1) - swPtii.ArrayData(1)) -                                           (swPt.ArrayData(1) - swPtz.ArrayData(1)) * (swPti.ArrayData(0) - swPtii.ArrayData(0))

                                                                 

                                                                      ' Check the values

                               

                                                                   Dim lii As Integer

                                                                   Dim Liii As Integer

                                                                   Liii = 0

                                                                   For lii = 0 To 2

                                                                      If kreuzprod(lii) < 0.00000001 Then

                                                                          If kreuzprod(lii) > -0.00000001 Then

                                                                           Liii = Liii + 1

                                                                          End If

                                                                       End If

                                                                   Next lii

                                                                   If Liii = 3 Then

                                                                      If ndist2 < 0.00000000001 Then

                                                                          Debug.Print oneComponent.Name; "    "; oneComponenti.Name; "    !!!!"

                                                                      End If

                                                                   End If

                                • Re: Closestdistance value close to zero
                                  Fifi Riri

                                  ...

                                  For lii = 0 To 2

                                       If IsMyValueNull(kreuzprod(lii)) Then

                                            Liii = Liii + 1

                                       End If

                                  Next lii

                                  If Liii = 3 Then

                                       If IsMyValueNull(ndist2) Then

                                            Debug.Print oneComponent.Name; "    "; oneComponenti.Name; "    !!!!"

                                       End If

                                  End If

                                  ...

                                  end sub

                                   

                                  Function IsMyValueNull(ByVal MyValue As Double) As Boolean

                                      If Abs(MyValue) < 0.00000001 Then

                                          IsMyValueNull = True

                                      Else

                                          IsMyValueNull = False

                                      End If

                                  End Function