14 Replies Latest reply on Nov 29, 2015 11:01 AM by Евгений Кустов

    Selecting faces in a Part

    Nick Hayman

      Evening all,

       

      I am looking for guidance on selecting faces within a part. Ive scan read a few api help pages and not quite sure where to go with this.

       

      I would like to select a particular face in the part so that i can create a sketch on it. But as the first point of call is selecting faces i thought it best i look into this in more detail.

       

      So first things first, a part has many faces and i know which face i want but how do i get the computer to reconise this? I read a little on the help that you can name the faces in your part. but again how does SW know what face to name what?

       

      Lets say i have a cube and i want to always skecth on the front face of that cube. How would i get the api to select the front face? What does it use to know that the front is the front?

       

      ... digging myself a hole here...

       

      Please can you link me into to useful api help pages and explain the very basics for me so i can understand more.

       

      Many Thanks

      Nick

        • Re: Selecting faces in a Part
          Nick Hayman

          Adding to the above on selecting faces...

           

          Can i name my sketch lines on the example cube as Left, Right, top & bottom. Will this carry through in regards to the faces.

           

          For example, the cube has 6 faces. 4 of those faces can be selected throught the name of the sketch line. Witht he other two faces selected through the sketch plane and NOT the sketch plane.

           

          Hope this makes sense? Is this possible?

            • Re: Selecting faces in a Part
              Simon Turner

              Each face has a unique identifier, but this is a long array of Bytes. You won't know that up-front.

               

              I suggest checking that the face is flat, and then using the Normal method to get a vector which is perpendicular to the face.

              For the Front face of a cube, the normal vector will have coordinate (0,0,1)

               

              So, for each face in the body (lets call the variable myFace):

               

              Dim myFace as Face2

              Dim mySurf as Surface

              dim myNormal as Variant

               

              Set mySurf = myFace.GetSurface

              If mySurf.IsPlane Then

                  myNormal = myFace.Normal

                  if myNormal(0) = 0 and myNormal(1) = 0 and myNormal(2) = 1 then

                        ......do your work here.

               

              Note that checking if a floating point number equals another one is fraught with danger. You should actually check if they are the same to within a tolerance:

              if Abs(myNormal(0)) < 0.0000001 and Abs(myNormal(1)) < 0.0000001 and Abs(myNormal(2) - 1.0) < 0.0000001 then

              ...

                • Re: Selecting faces in a Part
                  Nick Hayman

                  Thanks Simon

                   

                  I've read this a few times and it makes sense. I understand the concept and the tolerance issues.

                   

                  I think I'll need to read it and work out how to actually insert my work into it. Another big learning curve...

                   

                  I'll let you know how I get on.

                   

                  Thanks again for your input.

                    • Re: Selecting faces in a Part
                      Simon Turner

                      Plus each face has a temporary ID (GetFaceID) but that can't be relied on across sessions.

                      There is also the Entity:ModelName attribute. I haven't used this, so I don't know if that can be relied on across sessions (i.e. after saving, closing and re-opening).

                      Finally, the Attribute object can be used. Again, I haven't used it myself.

                • Re: Selecting faces in a Part
                  Patrick O'Hern

                  If you right click on a face (you may have to expand the menu),  there is an option called "Face Properties".  You can give your face a specific name (I called mine "TEST-Front" without the quotes), and then search for that name in your macro.

                   

                  Here is a macro I created to test this out:

                   

                  Sub main()
                      Dim swApp As SldWorks.SldWorks
                      Dim swModel As SldWorks.ModelDoc2
                      Dim swPart As SldWorks.PartDoc
                      Dim swBody As SldWorks.Body2
                      Dim swFace As SldWorks.Face2
                      Dim swEntity As SldWorks.Entity
                      Dim vBodies As Variant
                      Dim bStatus As Boolean
                      Dim i As Long
                     

                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swPart = swModel
                     
                      vBodies = swPart.GetBodies2(swSolidBody, True)
                     
                      For i = 0 To UBound(vBodies)
                          Set swBody = vBodies(i)
                          Set swFace = swBody.GetFirstFace
                          Do While Not swFace Is Nothing
                              Set swEntity = swFace
                             
                              If swEntity.ModelName = "TEST-Front" Then
                                  bStatus = swEntity.Select4(False, Nothing)
                                 
                                  '---------------------
                                  'Do your work
                                  '---------------------
                              End If
                          'Get next face
                              Set swFace = swFace.GetNextFace
                          Loop
                      Next i
                  End Sub

                  • Re: Selecting faces in a Part
                    Nick Hayman

                    The methods for chosing a face by name seem like a good way to do it however it still doesnt answer my original question (I may not have explained very well).

                     

                    To select a face via name you must first select the face yourself and name it. Or this can be done through a macro however it still raises the question that you have to chose the face to rename yourself.

                     

                     

                    Im looking for a way to select a face from the criteria below

                    (Imagine an extruded cube from a square sketch):

                     

                    1. The sketch plane (1 option only)

                    2. NOT the sketch plane (1 option only)

                    3. Vertical & perpendicular to the sketch plane (2 options)

                    4. Horizontal & perpendicular to the sketch plane (2 options)

                     

                    Items 3 & 4 will then need to be broken down further to ensure a difference between the two faces. How can this be done?

                     

                    Simon,

                    I believe your code is close to what i require as it choses faces from a vector. Can you explain how this actually works rather than just the code. Id like to make sure this is the correct route before i proceed.

                     

                    Bit of a tricky one but hopefully we can work it out. Your help is very welcome! Thankyou all.

                      • Re: Selecting faces in a Part
                        Nick Hayman

                        I suppose it doesnt have to be with the options above. Sketch plane not sketch plane etc...

                         

                        But I need to understand how faces CAN be selected. Does it go through them right to left, top to bottom? If i can understand that then it would be a big help.

                          • Re: Selecting faces in a Part
                            Simon Turner

                            Hi Nick,

                            you can't guarantee which order SolidWorks will return the faces to you, so that's why I recommend going through them one by one until you find one that matches your criteria. This could be by being flat and having a normal vector pointing in a certain direction, having a set number of outer edges (e.g. a rectangular face with a circular hole cut into it), being the smallest/largest face on a body.

                            The code I initially posted will find the first flat face on a body whose normal vector is parallel to the Z axis.

                            Hope that helps,
                            Simon

                        • Re: Selecting faces in a Part
                          Jacob Corder

                          Ok. Attatched is a .txt file that has the code needed to do what you want to do.

                          It basically takes a face name for example "FRONT","BACK","LEFT","RIGHT","TOP","BOTTOM" and gets you the face from a single body that matches the equivilent Face2.Normal values.

                           

                          If it is a multi body part then it will not work. you would have to modify the code i am posting to accept a body.

                           

                          Let me know if you need any help.

                            • Re: Selecting faces in a Part
                              Nick Hayman

                              Hi Jacob,

                               

                              That is some crazy code you've sent me!

                               

                              Just copying and pasting that code flags up an error which i believe is to do with the first line (in green).

                              'You will want to call either  of the 2 MaterialFaceFromNormal Functions, Depending on if you know the face name or the faces normal.

                               

                              Ill be honest this doesnt mean a lot to me at the moment. I was hoping you could explain further. Once the code works i think i need some time to understand how and what it is doing! This is a huge step for me... but im keen to learn.

                               

                              Please help me to get this code working and then maybe we can discuss how it works...

                               

                              Many Thanks

                              Nick

                                • Re: Selecting faces in a Part
                                  Jacob Corder

                                  Delete everything before

                                  Public Function MaterialFaceFromNormal(ByVal ISwapp As SldWorks, ByVal FaceName As String, ByVal RoundNormals As Integer) As Face2

                                   

                                  Important question. are you using VBA or the solidworks macro tool inside of Solidworks

                                   

                                  if you are using vba i will rewrite it so it will work.

                                  Basically my code takes a name of the face for example "FRONT" "LEFT" ect and converts it to a known Face Normal Which is the vector in the direction away from the face.  With this we can go through the faces that are created by the Extruded feature while disregarding any already created faces that are parallel to the face that you want.

                                   

                                  There are a few functions in there that i use to build an array of faces. dont worry about that code. its just sizing the array as a new one is added to it.

                                    • Re: Selecting faces in a Part
                                      Nick Hayman

                                      Thankyou Jacob,

                                       

                                      I have reviewed what i want to do and i think i need to leave this until later on in my code. I need to do other work before i decided what surface i need to pick.

                                       

                                      However at some point in the future i will chase you up for more help. If thats ok

                                       

                                      Thankyou

                                      Nick