20 Replies Latest reply on Jan 30, 2015 3:01 PM by Adam Hoffman

    How to compare a custom property?

    Gabriel Bueno

      I need to compare the custom property Material to a layer in the drawing. I just need a sample of a code like this to finish my API. All this will be part of a bigger Macro that I'm working.

       

      I saw this code at CadSharp and I believe that this will help me with what I really want. the Red line is wrong but maybe will give an idea about my question

       

       

      @

      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swDraw = swModel

          vSheetViews = swDraw.GetViews

          For i = 0 To UBound(vSheetViews)

              vViews = vSheetViews(i)

              For j = 0 To UBound(vViews)

                  Set swView = vViews(j)

                  Set swDrawComp = swView.RootDrawingComponent

                  If Not swDrawComp Is Nothing Then

                      vDrawComps = swDrawComp.GetChildren

                      If IsEmpty(vDrawComps) = False Then

                          For k = 0 To UBound(vDrawComps)

                              Set swDrawComp = vDrawComps(k)

                           

                              If InStr(1, swCustPropMgr "Material", "MDF") <> 0 Then Part.ChangeComponentLayer "MDF" = true

       

       

                          Next k

                      End If

                  End If

              Next j

          Next i

          swModel.EditRebuild3

      End Sub

        • Re: How to compare a custom property?
          Deepak Gupta

          Not sure as how this would work but try adding a comma , after swCustPropMgr in this line:

           

          If InStr(1, swCustPropMgr "Material", "MDF") <> 0 Then Part.ChangeComponentLayer "MDF" = true

          • Re: How to compare a custom property?
            Keith Rice

            Gabriel,

             

            The API Help contains everything you need to understand the syntax of this API call (and any other API call).

             

            Once you go to the API Help page for IDrawingDoc::ChangeComponentLayer, locate this:

            When you click the link:

            A few items:

             

            1. This requires two arguments. Right now you are only using one.

             

            2. You cannot set IDrawingDoc::ChangeComponentLayer to true since it is not a property, it is a method. Moreover, this method returns no value so you can't test it against any value, either.

             

             

            Methods can return values but you never set a method equal to a value. That is only for a property. A method is like a verb, a property is like a noun or adjective.

             

            3. You are not getting the custom property correctly. You need to use one of the members from ICustomPropertyManager, like Get.

             

            So the faulty line should look like this:

             

            If InStr(1, swModel.Extension.CustomPropertyManager(Empty).Get("Material"), "MDF") <> 0 Then Part.ChangeComponentLayer "MDF", True
            

             

            See the AllView parameter to determine whether to make the second argument true or false.

             

            Note that since you are not returning a value, the arguments are not in parentheses. This is an idiosyncracy with VBA. If returning a value, arguments must be parentheses, if not returning a value, arguments are not in parentheses. In .NET languages it is more consistent -- you always put the arguments in parentheses.

             

            Keith

            SolidWorks API Tutorials

              • Re: How to compare a custom property?
                Gabriel Bueno

                Keith, Thanks for answer,

                 

                Sorry about any english mistake, I'm Brazillian and my english writing is not that good.

                 

                I've start to study about API since december, I bought the SolidWorks API Training Book. So thanks for all your help.

                 

                          Since this API, I've found in your website you should know, I understand that the word "MDF" as a layer name and the true value is about the AllViews to set if this change will works in all the drawings view, But now starts to show another message box. The Code

                Capturar.JPG

                 

                Translating the error

                 

                Error in Time of execution '13':

                 

                Type Mismatch

                ---------------------------------------------------------------------------------------------------------------

                 

                I Still don't know what I am missing.

                  • Re: How to compare a custom property?
                    Keith Rice

                    I didn't look closely enough at your code. You had another error related to getting the custom property. I edited my answer above to contain what I think will work. Note point #3.

                      • Re: How to compare a custom property?
                        Gabriel Bueno

                        Ok, now I don't get any error, but still not working, I did everything that you ask, I press F8 to run slowly to see if there is anything wrong and can't see.

                        I Don't know if it is comparing the loop count is alright 47 parts.

                         

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                        Dim swDraw As SldWorks.DrawingDoc

                        Dim swView As SldWorks.View

                        Dim vSheetViews As Variant

                        Dim vViews As Variant

                        Dim swDrawComp As SldWorks.DrawingComponent

                        Dim vDrawComps As Variant

                        Dim swCustPropMgr As SldWorks.CustomPropertyManager

                        Dim pLayerMgr As SldWorks.LayerMgr

                        Dim Part As SldWorks.DrawingDoc

                        Dim i As Integer

                        Dim j As Integer

                        Dim k As Integer

                        Dim instance As IDrawingDoc

                        Dim AllViews As Boolean

                         

                         

                           

                        Sub main()

                            Set swApp = Application.SldWorks

                            Set swModel = swApp.ActiveDoc

                            Set swDraw = swModel

                            vSheetViews = swDraw.GetViews

                            For i = 0 To UBound(vSheetViews)

                                vViews = vSheetViews(i)

                                For j = 0 To UBound(vViews)

                                    Set swView = vViews(j)

                                    Set swDrawComp = swView.RootDrawingComponent

                                    If Not swDrawComp Is Nothing Then

                                        vDrawComps = swDrawComp.GetChildren

                                        If IsEmpty(vDrawComps) = False Then

                                            For k = 0 To UBound(vDrawComps)

                                                Set swDrawComp = vDrawComps(k)

                                                If InStr(1, swModel.Extension.CustomPropertyManager("").Get("Material"), "MDF CRÚ") <> 0 Then Part.ChangeComponentLayer "MDF", True

                                                                    Next k

                                        End If

                                    End If

                                Next j

                            Next i

                            swModel.EditRebuild3

                        End Sub

                  • Re: How to compare a custom property?
                    Gabriel Bueno

                    Ok,

                    I Know that is getting a custom property, I've create a custom property that use Material. So this should be working.

                     

                    At the material the right name is MDF CRU

                    And for the Layer is MDF

                     

                    the macro should look at the components of the view and see wich component has the custom property "material" with MDF CRU value and then change layer if It is.

                     

                    So the only thing now that I think is wrong about it is this point.

                     

                    • Your macro is only looking at drawing's custom properties, not the custom properties of the components in the view.

                     

                    How can I change the macro to look at the components custom properties?

                    • Re: How to compare a custom property?
                      Paul Churm

                      Hi,

                       

                      Why dont you pull the material through from the part onto the drawing as a normal custom property that get filled in when you put a view down on the drawing.  The you can run some code to look up the note value from the drawing.  The identifier of the note should not change if you use the same drawing template.

                       

                      You just need to know which "DetailItemXXX" is your material.

                       

                      To do that change the below code

                                      If vNotes(i).GetName = "DetailItem199" Then

                                          Debug.Print vNotes(i).GetName & " = " & vNotes(i).GetText

                                      End If

                       

                      to something like -

                                          Debug.Print vNotes(i).GetName & " = " & vNotes(i).GetText

                       

                      Then look for you material.

                       

                      So you can use something like this to get the value of a particular note on a drawing -

                       

                      Option Explicit

                       

                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swDrawDoc As SldWorks.DrawingDoc

                      Dim swView As SldWorks.View

                      Dim swNote As SldWorks.Note

                      Dim sheetCount As Long

                      Dim viewCount As Long

                      Dim noteCount As Long

                      Dim i As Long

                       

                      Sub main()

                       

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swDrawDoc = swModel

                       

                      Dim viewCount As Long

                      viewCount = swDrawDoc.GetViewCount

                       

                      Dim ss As Variant

                      ss = swDrawDoc.GetViews

                       

                          Dim vv As Variant

                          vv = ss(sheetCount)

                       

                          For viewCount = LBound(vv) To UBound(vv)

                              Dim vNotes As Variant

                              noteCount = vv(viewCount).GetNoteCount

                       

                              If noteCount > 0 Then

                                  vNotes = vv(viewCount).GetNotes

                                  For i = 0 To noteCount - 1

                                      If vNotes(i).GetName = "DetailItem199" Then

                                          Debug.Print vNotes(i).GetName & " = " & vNotes(i).GetText

                                      End If

                                  Next

                              End If

                          Next viewCount

                       

                      End Sub

                       

                      Cheers

                       

                      Paul

                        • Re: How to compare a custom property?
                          Gabriel Bueno

                          Keith,

                           

                                    Now I know why wasn't working, to get this property I need to use this command with the part opened It doesn't work at the drawing, I'm trying another options and when I find something I will come back here to show the final result.

                           

                          Paul,

                           

                                    I Don't know if I understood correctly your option, but I don't know how this will work with assy, lots of parts one note for each part? I thinking about get this from the BOM, maybe will be easier.

                            • Re: How to compare a custom property?
                              Paul Churm

                              Gabriel,

                               

                              Im not following what you want as an end result?  Are you wanting each part the the assembly to be on its own layer in a drawing?

                              When you said material I just assumed it was a part.

                               

                              Cheers

                               

                              Paul

                                • Re: How to compare a custom property?
                                  Gabriel Bueno

                                  At this sample, we can see the assy with all parts with the right layer(that green layer is "MDF"), Today to do this I have to select each part at the view and change the layer for each material. That API part of the macro will change the layer automatically for each material, when I learn how to do with one of the material, MDF in this case, I can do for all of the different material, even in the same macro, but i first need to learn how to change one material to its specific layer.

                                  sample.JPG

                            • Re: How to compare a custom property?
                              Adam Hoffman

                              Try this. I modified your original code a little bit, but it works like a champ on my machine.

                                • Re: How to compare a custom property?
                                  Gabriel Bueno

                                  Adam,

                                   

                                            Your macro really works, I just have to find a way to solve a problem at my parts, but now I can solve, It's the easy part now.

                                   

                                  My material property has this value, so it wasn't working at the beginning, But when I change the value instead this code, the macro worked fine.

                                  But now I can create a new custom property with the name layer and put the text with the layer I want the part to get.

                                  MACRO.JPG

                                   

                                  So Really thanks!!!

                                  • Re: How to compare a custom property?
                                    Gabriel Bueno

                                    Adam or anyone else that helped me.

                                     

                                              The macro that Adam edited works fine, but when in the assy I have a subassy, the macro don't change the layer of the components of the subassy, just the assy components, I don't know if I can add something to the code to run through the subassy components the same way of the assy.

                                     

                                    I think maybe something in this part of the code.

                                     

                                       Set swDrawComp = swView.RootDrawingComponent

                                          

                                       If Not swDrawComp Is Nothing Then

                                     

                                     

                                          vDrawComps = swDrawComp.GetChildren

                                     

                                     

                                          If IsEmpty(vDrawComps) = False Then

                                     

                                     

                                             For i = 0 To UBound(vDrawComps)

                                     

                                     

                                                 Set swDrawComp = vDrawComps(i)

                                                      

                                                 Set swComp = swDrawComp.Component

                                                      

                                                 Set swCompModel = swComp.GetModelDoc2

                                                      

                                                 Set swConFig = swCompModel.GetConfigurationByName(swComp.ReferencedConfiguration)

                                                      

                                                 If Not swConFig Is Nothing Then

                                                      

                                                    Set swCustPropMgr = swConFig.CustomPropertyManager

                                     

                                    If someone could see this i'd be grateful.

                                     

                                    Gabriel.