Saving selected object

Sami Piira

Saving selected object
Hello! I've been fighting with CATIA V5 Automation for few weeks now and COE forum has been valuable tool all this time so I decided to join too. I've searched through the forum and other deep dark parts of the internet but haven't found good answer to my question: Is there a way to save selected objects in assembly to some kind of collection or array and access those saved objects afterwards? I'm trying to create macro in VBA that measures sum of areas in selected Faces and then subtract areas of other selected Faces from that sum. As user selects Faces, its color is changed depending on if he is adding or subtracting and when the macro is done I want to reset those colors back to their originals. Everything else has been quite easy but problem lies here, I can't find any sensible way of saving those selected Faces and access them again later on so that I could reset the colors. Any help would be greatly appreciated. I am using CATIA V5 R17 SP6 HF48.

Georgios Eleftheriadis

RE: Saving selected object
(in response to Sami Piira)

Welcome SamizHC,

Try this

...................
Dim MySelection
Dim MyArray() As Face
Dim strReturn As String
      'Initialization Section
      'Initialize MyArray Dimension
....................

      Set MySelection=CATIA.ActiveDocument.Selection
      MySelection.Clear
      strReturn= MySelection.SelectElement2( Array("Face") , "Select a Face:", False)
      If strReturm="Normal" Then
            ReDim Preserve MyArray(Ubound(MyArray)+1)
            Set MyArray(Ubound(MyArray))=MySelection.Item2(1).Value
      End If
....................

This is not a complete function/sub. It just shows the concept/mechanism

I hope it helps.
-GEL

Steven Roemish

RE: Saving selected object
(in response to Sami Piira)
Hey Gel,
I can't figure out your "SelectElement2( Array("Face")" statement. Is it supposed to let the user select more than one surface?
I would have thought that the line would have read something like this:
strReturn= MySelection.SelectElement2("Face" , "Select a Face:", False)
What does "Array" do in your line statement?

Georgios Eleftheriadis

RE: Saving selected object
(in response to Sami Piira)
Hey Steeve,

1. The line of code you mentioned is as shown in my post ie
strReturn= MySelection.SelectElement2( Array("Face") , "Select a Face:", False)
2. The first argument of the Selection2 method must be of CATIASafeArrayVariant ie an array(not a string). In this way the programmer allows to the user to select more than one type of CATIA objects (for examble "Point" or "Line", where in this case the code line shall be
strReturn= MySelection.SelectElement2( Array("Point", "Line") , "Select a Point or a Line:", False)
)
3. As mentioned in the post this is not a complete function/sub. If the programmer needs to make several selections or multil-selections then he has to Do...Loop or to use the SelectElement2, SelectElement2...etc
4. Array is a VBA function which returns a Variant containing an array. It is the way to array several elements without creating an array variable and so consuming computer resources.

Little Cthulhu

RE: Saving selected object
(in response to Sami Piira)
Hi.

GEL is right, an array seems like a classic solution for you issue.
Although VBA provides another way to store items in a single container. It involves usage of Collection class.

To create container:

Dim MyCollection as New Collection
or
Dim MyCollection as Collection
Set MyCollection = New Collection

To store an item in container:

MyCollection.Add MyItem

To retrieve an item by it's index:

MyItem = MyCollection.Item(3)

To remove an item from container by it's index:

MyCollection.Remove 3

To count items in container:

INbItems = MyCollection.Count

In my opinion Collections are easier to use than arrays, and they are supported by CATscript as well.

Sami Piira

RE: Saving selected object
(in response to Sami Piira)
Thank you all!

I'm bit embarrassed when I found out what the problem was.
My array was working perfectly all this time but I just wasn't using Selection in proper a way, once it was resolved everything worked like charm.