Pack and go need to omit library parts

Programming and macros
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Pack and go need to omit library parts

Unread post by CarrieIves »

I found the example of how to Pack and Go an assembly with a macro (VBA). I will want to add a prefix.
This works great, except, how do I not include my library parts? I don't want to rename them or change the folder location. If I were doing a manual pack and go, I would uncheck my library parts and it would keep the reference back to the original file location.

I do have the advantage that this macro will only be run on a specific assembly, so I can know which files are library parts and hard code that into the macro if that helps.

Thanks,
Carrie
SolidWorks 2020 SP5.0
User avatar
AlexLachance
Posts: 2056
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2203
x 1902

Re: Pack and go need to omit library parts

Unread post by AlexLachance »

CarrieIves wrote: Tue Aug 03, 2021 1:41 pm I found the example of how to Pack and Go an assembly with a macro (VBA). I will want to add a prefix.
This works great, except, how do I not include my library parts? I don't want to rename them or change the folder location. If I were doing a manual pack and go, I would uncheck my library parts and it would keep the reference back to the original file location.

I do have the advantage that this macro will only be run on a specific assembly, so I can know which files are library parts and hard code that into the macro if that helps.

Thanks,
Carrie
SolidWorks 2020 SP5.0
Hey Carrie, if your library is in a specific location, perhaps you could do a filter to omit the items that are in that specific location? Just a thought. I know you're speaking macro wise, but maybe that could inspire your solution. I can't really help you programming wise, sorry.
User avatar
AlexB
Posts: 459
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 25
x 249
x 406

Re: Pack and go need to omit library parts

Unread post by AlexB »

AlexLachance wrote: Tue Aug 03, 2021 1:47 pm Hey Carrie, if your library is in a specific location, perhaps you could do a filter to omit the items that are in that specific location? Just a thought. I know you're speaking macro wise, but maybe that could inspire your solution. I can't really help you programming wise, sorry.
That's a good thought. If you get your full list of documents with IPackAndGo.GetDocumentSaveToNames and then remove the library files manually from the list based on their file directory. Then you could pass the updated array into IPackAndGo.SetDocumentSaveToNames before completing the procedure.

I haven't used this myself yet so I'm only assuming that's how this works based on the help files and few examples. Good Luck!
User avatar
Glenn Schroeder
Posts: 1461
Joined: Mon Mar 08, 2021 11:43 am
Answers: 22
Location: southeast Texas
x 1661
x 2060

Re: Pack and go need to omit library parts

Unread post by Glenn Schroeder »

I will preface this by saying I have only a vague idea about what macros are, with absolutely no knowledge about writing one, but when I'm going a Pack and Go and want to exclude my library parts I don't select them manually. I use the Select/Replace function, selecting "In Folder", enter a key word that's in the file path for my library parts, then select "Uncheck All" (thanks again @gupta9665 for teaching me that). Can you get your macro to exclude all components at a certain location? (I assume they're all in a single location.)

image.png
"On the days when I keep my gratitude higher than my expectations, well, I have really good days."

Ray Wylie Hubbard in his song "Mother Blues"
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Re: Pack and go need to omit library parts

Unread post by CarrieIves »

Currently, I'm doing everything on a network drive, so the part I'm messing with isn't actually in the library. I just want to leave it referencing the original file/file location at this time.

I have hard coded the file names and locations at the moment as I am working through this problem.

I started from the example here:
http://help.solidworks.com/2020/english ... ple_VB.htm

According to this help page:
http://help.solidworks.com/2020/english ... Names.html
"To remove a file from Pack and Go, specify an empty string for that file's element in the PathNameList array. To override the paths and filenames set by this method, use IPackAndGo::SetSaveToName."

So, I have my macro able to find the file in the list of files. I can't figure out how to change the name for that file to be an empty string. I think that will solve this problem, but I haven't figured out how to do that. I'm sure it's something that someone with more experience like @gupta9665 would know how to do with no problem.

I am attaching my macro and a zip file of my assembly I am using.

I am in Texas so my work hours are from about 9 - 6 Central time.

Thanks for the suggestions so far. And thanks in advance for any additional pointers.
Attachments
assy-and-macro.zip
(2.32 MiB) Downloaded 131 times
User avatar
JSculley
Posts: 606
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 8
x 837

Re: Pack and go need to omit library parts

Unread post by JSculley »

CarrieIves wrote: Tue Aug 03, 2021 5:30 pm ...
...
So, I have my macro able to find the file in the list of files. I can't figure out how to change the name for that file to be an empty string.
...
...
...
You just need to change this:

Code: Select all

If pgFileNames(i) = "C:\Users\JSculley.azz\Desktop\Solidworks Files\assy-and-macro\96817A310_M3x6_SCREW.SLDPRT" Then
        F1 = i
to this:

Code: Select all

If pgFileNames(i) = "C:\Users\JSculley.azz\Desktop\Solidworks Files\assy-and-macro\96817A310_M3x6_SCREW.SLDPRT" Then
        pgFileNames(i) = ""
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Re: Pack and go need to omit library parts

Unread post by CarrieIves »

@JSculley , Thanks for the suggestion.

I made that change and the list it prints looks OK, but I still end up with the screw being renamed as part of the pack and go. The new assembly is referencing the renamed screw.

There is something different between pgFileNames and pgGetFileNames and I'm not figuring out why they have the two different variables or why they do the ReDim on it either.
User avatar
JSculley
Posts: 606
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 8
x 837

Re: Pack and go need to omit library parts

Unread post by JSculley »

CarrieIves wrote: Tue Aug 03, 2021 6:26 pm @JSculley , Thanks for the suggestion.

I made that change and the list it prints looks OK, but I still end up with the screw being renamed as part of the pack and go. The new assembly is referencing the renamed screw.

There is something different between pgFileNames and pgGetFileNames and I'm not figuring out why they have the two different variables or why they do the ReDim on it either.
Once you have retrieved the current file names and have set the hardware item names to empty strings (as shown in my first reply), you have to call IPackAngGo::SetDocumentSaveToNames:

Code: Select all

' Get current paths and filenames of the assembly's documents
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print ""
Debug.Print " 1 Current path and filenames: "
If (Not (IsEmpty(pgFileNames))) Then
     For i = 0 To UBound(pgFileNames)
     'check if the library part is there and set it to empty
     If pgFileNames(i) = "C:\Users\jsculley.AZZ\Desktop\SOLIDWORKS Files\assy-and-macro\96817A310_M3x6_SCREW.SLDPRT" Then
        pgFileNames(i) = "" 'Set hardware item to empty string
        Debug.Print "We hit the IF " & F1
       ' swApp.SendMsgToUser2 "in if" & F1, swMbWarning, swMbOk
        End If
         Debug.Print "    The path and filename is: " & pgFileNames(i)
     Next i
    'Update pack and go file names
     status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
End If
Also, this line will cause an error:

Code: Select all

pgGetFileNames(F1) = ""
The variable pgGetFileNames is an array of strings. In this example it is a one-dimensional array, or simply a list of strings. You access the items in the list by their index, which is an integer. So, the third item in the list can be accessed like this:

Code: Select all

Dim anItem
anItem = pgGetFileNames(3)
Your F1 variable is a string, so writing this:

Code: Select all

pgGetFileNames(F1) = ""
won't work because the array index is supposed to be an integer and you passed it a string. It would be like asking for the appleth item in a list
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Re: Pack and go need to omit library parts

Unread post by CarrieIves »

@JSculley Thanks! >< That works!
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Re: Pack and go need to omit library parts

Unread post by CarrieIves »

So, first you make it work, and then try to understand it?

From the example
http://help.solidworks.com/2020/english ... ple_vb.htm

So most of the things are being done with the array pgFileNames.

then near the end, you suddenly see pgGetFileNames

Code: Select all

ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
Debug.Print ""
Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "
For i = 0 To (namesCount - 1)
      Debug.Print "    My path and filename is: " & pgGetFileNames(i)
Next i


' Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

Why is there a redim for it?

And why did we suddenly change from pgfileNames to pgGetFileNames?

Thanks,
Carrie
Austin Schukar
Posts: 98
Joined: Thu Mar 18, 2021 11:19 am
Answers: 1
Location: St. Louis, MO
x 289
x 56

Re: Pack and go need to omit library parts

Unread post by Austin Schukar »

CarrieIves wrote: Wed Aug 04, 2021 1:04 pm So, first you make it work, and then try to understand it?

From the example
http://help.solidworks.com/2020/english ... ple_vb.htm

So most of the things are being done with the array pgFileNames.

then near the end, you suddenly see pgGetFileNames

Code: Select all

ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
Debug.Print ""
Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "
For i = 0 To (namesCount - 1)
      Debug.Print "    My path and filename is: " & pgGetFileNames(i)
Next i


' Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)


Why is there a redim for it?

And why did we suddenly change from pgfileNames to pgGetFileNames?

Thanks,
Carrie
Looks like they're just using another variable (pgGetFileNames) to gather the newly renamed file list, to display in your Immediate Window, to show that it's working. Notice they're only 'getting' the new filenames, without doing anything else with it? Not sure the ReDim is necessary
Austin
User avatar
CarrieIves
Posts: 141
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 330
x 117

Re: Pack and go need to omit library parts

Unread post by CarrieIves »

Austin Schukar wrote: Wed Aug 04, 2021 2:14 pm Looks like they're just using another variable (pgGetFileNames) to gather the newly renamed file list, to display in your Immediate Window, to show that it's working. Notice they're only 'getting' the new filenames, without doing anything else with it? Not sure the ReDim is necessary
I commented out the last section of debug.prints and the macro still works.
Post Reply