Using IModelDocExtension.SelectByID2() API, how to select a face of a sub-part in an assembly?

3,782 views
Skip to first unread message

Olivier

unread,
Apr 3, 2009, 7:48:55 AM4/3/09
to SolidWorks-API
Hello,

I have an assembly with one sub-component (which is a part).

Using IModelDocExtension.SelectByID2(), I want to select a face of
this sub-part, in the context of the assembly.
I only know the name of the face (for instance "E115")

I tried something like the following but this does not work:

modelDocExtenion.SelectByID2
( "E115@part_body@assembly_component-1@assembly", "FACE", 0, 0, 0,
false, 1, null, swSelectOptionDefault )

I can select the part body, but not one of its faces:

modelDocExtenion.SelectByID2
( "part_body@assembly_component-1@assembly", "SOLIDBODY", 0, 0, 0,
false, 1, null, swSelectOptionDefault )

I also tried to select the face in the context of the part document,
but, of course, in the context of the assembly, the selection list is
empty...

Thanks in advance.

Olivier.

Georg D.

unread,
Apr 15, 2009, 10:23:17 AM4/15/09
to ole...@trace.fr, SolidWorks-API
This is an example from the solidworks API. It shows how to make distance mates. It also shows how to select a face by name. I marked the first in the example:

Add Distance Mates Example (VB)

This example shows how to add distance mates.

 

'---------------------------------------------

Option Explicit

 

Dim swApp As SldWorks.SldWorks

Dim assy As SldWorks.AssemblyDoc

Dim longstatus As Long

Dim mateFeature As Object

Dim mateSelMark As Long

Dim numberOfMatesCreated As Long

 

Sub main()

 

Set swApp = Application.SldWorks

Set assy = swApp.ActiveDoc

mateSelMark = 1

numberOfMatesCreated = 0

 

' Frame #1

    ' Front plane Mate

        assy.ClearSelection2 True

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Front@CLM24BT-94@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Front@CLM24BT-94@Before Macro"

        

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Front@UF8024GL-1@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Front@UF8024GL-1@Before Macro"

        Set mateFeature = assy.AddMate3(5, 0, False, 2.54000000001016E-02, 0, 0, 0, 0, 0, 0, 0, False, longstatus)

        If mateFeature Is Nothing Then

            MsgBox "Frame #1 : Front plane mate failed! "

        Else

            numberOfMatesCreated = numberOfMatesCreated + 1

        End If

    '---------------------------------------------

    

    ' Top plane mate

        assy.ClearSelection2 True

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Top@CLM24BT-94@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectionOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Top@CLM24BT-94@Before Macro"

        

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Top@UF8024GL-1@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Top@UF8024GL-1@Before Macro"

        Set mateFeature = assy.AddMate3(5, 0, True, 5.56260000002225E-03, 0, 0, 0, 0, 0, 0, 0, False, longstatus)

        If mateFeature Is Nothing Then

            MsgBox "Frame #1 : Top plane mate failed! "

        Else

            numberOfMatesCreated = numberOfMatesCreated + 1

        End If

    '---------------------------------------------

    

    ' Right plane mate

        assy.ClearSelection2 True

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Right@CLM24BT-94@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Right@CLM24BT-94@Before Macro"

        

        boolStat = swApp.ActiveDoc.Extension.SelectByID2("Right@UF8024GL-1@Before Macro", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOptionDefault)

        If Not boolStat Then MsgBox "Selection error! " & "Right@UF8024GL-1@Before Macro"

        Set mateFeature = assy.AddMate3(5, 0, False, 0.266700000001067, 0, 0, 0, 0, 0, 0, 0, False, longstatus)

        If mateFeature Is Nothing Then

            MsgBox "Frame #1 : Right plane mate failed! "

        Else

            numberOfMatesCreated = numberOfMatesCreated + 1

        End If

  '--------------------------------------

        

        assy.ClearSelection2 True

        Dim strMessage As String

        strMessage = "Number of mates created = "

        strMessage = strMessage + CStr(numberOfMatesCreated)

        MsgBox strMessage

End Sub




Greedings

Gegy
--
******************************************************************
*   Skype: gegy,,,gegy                                  
*   Web:   http://www.gegy.at.gg          
*   Mail:    geg...@gmail.com            
*              georg....@binder-co.at
*   Twitter: http://twitter.com/gegy    
* --------------------------------------------------------------------------------    
*   A strange game. The only winning move is not to play.          
******************************************************************

Sandman

unread,
Apr 16, 2009, 7:50:34 AM4/16/09
to SolidWorks-API
On API Help you'll find the following hint:

"When selecting IFace2 objects, this method uses the specified point
as input to the normal user-interface selection routines to use the
speed of ray tracing. As a result, if the view changes from the
original recorded size or orientation, then the same IFace2 might not
be selected next time. If your application has a pointer to the IFace2
object to be selected, then you can call the IEntity::Select4 method
directly. Otherwise, you can call IModelDoc2::SelectByRay or
IModelDoc2::ISelectByRay. Calling either of these methods allows for
tighter control over the starting point and the direction in which to
search."

So you've got to get a pointer to the Face object and then call
IEntity::Select4. Otherwise you got to provide the coordinates like:

Part.Extension.SelectByID2("", "FACE", 0.8361634305582,
-0.1714221781148, 0.1977050557264, False, 0, Nothing, 0)

Eduardo

Corey Scheich

unread,
Apr 21, 2009, 10:04:40 PM4/21/09
to solidwo...@googlegroups.com
Oliver,
 
To identify/select a named face in an assembly you have to recurs through the assembly components then the components bodies and the bodies faces filtering for the face name you are looking for.  ScheielectByID doesn't support named faces.
 
Combining these two examples from the API Help should get you most of the way there.
 
Traverse Assembly at Component Level Example (VB) - self explanatory
Select All Faces of Component Example (VB) - example of recursing bodies and faces of a component
 
Regards,
 
Corey
 
> Date: Fri, 3 Apr 2009 04:48:55 -0700
> Subject: Using IModelDocExtension.SelectByID2() API, how to select a face of a sub-part in an assembly?
> From: ole...@trace.fr
> To: SolidWo...@googlegroups.com

Windows Live™ SkyDrive™: Get 25 GB of free online storage. Check it out.
Reply all
Reply to author
Forward
0 new messages