9 Replies Latest reply on Jun 15, 2015 5:55 PM by Adam Hoffman

    Weldment BOM - Plate vs. Grating

    Shaun Jalbert

      When we create our grating for work platforms, etc., we model it as an extruded plate feature with no details. Of course, this throws off the weight, so I've combated this by adding a material in our database that corrects the mass by fudging a density so that the weight per square foot matches real life. this all works OK, but here's where my problem is...

       

      I run a macro (shown below) that does several things.

      1) it renames the weldment folders

      2) Creates a bounding box

      3) Adds a mass (for BOM)

      4) Adds a type (for BOM)

      5) Determines if a member is a plate or bent sheet metal (for BOM)

        5a) Modifies the Stock Size

        5b) Adds the Length

        5c) Modifies the description to suit

       

      My issue is that this macro identifies my grating as a plate and my BOM populates with plate.

       

      I didn't create the macro, I've only modified it to suit so i don't know if this is even possible, but is it possible to add a lookup function on the plate material and if it matches "W19-4 1 1/2" x 3/16" GRTG" to populate the properties as such:

       

      Grade= W19-4

      Stocksize=  1 1/2" x 3/16"

      Length= (as per bounding box)

      Type= GRTG

      Description= Grating

       

      My concern is that the grating material name could vary according to size (ex. W19-4 1” x 1/8” GRTG, or SP19-2 2 1/2” x 3/16” GRTG).

      Any suggestions on how to modify this code to suit?

       

      !

      Option Explicit

      Private Const propname1 As String = "Mass" ' change to "MASS" or whatever you prefer

      Private Const propname2 As String = "Description"

      Private Const propname3 As String = "Type"

      Private Const propname4 As String = "StockSize"

      Private Const propname5 As String = "Grade"

      Private Const propname6 As String = "Length"

      Private Const propname7 As String = "Mark"

      Private Const propname8 As String = "PARTNUMBER"

       

      Private Const propname9 As String = "Sheet Metal Thickness"

      Private Const propname10 As String = "ANGLE1"

       

      Private Const proptype As String = "Text" ' in english you must change to "Text". See comment in code…

       

       

      Dim swApp As Object

      Sub Main()

      Dim swApp As SldWorks.SldWorks

      Set swApp = Application.SldWorks

      Dim doc As SldWorks.ModelDoc2: Set doc = swApp.ActiveDoc

      Dim partdoc As SldWorks.partdoc: Set partdoc = doc

      Dim f As IFeature: Set f = doc.FirstFeature

      Dim bRet As Boolean

      Dim n As Integer: n = 0

      Dim num As String

       

      Dim modDocExt As SldWorks.ModelDocExtension

       

      Dim Part As Object

      Dim boolstatus As Boolean

       

      Dim BodyFolder As SldWorks.BodyFolder

      Dim BodyCount As Long

       

       

      Do While Not f Is Nothing

       

      If f.GetTypeName = "CutListFolder" Then

             

              Set BodyFolder = f.GetSpecificFeature2

              BodyCount = BodyFolder.GetBodyCount

       

          If BodyCount > 0 Then

              n = n + 1

              If n < 1000 Then

              num = "-" + Right(Str$(n), Len(Str$(n)) - 1)

              End If

              If n < 100 Then

              num = "-" + Right(Str$(n), Len(Str$(n)) - 1)

              End If

              If n < 10 Then

              num = "-0" + Right(Str$(n), Len(Str$(n)) - 1)

          End If

      End If

       

       

       

      If Right(doc.GetTitle, 7) = ".SLDPRT" Or Right(doc.GetTitle, 7) = ".SLDASM" Then

          f.Name = Left(doc.GetTitle, Len(doc.GetTitle) - 7) + num

      Else

          f.Name = Left(doc.GetTitle, Len(doc.GetTitle)) + num

      End If

      'f.Name = Left(Right(doc.GetTitle, 10), 3) + num

       

      'add 3d bounding box

         Set Part = swApp.ActiveDoc

         Set modDocExt = Part.Extension

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

         modDocExt.Create3DBoundingBox

        

                  

                     

                      'del Mark and PARTNUMBER

                      bRet = f.CustomPropertyManager.Delete(propname7)

                      bRet = f.CustomPropertyManager.Delete(propname8)

                     

                      'mass

                      bRet = f.CustomPropertyManager.Delete(propname1)

                      bRet = f.CustomPropertyManager.Add(propname1, proptype, Chr$(34) + "SW-Mass@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34))

                      'bRet = f.CustomPropertyManager.Add(propname1, proptype, Chr$(34) + "SW-Mass@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + " kg")

                      'If f.CustomPropertyManager.Add(propname1, proptype, Chr$(34) + "SW-Mass@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + "kg") Then

                      'Debug.Print "Property "; propname1; " added to feature "; f.Name

                      'End If

                     

                      'desc

                      'If f.CustomPropertyManager.Add(propname2, proptype, "PLATE") Then

                      'Debug.Print "Property "; propname2; " added to feature "; f.Name

                      'End If

                     

                      'type

                      If f.CustomPropertyManager.Add(propname3, proptype, "PL") Then

                      Debug.Print "Property "; propname3; " added to feature "; f.Name

                      End If

                     

                     

                      'Plate & Formed Plate stocksize and Length and desc

                      If f.CustomPropertyManager.Get(propname10) = "" Then

                         If f.CustomPropertyManager.Get(propname9) = "" Then

                             bRet = f.CustomPropertyManager.Delete(propname4)

                             bRet = f.CustomPropertyManager.Add(propname4, proptype, Chr$(34) + "PL" + "SW-3D-Bounding Box Thickness@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + "x" + Chr(34) + "SW-3D-Bounding Box Width@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34))

                             bRet = f.CustomPropertyManager.Delete(propname6)

                             bRet = f.CustomPropertyManager.Add(propname6, proptype, Chr$(34) + "SW-3D-Bounding Box Length@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34))

                            

                             'desc plate

                             bRet = f.CustomPropertyManager.Delete(propname2)

                             bRet = f.CustomPropertyManager.Add(propname2, proptype, "PLATE")

         

                             'If f.CustomPropertyManager.Add(propname4, proptype, Chr$(34) + "SW-3D-Bounding Box Thickness@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + "x" + Chr(34) + "SW-3D-Bounding Box Width@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34)) Then

                             'Debug.Print "Property "; propname4; " added to feature "; f.Name

                             'End If

                         Else

                             bRet = f.CustomPropertyManager.Delete(propname4)

                             bRet = f.CustomPropertyManager.Add(propname4, proptype, Chr$(34) + "PL" + "SW-Sheet Metal Thickness@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + "x" + Chr(34) + "SW-Bounding Box Width@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34))

         

                             bRet = f.CustomPropertyManager.Delete(propname6)

                             bRet = f.CustomPropertyManager.Add(propname6, proptype, Chr$(34) + "SW-Bounding Box Length@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34))

                            

                             'desc formed plate

                             bRet = f.CustomPropertyManager.Delete(propname2)

                             bRet = f.CustomPropertyManager.Add(propname2, proptype, "FORMED PLATE")

                            

                             'If f.CustomPropertyManager.Add(propname4, proptype, Chr$(34) + "SW-Sheet Metal Thickness@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34) + "x" + Chr(34) + "SW-Bounding Box Width@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34)) Then

                             'Debug.Print "Property "; propname4; " added to feature "; f.Name

                             'End If

                         End If

                      Else

                      End If

                    

                      'grade

                      If f.CustomPropertyManager.Add(propname5, proptype, Chr$(34) + "SW-Material@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34)) Then

                      Debug.Print "Property "; propname5; " added to feature "; f.Name

                      End If

                                   

       

       

      End If

       

      Set f = f.GetNextFeature

       

      Loop

       

      End Sub

        • Re: Weldment BOM - Plate vs. Grating
          Shaun Jalbert

          -=BUMP=-

           

          Still hasn't been resolved. Any idea's?

            • Re: Weldment BOM - Plate vs. Grating
              Adam Hoffman

              Paste this at the end of your program:

               

              Function GetFeatureCustomProp(ByVal sPropertyToGet As String, ByVal TheFeature As SldWorks.Feature) As String

              Dim swCustPropMgr As SldWorks.CustomPropertyManager

              Dim sTextExp As String

              Dim sEvalVal As String

              GetFeatureCustomProp = ""

              Set swCustPropMgr = TheFeature.CustomPropertyManager

              swCustPropMgr.Get2 sPropertyToGet, sTextExp, sEvalVal

              GetFeatureCustomProp = sEvalVal

              End Function

               

              Sub ChangeCustomPropForGrating(ByVal TheMaterial As String, ByVal TheFeature As SldWorks.Feature)

              Dim bRet        As Boolean
              Dim iGrtg       As Integer
              Dim iSpace      As Integer
              Dim sGrade      As String
              Dim sStockSize  As String
              Dim sType       As String

              sType = "GRTG"

              iGrtg = InStrRev(TheMaterial, sType)

              If iGrtg > 0 Then

                 iSpace = InStr(1, TheMaterial, " ")
                
                 sGrade = Left(TheMaterial, iSpace - 1)
                
                 sStockSize = Mid(TheMaterial, iSpace, iGrtg - iSpace)
                
                 sStockSize = Trim(sStockSize)
                
                 bRet = TheFeature.CustomPropertyManager.Set2(propname5, sGrade)
                
                 bRet = TheFeature.CustomPropertyManager.Set2(propname4, sStockSize)
                
                 bRet = TheFeature.CustomPropertyManager.Set2(propname3, sType)
                
                 bRet = TheFeature.CustomPropertyManager.Set2(propname2, "Grating")

              End If

              End Sub

               

              Then after this if statement:

                              'grade

                              If f.CustomPropertyManager.Add(propname5, proptype, Chr$(34) + "SW-Material@@@" + f.Name + "@" + doc.GetTitle + ".SLDPRT" + Chr(34)) Then

                              Debug.Print "Property "; propname5; " added to feature "; f.Name

                              End If

               

              Paste this:


                              'Let's see if the material is grating
                              Dim sMaterial As String               
                              sMaterial = GetFeatureCustomProp("Material", f)
                             
                              If Not sMaterial = "" Then               
                                 Dim iP As Integer                  
                                 iP = InStr(1, sMaterial, "GRTG")                  
                                 'If the material is grating then let's change the values
                                 If iP > 0 Then
                                    ChangeCustomPropForGrating sMaterial, f

                                 End If 'End of material being grating if   
                              End If 'End of material being blank if

            • Re: Weldment BOM - Plate vs. Grating
              Matt Martens

              If you get the IBody objects that are contained in the weldment folder you could try using IBody::GetMaterialUserName2 or IBody::GetMaterialIdName2 to get the material applied to the body.

                • Re: Weldment BOM - Plate vs. Grating
                  Shaun Jalbert

                  Matt Martens wrote:

                   

                  If you get the IBody objects that are contained in the weldment folder you could try using IBody::GetMaterialUserName2 or IBody::GetMaterialIdName2 to get the material applied to the body.

                  Hi Matt:

                   

                  Thanks for getting back to me. I sort of understand what you're saying, but please excuse my lack of programming comprehension... Naturally, I'm better at copying and pasting code and modifying it to suit than creating from scratch. I looked at the get material ID name from the API Help (2015 SOLIDWORKS API Help - GetMaterialIdName Method (IBody) ), but I'm not certain how I'd break it down to populate my Custom Properties. Can you elaborate?

                   

                  Thanks,

                  Shaun

                    • Re: Weldment BOM - Plate vs. Grating
                      Matt Martens

                      After testing out the two methods above I found they don't work so well but the method that does work consistently when you have assigned a material to an individual body is IBody::GetMaterialPropertyName. If you have only a single configuration you can just use double quotes for the 1st argument. Otherwise you need to get the current configuration name by going IModelDoc::GetActiveConfiguration::Name.

                       

                      I am assuming

                       

                      "W19-4 1 1/2" x 3/16" GRTG"

                       

                      is the material name that would be returned (and all other sizes are formatted similarly) and

                       

                      Grade= W19-4

                      Stocksize=  1 1/2" x 3/16"

                      Length= (as per bounding box)

                      Type= GRTG

                      Description= Grating

                       

                      are the properties you want created. You should assign the result from above to a string variable (I am using propName in the example below). Then:

                       

                      Grade = Left(propName, InStr(1, propName, " ") - 1

                      Stocksize = Mid(Replace(propName, " GRTG", ""), InStr(1, propName, " ") + 1, Len(propName)

                      Length = "SW-Length"

                       

                      Because I am assuming the Type and Description values wouldn't change per the differing size just consistently set those

                       

                      Type = "GRTG"

                      Description = "Grating"

                  • Re: Weldment BOM - Plate vs. Grating
                    Shaun Jalbert

                    Thanks Adam. That works just as I envisioned.

                    I really appreciate your time on this.


                    Kindest Regards,

                    Shaun