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

Hi.

You probably want to reference library that defines [tt]VPMRepReference[/tt] and use is operator instead:
Code:
if (iEditor.ActiveObject is VPMRepReference) { ... }

Anwering your question exactly you have to reference Microsoft.VisualBasic.dll and call
Code:
Microsoft.VisualBasic.Information.TypeName()
For more details see this stackoverflow thread.
 
So I loaded the Microsoft Visual Basic dll... But there is no 'Information.TypeName()' available.

In the code above, I hadn't yet referenced the dll for the VPMRepReference, but it's here:

Code:
using ProductStructureClientIDL;

 
Even so, why would I grab the visual basic dll, and not the C#? (not that it helps, because there isn't an equivalent functionality in C#, that I can see)

 
Yes, I read the link you provided. Did you not read my first reply? Did you try this yourself?

If I ask you a follow-up question, you can be sure that it's because your initial advice didn't work out.

The ONLY thing available in Micrososft.VisualBasic.dll, is 'VBCodeProvider'.

 
solid7 said:
Did you not read my first reply? Did you try this yourself?

Sure, see attachement.

solid7 said:
If I ask you a follow-up question, you can be sure that it's because your initial advice didn't work out.

Ok.

solid7 said:
The ONLY thing available in Micrososft.VisualBasic.dll, is 'VBCodeProvider'.

Where is this file located on your PC? What version does it have?
 
Little Cthulhu said:
Where is this file located on your PC? What version does it have?

14.8.3761.0

C:\Windows\WinSxS

 
Actually, there are several different dlls of that name.

They are all either in:

C:\Windows\Microsoft.NET\Framework
C:\Windows\Microsoft.NET\assembly
C:\Windows\WinSxS
C:\Program Files (x86)\Reference Assemblies

 
Try referencing ones from C:\Windows\Microsoft.NET instead.

But there is no 'Information.TypeName()' available

You get build errors or IntelliSense just can't recognize these classes? Try building the code I provided if latter is the case.

Do you have Visual Studio installed? Does the code work fine there?
 
Little Cthulhu said:
You get build errors or IntelliSense just can't recognize these classes?

Yes and yes.

Little Cthulhu said:
Do you have Visual Studio installed? Does it the code work fine there?

Visual Studio Professional 2013, per the 3DS documentation. All prerequisites met. I have uninstalled both 3DX and Visual Studio, multiple times, and reinstalled.

I can run code just fine. Can create compiled code (self-contained) all day long. The Catia APIs are what seems to be eluding me.

 
Finally got this to work, but another issue...

Code:
        static AxisSystem SelectAxis(Selection cSelection)
        {
            object[] cAny = new object[] {"AxisSystem"};
            string cMessage = "Select an AxisSystem";
            CATMultiSelectionMode cSet = CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection;

            Object cSelected = cSelection.SelectMultipleElements(cAny, cMessage, false, cSet, true);

            MessageBox.Show("Selection type = " + Information.TypeName(cSelected));

            return (AxisSystem)cSelected;
        }

This code always returns:

Selection type = string

Note: this is just a function that I'm calling from within my main .


 
Well, that's exactly what SelectMultipleElements returns. In particular successful selection returns "Normal".
Refer to [tt]DSYAutomation.chm[/tt]
 
OK, I see the error of my ways.

But the example that it shows in the documentation doesn't quite seem to work the same. If I use the "item" method of selection, the only thing it ever seems to return, is "selected element". What am I missing? If I try to to return any properties of "selected element", I get "runtime object" back.

 
Nevermind, I resolved this. Here is what I had to do to get this to work: (I will now go back and put the handlers in place)

Code:
                static AxisSystem SelectAxis(Selection cSelection)
        {
            object[] cAny = new object[] { "AxisSystem" };
            string cMessage = "Select an AxisSystem";
            CATMultiSelectionMode cSet = CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection;

            cSelection.SelectMultipleElements(cAny, cMessage, false, cSet, true);

            object cSelected = cSelection.Item(1).Value;
            bool isAxisSystem = (cSelected is AxisSystem);

            if (isAxisSystem == true) ;
            {
                MessageBox.Show("Selection type = " + Information.TypeName(cSelected));
            }

            return (AxisSystem)cSelected;
        }

Thanks for your help.

 
Thanks for posting your solution.

Common template when working with Selection looks like this:

Code:
if ("Normal" != selection.SelectionMethod())
{
    return null;
}
var selectedObject = selection.Item(1).Value;
var selectedProduct = selection.Item(1).LeafProduct;

// do actual processing
...
 
When you say "common template"... Is this an available resource, or just an internally developed best practice?

 
Got ya. I saw the method documented in the automation document, but yours looked just a bit more abstract. I thought maybe you had access to the C# refinement of the VBA method that is documented. Yours does vary slightly, hence the question.

 
Code:
            while (oIndex <= oCount)
            {
                var cSelItem = cSelection.Item(oIndex);
                var cSelType = Information.TypeName(cSelItem.Value);
                
                MessageBox.Show("" + cSelType);

                oIndex = oIndex + 1; 
            }

So using this code, the test message box always returns "Document". My inputs were 2 planes, manually selected.

I'm not quite finding a method that gets me base level information. I am always at the assembly level with my selection values. I did read the documentation, but I feel that I've missed something...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top