10 Replies Latest reply on Nov 5, 2015 11:48 AM by Firstname Lastname

    C# API AddMate

    Firstname Lastname

      Greetings!

       

      I have locator for my model of shaft:

      Безымянный.png

       

      And I must program conjugation with shaft to get next result:

       

      2.PNG

       

      Any ideas how i can do this?

      Which command of API I must use?

       

      Advance thanks for help!

        • Re: C# API AddMate
          Deepak Gupta

          You need to add two tangent mates for those faces with the shaft.

           

          Manually you can use the multi mate option but not sure if API can handle that. So you need to select those faces one by one.

          • Re: C# API AddMate
            Viktor Bovzdarenko

            U can define those faces initially by naming them manually; then in API it is possible to  get only named faces: Array of named entities = IPartDoc.GetNamedEntities(); and select each face: Boolean= IEntity.Select4(Append, Data); then add mate: Set matefeature = swAssy.AddMate3(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)

            matefeature.Name = MateName

             

              • Re: C# API AddMate
                Firstname Lastname

                Thanks you!

                 

                I will try it and report about result.

                • Re: C# API AddMate
                  Firstname Lastname

                  Yes, it work! Thanks you!
                  Now, I need align the prism with the shaft.

                   

                  1.PNG

                   

                  I cant do this without using named geometry and calculating the offset with GetBox()?

                  I want find the simple way.

                   

                  Thank for advance!

                    • Re: C# API AddMate
                      Viktor Bovzdarenko

                      if you can make face on shaft with  the smallest area on the ends of the shaft, then you can easily get it and add mate;

                       

                      otherwise, If you can distinguish shaft body from other bodies and know the direction of the axis then you can get extreme faces from it: first find extreme points from both ends of the shaft: boolean = IBody2.GetExtremePoint(X, Y, Z, Outx, Outy, Outz); then by taking all faces and comparing the coordinates from extreme point and Array= Face2.GetClosestPointOn(X, Y, Z)  needed faces could be taken; then mates can be added.

                        • Re: C# API AddMate
                          Firstname Lastname

                          Thank you for help! I will try make it and report about result.

                            • Re: C# API AddMate
                              Firstname Lastname

                              I could not make as you say.

                              But I found other solution:

                              1. Find the ends of shaft as pair of faces with maximum distance. For this I use the follow code:

                              2. For each end find relative plane of cylinder locator.

                               

                              1.PNG

                               

                              I want find the plane alpha, but I can't do it, because, when I take the normal propertie of  the plane S then I get [1, 0, 0] and same value I get for plane betta. Why? Because of this I can't find the plane alpha.

                               

                              How I can do this? Help me please. I don't have more good ideas.

                               

                              Here is code, that I use to do this

                              ===============================================================================================

                                        IBody2 body = null;

                                          object[] faceObjects = null;

                                          IFace2[] faces = null;

                               

                                          Debug.WriteLine("find ends of shaft...");

                                          body = shaft.Component.GetBody();

                                          faceObjects = body.GetFaces();

                                          faces = Array.ConvertAll(faceObjects, new Converter<object, IFace2>((o) => { return o as IFace2; }));

                                          RemovalPairlFaces removalPair = solidUtil.FindMaximumRemovalPlanes(faces);

                               

                                          (removalPair.From as IEntity).Select4(true, null);

                                          (removalPair.To as IEntity).Select4(true, null);

                               

                                          IFace2 removalFace;

                                          double distance;

                               

                                          body = firstCylinderLocator.Component.GetBody();

                                          faceObjects = body.GetFaces();

                                          faces = Array.ConvertAll(faceObjects, new Converter<object, IFace2>((o) => { return o as IFace2; }));

                                          solidUtil.FindRemovalFace(faces, removalPair.From, out removalFace, out distance);

                               

                                          double[] normal1 = removalPair.From.Normal;

                                          double[] normal2 = removalPair.To.Normal;

                                          Debug.WriteLine("normal1: " + string.Join(" | ", normal1));

                                          Debug.WriteLine("normal2: " + string.Join(" | ", normal2));

                               

                                          IMathVector v1 = solidUtil.MathUtility.CreateVector(normal1);

                               

                                          foreach (IFace2 face in faces)

                                          {

                                              (face as Entity).Select4(true, null);

                                              double[] normal = face.Normal;

                                              Debug.WriteLine(string.Join(" | ", normal));

                                              IMathVector v = solidUtil.MathUtility.CreateVector(normal);

                                              Debug.WriteLine("is parallel " +

                                                  (solidUtil.IsCoDirectional(v, v1) || solidUtil.IsCoDirectional(solidUtil.Negative(v), v1)));

                                          }

                              ===============================================================================================

                              Below show the util class

                               

                              class SolitUtil

                                  {

                                      public IMathUtility MathUtility { get; set; }

                               

                                      public SolitUtil(IMathUtility mathUtility)

                                      {

                                          MathUtility = mathUtility;

                                      }

                               

                                      public void FindRemovalFace(IFace2[] faces, IFace2 fromFace, out IFace2 removalFace, out double distance)

                                      {

                                          distance = 0.0;

                                          removalFace = null;

                                          ISurface fromSurface = fromFace.GetSurface();

                                          IMathVector fromFaceNormal = MathUtility.CreateVector(fromFace.Normal);

                               

                                          for (int i = 0; i < faces.Length; ++i)

                                          {

                                              IFace2 toFace = faces[i];

                                              ISurface toSurface = toFace.GetSurface();

                                              if (toSurface.IsPlane())

                                              {

                                                  IMathVector toFaceNormal = MathUtility.CreateVector(toFace.Normal);

                                                  if (IsCoDirectional(fromFaceNormal, toFaceNormal) || IsCoDirectional(fromFaceNormal, Negative(toFaceNormal)))

                                                  {

                                                      double currentDistance = DistanceBetweenParallelPlanes(fromSurface, toSurface);

                                                      if (currentDistance > distance)

                                                      {

                                                          removalFace = toFace;

                                                          distance = currentDistance;

                                                      }

                                                  }                   

                                              }

                                          }

                                      }

                               

                                      public RemovalPairlFaces FindMaximumRemovalPlanes(IFace2[] faces)

                                      {

                                          List<RemovalPairlFaces> list = new List<RemovalPairlFaces>();

                                          for (int i = 0; i < faces.Length; ++i)

                                          {

                                              IFace2 fromFace = faces[i];

                                              ISurface fromSurface = fromFace.GetSurface();

                                              if (fromSurface.IsPlane())

                                              {                   

                                                  IFace2 toFace;

                                                  double distance;

                                                  FindRemovalFace(faces, fromFace, out toFace, out distance);

                               

                                                  if (toFace != null)

                                                  {

                                                      list.Add(new RemovalPairlFaces()

                                                      {

                                                          From = fromFace,

                                                          To = toFace,

                                                          Distance = distance

                                                      });

                                                  }

                                                 

                                              }

                                          }

                               

                                          return list.OrderBy((x) => { return -x.Distance; }).First();

                                      }

                               

                                      public double DistanceBetweenParallelPlanes(ISurface plane1, ISurface plane2)

                                      {

                                          double[] params1 = plane1.PlaneParams as double[];

                                          double[] parans2 = plane2.PlaneParams as double[];

                                          double distance;

                               

                                          //get equation for first plane

                                          //aX + bY + cZ + d = 0

                                          double a, b, c, d;

                                          a = params1[0];

                                          b = params1[1];

                                          c = params1[2];

                                          d = -a * params1[3] - b * params1[4] - c * params1[5];

                               

                                          //calculate distance

                                          distance = Math.Abs(a * parans2[3] + b * parans2[4] + c * parans2[5] + d) / Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2) + Math.Pow(c, 2));

                               

                                          return distance;

                                      }

                               

                                      public bool IsCoDirectional(IMathVector one, IMathVector two)

                                      {

                                          IMathVector oneN = one.Normalise();

                                          IMathVector twoN = two.Normalise();

                               

                                          double[] oneVector = oneN.ArrayData as double[];

                                          double[] twoVector = twoN.ArrayData as double[];

                                          double[] difference = new double[3];

                               

                                          difference[0] = oneVector[0] - twoVector[0];

                                          difference[1] = oneVector[1] - twoVector[1];

                                          difference[2] = oneVector[2] - twoVector[2];

                               

                                          return Equal(difference[0], 0, 1) && Equal(difference[1], 0, 1) && Equal(difference[2], 0, 1);

                                      }

                               

                                      public bool Equal(double d1, double d2, int toleranceDecimalPlaces)

                                      {

                                          double tolerance = 1.0 / (Math.Pow(10, toleranceDecimalPlaces));

                               

                                          return Math.Abs(d1 - d2) < tolerance;

                                      }

                               

                                      public IMathVector Negative(IMathVector origin)

                                      {

                                          double[] negativeVector = origin.ArrayData as double[];

                                          negativeVector[0] = -negativeVector[0];

                                          negativeVector[1] = -negativeVector[1];

                                          negativeVector[2] = -negativeVector[2];

                               

                                          return MathUtility.CreateVector(negativeVector);

                                      }

                                  }

                      • Re: C# API AddMate
                        Firstname Lastname

                        Anyone, please help me.