11 Replies Latest reply on Jan 18, 2014 9:59 AM by Luis Oliveira

    Macro help

    Luis Oliveira

      Hello,

       

      I am building a Macro to fill my cutom properties list. So far, so good. it already populate the most important fields

       

      But now I want to upgrade it. In order to save time, I want the macro to see if the part is a sheet metal part and if yes, add the thickness and other properties to that list

       

      If anyone can help me I would apreciate..

       

      Thank you

        • Re: Macro help
          Artem Taturevych

          Hello Luis,

           

          Try this: PartDoc::GetBodies2->Body2::IsSheetMetal

            • Re: Macro help
              Luis Oliveira

              Sorry, I didn´t understand. I'm programming in VBA. I didn't follow your thought. Can you explain a little better?

                • Re: Macro help
                  Chris Chu

                  There is an add-in called "Property Tab Builder" included with your SW

                  You can use this addin to customize the SW properties - it's nice features and what I mean here is you don't need to write your own macro for this task - there is one available for you to use

                  Chris

                  Capture.JPG

                  • Re: Macro help
                    Artem Taturevych

                    You can call PartDoc::GetBodies2 to get an array of bodies from your part. Then you can call Body2::IsSheetMetal for each body to identify whether it is sheet-metal body or not.

                      • Re: Macro help
                        Luis Oliveira

                        I've been trying to use those but it's still wrong. could you see were I miss?

                         

                        Dim swApp As SldWorks.SldWorks

                         

                            Dim swModel As ModelDoc2

                            Dim swPart As IPartDoc

                            Dim Bodies As Object

                            Dim Body As IBody2

                            Dim Sheet_metal As Boolean

                            Dim BodyType As Integer

                            Dim BVisibleOnly As Boolean

                         

                         

                           

                            Sub main()

                            Set swApp = Application.SldWorks

                            Set swModel = swApp.ActiveDoc

                            Set swPart = Ctype(swModel, PartDoc)

                         

                            Bodies = swPart.GetBodies2(swBodyType_e.swAllBodies, False)

                            Body = Bodies(1)

                            Sheet_metal = Body.IsSheetMetal()

                           

                            If Sheet_metal = True Then

                           

                            swModel.AddCustomInfo2 "QUANTIDADE", swCustomInfoText, InputBox("QUANTIDADE")

                           

                            End If

                            End Sub

                          • Re: Macro help
                            Artem Taturevych

                            Dim Bodies As Variant

                            Dim swBody As IBody2

                            ...

                             

                            Set swBody = Bodies(0)

                            Sheet_metal = swBody.IsSheetMetal()

                              • Re: Macro help
                                Luis Oliveira

                                now I have this message: sub or function not defined. I think its the Ctype...

                                  • Re: Macro help
                                    Artem Taturevych

                                    CType is VB.NET function not VBA:

                                     

                                    Set swPart = Ctype(swModel, PartDoc)

                                     

                                    Set swPart = swModel

                                      • Re: Macro help
                                        Luis Oliveira

                                        this is funny. I probably am doing a very basic error but I don't know where. look at the code:

                                         

                                        Dim swApp As SldWorks.SldWorks

                                         

                                            Dim swModel As ModelDoc2

                                            Dim swPart As IPartDoc

                                            Dim Bodies As Variant

                                            Dim swBody As IBody2

                                            Dim Sheet_metal As Boolean

                                            Dim BodyType As Integer

                                            Dim BVisibleOnly As Boolean

                                         

                                         

                                          

                                            Sub main()

                                            Set swApp = Application.SldWorks

                                            Set swModel = swApp.ActiveDoc

                                            Set swPart = swModel

                                           

                                            Bodies = swPart.GetBodies2(swBodyType_e.swAllBodies, True)

                                            Set swBody = Bodies(0)

                                            Sheet_metal = swBody.IsSheetMetal()

                                           

                                            swModel.AddCustomInfo2 "A", swCustomInfoText, Sheet_metal

                                           

                                            If Sheet_metal = True Then

                                            swModel.AddCustomInfo2 "B", swCustomInfoText, InputBox("B")

                                            ElseIf Sheet_metal = False Then

                                            swModel.AddCustomInfo2 "C", swCustomInfoText, InputBox("C")

                                            End If

                                            End Sub

                                         

                                        the result: it adds a custom property named A with True or False. All good until now, but then nothing happens. Do you know why?

                                          • Re: Macro help
                                            Simon Turner

                                            Never test whether a Boolean = True.

                                            In VBA, False equals zero, but any other number is considered to be True. So testing whether 2 Booleans variables are equal to one another could be equivalent to asking if -1=1.

                                            It is safe to ask if a Boolean <> False

                                             

                                            Also, you are using AddCustomInfo. If the part already has a custom property, then AddCustomInfo doesn't work. You need to test if the variable already exists. If so, you need to set it rather than try to add it again.