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!

C# - Can't seem to return the object type of the active object

Status
Not open for further replies.

solid7

Mechanical
Jun 7, 2005
1,403
US
I'm just starting into C#, and I'm attempting to write a little code that only works if we have a 3D Shape active.

I'm basically working through the object browser to try to figure this out, so any pro tips on making this struggle a bit easier, would be greatly appreciated.

Code:
using System;
using INFITF;
using MECMOD;
using HybridShapeTypeLib;


namespace VSTA_Applications_xxxxxx_vst00000001_A_1
{
    public sealed partial class ThisApplication : ObjectModelAgentLib.CATIAEntryPoint
    {
        public static void Macro_xxxxxxx()
        {
            Editor iEditor = CATIA.ActiveEditor;
            string iEditName = iEditor.ActiveObject.GetType().ToString();

            System.Windows.Forms.MessageBox.Show("Editor name = " + iEditName);


            if (iEditName != "VPMRepReference")
            {
                 <do more stuff>
            }

        }
    }
}

With this code, the message box always returns a value of 'System.__ComObject'. I don't know if that's because I'm trying to obtain the type from the system library, instead of a Catia library...

 
Replies continue below

Recommended for you

How did you perform actual selection? Manually or with SelectElement? If latter, what typestrings were passed as a first argument?
 
I didn't pass anything into a separate method. For the time being, this was written procedurally, and I've just called the selection from CATIA.Application.ActiveEditor. (so the geometry has to be manually selected before I go into the macro) The only other major thing my code omitted, was the selection count. It's a barebones syntax test.


 
So after a restart, that "Document" object type goes away. I get something. Still not quite the right object. More on that in a moment...

Automation CHM said:
o Property LeafProduct( ) As AnyObject (Read Only)

Returns the leaf product instance which aggregates this selected element in the specification tree.
Role: The AnyObject returned is a VPMRepInstance , VPMInstance or VPMReference if a product appears in the specification tree, in the path corresponding to the current selection, or a fake AnyObject whose AnyObject.Name property is equal to "InvalidLeafProduct" otherwise.
Used in combination with AnyObject.Parent property (which enables navigation in the object structure), the script can browse the path of the selected element.

When I use the .LeafProduct property, I always get a VPMRepOccurence back as my "AnyObject" type. That's not even in the list of return types! What am I missing?

As for the object type being returned by the selection, I am doing a baseline test with a single plane. It is coming back to me now as "Planar Face", when I select the geometry directly, and "HybridPlaneShape..." when I select from the tree. (telling me how it was created) However, I can't get the actual name off of the object.

Code:
 public void Macro_GetParents()
        {
            Editor cEditor = CATIA.Editors.Application.ActiveEditor;
            PLMProductService cService = (PLMProductService)cEditor.GetService("PLMProductService");

            Selection cSelection = cEditor.Selection;
            
            MessageBox.Show("Number of items selected = " + cSelection.Count);

            int oCount = cSelection.Count;
            int oIndex = 1;

            while (oIndex <= oCount)
            {
                var cSelItem = cSelection.Item(oIndex);
                var cSelRet = cSelection.Item(oIndex).Value;

                var cSelAnyObject = cSelItem.LeafProduct;
                
                MessageBox.Show(Information.TypeName(cSelItem.Value) + " = " + cSelItem.get_Name());
                MessageBox.Show(Information.TypeName(cSelAnyObject) + " = " + cSelAnyObject.get_Name());
                
                oIndex = oIndex + 1; 
            }
        }

 
Can't help with VPMRepOccurence, but as for feature name you retrieve it from [tt]SelectedElement.Value[/tt] casted to INFITF.AnyObject:

Code:
var cSelRet = (INFITF.AnyObject)cSelection.Item(oIndex).Value;                
MessageBox.Show(cSelRet.get_Name());
 
That actually doesn't return anything different.

Code:
            while (oIndex <= oCount)
            {
                var cSelItem = (AnyObject)cSelection.Item(oIndex);
                var cSelRet = (AnyObject)cSelection.Item(oIndex).Value;

                MessageBox.Show(Information.TypeName(cSelRet) + " = " + cSelItem.get_Name());
               

                oIndex = oIndex + 1; 
            }

This ends up with the following values: (see attachment)



 
 https://files.engineering.com/getfile.aspx?folder=41878c75-66f2-48da-8433-d493f5bb8924&file=WTFObjects.JPG
I think that the foul result from the LeafProduct is coming from the lack of a specific object type. I mean, I get "planar face" as my result, but I want the whole plane. Using the "parent" property always gives me a fatal error. VS doesn't squawk it, but it never works. I would have thought that the parent of a planar face, would be a plane. But nope.



 
solid7 said:
That actually doesn't return anything different.

Because that:

Code:
MessageBox.Show(Information.TypeName(cSelRet) + " = " + cSelItem.get_Name());

is not what I wrote:

Code:
MessageBox.Show(cSelRet.get_Name());

solid7 said:
I mean, I get "planar face" as my result, but I want the whole plane.

If you select a feature in geometry viewer then Selection contains it's generic geometry (vertex, edge or face). If you select it in specification tree Selection contains object of Automation type that corresponds to the feature (i.e. [tt]HybridShapeSurfaceExplicit[/tt]). This is one of the things to be aware of in "free selection" scenario.

[tt]SelectElement...[/tt] methods on the other hand require type filter string and return objects of specified Automation types only. So when you call [tt]SelectElement...(new object[] { "HybridShapeSurfaceExplicit" }, ...)[/tt] and then select face in geometry viewer Selection contains object of [tt]HybriShapePlaneExplicit[/tt] type, not [tt]Face[/tt] type.

You're absolutely right when using [tt]Parent[/tt] property to move from generic geometry to it's feature. Unfortunately I've no idea why it fails.
 
Little Cthulhu said:
If you select a feature in geometry viewer then Selection contains it's generic geometry (vertex, edge or face). If you select it in specification tree Selection contains object of Automation type that corresponds to the feature (i.e. HybridShapeSurfaceExplicit). This is one of the things to be aware of in "free selection" scenario.

So, I've always kinda known this. However, what throws me, is that if I use the finder, and select onscreen "from element", it always returns an axis system, no matter what part of the axis system I select.

How do I either: A) force the selection method to return the principal object type, or B) figure out the explicit feature types to include in the object filter?

I know that I can write a test script, to return the feature type, based on selection. But I think this would be time consuming, and miss lots of scenarios.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top