9 Replies Latest reply on Aug 3, 2017 1:50 AM by BJ de Bruijn

    Finding references of Features and Sketches

    BJ de Bruijn

      Hi,

       

      I've been trying to find references of features,

      Traversing the feature tree gives me the features and child features, but I would like to know the parents (those parent features the feature uses) for example:

      • on which plane a sketch is made
      • which sketches are used in a feature (extrude, loft, revolve)
      • which plane is used for a reference geometry, for example a plane 20 mm above the top plane
      • which face is used for the sketch of a boss extrude

       

      My code:

      // show parents of current feature
      parents = currentFeature.GetParents();
      if (parents != null)
      {
        parentscount = parents.Length;
        for (int nr = 0; nr < parentscount; nr++)
          {
             swParentFeature = (Feature)parents[nr];
             Debug.Print("ParentFeature ----> " + swParentFeature.Name);
          }
      }
      
      

       

       

      In my example I have a boss extrude "Boss-Extrude2" on top of another feature (a face of "Boss-Extrude1")

      How can I find the exact references (the face who is the base for the sketch plane) of "Boss-Extrude1" on which "Sketch2" has been used?

       

      Thanks,

      Bas

        • Re: Finding references of Features and Sketches
          Nilesh Patel

          Hi,

           

          Have a look at the following example from API Help : Get Plane on which Sketch Created Example (VBA)

           

          In your case, the value of 'longstatus' will be 2 = swSelectType_e.swSelFACES

           

          Regards,

            • Re: Finding references of Features and Sketches
              BJ de Bruijn

              Hi Nilesh,

               

              I've been looking after your proposal, (in fact after I posted I've tried similar)

              I could not translate the VB code "Set swSketchPlane = swSketch.GetReferenceEntity(longstatus)" into C#

              I ended up with the following:

               

              Debug.Print("Featurename: " + currentFeature.Name);
              Debug.Print("Featuretype: " + currentFeature.GetTypeName2());
              object swSketchPlane;
              swSketch = currentFeature.GetSpecificFeature2();
              swSketchPlane = swSketch.GetReferenceEntity((int)swSelectType_e.swSelDATUMPLANES);
              Debug.Print("Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> " + swSketchPlane);
              

               

              this gives me the following debug output for a sketch on the top plane

               

              Featurename: Sketch1
              Featuretype: ProfileFeature
              Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> System.__ComObject
              

               

              and the output for a sketch on the face of a Boss-Extrude:

               

              Featurename: Sketch2
              Featuretype: ProfileFeature
              Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ---->
              

               

              Could you tell me what I'm doing wrong?

                • Re: Finding references of Features and Sketches
                  BJ de Bruijn

                  I'm a but further, I can get the referenced entity. The code looks like this:

                   

                  Debug.Print("Featurename: " + currentFeature.Name);
                  Debug.Print("Featuretype: " + currentFeature.GetTypeName2());
                  swSketch = currentFeature.GetSpecificFeature2();
                  Entity swEntity;
                  int number = 0;
                  swEntity = swSketch.GetReferenceEntity(ref number);
                  Debug.Print("Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> " + number);
                  

                   

                  which gives output for the sketch on a plane:

                  Featurename: Sketch1
                  Featuretype: ProfileFeature
                  Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> 4
                  

                   

                  and on the face:

                  Featurename: Sketch2
                  Featuretype: ProfileFeature
                  Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> 0
                  

                   

                  Reading the remarks section of 2017 SOLIDWORKS API Help - GetReferenceEntity Method (ISketch) I'll be looking at editing the sketch to get the name.

                    • Re: Finding references of Features and Sketches
                      Nilesh Patel

                      Hi,

                       

                      As per the remarks in ISketch.GetReferenceEntity method, your face has been consumed bu subsequent feature and therfore it returns 0. In this case, you will have to edit the sketch to get the reference entity.

                       

                      Add following lines:

                      bool boolstatus = swModel.Extension.SelectByID2(currentFeature.Name, "SKETCH", 0, 0, 0, False, 0, Null, 0);
                      swModel.EditSketch();
                      

                       

                      after line:

                      swEntity = swSketch.GetReferenceEntity(ref number);
                      

                       

                      add following lines:

                      swModel.ClearSelection2(true);
                      swModel.InsertSketch2(true);
                      

                       

                      after line:

                          

                      Debug.Print("Reference entity (4 = swSelectType_e.swSelDATUMPLANES): ----> " + number);

                       

                      Regards,

                        • Re: Finding references of Features and Sketches
                          BJ de Bruijn

                          Hi Nilesh,

                           

                          Thanks so far, I would like to explain what I'm trying to do. I'm trying to find the face used by the sketch (or other features referencing this face). So I would like to find a way to be able to select the same face which was used for Sketch2.

                           

                           

                          swSketch = currentFeature.GetSpecificFeature2();
                          Entity swEntity;
                          int number = 0;
                          swEntity = swSketch.GetReferenceEntity(ref number);
                          if (number == 0)
                          {
                          Debug.Print("Reference entity sketch has been consumed");
                          bool bs = swModel.Extension.SelectByID2(currentFeature.Name, "SKETCH", 0, 0, 0, false, 0, null, 0);
                          swModel.EditSketch();
                          swModel.ClearSelection2(true);
                          swModel.InsertSketch2(true);
                          }
                          

                           

                          this will let me edit the sketch, but I can't work out which face (id) of Boss-Extrude1 has been used for this sketch..

                           

                          Same problem for Plane2 which has a reference to Boss-Extrude1 also. I'd like to select this face too.

                           

                          I hope to find a way to use SelectByID2 (or something else, whichever works) for both features to select the exact same face which has been used.

                            • Re: Finding references of Features and Sketches
                              Nilesh Patel

                              Hi,

                               

                              I don't have VSTA installed on my work computer but I have attached a VBA macro to select the face on which the sketch is created or plane is referenced from and get face area (to double check). It only works for Boss Extrude and Planes. You need to select one of them before running the macro. Macro works OK even if the face is consumed by another feature. You should be able to convert the code into C#.

                               

                              Hope this helps.

                                • Re: Finding references of Features and Sketches
                                  BJ de Bruijn

                                  Hi Nilesh,

                                   

                                  Thank you, I understand what's happening. I guess the thing I'm searching for does not exist. I'm looking for selecting face[n]@BossExtrude-1. Something like an ID/string/identifier which is known the moment after a feature is made. Like SelectById but instead of a feature.Name a face.Name

                                  So my original question (in hindsight) is lacking.

                                   

                                  To elaborate, I'd like to copy features or build a part from the ground up. So putting a sketch on top of a newly created identical Boss-Extrude, on the exact same face.

                                   

                                  I've been looking at referencing to an entity name (I can give the face a name) but that probably won't work because of the first time such an entity name must be given, the face (of the feature) must be (automatically from the program) selected.

                                   

                                  I'll look into the following:

                                  • automatically have names assigned to faces of the body (do that after each feature in the tree so that a face has a name before inserting a new feature/sketch/geometry)
                                  • Find the face, get the area and normal, and use SelectByRay
                                    • Re: Finding references of Features and Sketches
                                      Nilesh Patel

                                      Sorry, I guess I haven't explain in detail that when you run the macro step by step after selecting the Boss Extrude feature or Plane, it does select the parent face on which the feature or plane is created on as you want. At that point, you can either set name of the face using IPartDoc.SetEntityName to be used later or you can add your codes directly there to do your stuff and finish the macro.

                                       

                                      I hope it is clear now.

                                        • Re: Finding references of Features and Sketches
                                          BJ de Bruijn

                                          Hi Nilesh,

                                           

                                          I understand your macro and actually used your macro to do exactly what you say, give the face a name. The thing is that i can only access the face and give it a name after an sketch has been made which uses that face.

                                           

                                          imagine traversing the feature tree, then open a  part, create a Boss-Extrude1 and then creating a second boss-extrude on top of that "parent" face of Boss-extrude1. How to select that face in the first place is the problem, cause it hasn't got a name yet.