Access problem to Face2 object by its name

Hello everyone. I am trying to develop a C# application by Solidworks API. I got the following code block from Codestack and modified it for my purpose. It works perfectly for some components but
“swEnt = swComp.GetCorresponding(swEnt);” returns null for some components. If I dont use GetCorresponding() method this time I can’t select the returned “Entity” object in my another method. I couldn’t find a way out to solve it and I would really appriciate your help. Please see the codes below:

private Entity GetNamedEntityFromPart(Component2 swComp, string name, NamedEntityType_e entType)
{
ModelDoc2 model = (ModelDoc2)swComp.GetModelDoc2();
swSelectType_e selType = swSelectType_e.swSelFACES; // Default to Faces

        switch (entType)
        {
            case NamedEntityType_e.Face:
                selType = swSelectType_e.swSelFACES;
                break;
            case NamedEntityType_e.Edge:
                selType = swSelectType_e.swSelEDGES;
                break;
            case NamedEntityType_e.Vertex:
                selType = swSelectType_e.swSelVERTICES;
                break;
        }

        Entity swEnt = null;

        if (model != null)
        {
            if (model.GetType() == (int)swDocumentTypes_e.swDocPART)
            {
                PartDoc swPart = (PartDoc)model;
                swEnt = swPart.GetEntityByName(name, (int)selType);
                swEnt = swComp.GetCorresponding(swEnt);         //This line returns null in some cases
            }
        }
        return swEnt;
    }
1 Like

Hi,

Have you tried making the component resolved before manipulating its obecjt data?

Hi Alexandre, the component is already resolved.

Please share your solution so it’s useful for others as well. Nothing is as frustrating as finding someone with the same problem as you, and no solution :wink:

I will share if I find a solution Peter.

Try using:

swEnt = swEnt.GetSafeEntity;

before:

swEnt = swComp.GetCorresponding(swEnt); 

Remarks from API help documentation:

A safe entity is an entity that continues to be valid after rebuilds until the pointer becomes invalid if the entity object to which it points is deleted.

To determine if an entity is safe, use the IEntity::IsSafe property. This property is read-only and does not persist from session to session.

Thank you Alexandre, I will try it and tell if it works.