7 Replies Latest reply on Dec 5, 2014 11:07 PM by Frank Wu Frank Wu

    Who can tell me How to create axis  by the selected Cylinder Surface?

    Frank Wu Frank Wu

      Good morning,

      Who can tell me How to create axis  by the selected Cylinder Surface?

      Here is my code(VBA):


      Dim swSelMgr                As SldWorks.SelectionMgr
      Dim Part                    As SldWorks.ModelDoc2
      Dim boolstatus              As Boolean
      Dim k                       As Integer
      Dim swskobj                 As SldWorks.Surface
      Sub main()
              Set swApp = Application.SldWorks
              Set Part = swApp.ActiveDoc
              Set swSelMgr = Part.SelectionManager
              Set Part = swApp.ActiveDoc
              
              Set swSelMgr = Part.SelectionManager
              
              Part.Extension.SelectAll
              On Error Resume Next
              
              For k = 1 To swSelMgr.GetSelectedObjectCount2(-1)
                  swskobj = swSelMgr.GetSelectedObject6(k, -1)
                  If swskobj.IsCylinder Then
                      
                  'Set Part = swApp.ActiveDoc
                  'boolstatus = Part.Extension.SelectByID2("", "FACE", 1.04985868722451E-03, 4.88853727966898E-03, 4.37064052035794E-02, True, 0, Nothing, 0)
                  boolstatus = Part.InsertAxis2(True)
                  Part.ClearSelection2 True
                  End If
              Next
      End Sub
      
        • Re: Who can tell me How to create axis  by the selected Cylinder Surface?
          Rajat Jain

          Frank,

          It appears that you have many items selected when you called ModelDoc2::InsertAxis2().

           

          The loop which iterates through the selected items makes no sense because you are clearing the selected items after processing the first selected item. Please check the attached macro.

           

          Hope it helps!!!

           

          Rajat Jain

          1 person found this helpful
            • Re: Who can tell me How to create axis  by the selected Cylinder Surface?
              Frank Wu Frank Wu

              Hello Rajat,

              I have run your code,but the result is not what I need . Why can't it create Axis in all Cylinder Surfaces?

              in all Cylinder Surfaces?

               

               

              Frank

               

              1111.JPG

              • Re: Who can tell me How to create axis  by the selected Cylinder Surface?
                Frank Wu Frank Wu

                Hi Rajat,

                I tried to run code in an assembly model, but there was no output.I feel so strange , is there anything special for the code in the assembly model?

                Thank you so much for your help!

                 

                code:

                Option Explicit
                Dim swApp                  As SldWorks.SldWorks
                Dim swSelMgr                As SldWorks.SelectionMgr
                Dim Part                    As SldWorks.ModelDoc2
                Dim swSelData              As SldWorks.SelectData
                Dim swModel As SldWorks.ModelDoc2
                Dim SelMgr As Object
                Dim params As Variant
                Dim swFeat          As SldWorks.Feature
                Dim swRefAxis      As SldWorks.RefAxis
                Dim vAxisParam      As Variant
                Dim skSegment      As Object
                Dim j              As Integer
                Sub main()
                    On Error Resume Next
                    Set swApp = Application.SldWorks
                    Set Part = swApp.ActiveDoc
                    Set swSelMgr = Part.SelectionManager
                    Set swSelData = swSelMgr.CreateSelectData()
                    
                    Dim swCurrentSelectionFilters As Variant
                    swCurrentSelectionFilters = swApp.GetSelectionFilters()
                    
                    'set selection filters to face selection only..
                    Dim selfils(0) As Long
                    selfils(0) = swSelFACES
                    Call swApp.SetSelectionFilters(selfils, True)
                    Part.Extension.SelectAll ' all faces shall be selected now..
                    
                    'set selection filters back to default..
                    Call swApp.SetSelectionFilters(swCurrentSelectionFilters, True)
                    
                    Dim oCylinderFaceCollection As Collection
                    Set oCylinderFaceCollection = New Collection
                    Dim boolstatus As Boolean
                    Dim swskFace As SldWorks.Face2
                    Dim swskobj As SldWorks.Surface
                      
                    Dim k As Integer
                    For k = 1 To swSelMgr.GetSelectedObjectCount2(-1)
                        If swSelMgr.GetSelectedObjectType2(k) = swSelFACES Then
                            Set swskFace = swSelMgr.GetSelectedObject6(k, -1)
                            If Not swskFace Is Nothing Then
                                Set swskobj = swskFace.GetSurface()
                                If Not swskobj Is Nothing Then
                                    If swskobj.IsCylinder Then
                                        Call oCylinderFaceCollection.Add(swskFace)
                                    End If
                                End If
                            End If
                        End If
                    Next k
                    
                    Call Part.ClearSelection2(True)
                    Dim oEntity As SldWorks.Entity
                    For k = 1 To oCylinderFaceCollection.Count
                        Set oEntity = oCylinderFaceCollection.Item(k)
                        boolstatus = oEntity.Select4(False, swSelData)
                        boolstatus = Part.InsertAxis2(True)
                        Set oEntity = Nothing
                    Next k
                    Set swApp = Nothing
                    Set Part = Nothing
                    Set swSelMgr = Nothing
                    Set swSelData = Nothing
                    Set oCylinderFaceCollection = Nothing
                    If Err.Number <> 0 Then Err.Clear
                
                End Sub