14 Replies Latest reply on Nov 2, 2018 3:21 PM by Artem Taturevych

    Select Normal

    alia sfaxya

      Good morning, i 'm trying to determine the normal of part in solidworks using this algorithm in vb.net :

      swModel = swApp.ActiveDoc

              swAssy = swModel

              swModel.ClearSelection2(True)

              swDocExt = swModel.Extension()

              boo = swDocExt.SelectByID2("part@assembly", "COMPONENT", 0, 0, 0, True, 1, Nothing, 0)

       

       

        Dim vNorm As Object

      swModel = swApp.ActiveDoc

          swSelMgr = swModel.SelectionManager

          swFace = swSelMgr.GetSelectedObject6(1, 0)

              vNorm = swFace.Normal

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

       

      but, i got an error in red line : Object reference not set to an instance of an object.

        • Re: Select Normal
          Marco Maso

          Are you sure that swSelMgr.GetSelectedObject6(1, 0) actually returns a face?

           

          Please try the following macro:

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim selectedFace As SldWorks.Face2

          Dim vNorm As Variant

           

          Sub main()

           

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          Set selectedFace = swSelMgr.GetSelectedObject6(1, -1)

           

          vNorm = selectedFace.Normal

          swApp.SendMsgToUser ("Normal [" & vNorm(0) & ", " & vNorm(1) & ", " & vNorm(2) & "]")

           

          End Sub

           

          Select a face in Solidworks and launch the macro. Beware that the macro will fail if:

          • nothing is selected
          • the selected object is not a face
            • Re: Select Normal
              alia sfaxya

              Hello,

              i replaced swSelMgr.GetSelectedObject6(1, 0)  by swSelMgr.GetSelectedObject6(1, -1)

              but, it showed me this error :

              Can not cast a COM object of type 'System .__ ComObject' to interface type 'SolidWorks.Interop.sldworks.Face2'. This operation failed because the QueryInterface call on the COM component for the IID interface '{4A8BA4D8-DA25-4B75-8E2D-4922B74D81ED}' failed because of the following error: This interface is not not supported (HRESULT exception: 0x80004002 (E_NOINTERFACE)).

                • Re: Select Normal
                  Marco Maso

                  Are you sure you are selecting a face?

                   

                  Please try this macro, I added some extra checks.

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swSelMgr As SldWorks.SelectionMgr

                  Dim selectedFace As SldWorks.Face2

                  Dim vNorm As Variant

                   

                  Sub main()

                   

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                   

                  If swSelMgr.GetSelectedObjectCount2(-1) > 0 Then

                      If swSelMgr.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelFACES Then

                          Set selectedFace = swSelMgr.GetSelectedObject6(1, -1)

                          vNorm = selectedFace.Normal

                          swApp.SendMsgToUser ("Normal [" & vNorm(0) & ", " & vNorm(1) & ", " & vNorm(2) & "]")

                      Else

                          swApp.SendMsgToUser "Invalid selection (" & swSelMgr.GetSelectedObjectType3(1, -1) & ")"

                      End If

                  Else

                      swApp.SendMsgToUser "Nothing selected"

                  End If

                   

                  End Sub

                   

                  If you don't select a face the macro will warn you with a "Invalid selection (*)" where * will be the selection type you are actually selecting instead of the face. You can check which entity you are selecting by checking the returned selection type (if you get the error) with the list on 2018 SOLIDWORKS API Help - swSelectType_e Enumeration .

                    • Re: Select Normal
                      alia sfaxya

                      i got this response :

                      as you said, i this it's a problem of selection, i tried with this algorithm to select automaticly the face of the part :

                      swDocExt = swModel.Extension()

                              boo = swDocExt.SelectByID2("part@assembly", "FACE", 0, 0, 0, True, 1, Nothing, 0)

                      but, igot the  same error.

                        • Re: Select Normal
                          Josh Brady

                          You are going to have to figure out how to select a face programmatically, or you are going to have to pre-select a face before running the macro.

                           

                          Figuring out which face to select using code is difficult.  Does your part have only one face?  If not, how in the world do you expect this code to select the face you want?  You should pre-select the face before running the code.  You should probably use Marco's code.

                          • Re: Select Normal
                            Attilio Colangelo

                            It appears you are trying to get a face by name.  If so, from the documentation for SelectByID2

                            "To get IFace2, IEdge or IVertex objects by name, use IPartDoc::GetEntityByName or IPartDoc::IGetEntityByName."

                            so you would NOT use SelectByID2

                             

                            (I have been down this road many times)

                            You need to name the face first interactively. However this will not work at the assembly level, only IPartDoc.

                    • Re: Select Normal
                      Gertjan Van Dijk

                      This is something i struggled with for a while as well.

                       

                      The problem is that (at least up to SW2017), "ModeldocExtension.SelectByID2()" does not, in fact, select Faces. if you debug and check the value for 'boo', you will see that is becomes/stays "false".

                       

                      To select a face in this way, you will need to use "ModeldocExtension.SelectByRay()".

                      This method requires you to first know the coordinates of a random point on the face, which is a challenge in itself.

                      • Re: Select Normal
                        Artem Taturevych

                        Check this thread about detailed explanation of selections: Selections in SOLIDWORKS API