22 Replies Latest reply on May 15, 2018 4:26 PM by Mário Neto

    Little help with HoleWizard API

    Mário Neto

      Hello!

       

      I'm trying to write a macro that makes a guiding hole in every end/start point of the bend lines in a sheet metal component. But i'm stuck with this code and can't make it.

       

         

      Public Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swPartDoc As SldWorks.ModelDoc2

          Dim swFeature As SldWorks.Feature

          Dim swSubFeature As SldWorks.Feature

       

       

          Dim vSkSegArr As Variant

          Dim vSkSeg As Variant

          Dim swSkSeg  As SldWorks.SketchSegment

          Dim swSkLine As SldWorks.SketchLine

       

       

          Set swApp = Application.SldWorks

          Set swPartDoc = swApp.ActiveDoc

          

          Set swFeature = swPartDoc.FirstFeature

            

          While Not swFeature Is Nothing

                 

              Select Case swFeature.GetTypeName2

                  Case "FlatPattern"

                      Set swSubFeature = swFeature.GetFirstSubFeature

                        

                      While Not swSubFeature Is Nothing

                          If swSubFeature.Name Like "*Curva-Linhas*" Then

                              Dim swSketch As Sketch

                              Set swSketch = swSubFeature.GetSpecificFeature2

                              vSkSegArr = swSketch.GetSketchSegments

                              For Each vSkSeg In vSkSegArr

                                  Set swSkSeg = vSkSeg

                                  Select Case swSkSeg.GetType

                                      Case swSketchLINE

                                          Set swSkLine = swSkSeg

                                          ProcessSketchLine swApp, swPartDoc, swSketch, swSkLine

                                  End Select

                              Next vSkSeg

                          End If

                          Set swSubFeature = swSubFeature.GetNextSubFeature

                      Wend

              End Select

              Set swFeature = swFeature.GetNextFeature()

          Wend

       

       

      End Sub

       

       

      Private Sub ProcessSketchLine(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swSketch As SldWorks.Sketch, swSkLine As SldWorks.SketchLine)

          Dim swStartPt As SldWorks.SketchPoint

          Dim swEndPt As SldWorks.SketchPoint

          Dim swStartModPt As SldWorks.MathPoint

          Dim swEndModPt As SldWorks.MathPoint

          Dim swFeatMgr               As SldWorks.FeatureManager

          Dim swFeat                  As SldWorks.Feature

          Dim swWizHole               As SldWorks.WizardHoleFeatureData2

         

               Set swFeatMgr = swModel.FeatureManager

            

       

          Set swStartPt = swSkLine.GetStartPoint2

          Set swEndPt = swSkLine.GetEndPoint2

         

      Set swFeat = swFeatMgr.HoleWizard(swWzdHole, _

                          swStandardAnsiMetric, _

                          swStandardAnsiMetricSocketHeadCapScrew, _

                          "M2.0", _

                          swEndCondThroughAll, _

                          2, _

                          2, _

                          swStartPt, _

                          swEndPt, False, False, False, False, False, False, False, False, False, False)

      End Sub

        • Re: Little help with HoleWizard API
          Nilesh Patel

          Hi Mario,

           

          IFeatureManager::HoleWizard is obsolete now and you should use IFeatureManager::HoleWizard5 method. Have a look at the following example: 2018 SOLIDWORKS API Help - Create Hole Wizard Hole Example (VBA)

           

          As the API help, you need to select both sketch point before inserting hole wizard.

           

          Hope this helps.

           

          Regards,

          Nilesh

          • Re: Little help with HoleWizard API
            Jacob Corder

            2017 SOLIDWORKS API Help - HoleWizard5 Method (IFeatureManager)

             

            you cant set swStartPt as an argument to HoleWizard.

             

            you must preselect those sketch points prior to calling HoleWizard.

             

            2017 SOLIDWORKS API Help - Insert Hole Wizard Slot and Hole Example (VBA)

              • Re: Little help with HoleWizard API
                Mário Neto

                Thanks!

                 

                Do i have to select the face also or just the points are enough?

                  • Re: Little help with HoleWizard API
                    Jacob Corder

                    try it and find out.  i am sure selecting the points only works.  because points have a reference to the face they are placed on.

                      • Re: Little help with HoleWizard API
                        Mário Neto

                        Thank you!

                        I'll try it once i have time. Also, i was trying to use this macro http://help.solidworks.com/2016/english/api/sldworksapi/flatten_sheet_metal_part_example_vb.htm?verRedirect=1 to flatten the part, but it doesn't seems to work

                          • Re: Little help with HoleWizard API
                            Nilesh Patel

                            Have a look at the following thread in regards to flatten the part: Bended part flatening script does not work

                              • Re: Little help with HoleWizard API
                                Mário Neto

                                Okay, i managed to flatten the part and select the endpoint, but i can't make the hole, i think i may be doing something wrong...

                                  • Re: Little help with HoleWizard API
                                    Nilesh Patel

                                    Hi Mario,

                                     

                                    Sorry for the delayed response.

                                     

                                    Change SSize = "M2.0" to  SSize = "Ø2.0". If it doesn't work, could you post your macro?

                                     

                                    Regards,

                                    Nilesh

                                      • Re: Little help with HoleWizard API
                                        Mário Neto

                                        Hello Nilesh, no problem.

                                        I tried that but no sucess, here's the macro:

                                         

                                        Public Sub main()

                                            Dim swApp           As SldWorks.SldWorks

                                            Dim swPartDoc       As SldWorks.ModelDoc2

                                            Dim swFeature       As SldWorks.Feature

                                            Dim swSubFeature    As SldWorks.Feature

                                         

                                         

                                            Dim vSkSegArr       As Variant

                                            Dim vSkSeg          As Variant

                                            Dim vConfNameArr    As Variant

                                            Dim vSuppStateArr   As Variant

                                            Dim swSkSeg         As SldWorks.SketchSegment

                                            Dim swSkLine        As SldWorks.SketchLine

                                           

                                            Dim value           As Boolean

                                         

                                         

                                            Set swApp = Application.SldWorks

                                            Set swPartDoc = swApp.ActiveDoc

                                            

                                            Set swFeature = swPartDoc.FirstFeature

                                              

                                            While Not swFeature Is Nothing

                                                   

                                                Select Case swFeature.GetTypeName2

                                                    Case "FlatPattern"

                                                   

                                                        vConfNameArr = swPartDoc.GetConfigurationNames

                                                        vSuppStateArr = swFeature.IsSuppressed2(swThisConfiguration, vConfNameArr)

                                                       

                                                            Debug.Print "Component Flat Pattern Suppression = " & vSuppStateArr(i)

                                                           

                                                   

                                                            If vSuppStateArr(i) = True Then value = swFeature.Select(True)

                                                           

                                                                value = swPartDoc.EditUnsuppress2

                                                           

                                                   

                                                        Set swSubFeature = swFeature.GetFirstSubFeature

                                                          

                                                        While Not swSubFeature Is Nothing

                                                       

                                                                Debug.Print swSubFeature.Name

                                                       

                                                            If swSubFeature.Name Like "*Curva-Linhas*" Then

                                                                Dim swSketch As Sketch

                                                                Set swSketch = swSubFeature.GetSpecificFeature2

                                                                vSkSegArr = swSketch.GetSketchSegments

                                                                For Each vSkSeg In vSkSegArr

                                                                    Set swSkSeg = vSkSeg

                                                                    Select Case swSkSeg.GetType

                                                                        Case swSketchLINE

                                                                            Set swSkLine = swSkSeg

                                                                            ProcessSketchLine swApp, swPartDoc, swSketch, swSkLine

                                                                    End Select

                                                                Next vSkSeg

                                                            End If

                                                            Set swSubFeature = swSubFeature.GetNextSubFeature

                                                        Wend

                                                End Select

                                                Set swFeature = swFeature.GetNextFeature()

                                            Wend

                                         

                                         

                                        End Sub

                                         

                                         

                                        Private Sub ProcessSketchLine(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swSketch As SldWorks.Sketch, swSkLine As SldWorks.SketchLine)

                                           

                                           

                                            Dim swStartPt       As SldWorks.SketchPoint

                                           

                                            Dim swEndPt         As SldWorks.SketchPoint

                                           

                                            Dim swFeature       As SldWorks.Feature

                                           

                                            Dim swFeatureMgr    As SldWorks.FeatureManager

                                           

                                           

                                            Dim status          As Boolean

                                           

                                                Dim SlotType                    As Long

                                                Dim HoleType                    As Long

                                                Dim StandardIndex               As Long

                                                Dim FastenerTypeIndex           As Long

                                                Dim SSize                       As String

                                                Dim EndType                     As Long

                                                Dim ConvFactorLength            As Double

                                                Dim ConvFactorAngle             As Double

                                                Dim Diameter                    As Double

                                                Dim Depth                       As Double

                                                Dim Length                      As Double

                                                Dim NominalFit                  As Double

                                                Dim ClearanceFit                As Double

                                                Dim PressFit                    As Double

                                                Dim TransitionalFit             As Double

                                                Dim value5                      As Double

                                                Dim value6                      As Double

                                                Dim value7                      As Double

                                                Dim ThreadClass                 As String

                                                Dim value8                      As Double

                                                Dim value9                      As Double

                                                Dim value10                     As Double

                                                Dim value11                     As Double

                                                Dim value12                     As Double

                                                Dim swWizardHoleFeatData        As SldWorks.WizardHoleFeatureData2

                                           

                                         

                                                Set swStartPt = swSkLine.GetStartPoint2

                                                Set swEndPt = swSkLine.GetEndPoint2

                                                Set swFeatureMgr = swModel.FeatureManager

                                               

                                                    status = swStartPt.Select(True)

                                                   

                                                HoleType = swWzdGeneralHoleTypes_e.swWzdHole

                                                StandardIndex = swWzdHoleStandards_e.swStandardAnsiMetric

                                                FastenerTypeIndex = swWzdHoleStandardFastenerTypes_e.swStandardAnsiMetricDowelHole

                                                SSize = "Ø2.0"

                                                EndType = swEndConditions_e.swEndCondThroughAllBoth

                                                Diameter = 2

                                                Depth = 2

                                                Length = -1

                                                'Value1 to Value12 arguments; SOLIDWORKS

                                                'ignores parameters set to -1

                                                NominalFit = 2

                                                ClearanceFit = 2

                                                PressFit = 2

                                                TransitionalFit = 2

                                                value5 = -1

                                                value6 = -1

                                                value7 = -1

                                                value8 = -1

                                                value9 = -1

                                                value10 = -1

                                                value11 = -1

                                                value12 = -1

                                                ThreadClass = ""

                                                Set swFeature = swFeatureMgr.HoleWizard5(HoleType, StandardIndex, FastenerTypeIndex, SSize, EndType, Diameter, Depth, Length, NominalFit, ClearanceFit, PressFit, TransitionalFit, value5, value6, value7, value8, value9, value10, value11, value12, ThreadClass, False, True, True, True, True, False)

                                               

                                                   

                                           

                                        End Sub

                                          • Re: Little help with HoleWizard API
                                            Mário Neto

                                            Hi, anything to help here?

                                              • Re: Little help with HoleWizard API
                                                Nilesh Patel

                                                Hi Mario,

                                                 

                                                I finally had a chance to play with your macro. There are few things I would like to highlight:

                                                 

                                                1. Change the value of 'Diameter' to 0.002 as values in API are in meters. You can either specify 0 or -1 as value for 'Depth' as you have specified the end condition to be through all both.

                                                2. Values of NominalFit, ClearanceFit, PressFit, TransitionalFit actually represent values of Screw Fit, Drill Angle, Near Csink Diameter, Near Csink Angle respectively for regular holes as per API help. Change value of NominalFit to 1 or 2 (depending on the screw fit) and change values of ClearanceFit, PressFit, TransitionalFit to -1.

                                                 

                                                If you make these changes, it will work well on simple boss extrude feature (even if you select sketch points) but it doesn't work on bend lines. I have tried match transform also but it still doesn't work. When I tried to create the hole using hole wizard manually on bend lines end points, I get the error as per attached image. I guess this is why macro is not working too. Someone else might have better explanation.

                                                 

                                                The work around is to either create a sketch after you flatten the part and use its sketch points and create hole using hole wizard or simply create cut extrude feature for each bend line end points rather than hole wizard.

                                                 

                                                Hope this helps.

                                                 

                                                Regards,

                                                Nilesh

                                                • Re: Little help with HoleWizard API
                                                  Nilesh Patel

                                                  Hi Mario,

                                                   

                                                  I forgot to mention couple of things:

                                                   

                                                  1. If I select the face (fixed face or the other face) and then run the macro, it creates the holes for bend line end points but it also creates an extra hole for some reason. Also sketch points for hole wizard are not constrained to bend line end points, so if you update the part in future, hole wizard holes won't update properly.

                                                   

                                                  2. If you create a simple sketch line on the flatten part (even if one of the end point is on the edge)  and then select two end points (instead of selecting bend line end points, you select sketch line end points manually) and run the macro, the macro works fine.

                                                   

                                                  Another simple work around would be to select the fixed face of the flatten part, create one hole using hole wizard. Then access this hole wizard feature and make the sketch point co-incident with bend line end point. At the same time, you can also create more sketch points (equal to bend line end points) and then make them co-incident. This way, you will only have one hole wizard feature in the feature tree.

                                                   

                                                  Regards,

                                                  Nilesh