16 Replies Latest reply on Jun 30, 2017 10:15 AM by John Alexander

    GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT

    Korbi Anis

      Hi brothers , I wrote a macro to get the XYZ coordinates of each nodes on selected entites ( face).but it does not work ! can somone help me !thanks

       

       

      THIS IS THE MACRO :

      sub main

      Set swModel = swApp.ActiveDoc

          Set swModExt = swModel.Extension

          Set swSelMgr = swModel.SelectionManager

         

        

          vBodies = Part.GetBodies2(0, False)

          Set Body = vBodies(0)

          Set Face = Body.GetFirstFace

          Do While Not Face Is Nothing

              CurFaceName = Part.GetEntityName(Face)

              If (CurFaceName = "LV") Then

                  Face.Select (0) ' Select the face

                  Set swSelObj = swSelMgr.GetSelectedObject5(1)

                  vPIDarr = swModExt.GetPersistReference3(swSelObj)

                  sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

                  Set pDisp1 = GetObjectFromString(swApp, swModel, sIDstring)

                  varArray1 = Array(pDisp1)

            Set entity(0) = pDisp1

             Set CWMesh = Study.Mesh

             value = CWMesh.GetNodeDataFromEntity(pDisp1, NOD)

            

            

            

                End If

              Set Face = Face.GetNextFace

          Loop

      end sub

        • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
          Korbi Anis

          Plzzz I need your help ! maybe I had some errors in Dim pDisp1 and Dim NOD !!

            • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
              John Alexander

              I'm confused by this portion.

               

                   Face.Select (0) ' Select the face

                   Set swSelObj = swSelMgr.GetSelectedObject5(1)

                   vPIDarr = swModExt.GetPersistReference3(swSelObj)

               

              Why select the face just to get a reference to it? Is there some trick here that I'm missing? Isn't swSelObj pointing to the same object as Face?

               

              Also, it looks like GetSelectedObject5 is deprecated, I would advise using GetSelectedObject6. If you want to use it in the same way as GetSelectedObject5, just pass -1 in the second argument.

               

              2012 SOLIDWORKS API Help - GetSelectedObject6 Method (ISelectionMgr)

                • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                  John Alexander

                  Also, are these user-defined functions? I don't see them in the documentation.

                   

                  sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

                  Set pDisp1 = GetObjectFromString(swApp, swModel, sIDstring)

                    • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                      Korbi Anis

                      Thaks dear John for your answer.. I will explain my goal. its simple ! . I n the first time I create a solidworks simulation study. I tpook a part , I mesh it , I made loads , retrains ...and I run the results.my goal is that I want to display the nodes of the mesh , theirs coordinates  of a specific face named 'LV".I used this function like in solidworks help :

                      2016 SOLIDWORKS API Help - GetNodeDataFromEntity Method (ICWMesh)

                      . I tried to select a face using the macro That I wrote in my last message , but when I tried to run it there are a error in line that I specifed!

                        • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                          John Alexander

                          I may not be very much help with the simulation portion of your question. I don't have any experience with SW Simulation stuff.

                           

                          I would recommend setting Option Explicit at the top of every module. This forces you to keep track of your variable instantiation and provides an opportunity for keeping track of their types.

                           

                          I started to take a crack at modifying your code but there is some information that I'm missing. I left some comments where I wasn't sure how to proceed.

                           

                          Can you link the example that you copied from? I think there are a couple of crucial steps missing to to get a reference to the CWMesh.

                           

                          Also, NOD does not appear to be assigned anywhere in your code. That would explain the runtime error that you are having. I think NOD should be an integer equal to the number of nodes on the Face/Entity.

                           

                          Option Explicit

                          'always use this Option Explicit

                           

                          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 vPIDarr As Variant

                              Dim vBodies As Variant

                             

                              'Dim pDisp1 as ??

                              'Dim varArray1 as ??

                              'Dim Entity as ??

                              'Dim CWMesh as ??

                              'Dim Study as ??

                              'Dim pDisp1 as ??

                              'Dim NOD as ??

                             

                              Set swApp = Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                              Set swPart = swModel

                             

                              vBodies = swPart.GetBodies2(0, False)

                              Set swBody = vBodies(0)

                              Set swFace = swBody.GetFirstFace

                              Do While Not swFace Is Nothing

                                  If (swPart.GetEntityName(swFace) = "LV") Then

                                      vPIDarr = swModel.Extension.GetPersistReference3(swFace)

                                     

                                      'I can't find GetStringFromID(), not sure what it does or how it is meant to be used

                                      sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

                           

                                      'I can't find GetObjectFromString, I assume it is related to GetStringFromID

                                      Set pDisp1 = GetObjectFromString(swApp, swModel, sIDstring)

                                     

                                      varArray1 = Array(pDisp1)

                                      Set Entity(0) = pDisp1

                                     

                                      'Where are you getting a reference to Study?

                                      Set CWMesh = Study.Mesh

                                      Value = CWMesh.GetNodeDataFromEntity(pDisp1, NOD)

                                  End If

                                  Set swFace = swFace.GetNextFace

                              Loop

                          End Sub

                            • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                              John Alexander

                              I just noticed these two variables don't appear to be used at all.

                               

                              varArray1 = Array(pDisp1)

                              Set Entity(0) = pDisp1

                               

                              I think you can get rid of those two lines.

                                • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                                  Korbi Anis

                                  Thank you dear John. I will make some comments on the macro that you sent to me.

                                   

                                   

                                  Option Explicit

                                  'always use this Option Explicit

                                   

                                  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 vPIDarr As Variant

                                      Dim vBodies As Variant

                                     =====================================================================

                                      'Dim pDisp1 as ??      

                                      'Dim varArray1 as ??

                                      'Dim Entity as ??

                                      'Dim CWMesh as ??                   IN THIS  PART ,I AM NOT SURE HERE WITH TYPES . I WANT YOUR ADVICE

                                      'Dim Study as ??

                                      'Dim pDisp1 as ??

                                      'Dim NOD as ??

                                  ================================================================== 

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                      Set swPart = swModel

                                    

                                      vBodies = swPart.GetBodies2(0, False)

                                      Set swBody = vBodies(0)

                                      Set swFace = swBody.GetFirstFace

                                      Do While Not swFace Is Nothing

                                          If (swPart.GetEntityName(swFace) = "LV") Then

                                              vPIDarr = swModel.Extension.GetPersistReference3(swFace)

                                            

                                              'I can't find GetStringFromID(), not sure what it does or how it is meant to be used ( I think its not necessary to use this just I want a line or code to select face like entity ...)

                                              sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

                                   

                                              'I can't find GetObjectFromString, I assume it is related to GetStringFromID ( I think its not necessay this )

                                              Set pDisp1 = GetObjectFromString(swApp, swModel, sIDstring)

                                            

                                              varArray1 = Array(pDisp1)

                                              Set Entity(0) = pDisp1

                                            

                                              'Where are you getting a reference to Study? ( FOR THE MESH I MADE IT MANUALLY )

                                              Set CWMesh = Study.Mesh

                                              Value = CWMesh.GetNodeDataFromEntity(pDisp1, NOD)

                                          End If

                                          Set swFace = swFace.GetNextFace

                                      Loop

                                  End Sub

                                    • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                                      Korbi Anis

                                      I changed the macro like this :

                                       

                                      dim OBJ as object

                                      dim valu as object

                                      dim swFface as sldworks.face2

                                      dim C as long

                                       

                                      Set swApp = CreateObject("SldWorks.Application")

                                      Set swModel = swApp.ActiveDoc

                                      Set swPart = swModel

                                      Set swSelMgr = swModel.SelectionManager

                                      Set swSelData = swSelMgr.CreateSelectData

                                      vBodies = swPart.GetBodies2(swAllBodies, True)

                                      Set swBody = vBodies(0)

                                      Set swFace = swBody.GetFirstFace

                                      Do While Not swFace Is Nothing

                                      sCurFaceName = swModel.GetEntityName(swFace)

                                      If sCurFaceName Like "LV" & "*" Then

                                      Set swEnt = swFace

                                      bRet = swEnt.Select4(True, swSelData)

                                      swEnt.SelectByMark False, 1

                                       

                                       

                                      End If

                                      Set swFace = swFace.GetNextFace

                                      Loop

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

                                       

                                       

                                                  

                                         Set CWMesh = Study.Mesh

                                              C = CWMesh.NodeCount

                                       

                                      valu = CWMesh.GetNodeDataFromEntity(OBJ, C)   ===========> in this line I have an error message like in attachement

                                       

                                       

                                      I have the same problem ( ERROR)

                                        • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                                          John Alexander

                                          So the problem is that CWMesh  = Nothing and you are trying to call a method on it like it was a CWMesh.

                                           

                                          This is from the previous thread, I incorporated CWMesh.Nodecount in the call to GetNodeDataFromEntity. I can't run it because I don't have COSMOS so I'm just crossing my fingers that it works - very long shot.

                                           

                                          Option Explicit

                                           

                                          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 COSMOSWORKS As Object

                                              Dim COSMOSObject As CosmosWorksLib.CwAddincallback

                                              Dim ActDoc As CosmosWorksLib.CWModelDoc

                                              Dim StudyMngr As CosmosWorksLib.CWStudyManager

                                              Dim Study As CosmosWorksLib.CWStudy

                                              Dim CwMesh As CosmosWorksLib.CwMesh

                                             

                                              Dim vPIDarr As Variant

                                              Dim vBodies As Variant

                                              Dim vBody   As Variant

                                             

                                             

                                              Set swApp = Application.SldWorks

                                              Set swModel = swApp.ActiveDoc

                                              If swModel.GetType = swDocumentTypes_e.swDocPART Then

                                                  Set swPart = swModel

                                              Else

                                                  MsgBox ("Active document must be a Part")

                                                  End

                                              End If

                                             

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

                                              'stolen from example: http://help.solidworks.com/2012/english/api/swsimulationapi/Create_Curvature-based_Mesh_Example_vb.htm

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

                                              Set COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")

                                              If COSMOSObject Is Nothing Then ErrorMsg swApp, "COSMOSObject object not found", True

                                              Set COSMOSWORKS = COSMOSObject.COSMOSWORKS

                                              If COSMOSWORKS Is Nothing Then ErrorMsg swApp, "COSMOSWORKS object not found", True

                                             

                                              Set ActDoc = COSMOSWORKS.ActiveDoc()

                                             

                                              Set StudyMngr = ActDoc.StudyManager()

                                              If StudyMngr Is Nothing Then ErrorMsg swApp, "No StudyMngr object.", True

                                              Set Study = StudyMngr.GetStudy(0)

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

                                             

                                              vBodies = swPart.GetBodies2(swAllBodies, False)

                                              'do this for all bodies in the part?

                                              For Each vBody In vBodies

                                                  Set swBody = vBody

                                                 

                                                  If Not swBody Is Nothing Then

                                                      Set swFace = swBody.GetFirstFace

                                                      Do While Not swFace Is Nothing

                                                          If (swPart.GetEntityName(swFace) = "LV") Then

                                                              swFace.Select (0) ' Select the face

                                                             

                                                              vPIDarr = swModel.Extension.GetPersistReference3(swFace)

                                                             

                                                              'I really think these are important, I can't tell you how to use pDisp1 if I have no idea what type it is not how it is obtained. Where did you get these function calls from?

                                                              'sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

                                                              'Set pDisp1 = GetObjectFromString(swApp, swModel, sIDstring)

                                                             

                                                             

                                                              Set CwMesh = Study.Mesh

                                                              If CwMesh Is Nothing Then ErrorMsg swApp, "No mesh object.", False

                                                             

                                                              'because I don't know what is returned to pDisp1 from GetObjectFromString

                                                              'cant use Value = CwMesh.GetNodeDataFromEntity(pDisp1, NOD)

                                                              'try passing Face instead

                                                             

                                                              Value = CwMesh.GetNodeDataFromEntity(swFace, CwMesh.NodeCount)

                                                             

                                                              Debug.Print Value 'this might cause an error because you can't print a variant but at least you know it returned something

                                                             

                                                              'stop the macro if it found the face named "LV"

                                                              End

                                                          End If

                                                          Set swFace = swFace.GetNextFace

                                                      Loop

                                                  End If

                                              Next vBody

                                          End Sub

                          • Re: GET NODES COORDINATES ON SELECTED ENTITY AFTER MESHING IT
                            Korbi Anis

                            Really I don't know what's the probelem ;( . plz someone can help with the support of John ? thanks