Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

How can I use FOR EACH Loop with Selectelement3(or 2) through tree? 1

Status
Not open for further replies.

Catiaer

Mechanical
Nov 9, 2019
13
Hello All,
I learn CATIA VBA step by step and now I have a small question.
Before I make VBA in CATIA, I usually tried to use FOR EACH Loop in Excel VBA.
Because as far as I know, FOR EACH Loop is faster and lighter than FOR Loop.
And I think that when I use FOR EACH Loop in a huge assembly that has more than 500 components,
then it could be big different between FOR EACH and FOR Loop.
Here is a simple code that I'm usually aware of FOR Loop.
And I would like to change the FOR Loop to FOR EACH Loop.
Is it possible or is there a similar way?
Code:
Status1 = selection1.SelectElement3(Inputobject, "select parts", True, CATMultiSelTriggWhenUserValidatesSelection, False)
    
selection1.Search "CATAsmSearch.Product,sel"
    
'    For n = 1 To selection1.Count
'        Dim selectedProd1
'        Set selectedProd1 = selection1.Item(n).Value
'        MsgBox selectedProd1.Name
'    Next

    N = 1
    For Each selectedProd1 In [b]selection1.Product.Products[/b]
        Set selectedProd1 = selection1.Item(n).Value
        MsgBox selectedProd1
        n = n + 1
    Next
 
Replies continue below

Recommended for you

Selection object doesn't support for..each


The fastest way in Automation is to use recursive function based on Product.Products collection.
 
Thank you for your information.
I didn't know that SelectElement doesn't support FOR EACH.
Okay, then I have to try to use the Product.Products collection, after I open new window a product what I want.
Is there any another way?
 
Okay, then I have to try to use the Product.Products collection, after I open new window a product what I want

Actually, you don't have to open a product in a new window to enumerate it's children. You can even read product document in memory without opening it in CATIA at all.

And no, there's no other ways to list products except for these two.

By the way, if you want to really speed things up use CATScript instead of VBA.
 
I used CATScript also for a test fastly. Actually I'm making code in VB.NET Visual Studio. Because I have learned that the CATScript or VBA are not safe for my code. That is reason why I use VB.NET. I know that there is a function of Obfuscate in CATScript. But that is also not safe.
 
Your VB.NET code is going to be 10x-100x times slower due to how .NET manages COM objects (google RCW).

What is the reason that you want to obfuscate the code in the first place?
 
Hi, little Cthulhu
are you sure that VB.NET is x10-x100 slower? I think that it's true that vb is slower, but not 10 or 100 times. And I do not want code to leak. Is there any way to protect your scriptcode?
 
You get regression for a short-lived applications which is basically what scripts are about. It takes a lot of time to create RCW when you interact with COM object for the first time.

Protect from what? What do you mean by "code leak"? Are you talking about source code leaks or memory leaks? What is that in your sorce code that makes it so valuable?
 
If I put time, effort, and know-how into my code, I think it's well worth it. And that doesn't mean material things. And I would like to protect it from another person.
 
Fair enough. Look for .NET obfuscator then.

Just to be clear, everything can be reverse-engineered, it's just the matter of time. If you want to protect your code even more build client-server application and place sensitive code on a server side.
 
I appreciate for your advices. I'm just a mechanical engineer, I haven't even learned programming language with any formal classes. These days I'm falling in learning to make a code step by step personally. Because it's very helpful for my work.
 
Hello,
I suggest you go with .NET, the script is so limited and not user friendly. There is really a lot of disadvantages to it. Because of that BMW and Mercedes have their macros written in.NET.
BMW does everything in it, it's like a masterpiece. If you need more info please to visit my website catiavbmacro.com
Sry for the late answer.
 
May I ask, what exactly do you call a masterpiece?

CATScript applied properly outperforms .NET
So you really have to learn both to pick the right tool for the job.


As for large-scale enterprise CATIA customization main programming framework is CAA RADE, not Automation.
 
It's CATIA macro APP with a lot of integrated macros inside. You can't do anything without it (BMW standard).

Here is my App, it's like 5% of it.
Capturexcxc_arz4ot.png


Ofc CATScript has its own strength, some things it's not possible to do In .Net.

If we talk about speed, .NET is so fast, I never had problems with the speed, and I work on huge models.
 
You can do anything in .NET that can be done with CATScript because both use the same APIs called Automation (pointing that out for clarity).

How large are your models? How many parts do they have?
Mine have thousands of parts with dozens of attributes each and you don't even need to do profiling to estimate that scripts run many times faster than .NET code (because of RCWs I mentioned in previous posts).

Overhead can somewhat be neglected with smart caching and reusing of created RCWs, but not always. And for crucial time-consuming operations I tend to generate scripts and on-the-fly and run them from .NET with SystemService.Evaluate.
 
I had with .NET some problems, and with script not, like save to folder with the name from CATIA.

Well not that big, I would say up to 2000, but from other side it's not all to part numbers, it's connected with part complexity also.

From my perspective .NET is better option, and I would recomand for the people who want to start with the macros.


 
I forgot to mention that. NET has a lot of other things to offer.

For example in my case, I had app with the users, I have data collection and all of that is connected database.

Also this is allowing me that I can add new users, remove uses, sell the app etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor