11 Replies Latest reply on Mar 9, 2017 3:25 AM by Deepak Gupta

    How to select Cutlist Item and create a 3D Bounding Box

    Raymond Yau

      Hi All,

       

      I've been programming a macro that would select the first feature in my Cut-list folder and then create a 3D Bounding Box for the selected item.

       

      From what I've gathered from online resources, here is my (maybe 100th) attempt at the macro.

       

      I'm struggling with the cut-list-item selection part. Some sources I viewed online demonstrates how to select Solid Bodies, but not cut-list item IDs. Perhaps I'm over complicating things?

       

      Help much appreciated.

       

      Sources used:

      2016 SOLIDWORKS API Help - Create 3D Bounding Box for Cut List Item Example (VBA)

      2015 SOLIDWORKS API Help - Get Names of Bodies in Multibody Part Example (VBA)

      2016 SOLIDWORKS API Help - Get Weldment Cut List Feature and Annotations Example (VB.NET)

      Dim swApp As SldWorks.SldWorks

      Dim Part As SldWorks.ModelDoc2

      Dim modDocExt As SldWorks.ModelDocExtension

      Dim boolstatus As Boolean

      Dim swfeat As Feature

      Dim CutItem As String

       

      Option Explicit

       

      Sub main()

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

          Set modDocExt = Part.Extension

         

         

          ' Traverse FeatureManager design tree

          ' Get first feature in FeatureManager design tree

          swfeat = Part.FirstFeature

         

         

          'Get ID of first feature

          CutItem = swfeat.GetID

       

       

          ' Once body selected, create a bounding box

          boolstatus = modDocExt.SelectByID2(CutItem, "SUBWELDFOLDER", 0, 0, 0, False, 0, Nothing, 0)

          modDocExt.Create3DBoundingBox

      End Sub

       

        • Re: How to select Cutlist Item and create a 3D Bounding Box
          Amen Allah Jlili

          How about feature.Name?
              CutItem = swfeat.Name

          • Re: How to select Cutlist Item and create a 3D Bounding Box
            Dariusz Karcz

            Hi Raymond,

            try this one:

             

            Sub main()

                Dim swApp As Object

                Dim swmodel As ModelDoc2

                Dim swPart As PartDoc

                Dim boolstatus As Boolean

                Dim swSelMgr As ISelectionMgr

                Dim swBody As Body2

                Dim vBodies As Variant

                Dim j  As Integer

                Dim BodyFolder As SldWorks.BodyFolder

                Dim CutListBdy As Body2

             

                Set swApp = Application.SldWorks

                Set swmodel = swApp.ActiveDoc

             

                Set swPart = swmodel

                Set swSelMgr = swmodel.SelectionManager

                Set swBody = swSelMgr.GetSelectedObject6(0, -1)

               

                Dim Feat As Feature

                Set Feat = swmodel.FirstFeature

                While Not Feat Is Nothing

                    If Feat.GetTypeName2 = "CutListFolder" Then

                    Dim swFeatName As String

                

                    swFeatName = Feat.Name

                    Feat.Select2 True, 0

                

                    Debug.Print swFeatName

                

                        Set BodyFolder = Feat.GetSpecificFeature2

                        vBodies = BodyFolder.GetBodies

                        For j = 0 To UBound(vBodies)

                            Set CutListBdy = vBodies(j)

                            Debug.Print CutListBdy.Name

                        

                            swmodel.Extension.Create3DBoundingBox

                        

                        

                        Next j

                    End If

                    Set Feat = Feat.GetNextFeature()

                Wend

            End Sub

             

            It is little bit messy, but it works.

            It traverse thru all features, then select cutlist folders, then creates a bounding box for every body in cutlist folders.

            Loop j is responsible for it, if you want to select only first item, then change this loop.

             

            Preconditions:

            Part need to have weldment feature and updated cutlist.

            You can achive it, by using IbodyFolder --> UpdateCutList, and InsertWeldmentFeature Method (IFeatureManager)

             

            I hope it will help you.

              • Re: How to select Cutlist Item and create a 3D Bounding Box
                Raymond Yau

                Thanks Dariusz, your solution works very well.

                 

                I added some extra lines of code as I just realised that a weldment-cut-list table would have to first exist in the FeatureManagerTree before running your macro, so I added a line of code to create a weldment table before the While Not statement.

                 

                Here is the line of code added (in BOLD) to you your solution:

                .....

                Set swPart = swmodel

                Set swSelMgr = swmodel.SelectionManager

                Set swBody = swSelMgr.GetSelectedObject6(0, -1)

                 

                Dim Feat As Feature

                 

                Set Feat = swmodel.FeatureManager.InsertWeldmentFeature() 'Insert weldment feature

                 

                Set Feat = swmodel.FirstFeature

                While Not Feat Is Nothing

                     If Feat.GetTypeName2 = "CutListFolder" Then

                .....

                 

                This successfully creates a weldment cutlist table, but it doesn't continue to process the rest of the code and create a 3D Bounding Box. When I re-run the same macro, it unusually generates multiple weldment cutlists. Any ideas why this might happen?

                 

                 

                  • Re: How to select Cutlist Item and create a 3D Bounding Box
                    Dariusz Karcz

                    Raymond,

                     

                    currently I have do access to my PC with Sw installed, but try this:

                     

                    Delete "Set Feat =" and leave only

                     

                    swmodel.FeatureManager.InsertWeldmentFeature()

                     

                    or

                     

                    try assign a boolean for example:

                     

                    dim bool as boolean

                    bool = swmodel.FeatureManager.InsertWeldmentFeature()

                     

                    Please check it, it it fails, i will look into it later.

                      • Re: How to select Cutlist Item and create a 3D Bounding Box
                        Raymond Yau

                        I've tried assigning a boolean, but it still creates multiple weldment tables (as shown in the image) and it doesn't execute the latter part of the code. Since I've removed the 'Set Feat', I also get an object error:

                         

                          • Re: How to select Cutlist Item and create a 3D Bounding Box
                            Dariusz Karcz

                            Raymond, here you go (still messy, but with little bit of comments inside of code), now its working, and does not insert multiple instances of weldment feature, if part is already a weldment:

                             

                            Sub main()

                                Dim swApp As Object

                                Dim swmodel As ModelDoc2

                                Dim swPart As PartDoc

                                Dim boolstatus As Boolean

                                Dim swSelMgr As ISelectionMgr

                                Dim swBody As Body2

                                Dim vBodies As Variant

                                Dim j  As Integer

                                Dim BodyFolder As SldWorks.BodyFolder

                                Dim CutListBdy As Body2

                                Dim Feat As Feature

                                Dim swIsWeldment As Boolean

                               

                                

                                Set swApp = Application.SldWorks

                                Set swmodel = swApp.ActiveDoc

                                Dim bool As Boolean

                                   

                                Set swPart = swmodel

                                Set swSelMgr = swmodel.SelectionManager

                                Set swBody = swSelMgr.GetSelectedObject6(0, -1)

                                  

                                   swIsWeldment = swPart.IsWeldment

                                  

                                    If swIsWeldment = False Then 'Check if part is weldment, if not, create weldment feature

                                  

                                    swmodel.FeatureManager.InsertWeldmentFeature

                                swmodel.EditRebuild3

                                    Else

                               

                                    End If

                                  

                               

                                Set Feat = swmodel.FirstFeature 'get first feature

                                While Not Feat Is Nothing

                                    If Feat.GetTypeName2 = "CutListFolder" Then 'check that first feature is cutlistfolder

                                    Dim swFeatName As String

                                                      

                                        Set BodyFolder = Feat.GetSpecificFeature2 'get body folder interface

                                        BodyFolder.GetAutomaticCutList

                                        BodyFolder.UpdateCutList

                                        swmodel.EditRebuild3

                                       

                                        swFeatName = Feat.Name

                                    Feat.Select2 True, 0

                                   

                                    Debug.Print swFeatName

                                        vBodies = BodyFolder.GetBodies

                                        For j = 0 To UBound(vBodies)

                                            Set CutListBdy = vBodies(j)

                                            Debug.Print CutListBdy.Name

                                           

                                            swmodel.Extension.Create3DBoundingBox

                                           

                                           

                                        Next j

                                    End If

                                    Set Feat = Feat.GetNextFeature()

                                Wend

                            End Sub