简体   繁体   中英

Solidworks C# can't extract item from feature that has TypeName equals "Reference"

i have opened solidworks assembly (swDocumentTypes_e.swDocASSEMBLY) using C# and i have iterated through all the features in order to get all the Sketchs called 'ISO/XXX' under each part of the assembly, here is the code

public void openFile(string skeletonFilePath)
    {
        object[] Features = null;
        int i = 0;
        string FeatType = null;[1]
        string FeatTypeName = null;

        if (string.IsNullOrWhiteSpace(skeletonFilePath)) { return; }
        ModelDoc2 model = _sldWorks.OpenDoc("C:PATH/fileName.SLDASM", (int)swDocumentTypes_e.swDocASSEMBLY);
        Feature swFeat = default(Feature);
        SelectionMgr swSelMgr = default(SelectionMgr);
        swSelMgr = (SelectionMgr)model.SelectionManager;
        swFeat = (Feature)model.FirstFeature();

        while ((swFeat != null))
        {
            FeatType = swFeat.Name;
            FeatTypeName = swFeat.GetTypeName2();
            if ((FeatTypeName == "Reference")
            {
                Debug.Print(" Name of feature: " + swFeat.Name);
                    Debug.Print(" Type of feature: " + swFeat.GetTypeName2());


            }
            swFeat = (Feature)swFeat.GetNextFeature();

        }

    }

the problem : each time i try to extract the items under the feature (of one part) i got an exception, i have to tried these ways:

  • swFeat.GetDefinition() // i've got null exception
  • swFeat.GetSpecificFeature2() // i've got dynamic value which i don't know the class i need to cast with
  • var childs = (Object[])swFeatSupport.GetChildren(); // i've got only to constraints under the part

example of project

Your code is only iterating over top level features. You can use IFeature::GetFirstSubFeature() and IFeature::GetNextSubFeature to get sub feature. Make this function recursive so it will iterate over all features, regardless of how many levels deep they are. Another layer you need to consider is the components - you need to iterate over components in an assembly first if you need feature data in the context of individual parts.

Here's an example from the Solidworks API documentation. Its poorly written (IMO) but it will guide you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM