Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cast to AxisSystem not working 1

Status
Not open for further replies.

solid7

Mechanical
Jun 7, 2005
1,403
US
This was a super quick script, scrambled together to unit test a larger bit of functionality.

I think it's pretty straightforward, and there's a relevant comment. So help me understand why this doesn't work. I cast selections to object types all over the place, in all kinds of code. This PARTICULAR case is nagging me hard.

Code:
public void Macro_FindPartParent()
{
     MessageBox.Show("This function will find the Parent Object.");

     Editor cEditor = CATIA.Editors.Application.ActiveEditor;
     Selection cSelection = cEditor.Selection;

     MessageBox.Show(Information.TypeName(cSelection.Item2(1).Value));

     var e = Information.TypeName(cSelection.Item2(1).Value);

     if (e == "AxisSystem")
     {
          MessageBox.Show("This is an AxisSystem");

          // BOOM!!!
          AxisSystem cAxisSystem = (AxisSystem)cSelection.Item2(1);

          MessageBox.Show("" + cAxisSystem.get_Name());
          
     }
     else
     {
          MessageBox.Show("Not an AxisSystem");
     }
}

 
Replies continue below

Recommended for you

cSelection.Item2(1) has type SelectedElement

Correct way to address actual selected object:
(AxisSystem)cSelection.Item2(1).Value
 
Yep, it sure is.

Can you also explain, why .Parent property doesn't work? (even after returning this as a properly cast object)

 
In .NET Parent property should be invoked on actual object interface, not just INFITF.AnyObject which is a base for (almost) all interfaces.

For instance, both of these lines will compile but it's the second one that actually returns correct interface:

Code:
PartDocument doc;
...
doc = ((INFITF.AnyObject)partRoot).Parent;
doc = ((MECMOD.Part)partRoot).Parent;

To be completely clear, Parent there are cases when Parent do work with AnyObject, but I find it more reliable to perform direct cast to a well-known interface and then invoke Parent (as above).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top