9 Replies Latest reply on Jul 2, 2014 10:24 PM by Gabriel Lorenzo Yangzon

    C# Select top and bottom face

    Gabriel Lorenzo Yangzon

      Hi guys

       

      I have some problem selecting the top and bottom face of a rectangle

      Is there anyway I can select only the top and bottom face of an object ?

       

       

      Capture.PNG

      heres my code:

       

      Is there anyway I can select only the top and bottom face of an object ?

       

      ModelDoc2 swDoc = ((ModelDoc2)(swApp.ActiveDoc));
                  bool boolstatus = false;
                  int i = 0;
                  int ctr = 0;
                  Feature ft = null;

                  SelectionMgr swSelmgr = swDoc.SelectionManager;
                  SelectData swSelData = (SelectData)swSelmgr.CreateSelectData();
                  boolstatus = swDoc.Extension.SelectByID2("Thicken1", "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
                  Feature feat = swSelmgr.GetSelectedObject6(1,-1);

                  object[] objFace = feat.GetFaces();

                  foreach (Face fce in objFace)
                  {                            
                      ctr++;
                      Entity swEntity = (Entity)fce;
                      swEntity.Select2(false, 1);
                      swDoc.ViewZoomToSelection();

                      ft = swDoc.FeatureManager.InsertMoveFace2(0, true, 0, 0.7, 0, 0);               
                  }

       

      thanks

      Gab

          • Re: C# Select top and bottom face
            Sean Phillips

            If your face is always flat. Why not check the normal direction. Normal Property (IFace2)

              • Re: C# Select top and bottom face
                Gabriel Lorenzo Yangzon

                thank you for the reply

                 

                how can I code that

                 

                Entity swEntity = (Entity)fce;
                  swEntity.Select2(false, 1);

                object[]objj =  fce.Normal();

                 

                if( ???)

                {

                 

                }

                  • Re: C# Select top and bottom face
                    Sean Phillips

                    its an array of doubles.

                     

                    top face will look like this

                     

                    Normal = (-0, 1, -0)

                     

                    bottom face will look like this

                     

                    Normal = (-0, -1, -0)

                     

                    that is if your faces are parallel to the origin cords

                     

                     

                    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                    Sub main()

                     

                        Dim swApp               As SldWorks.SldWorks

                     

                        Dim swModel             As SldWorks.ModelDoc2

                     

                        Dim swSelMgr            As SldWorks.SelectionMgr

                     

                        Dim swFace              As SldWorks.Face2

                     

                        Dim vNorm               As Variant

                     

                       

                     

                        Set swApp = Application.SldWorks

                     

                        Set swModel = swApp.ActiveDoc

                     

                        Set swSelMgr = swModel.SelectionManager

                     

                        Set swFace = swSelMgr.GetSelectedObject5(1)

                     

                       vNorm = swFace.Normal

                     

                       Debug.Print "Normal = (" & vNorm(0) & ", " & vNorm(1) & ", " & vNorm(2) & ")"

                     

                        End Sub

                      • Re: C# Select top and bottom face
                        Gabriel Lorenzo Yangzon

                        hmmm thank you for the information,

                         

                        i can now select the top

                         

                        but

                         

                        can you help me this one my bottom part sometimes not parallel to origin coords  sometimes it shape is square extrude but there is a special case like " J " shape extrude.

                         

                        thanks

                        gab

                          • Re: C# Select top and bottom face
                            Sean Phillips

                            can you attach a picture of the J shape part?

                              • Re: C# Select top and bottom face
                                Gabriel Lorenzo Yangzon

                                thank you for the reply

                                 

                                Untitled.png

                                  • Re: C# Select top and bottom face
                                    Sean Phillips

                                    'this should select the most bottom pointing face.

                                     

                                    'Cheers,

                                    'Sean Phillips

                                     

                                    Sub main()

                                     

                                        Dim swApp               As SldWorks.SldWorks

                                     

                                        Dim swModel             As SldWorks.ModelDoc2

                                     

                                        Dim swSelMgr            As SldWorks.SelectionMgr

                                     

                                        Dim swFace              As SldWorks.face2

                                     

                                        Dim vNorm               As Variant

                                     

                                        Set swApp = Application.SldWorks

                                     

                                     

                                        Set swModel = swApp.ActiveDoc

                                     

                                     

                                        Set swSelMgr = swModel.SelectionManager

                                     

                                        Dim bodies As Variant

                                       

                                        Dim partdoc As partdoc

                                       

                                        Set partdoc = swModel

                                       

                                        bodies = partdoc.GetBodies2(SwConst.swBodyType_e.swSolidBody, True)

                                     

                                        Dim body As Body2

                                       

                                        Dim i As Integer

                                       

                                        Dim holdface As face2

                                       

                                        For i = 0 To UBound(bodies)

                                       

                                        Set body = bodies(i)

                                          

                                        Dim Face As face2

                                       

                                        Dim faces As Variant

                                          

                                        faces = body.GetFaces

                                       

                                        Dim j As Integer

                                       

                                        For j = 0 To UBound(faces)

                                       

                                            Set Face = faces(j)

                                       

                                            Dim face2 As face2

                                           

                                            Dim cords1 As Variant

                                           

                                            cords1 = Face.Normal

                                           

                                            Dim k As Integer

                                           

                                            For k = 0 To UBound(faces)

                                           

                                                Set face2 = faces(k)

                                           

                                                Dim cords2 As Variant

                                               

                                                cords2 = face2.Normal

                                               

                                               

                                                If cords2(1) < cords1(1) Then

                                               

                                                    Set holdface = face2

                                               

                                                End If

                                           

                                            Next

                                       

                                        Next

                                           

                                        Next

                                       

                                        swSelMgr.AddSelectionListObject holdface, Nothing

                                       

                                        End Sub