1

I'd like to search through the tree in CATIA, and return the names of the parts in the tree using C#. My current code is as follows:

    private void Search(object sender, EventArgs e)
    {
        string searchName = OriginalBox.Text;
        string name;
        INFITF.SelectedElement part;

        //CATIA.StartCommand("Search");
        try
        {
            Sel.Search("Name=" + searchName + "*, all");

            for (int i = 1; i <= Sel.Count; i++)
            {
                part = Sel.Item(i);
                name = part.get_Name();

                MessageBox.Show(i.ToString() + " : " + name);
            }
        }
        catch (IOException ex)
        {
            if (ex.Source != null)
                MessageBox.Show(ex.Source);
            throw;

        }
    }

The MessageBox displays "CATIASelectedElement45". I am receiving the message "ERROR HRESULT E_FAIL has been returned from a call to a COM component." when assigning the Sel.Item(i) to part.

How can I access the part name using Selection.Search?

  • You are not searching for parts, but for anything. If you want to find only parts, restrict your search string to that type. You should try this out with the interactive Edit + Search command. It uses and displays (nearly) the same search strings as the Slection.Search method in the Automation API. – gdir Feb 08 '18 at 08:40

3 Answers3

0

On Sel.Search("Name=" + searchName + "*, all"); you are searching for any object containing that name, if you would like to search exclusively for Part only, you should use a type search:

Sel.Search("(((((('Product Structure'.Part + FreeStyle.Part) + 'Assembly Design'.Part) + 'Part Design'.Part) + 'Generative Shape Design'.Part) + 'Functional Molded Part'.Part) + 'Process Applications'.Part);all");
//To get any search string, use search on Catia and copy it from there.

Also, to get the real value from selection inside your For use this:

Sel.Item(i).Value; // Object retrieved from selection
name = ((INFITF.AnyObject) Sel.Item(i).Value).get_Name(); // Object Name

Note that the SelectedElement class is just a middle class to abstract all kinds of objects into a selection. It contains some properties and methods that may be usefull sometimes though, such as Reference and LeafProduct.

Quima
  • 894
  • 11
  • 21
  • The search function you provided works, however, I am still receiving a HRESULT error and cannot grab the Selected Part name. In the end, I would like to Select a set of parts, and replace it/them with a part I open using the FileDialog. – Roots_radicals Feb 08 '18 at 15:14
  • @Roots_radicals According to the Automation API documentation, Selection.Item has been deprecated as of Catia V5R16, try using Selection.Item2 instead and see it that works. – Nathan Aug 16 '18 at 07:18
0

You can try this:

 Sel.Search ("Name=" + searchName + "* & CATPrtSearch,all");
 for (int i = 1; i <= Sel.Count; i++)
 {
     MECMOD.Part selectedPart;
     selectedPart = Sel.Item(i).Value;
     name = selectedPart.get_Name();
   
     MessageBox.Show(i.ToString() + " : " + name);
 }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user3714887
  • 77
  • 3
  • 16
  • Working with Catia using C# requires the same steps as working with VB.Net. You will probably find more answers of VB.Net than C#. Also, please, keep in mid answers are not place for questions. Post a new question and the community will be glad to answer it for you. – Quima Feb 08 '18 at 15:27
  • I've found that there is more documentation for VB than C#, but C# is a more versatile and powerful language, so I am attempting to switch over to it. I tried your code, unfortunately, I still recieve the HRESULT error, source- mscorlib – Roots_radicals Feb 08 '18 at 18:03
0

Then instead of using Selection search I think it is ideal to loop through a specific type of feature like Bodies, Hybridbodies, Shapes..etc

While looping through it you can compare the item name value with your string value and store it. Something like this:

  int bodiesCount = Part.Bodies.count
  for (int i = 1; i <= bodiesCount; i++)
  {
     string name = Part.Bodies.Item(i).Name;
     if(name == TextBox.Text("your string value"))
     {
        Sel.Add(Part.Bodies.Item(i));
        MessageBox.Show(i.ToString() + " : " + name);
     }

  }
user3714887
  • 77
  • 3
  • 16