1 Reply Latest reply on Nov 16, 2014 5:47 PM by Dmitry Zamoshnikov

    Feature tree traversal w/ dimension retrieval

    Dmitry Zamoshnikov

      I was looking for a way to get a dimension by name through c# and I can't seem to find one.  I'm guessing I can create another function that traverses through each dimension in the feature and search for the dimension by name.  Then I can go through each sub-feature and traverse through each dimension until I find the name I'm looking for.  Does the solidworks API have a dimension by name retrieval function?

       

      This is how I'm looking for the dimension in question:

       

                      // convert the component to a model document

                      swSelMod = (ModelDoc2)swCom.GetModelDoc2(); // select the model document of the object

       

       

                      if (GetFeature(swSelMod, __FEATURE_NAME) != null)

                      {

                          swFeat = GetFeature(swSelMod, __FEATURE_NAME);

                          swDispDim = (DisplayDimension)swFeat.GetFirstDisplayDimension();

                          // note: if i want to get the sketch of the feature I just found, I would have to call

                          //  swFeat.GetFirstSubFeature();

                          // this will be a pointer to the sketch.  From there I can find the dimensions of the geometry.

                          swDim = swDispDim.GetDimension();   // get the first dimension in the feature

                          runningTotal += swDim.Value;    // adds up all of the dimensions

                      }

                      else

                          TxtBx(ctrTextboxMessage, "Feature not found");

       

       

              // this will go through each feature in the model document, gotta get the first feature

              // then traverse through the feature tree by calling feat.GetNextFeature().

              // note: many things are considered features: folders, reference geometry, and etc...

              // one way to find what you're looking for is to find the name of the feature

              public Feature GetFeature(ModelDoc2 model, string fieldName)

              {

                  int featCount = model.GetFeatureCount();    // get a head count of features

                  Feature feat = (Feature)model.FirstFeature();   // target the first feature

       

       

                  // traverse through each feature in the feature tree in the model document

                  for (int i = 1; i <= featCount; i++)

                  {

                      if (feat.Name == fieldName) // if the feature name is found, return a pointer to the feature

                          return feat;

                      feat = feat.GetNextFeature();   // traverse through each feature in the feature tree

                  }

       

       

                  return null;    // feature not found

              }

       

      Message was edited by: Dmitry Zamoshnikov

        • Re: Feature tree traversal w/ dimension retrieval
          Dmitry Zamoshnikov

          Well I just made a function that traverses through sub-features.  This seems to do the job:

           

                  public Dimension GetSketchDimension(Feature feature, string fieldName)

                  {

                      // note: sweeps, lofts, and other features that have multiple sub-features will require

                      // multiple calls to GetNextSubFeature()

                      Feature feat = feature.GetFirstSubFeature();    // get the first sub feature, the sketch

                      DisplayDimension Dispdim = (DisplayDimension)feat.GetFirstDisplayDimension();           

                      Dimension dim = (Dimension)Dispdim.GetDimension();

           

           

                      //TxtBx(ctrTextboxMessage, "pre-dimension traversal loop");

                      while (dim != null)

                      {

                          //TxtBx(ctrTextboxMessage, "in dimension traversal loop");

                          if (dim == null)    // dimension not found

                              return null;

           

           

                          //TxtBx(ctrTextboxMessage, "Dimension: " + dim.Name);

                          if (dim.Name == fieldName)

                              return dim;

           

           

                          // keep traversing through the feature until the dimension is found

                          // have to call this from the sub-feature to get the next dimension

                          Dispdim = (DisplayDimension)feat.GetNextDisplayDimension(Dispdim);

                          dim = (Dimension)Dispdim.GetDimension();

                      };

           

           

                      return null;    // no dimensions in this feature

                  }