4 Replies Latest reply on Oct 12, 2018 12:12 AM by Varun Bhartiya

    Error on casting Body 2 object to Entity object

    Varun Bhartiya

      When we are casting the Body2 object to Entity object we are getting an exception. (Please refer to the method "BodyOperation")

      Note:-
      This operation works correctly in c++ but failing in C#.
      Also, we can successfully cast the Face2 object to Entity object. (Please refer to the method "FaceOperation")

       

      public void BodyOperation(ModelDoc2 swModel)

              {

                  PartDoc swPart = default(PartDoc);

                  Body2 swBody = default(Body2);

                  object[] vBodies = null;

                  int i = 0;

                  int j = 0;

                  swPart = (PartDoc)swModel;

                   vBodies = (object[])swPart.GetBodies((int)swBodyType_e.swSolidBody);

       

       

                      Debug.Print(" Number of solid bodies: " + (vBodies.Length + 1));

                      for (j = 0; j <= vBodies.Length - 1; j++)

                      {

                          Debug.Print(" Body number: " + (j + 1));

       

       

                          swBody = (Body2)vBodies[j];

                          //NameAllFaces(ref swBody);

       

       

                          Debug.Print(" Body name: " + swBody.Name);

       

       

                          Entity ey = (Entity)swBody; // Giving an Invalid Cast Exception

                          ey.ModelName= "abcd"; // Any name to verify the conversion

                      }

              }

       

      private void FaceOperation(ref Body2 swBody)

              {

                  Face2 swFace;

                  Entity swEnt;

                  swFace = (Face2)swBody.GetFirstFace();

                  while (swFace != null)

                  {

                      swEnt = (Entity)swFace;

                      swEnt.ModelName = "xyz";

                      swFace = (Face2)swFace.GetNextFace();

                  }

              }

       

      We need the entity object so that we can update the attribute information when we are exporting the part file to Parasolid file.