AnsweredAssumed Answered

Feature tree traversal w/ dimension retrieval

Question asked by Dmitry Zamoshnikov on Nov 16, 2014
Latest reply on Nov 16, 2014 by 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



                    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