12 Replies Latest reply on Sep 14, 2016 7:42 AM by Mahmood Satari

    Autofill a Custom Property on Parts with Portion of FileName

    Paul Hitchcock

      We save all of our parts with the file name in a format that has the "part number" on the end of the file name following a period.  We reference this part number on BOM and callout tags on exploded views.

      Part Filename = <Assembly FileName>.<Part Number> (Example: 1234-10.105)

       

      We have a custom property on each part named "PARTNO" that we are manually filling with the last number (105 in the example).

       

      Is it possible to write this in a calculated field either on the Custom Property or inside a Table like a BOM to avoid having to run a macro?

      In Excel I would use something like this: Equation = Replace(PartFileName, AssemblyFileName, "")

       

      If not, can somebody get me started on the macro?  I'm very familiar with VB but brand new to SolidWorks.

       

      Below is a quick outline of the steps I would like to do, I just need help getting the correct SolidWorks functions and objects to make it work.

       

      For each Part in Assembly

       

        get PartFileName

         get PartNo from PartFileName - (I can handle the string manipulation steps)

         assign CustomPartProperty "PartNo" to PartNo

       

      Next Part

       

      Or is there a better way to do this that I'm no aware of?  We're using SW 2013 Standard.

        • Re: Autofill a Custom Property on Parts with Portion of FileName
          Glenn Schroeder

          Hello Paul,

           

          Welcome to the forum.  I'm pretty sure there isn't a way to do that without a macro.  You might get quicker help with that if you move this discussion to the API section.  Don't create a new discussion, just click where shown below and choose API.

           

          move discussion.png

          • Re: Autofill a Custom Property on Parts with Portion of FileName
            Josh Brady

            This can be done with VBA in an equation.  I believe I have an almost identical example in my SWW presentation from 2011.  You can download the presentation from Wayne's link in the thread below, and the files referenced in the presentation are posted in that thread.  You can put this equation into the part template so the user never has to mess with it.

             

            https://forum.solidworks.com/message/211324#211324

             

            2013 really made writing these equations a pain.  The "smarter" interface balks at the syntax of any equations containing VBA.  However, once you get the equation entered, it works just like before.  To bypass the "smart" interface, get Luke Malpass's multi equation edit macro that lets you edit them in plain text.

              • Re: Autofill a Custom Property on Parts with Portion of FileName
                Paul Hitchcock

                This looks like exactly what I need.  However I can't get anything to work in the 2013 Equation editor.  It says I have bad syntax no matter what I try to do.

                 

                Sounds like I need to bypass the "smart" interface like you mentioned but I can't find the "multi equation edit macro" from Luke Malpass that you referenced.  Can you send me a link or point me in the right direction?

                 

                Here is the code I came up with to make this work.  The only function I'm not sure of is the Part.GetName function.  Is this a valid function?  I'm trying to get the filename without the path.  Do I need to get the entire filepath and then strip everything left of the last backslash?  I've tested the string manipulation to get everything from the "PartName" string after the first period in VBA in excel and it runs great.

                 

                Dim PartName as String

                PartName = Part.GetName

                Part.Extensions.CustomPropertyManager("").Add("PARTNO")

                Part.Extensions.CustomPropertyManager("").Set("PARTNO",Right(PartName,Len(PartName)-InStr(1,PartName,".",vbTextCompare))

                  • Re: Autofill a Custom Property on Parts with Portion of FileName
                    Josh Brady

                    http://www.angelsix.com/cms/downloads/solidworks-macros/23-equation-editor

                     

                    Did you download the sample files I posted in the thread? 

                     

                    GetName is not a method on the ModelDoc2 object.  GetTitle is.  It will return the filename without path, but might include .sldprt or .sldasm, depending on whether or not your windows setting is to hide or show extensions for known file types.

                      • Re: Autofill a Custom Property on Parts with Portion of FileName
                        Paul Hitchcock

                        I'm getting closer.  The equation editor macro was throwing an error when it tried to add the equation.  Not sure what the issue is but I moved on.

                         

                        I was able to bypass the "smart" editor by using Import to get my equation into the Equation list.  It still said I had a syntax error but at least let me save it.

                         

                        I ran this in a macro with a few extra lines to get the ActiveDoc to replace the "Part" object used in the equation and it works fine and sets the custom property like I want.  I can't get it to work in the equation though.

                         

                        Below is the equation I'm importing.  It still doesn't work.  Do you see anything I have wrong in there?  Do I need something to return a number to the variable "PartVBA" on the first line?  Am I using the double-colon to use multiple lines of code correctly?

                         

                        "PartVBA" = Dim PartName As String::Dim PartNo As String::PartName = Part.GetTitle::If InStr(1, PartName, ".SLDPRT") > 0 Then::PartName = Replace(PartName, ".SLDPRT", "")::PartNo = Right(PartName, Len(PartName) - InStr(1, PartName, "."))::b = sModel.Extension.CustomPropertyManager("").Set("ID", PartNo)::End If

                         

                        Here is the same code with returns instead of the double colons for easier reading...

                         

                        "PartVBA" = Dim PartName As String

                        Dim PartNo As String

                        PartName = Part.GetTitle

                        If InStr(1, PartName, ".SLDPRT") > 0 Then

                           PartName = Replace(PartName, ".SLDPRT", "")

                           PartNo = Right(PartName, Len(PartName) - InStr(1, PartName, "."))

                           b = sModel.Extension.CustomPropertyManager("").Set("ID", PartNo)

                        End If

                         

                        I also tried replacing the beginning of the equation with the text below so it would assign a positive number to "PartVBA" but that didn't work either.

                         

                        "PartVBA" = 1::Dim PartName...

                          • Re: Autofill a Custom Property on Parts with Portion of FileName
                            Josh Brady

                            Your equation cannot have "=" signs in it anywhere except for the one "=" sign that makes it an equation.

                             

                            If you want to run a multiline macro with branching, variables, assignments, etc. you HAVE to pack the code into a custom property.  If you want all the code to just be in the equation you have to be able to string it all together in one line with no branching and no variables.

                             

                            You can't have the name of a custom property in your equation unless you break it up into a concatenated string.  Otherwise the special values filter gets it.

                             

                            Here is the equation you need:

                             

                            PartVBA = part.Extension.CustomPropertyManager("").Set("I" & "D", Right(Replace(part.GetTitle, ".sldprt", "", 1, , 1), Len(Replace(part.GetTitle, ".sldprt", "", 1, , 1)) - InStrRev(Replace(part.GetTitle, ".sldprt", "", 1, -1, 1), ".")))

                             

                            I've attached a part file with this equation already in it.

                             

                            Not to be a jerk, but this is undocumented, unsupported stuff. You have to do it all exactly right or it just doesn't work. Most things in SolidWorks are easy and intuitive.  If you have the general idea you can bang away at it and it works.  This technique is not.  It's pretty powerful, and you can do some neat stuff, but you have to follow the stuff I outlined in my presentation or it just won't work and it won't tell you why.

                              • Re: Autofill a Custom Property on Parts with Portion of FileName
                                Paul Hitchcock

                                Thanks for all of your help, Josh.  Works like a charm.  Had to use the Equation Editor Macro to get past the SW equation interface.  It didn't like the syntax and would not allow me to exit to the cell to save the equation.

                                  • Re: Autofill a Custom Property on Parts with Portion of FileName
                                    Jeff Taylor

                                    Paul,

                                     

                                    I don't have any experience with VB but want to accomplish a similar feat.

                                    We neame our files "12345-100 - Block.sldprt" (where 12345 is the job number, 100 is the detail number and Block is the description) and I would like to be able to pull the "100" value from the part name and have it populate its own property named DET # in the parts custom properties. Can you porivde any suggestions to get me there?

                                     

                                    Thanks,

                                    Jeff

                                      • Re: Autofill a Custom Property on Parts with Portion of FileName
                                        Paul Hitchcock

                                        Jeff,

                                         

                                        You should be able to use the same equation with some mods. First change, to change the custom property from (ID) to (DET #), modify the equation where it says

                                         

                                        Set("I" & "D"

                                         

                                        to

                                         

                                        Set("DET" & "#"

                                         

                                        Second, you'll need to modify which portion of the filename it's grabbing to get the number you want.

                                         

                                        Where it says

                                         

                                        Right(Replace(part.GetTitle, ".sldprt", "", 1, , 1), Len(Replace(part.GetTitle, ".sldprt", "", 1, , 1)) - InStrRev(Replace(part.GetTitle, ".sldprt", "", 1, -1, 1), "."))

                                         

                                        Replace with something like this:

                                         

                                        Mid(part.GetTitle, InStr(part.GetTitle, "-")+1, InStrRev(part.GetTitle, "-") - InStr(part.GetTitle, "-") -1)


                                        My syntax on the second portion of code is probably off and you might need to add or subtract a digit or two to get the correct portion of the part name. I'm rusty with VBA and don't really have the time to test it out for you but the basic concept would be to use the InStr and/or InStrRev functions to find the indexes of the first and second "-" (dash) in part.GetTitle and then use a function like Mid to return everything in between the two dashes. You could probably post this on any VBA forum and find somebody who could help you with this portion of the code if this doesn't work.

                                         

                                        Also you'll probably have to read above about using the Equation Editor Macro to actually insert this equation into your custom property.

                                  • Re: Autofill a Custom Property on Parts with Portion of FileName
                                    Keith Rice

                                    Paul,

                                     

                                    Alternatively, you can keep the code in an .swp file and attach it to the Design Binder or run it from a hard-drive / network location. Writing so much code in an equation or custom property can be tedious. Watch a video on how to do that here. Kudos to Josh for showing teaching me his jedi ways with equations...

                                     

                                    Keith

                                    SolidWorks API Tutorials

                            • Re: Autofill a Custom Property on Parts with Portion of FileName
                              Mahmood Satari

                              Hi to all

                              we save files with this format: KS-2595-2654-R0

                              I want have portion of this name (eliminate R0)  (KS-2595-2654) in my custom property.

                              how i do this?